-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.test.mjs
5514 lines (5157 loc) · 159 KB
/
index.test.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { describe, before, after, it } from 'node:test'
import { spawn } from 'node:child_process'
import assert from 'node:assert'
import querystring from 'node:querystring'
import crypto from 'node:crypto'
import path from 'node:path'
import https from 'node:https'
import fs from 'node:fs'
import { promisify } from 'node:util'
import { Blob } from 'node:buffer'
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0
const MAIN_PORT = 50941 // V
const REMOTE_PORT = 51996 // Cr
const CLIENT_PORT = 54938 // Mn
const THIRD_PORT = 55845 // Fe
const FOURTH_PORT = 58933 // Co
const FIFTH_PORT = 58693 // Ni
const CLIENT_ID = `https://localhost:${CLIENT_PORT}/client`
const REDIRECT_URI = `https://localhost:${CLIENT_PORT}/oauth/callback`
const AS2 =
'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
const AS2_CONTEXT = 'https://www.w3.org/ns/activitystreams'
const AS2_MEDIA_TYPE = 'application/activity+json; charset=utf-8'
const PUBLIC = 'https://www.w3.org/ns/activitystreams#Public'
const generateKeyPair = promisify(crypto.generateKeyPair)
const delay = (t) => new Promise((resolve) => setTimeout(resolve, t))
const startServer = (port = MAIN_PORT, props = {}) => {
return new Promise((resolve, reject) => {
const server = spawn('node', ['index.mjs'], {
env: {
OPP_HOSTNAME: 'localhost',
...process.env,
...props,
OPP_PORT: port
}
})
server.on('error', reject)
server.stdout.on('data', (data) => {
if (data.toString().includes('Listening')) {
resolve(server)
}
console.log(`SERVER ${port}: ${data.toString()}`)
})
server.stderr.on('data', (data) => {
console.log(`SERVER ${port} ERROR: ${data.toString()}`)
})
})
}
const defaultClient = {
'@context': [AS2_CONTEXT, 'https://purl.archive.org/socialweb/oauth'],
type: 'Application',
id: CLIENT_ID,
redirectURI: REDIRECT_URI,
nameMap: {
en: 'Test scripts for onepage.pub'
}
}
const startClientServer = (
port = CLIENT_PORT,
client = JSON.stringify(defaultClient),
contentType = AS2
) => {
return new Promise((resolve, reject) => {
const options = {
key: fs.readFileSync('localhost.key'),
cert: fs.readFileSync('localhost.crt')
}
const server = https.createServer(options, (req, res) => {
if (req.url.startsWith('/client')) {
res.writeHead(200, {
'Content-Type': contentType
})
res.end(client)
} else {
res.writeHead(404)
res.end()
}
})
server.on('error', reject)
server.listen(port, 'localhost', () => {
resolve(server)
})
})
}
const registerUser = (() => {
let i = 100
return async (port = MAIN_PORT) => {
i++
const username = `testuser${i}`
const password = `testpassword${i}`
const reg = await fetch(`https://localhost:${port}/register`, {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: querystring.stringify({
username,
password,
confirmation: password
})
})
const text = await reg.text()
const token = text.match(/<span class="token">(.*?)<\/span>/)[1]
const cookie = reg.headers.get('Set-Cookie')
return [username, token, password, cookie]
}
})()
const userToActor = async (username, port = MAIN_PORT) => {
const res = await fetch(
`https://localhost:${port}/.well-known/webfinger?resource=acct:${username}@localhost:${port}`
)
const obj = await res.json()
const actorId = obj.links[0].href
const actorRes = await fetch(actorId)
return await actorRes.json()
}
const registerActor = async (port = MAIN_PORT) => {
const [username, token, , cookie] = await registerUser(port)
const actor = await userToActor(username, port)
return [actor, token, cookie]
}
const doActivity = async (actor, token, activity) => {
const res = await fetch(actor.outbox, {
method: 'POST',
headers: {
'Content-Type':
'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
Authorization: `Bearer ${token}`
},
body: JSON.stringify(activity)
})
if (res.status !== 201) {
const body = await res.text()
throw new Error(`Bad status code ${res.status}: ${body}`)
}
return await res.json()
}
const failActivity = async (actor, token, activity) => {
const res = await fetch(actor.outbox, {
method: 'POST',
headers: {
'Content-Type':
'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
Authorization: `Bearer ${token}`
},
body: JSON.stringify(activity)
})
const body = await res.text()
if (res.status >= 200 && res.status <= 299) {
throw new Error(
`Good status code ${res.status} for activity that should fail: ${body}`
)
}
return res.status
}
const getObject = async (id, token = null) => {
const headers = {
Accept:
'application/ld+json; profile="https://www.w3.org/ns/activitystreams",application/activity+json,application/json'
}
if (token) {
headers.Authorization = `Bearer ${token}`
}
const res = await fetch(id, {
headers
})
if (res.status !== 200) {
throw new Error(`Bad status code ${res.status}`)
}
return await res.json()
}
const getMembers = async (collection, token = null) => {
const url = typeof collection === 'string' ? collection : collection.id
if (!url) {
throw new Error(`Invalid collection ${collection}`)
}
const coll = await getObject(url, token)
if ('orderedItems' in coll) {
return coll.orderedItems
} else if ('items' in coll) {
return coll.items
} else if ('first' in coll) {
let members = []
let pageObj = null
for (let page = coll.first; page; page = pageObj.next) {
const pageId = typeof page === 'string' ? page : page.id
pageObj = await getObject(pageId, token)
for (const prop of ['orderedItems', 'items']) {
if (prop in pageObj) {
members = members.concat(pageObj[prop])
}
}
}
return members
} else {
throw new Error(
`Invalid collection ${url}: no items, orderedItems, or first`
)
}
}
const isInStream = async (collection, object, token = null) => {
const objectId = typeof object === 'string' ? object : object.id
const members = await getMembers(collection, token)
return members.some((item) => item.id === objectId)
}
const getProxy = async (id, actor, token) => {
const res = await fetch(actor.endpoints.proxyUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Authorization: `Bearer ${token}`
},
body: querystring.stringify({ id })
})
if (res.status !== 200) {
return false
} else {
return await res.json()
}
}
const canGetProxy = async (id, actor, token) => {
const result = await getProxy(id, actor, token)
return !!result
}
const getAuthCode = async (actor, cookie, scope = 'read write') => {
const state = crypto.randomBytes(16).toString('hex')
const authz = actor.endpoints.oauthAuthorizationEndpoint
const responseType = 'code'
const codeVerifier = crypto.randomBytes(32).toString('hex')
const codeChallenge = base64URLEncode(
crypto.createHash('sha256').update(codeVerifier).digest()
)
const qs = querystring.stringify({
response_type: responseType,
client_id: CLIENT_ID,
redirect_uri: REDIRECT_URI,
scope,
state,
code_challenge_method: 'S256',
code_challenge: codeChallenge
})
const res = await fetch(`${authz}?${qs}`, {
headers: { Cookie: cookie }
})
const body = await res.text()
const csrfToken = body.match(/name="csrf_token" value="(.+?)"/)[1]
const res2 = await fetch(authz, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Cookie: cookie
},
body: querystring.stringify({
csrf_token: csrfToken,
client_id: CLIENT_ID,
redirect_uri: REDIRECT_URI,
scope,
state,
code_challenge: codeChallenge
}),
redirect: 'manual'
})
const location = res2.headers.get('Location')
const locUrl = new URL(location)
const code = locUrl.searchParams.get('code')
return [code, codeVerifier]
}
const getTokens = async (actor, code, codeVerifier) => {
const tokUrl = actor.endpoints.oauthTokenEndpoint
const res3 = await fetch(tokUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: querystring.stringify({
grant_type: 'authorization_code',
code,
redirect_uri: REDIRECT_URI,
code_verifier: codeVerifier,
client_id: CLIENT_ID
})
})
const body3 = await res3.json()
return [body3.access_token, body3.refresh_token]
}
const getAccessToken = async (actor, cookie, scope = 'read write') => {
const [code, codeVerifier] = await getAuthCode(actor, cookie, scope)
const [accessToken] = await getTokens(actor, code, codeVerifier)
return accessToken
}
const base64URLEncode = (str) =>
str
.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=/g, '')
const cantUpdate = async (actor, token, object, properties) => {
return failActivity(actor, token, {
type: 'Update',
object: { ...object, ...properties }
})
}
const settle = async (port = MAIN_PORT) => {
let count = null
do {
const res = await fetch(`https://localhost:${port}/queue`)
count = await res.json()
if (count > 0) {
await delay(1)
}
} while (count > 0)
}
async function signRequest (keyId, privateKey, method, url, date) {
url = typeof url === 'string' ? new URL(url) : url
const target =
url.search && url.search.length
? `${url.pathname}?${url.search}`
: `${url.pathname}`
let data = `(request-target): ${method.toLowerCase()} ${target}\n`
data += `host: ${url.host}\n`
data += `date: ${date}`
const signer = crypto.createSign('sha256')
signer.update(data)
const signature = signer.sign(privateKey).toString('base64')
signer.end()
const header = `keyId="${keyId}",headers="(request-target) host date",signature="${signature.replace(
/"/g,
'\\"'
)}",algorithm="rsa-sha256"`
return header
}
describe('onepage.pub', () => {
let child = null
let remote = null
let client = null
before(async () => {
child = await startServer(MAIN_PORT)
remote = await startServer(REMOTE_PORT)
client = await startClientServer(CLIENT_PORT)
})
after(() => {
child.kill('SIGTERM')
remote.kill('SIGTERM')
client.close()
})
describe('Root object', () => {
it('can get the root object', async () => {
const res = await fetch(`https://localhost:${MAIN_PORT}/`, {
headers: {
Accept:
'application/activity+json,application/ld+json,application/json'
}
})
const obj = await res.json()
assert.strictEqual(obj.type, 'Service')
assert.strictEqual(obj.name, 'One Page Pub')
assert.strictEqual(obj.id, `https://localhost:${MAIN_PORT}/`)
})
})
describe('Registration', () => {
it('can get a registration form', async () => {
const res = await fetch(`https://localhost:${MAIN_PORT}/register`)
const body = await res.text()
assert.strictEqual(res.status, 200)
assert.strictEqual(
res.headers.get('Content-Type'),
'text/html; charset=utf-8'
)
assert(body.includes('<form'))
assert(body.includes('name="username"'))
assert(body.includes('name="password"'))
assert(body.includes('name="confirmation"'))
})
it('can register a user', async () => {
const username = 'testuser1'
const password = 'testpassword1'
const res = await fetch(`https://localhost:${MAIN_PORT}/register`, {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: querystring.stringify({
username,
password,
confirmation: password
})
})
const body = await res.text()
assert.strictEqual(
res.status,
200,
`Bad status code ${res.status}: ${body}`
)
assert.strictEqual(
res.headers.get('Content-Type'),
'text/html; charset=utf-8'
)
assert(body.includes('Registered'))
assert(body.includes(username))
assert(body.match('<span class="token">.+?</span>'))
})
})
describe('Webfinger', () => {
let username = null
before(async () => {
[username] = await registerUser()
})
it('can get information about a user', async () => {
const res = await fetch(
`https://localhost:${MAIN_PORT}/.well-known/webfinger?resource=acct:${username}@localhost:${MAIN_PORT}`
)
assert.strictEqual(res.status, 200)
assert.strictEqual(
res.headers.get('Content-Type'),
'application/jrd+json; charset=utf-8'
)
const obj = await res.json()
assert.strictEqual(
obj.subject,
`acct:${username}@localhost:${MAIN_PORT}`
)
assert.strictEqual(obj.links[0].rel, 'self')
assert.strictEqual(obj.links[0].type, 'application/activity+json')
assert(
obj.links[0].href.startsWith(`https://localhost:${MAIN_PORT}/person/`)
)
})
})
describe('Actor endpoint', () => {
let username = null
let actorId = null
let actorRes = null
let actorBody = null
let actorObj = null
before(async () => {
[username] = await registerUser()
const res = await fetch(
`https://localhost:${MAIN_PORT}/.well-known/webfinger?resource=acct:${username}@localhost:${MAIN_PORT}`
)
const obj = await res.json()
actorId = obj.links[0].href
actorRes = await fetch(actorId)
actorBody = await actorRes.text()
actorObj = actorRes.status === 200 ? JSON.parse(actorBody) : null
})
it('can fetch the actor endpoint', async () => {
assert.strictEqual(
actorRes.status,
200,
`Bad status code ${actorRes.status}: ${actorBody}`
)
assert.strictEqual(actorRes.headers.get('Content-Type'), AS2_MEDIA_TYPE)
})
it('has the as2 @context', () => {
assert(actorObj['@context'])
assert.notEqual(-1, actorObj['@context'].indexOf(AS2_CONTEXT))
})
it('has the security @context', () => {
assert(actorObj['@context'])
assert.notEqual(
-1,
actorObj['@context'].indexOf('https://w3id.org/security/v1')
)
})
it('has the blocked @context', () => {
assert(actorObj['@context'])
assert(
actorObj['@context'].includes(
'https://purl.archive.org/socialweb/blocked'
)
)
})
it('has the pending @context', () => {
assert(actorObj['@context'])
assert(
actorObj['@context'].includes(
'https://purl.archive.org/socialweb/pending'
)
)
})
it('has the correct id', () => {
assert.strictEqual(actorObj.id, actorId)
})
it('has the correct type', () => {
assert.strictEqual(actorObj.type, 'Person')
})
it('has the correct name', () => {
assert.strictEqual(actorObj.name, username)
})
it('has a valid inbox', () => {
assert.equal('string', typeof actorObj.inbox)
assert(
actorObj.inbox.startsWith(
`https://localhost:${MAIN_PORT}/orderedcollection/`
)
)
})
it('has a valid outbox', () => {
assert.equal('string', typeof actorObj.outbox)
assert(
actorObj.outbox.startsWith(
`https://localhost:${MAIN_PORT}/orderedcollection/`
)
)
})
it('has a valid followers', () => {
assert.equal('string', typeof actorObj.followers)
assert(
actorObj.followers.startsWith(
`https://localhost:${MAIN_PORT}/orderedcollection/`
)
)
})
it('has a valid following', () => {
assert.equal('string', typeof actorObj.following)
assert(
actorObj.following.startsWith(
`https://localhost:${MAIN_PORT}/orderedcollection/`
)
)
})
it('has a valid liked', () => {
assert.equal('string', typeof actorObj.liked)
assert(
actorObj.liked.startsWith(
`https://localhost:${MAIN_PORT}/orderedcollection/`
)
)
})
it('has a blocked property', () => {
assert.equal('object', typeof actorObj.blocked)
assert.equal('string', typeof actorObj.blocked.id)
assert(
actorObj.blocked.id.startsWith(
`https://localhost:${MAIN_PORT}/orderedcollection/`
)
)
})
it('has a pendingFollowers property', () => {
assert.equal('object', typeof actorObj.pendingFollowers)
assert.equal('string', typeof actorObj.pendingFollowers.id)
assert(
actorObj.pendingFollowers.id.startsWith(
`https://localhost:${MAIN_PORT}/orderedcollection/`
)
)
})
it('has a pendingFollowing property', () => {
assert.equal('object', typeof actorObj.pendingFollowing)
assert.equal('string', typeof actorObj.pendingFollowing.id)
assert(
actorObj.pendingFollowing.id.startsWith(
`https://localhost:${MAIN_PORT}/orderedcollection/`
)
)
})
it('has a public key', () => {
assert(actorObj.publicKey)
assert.equal('object', typeof actorObj.publicKey)
assert.equal('string', typeof actorObj.publicKey.id)
assert(
actorObj.publicKey.id.startsWith(`https://localhost:${MAIN_PORT}/key/`)
)
assert.equal('string', typeof actorObj.publicKey.type)
assert.equal('Key', actorObj.publicKey.type)
assert.equal('string', typeof actorObj.publicKey.owner)
assert.equal(actorObj.publicKey.owner, actorId)
})
it('has an endpoints property', () => {
assert(actorObj.endpoints)
assert.equal('object', typeof actorObj.endpoints)
})
it('has an proxyUrl endpoint', () => {
assert(actorObj.endpoints)
assert.equal('object', typeof actorObj.endpoints)
assert.equal('string', typeof actorObj.endpoints.proxyUrl)
})
})
describe('Actor streams', () => {
let actor = null
let token = null
let token2 = null
before(async () => {
[actor, token] = await registerActor();
[, token2] = await registerActor()
})
it('can get actor inbox', async () => {
const res = await fetch(actor.inbox)
assert.strictEqual(res.status, 200)
assert.strictEqual(res.headers.get('Content-Type'), AS2_MEDIA_TYPE)
const obj = await res.json()
assert.strictEqual(obj.id, actor.inbox)
assert.strictEqual(obj.type, 'OrderedCollection')
assert.strictEqual(obj.totalItems, 0)
assert(obj.nameMap?.en)
assert(obj.first)
assert(
obj.first.id.startsWith(
`https://localhost:${MAIN_PORT}/orderedcollectionpage/`
)
)
})
it('can get actor outbox', async () => {
const res = await fetch(actor.outbox)
assert.strictEqual(res.status, 200)
assert.strictEqual(res.headers.get('Content-Type'), AS2_MEDIA_TYPE)
const obj = await res.json()
assert.strictEqual(obj.id, actor.outbox)
assert.strictEqual(obj.type, 'OrderedCollection')
assert.strictEqual(obj.totalItems, 0)
assert(obj.nameMap?.en)
assert(obj.first)
assert(
obj.first.id.startsWith(
`https://localhost:${MAIN_PORT}/orderedcollectionpage/`
)
)
})
it('can get actor followers', async () => {
const res = await fetch(actor.followers)
assert.strictEqual(res.status, 200)
assert.strictEqual(res.headers.get('Content-Type'), AS2_MEDIA_TYPE)
const obj = await res.json()
assert.strictEqual(obj.id, actor.followers)
assert.strictEqual(obj.type, 'OrderedCollection')
assert.strictEqual(obj.totalItems, 0)
assert(obj.nameMap?.en)
assert(obj.first)
assert(
obj.first.id.startsWith(
`https://localhost:${MAIN_PORT}/orderedcollectionpage/`
)
)
})
it('can get actor following', async () => {
const res = await fetch(actor.following)
assert.strictEqual(res.status, 200)
assert.strictEqual(res.headers.get('Content-Type'), AS2_MEDIA_TYPE)
const obj = await res.json()
assert.strictEqual(obj.id, actor.following)
assert.strictEqual(obj.type, 'OrderedCollection')
assert.strictEqual(obj.totalItems, 0)
assert(obj.nameMap?.en)
assert(obj.first)
assert(
obj.first.id.startsWith(
`https://localhost:${MAIN_PORT}/orderedcollectionpage/`
)
)
})
it('can get actor liked', async () => {
const res = await fetch(actor.liked)
assert.strictEqual(res.status, 200)
assert.strictEqual(res.headers.get('Content-Type'), AS2_MEDIA_TYPE)
const obj = await res.json()
assert.strictEqual(obj.id, actor.liked)
assert.strictEqual(obj.type, 'OrderedCollection')
assert.strictEqual(obj.totalItems, 0)
assert(obj.nameMap?.en)
assert(obj.first)
assert(
obj.first.id.startsWith(
`https://localhost:${MAIN_PORT}/orderedcollectionpage/`
)
)
})
it('cannot get actor blocked without authentication', async () => {
const res = await fetch(actor.blocked.id)
assert.strictEqual(res.status, 401)
})
it('cannot get actor blocked with other user authentication', async () => {
const res = await fetch(actor.blocked.id, {
headers: {
Authorization: `Bearer ${token2}`
}
})
assert.strictEqual(res.status, 403)
})
it('can get actor blocked with authentication', async () => {
const res = await fetch(actor.blocked.id, {
headers: {
Authorization: `Bearer ${token}`
}
})
assert.strictEqual(res.status, 200)
assert.strictEqual(res.headers.get('Content-Type'), AS2_MEDIA_TYPE)
const obj = await res.json()
assert.strictEqual(obj.id, actor.blocked.id)
assert.strictEqual(obj.type, 'OrderedCollection')
assert.strictEqual(obj.totalItems, 0)
assert(obj.nameMap?.en)
assert(obj.first)
assert(
obj.first.id.startsWith(
`https://localhost:${MAIN_PORT}/orderedcollectionpage/`
)
)
})
it('cannot get actor pendingFollowers without authentication', async () => {
const res = await fetch(actor.pendingFollowers.id)
assert.strictEqual(res.status, 401)
})
it('cannot get actor pendingFollowers with other user authentication', async () => {
const res = await fetch(actor.pendingFollowers.id, {
headers: {
Authorization: `Bearer ${token2}`
}
})
assert.strictEqual(res.status, 403)
})
it('can get actor pendingFollowers', async () => {
const res = await fetch(actor.pendingFollowers.id, {
headers: {
Authorization: `Bearer ${token}`
}
})
assert.strictEqual(res.status, 200)
assert.strictEqual(res.headers.get('Content-Type'), AS2_MEDIA_TYPE)
const obj = await res.json()
assert.strictEqual(obj.id, actor.pendingFollowers.id)
assert.strictEqual(obj.type, 'OrderedCollection')
assert.strictEqual(obj.totalItems, 0)
assert(obj.nameMap?.en)
assert(obj.first)
assert(
obj.first.id.startsWith(
`https://localhost:${MAIN_PORT}/orderedcollectionpage/`
)
)
})
it('cannot get actor pendingFollowing without authentication', async () => {
const res = await fetch(actor.pendingFollowing.id)
assert.strictEqual(res.status, 401)
})
it('cannot get actor pendingFollowing with other user authentication', async () => {
const res = await fetch(actor.pendingFollowing.id, {
headers: {
Authorization: `Bearer ${token2}`
}
})
assert.strictEqual(res.status, 403)
})
it('can get actor pendingFollowing', async () => {
const res = await fetch(actor.pendingFollowing.id, {
headers: {
Authorization: `Bearer ${token}`
}
})
assert.strictEqual(res.status, 200)
assert.strictEqual(res.headers.get('Content-Type'), AS2_MEDIA_TYPE)
const obj = await res.json()
assert.strictEqual(obj.id, actor.pendingFollowing.id)
assert.strictEqual(obj.type, 'OrderedCollection')
assert.strictEqual(obj.totalItems, 0)
assert(obj.nameMap?.en)
assert(obj.first)
assert(
obj.first.id.startsWith(
`https://localhost:${MAIN_PORT}/orderedcollectionpage/`
)
)
})
})
describe('Post to outbox', () => {
let actor = null
let token = null
let res = null
let body = null
let obj = null
const activity = {
'@context': AS2_CONTEXT,
type: 'IntransitiveActivity',
to: PUBLIC
}
before(async () => {
[actor, token] = await registerActor()
res = await fetch(actor.outbox, {
method: 'POST',
headers: {
'Content-Type': AS2_MEDIA_TYPE,
Authorization: `Bearer ${token}`
},
body: JSON.stringify(activity)
})
body = await res.text()
obj = JSON.parse(body)
})
it('has the correct HTTP response', async () => {
assert.strictEqual(
res.status,
201,
`Bad status code ${res.status}: ${body}`
)
assert.strictEqual(res.headers.get('Content-Type'), AS2_MEDIA_TYPE)
})
it('has an object id', async () => {
assert(obj.id)
})
it('has the correct object type', async () => {
assert.strictEqual(obj.type, activity.type)
})
it('has the correct addressees', async () => {
assert.strictEqual(obj.to.id, activity.to)
})
it("appears in the actor's inbox", async () => {
const inbox = await (await fetch(actor.inbox)).json()
const inboxPage = await (await fetch(inbox.first.id)).json()
assert(inboxPage.orderedItems.some((act) => act.id === obj.id))
})
it("appears in the actor's outbox", async () => {
const outbox = await (await fetch(actor.outbox)).json()
const outboxPage = await (await fetch(outbox.first.id)).json()
assert(outboxPage.orderedItems.some((act) => act.id === obj.id))
})
})
describe('Post to outbox with application/ld+json', () => {
let actor = null
let token = null
let res = null
let body = null
let obj = null
const activity = {
'@context': AS2_CONTEXT,
type: 'IntransitiveActivity',
to: PUBLIC
}
before(async () => {
[actor, token] = await registerActor()
res = await fetch(actor.outbox, {
method: 'POST',
headers: {
'Content-Type':
'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
Authorization: `Bearer ${token}`
},
body: JSON.stringify(activity)
})
body = await res.text()
obj = JSON.parse(body)
})
it('has the correct HTTP response', async () => {
assert.strictEqual(
res.status,
201,
`Bad status code ${res.status}: ${body}`
)
assert.strictEqual(res.headers.get('Content-Type'), AS2_MEDIA_TYPE)
})
it('has an object id', async () => {
assert(obj.id)
})
it('has the correct object type', async () => {
assert.strictEqual(obj.type, activity.type)
})
it('has the correct addressees', async () => {
assert.strictEqual(obj.to?.id, activity.to)
})
it("appears in the actor's inbox", async () => {
const inbox = await (await fetch(actor.inbox)).json()
const inboxPage = await (await fetch(inbox.first.id)).json()
assert(inboxPage.orderedItems.some((act) => act.id === obj.id))
})
it("appears in the actor's outbox", async () => {
const outbox = await (await fetch(actor.outbox)).json()
const outboxPage = await (await fetch(outbox.first.id)).json()
assert(outboxPage.orderedItems.some((act) => act.id === obj.id))
})
})
describe('Filter collections', () => {
let actor1 = null
let token1 = null
let token2 = null
let activity = null
before(async () => {
[actor1, token1] = await registerActor();
[, token2] = await registerActor()
const input = {
'@context': AS2_CONTEXT,
type: 'IntransitiveActivity',
to: [actor1.id]
}
const res = await fetch(actor1.outbox, {
method: 'POST',
headers: {
'Content-Type':
'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
Authorization: `Bearer ${token1}`
},
body: JSON.stringify(input)
})
const body = await res.text()
activity = JSON.parse(body)
})
it('author can see own private activity', async () => {
const outbox = await (
await fetch(actor1.outbox, {
headers: {
Authorization: `Bearer ${token1}`
}
})
).json()
const outboxPage = await (
await fetch(outbox.first.id, {
headers: {
Authorization: `Bearer ${token1}`
}
})
).json()
assert(outboxPage.orderedItems.some((val) => val.id === activity.id))
})
it('others cannot see a private activity', async () => {
const outbox = await (
await fetch(actor1.outbox, {
headers: {
Authorization: `Bearer ${token2}`
}
})
).json()
const outboxPage = await (