-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexclude-members.test.js
692 lines (562 loc) · 18.3 KB
/
exclude-members.test.js
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
// SPDX-FileCopyrightText: 2022 Andre 'Staltz' Medeiros <[email protected]>
//
// SPDX-License-Identifier: CC0-1.0
const test = require('tape')
const pull = require('pull-stream')
const { promisify: p } = require('util')
const ssbKeys = require('ssb-keys')
const { where, author, toPromise } = require('ssb-db2/operators')
const { fromMessageSigil } = require('ssb-uri2')
const Testbot = require('./helpers/testbot')
const replicate = require('./helpers/replicate')
const countGroupFeeds = require('./helpers/count-group-feeds')
test('add and exclude a person, post on the new feed', async (t) => {
// Alice's feeds should look like
// first: initGroup->excludeBob
// second: initEpoch->post
// additions: addAlice->addBob->reAddAlice
const alice = Testbot({
keys: ssbKeys.generate(null, 'alice'),
mfSeed: Buffer.from(
'000000000000000000000000000000000000000000000000000000000000a1ce',
'hex'
),
})
const bob = Testbot({
keys: ssbKeys.generate(null, 'bob'),
mfSeed: Buffer.from(
'0000000000000000000000000000000000000000000000000000000000000b0b',
'hex'
),
})
await Promise.all([alice.tribes2.start(), bob.tribes2.start()])
t.pass('tribes2 started for both alice and bob')
const aliceRoot = await p(alice.metafeeds.findOrCreate)()
const bobRoot = await p(bob.metafeeds.findOrCreate)()
await replicate(alice, bob)
t.pass('alice and bob replicate their trees')
const {
id: groupId,
root,
writeKey: writeKey1,
subfeed: { id: firstFeedId },
} = await alice.tribes2
.create()
.catch((err) => t.error(err, 'alice failed to create group'))
const addBobMsg = await alice.tribes2
.addMembers(groupId, [bobRoot.id])
.catch((err) => t.error(err, 'add member fail'))
t.equals(
await p(countGroupFeeds)(alice),
1,
'before exclude alice has 1 group feed'
)
await alice.tribes2
.excludeMembers(groupId, [bobRoot.id])
.catch((err) => t.error(err, 'exclude member fail'))
t.equals(
await p(countGroupFeeds)(alice),
2,
'after exclude alice has 2 group feeds'
)
const {
value: { author: secondFeedId },
} = await alice.tribes2
.publish({
type: 'test',
text: 'post',
recps: [groupId],
})
.catch(t.fail)
t.equals(
await p(countGroupFeeds)(alice),
2,
'alice still has 2 group feeds after publishing on the new feed'
)
t.notEquals(
secondFeedId,
firstFeedId,
'feed for publish is different to initial feed'
)
const { writeKey: writeKey2 } = await alice.tribes2.get(groupId)
t.false(writeKey1.key.equals(writeKey2.key), "there's a new key for writing")
const msgsFromFirst = await alice.db.query(
where(author(firstFeedId)),
toPromise()
)
const firstContents = msgsFromFirst.map((msg) => msg.value.content)
t.equal(firstContents.length, 2, '2 messages on first feed')
const firstInit = firstContents[0]
t.equal(firstInit.type, 'group/init')
t.equal(firstInit.groupKey, writeKey1.key.toString('base64'))
const excludeMsg = firstContents[1]
t.equal(excludeMsg.type, 'group/exclude')
t.deepEqual(excludeMsg.excludes, [bobRoot.id])
t.deepEqual(excludeMsg.recps, [groupId])
t.deepEqual(excludeMsg.tangles.members, {
root,
previous: [fromMessageSigil(addBobMsg.key)],
})
const msgsFromSecond = await alice.db.query(
where(author(secondFeedId)),
toPromise()
)
const secondContents = msgsFromSecond.map((msg) => msg.value.content)
t.equal(secondContents.length, 2, '2 messages on second (new) feed')
const secondInit = secondContents[0]
t.equal(secondInit.type, 'group/init')
t.equal(secondInit.version, 'v2')
t.equal(secondInit.groupKey, writeKey2.key.toString('base64'))
t.deepEqual(secondInit.tangles.members, { root: null, previous: null })
t.deepEqual(
secondInit.tangles.epoch,
{ root, previous: [root] },
'epoch tangle is correct on new epoch init'
)
const post = secondContents[1]
t.equal(post.text, 'post', 'found post on second feed')
const aliceAdditions = await p(alice.metafeeds.findOrCreate)({
purpose: 'group/additions',
})
const msgsFromAdditions = await alice.db.query(
where(author(aliceAdditions.id)),
toPromise()
)
const additionsContents = msgsFromAdditions.map((msg) => msg.value.content)
t.equal(additionsContents.length, 3, '3 messages on additions feed')
const reinviteMsg = additionsContents[2]
t.equal(reinviteMsg.type, 'group/add-member')
t.deepEqual(reinviteMsg.recps, [groupId, aliceRoot.id])
const secondInitKey = fromMessageSigil(msgsFromSecond[0].key)
t.deepEqual(
reinviteMsg.tangles.members,
{
root: secondInitKey,
previous: [secondInitKey],
},
'members tangle resets after new epoch'
)
await Promise.all([p(alice.close)(true), p(bob.close)(true)])
})
test('Verify that you actually get excluded from a group', async (t) => {
const alice = Testbot({
keys: ssbKeys.generate(null, 'alice'),
mfSeed: Buffer.from(
'000000000000000000000000000000000000000000000000000000000000a1ce',
'hex'
),
})
const bob = Testbot({
keys: ssbKeys.generate(null, 'bob'),
mfSeed: Buffer.from(
'0000000000000000000000000000000000000000000000000000000000000b0b',
'hex'
),
})
await Promise.all([alice.tribes2.start(), bob.tribes2.start()])
t.pass('tribes2 started for both alice and bob')
await p(alice.metafeeds.findOrCreate)()
const bobRoot = await p(bob.metafeeds.findOrCreate)()
await replicate(alice, bob)
t.pass('alice and bob replicate their trees')
const { id: groupId } = await alice.tribes2
.create()
.catch((err) => t.error(err, 'alice failed to create group'))
await alice.tribes2
.addMembers(groupId, [bobRoot.id])
.catch((err) => t.error(err, 'add member fail'))
await replicate(alice, bob)
await bob.tribes2.acceptInvite(groupId).catch(t.error)
await bob.tribes2
.publish({
type: 'test',
text: 'bob first post',
recps: [groupId],
})
.then(() => t.pass("bob posts in the group while he's in it"))
.catch(t.error)
await replicate(alice, bob)
await alice.tribes2
.excludeMembers(groupId, [bobRoot.id])
.catch((err) => t.error(err, 'exclude member fail'))
const { key: aliceNewEpochPostKey } = await alice.tribes2.publish({
type: 'shitpost',
text: 'alicepost',
recps: [groupId],
})
await replicate(alice, bob)
await p(setTimeout)(500)
t.pass('replicated, about to publish')
await bob.tribes2
.publish({
type: 'test',
text: 'bob second post',
recps: [groupId],
})
.then(() =>
t.fail("Bob posted again in the group even if he's excluded from it")
)
.catch(() =>
t.pass("Bob can't post in the group anymore since he's excluded from it")
)
const bobGroups = await pull(
bob.tribes2.list({ excluded: true }),
pull.collectAsPromise()
)
t.deepEquals(
bobGroups,
[{ id: groupId, excluded: true }],
"bob is excluded from the only group he's been in. sad."
)
const bobGotMsg = await p(bob.db.get)(aliceNewEpochPostKey)
t.equals(
typeof bobGotMsg.content,
'string',
"bob didn't manage to decrypt alice's new message"
)
const invites = await pull(bob.tribes2.listInvites(), pull.collectAsPromise())
t.deepEquals(invites, [], 'Bob has no invites')
await pull(bob.tribes2.listMembers(groupId), pull.collectAsPromise())
.then(() =>
t.fail(
"Bob didn't get an error when trying to list members of the group he's excluded from"
)
)
.catch(() =>
t.pass(
"Bob gets an error when trying to list members of the group he's excluded from"
)
)
await Promise.all([p(alice.close)(true), p(bob.close)(true)])
})
test("If you're not the excluder nor the excludee then you should still be in the group and have access to the new epoch", async (t) => {
// alice creates the group. adds bob and carol. removes bob.
// bob gets added and is removed
// carol stays in the group
const alice = Testbot({
keys: ssbKeys.generate(null, 'alice'),
mfSeed: Buffer.from(
'000000000000000000000000000000000000000000000000000000000000a1ce',
'hex'
),
})
const bob = Testbot({
keys: ssbKeys.generate(null, 'bob'),
mfSeed: Buffer.from(
'0000000000000000000000000000000000000000000000000000000000000b0b',
'hex'
),
})
const carol = Testbot({
keys: ssbKeys.generate(null, 'carol'),
mfSeed: Buffer.from(
'00000000000000000000000000000000000000000000000000000000000ca201',
'hex'
),
})
await Promise.all([
alice.tribes2.start(),
bob.tribes2.start(),
carol.tribes2.start(),
])
t.pass('tribes2 started for everyone')
await p(alice.metafeeds.findOrCreate)()
const bobRoot = await p(bob.metafeeds.findOrCreate)()
const carolRoot = await p(carol.metafeeds.findOrCreate)()
await replicate(alice, bob)
await replicate(alice, carol)
await replicate(bob, carol)
t.pass('everyone replicates their trees')
const { id: groupId, writeKey: writeKey1 } = await alice.tribes2
.create()
.catch((err) => t.error(err, 'alice failed to create group'))
await replicate(alice, carol)
await alice.tribes2
.addMembers(groupId, [bobRoot.id, carolRoot.id])
.catch((err) => t.error(err, 'add bob fail'))
await replicate(alice, carol)
await carol.tribes2.acceptInvite(groupId)
await replicate(alice, carol)
const {
value: { author: firstFeedId },
} = await carol.tribes2
.publish({
type: 'test',
text: 'first post',
recps: [groupId],
})
.catch((err) => t.error(err, 'carol failed to publish on first feed'))
if (firstFeedId) t.pass('carol posted first post')
await alice.tribes2
.excludeMembers(groupId, [bobRoot.id])
.then((res) => {
t.pass('alice excluded bob')
return res
})
.catch((err) => t.error(err, 'remove member fail'))
await replicate(alice, carol).catch(t.error)
// let carol find the new epoch and switch to the new key
await p(setTimeout)(500)
const {
value: { author: secondFeedId },
} = await carol.tribes2
.publish({
type: 'test',
text: 'second post',
recps: [groupId],
})
.catch(t.fail)
if (secondFeedId) t.pass('carol posted second post')
t.notEquals(
secondFeedId,
firstFeedId,
'feed for second publish is different to first publish'
)
const { writeKey: writeKey2 } = await carol.tribes2.get(groupId)
const branches = await pull(
carol.metafeeds.branchStream({
root: carolRoot.id,
old: true,
live: false,
}),
pull.collectAsPromise()
)
const groupFeedPurposes = branches
.filter((branch) => branch.length === 4)
.map((branch) => branch[3])
.filter((feed) => feed.recps && feed.purpose.length === 44)
.map((feed) => feed.purpose)
t.true(
groupFeedPurposes.includes(writeKey1.key.toString('base64')),
'Carol has a feed for the old key'
)
t.true(
groupFeedPurposes.includes(writeKey2.key.toString('base64')),
'Carol has a feed for the new key'
)
await Promise.all([
p(alice.close)(true),
p(bob.close)(true),
p(carol.close)(true),
])
})
test('Get added to an old epoch but still find newer epochs', async (t) => {
const alice = Testbot({
keys: ssbKeys.generate(null, 'alice'),
mfSeed: Buffer.from(
'000000000000000000000000000000000000000000000000000000000000a1ce',
'hex'
),
})
const bob = Testbot({
keys: ssbKeys.generate(null, 'bob'),
mfSeed: Buffer.from(
'0000000000000000000000000000000000000000000000000000000000000b0b',
'hex'
),
})
const carol = Testbot({
keys: ssbKeys.generate(null, 'carol'),
mfSeed: Buffer.from(
'00000000000000000000000000000000000000000000000000000000000ca201',
'hex'
),
})
await alice.tribes2.start()
await bob.tribes2.start()
await carol.tribes2.start()
t.pass('tribes2 started for everyone')
await p(alice.metafeeds.findOrCreate)()
const bobRoot = await p(bob.metafeeds.findOrCreate)()
const carolRoot = await p(carol.metafeeds.findOrCreate)()
await replicate(alice, bob)
await replicate(alice, carol)
await replicate(bob, carol)
t.pass('everyone replicates their trees')
const { id: groupId } = await alice.tribes2
.create()
.catch((err) => t.error(err, 'alice failed to create group'))
const { key: firstPostId } = await alice.tribes2
.publish({
type: 'test',
text: 'first post',
recps: [groupId],
})
.catch(t.fail)
// replicate the first group messages to bob when he can't decrypt them
await replicate(alice, bob).catch(t.error)
await alice.tribes2
.addMembers(groupId, [bobRoot.id, carolRoot.id])
.then(() => t.pass('added bob and carol'))
.catch((err) => t.error(err, 'add bob and carol fail'))
await alice.tribes2
.excludeMembers(groupId, [carolRoot.id])
.then(() => t.pass('alice excluded carol'))
.catch((err) => t.error(err, 'remove member fail'))
const { key: secondPostId } = await alice.tribes2
.publish({
type: 'test',
text: 'second post',
recps: [groupId],
})
.catch(t.fail)
// only replicate bob's invite to him once we're already on the new epoch
await replicate(alice, bob).catch(t.error)
const bobInvites = await pull(
bob.tribes2.listInvites(),
pull.collectAsPromise()
).catch(t.fail)
t.deepEquals(
bobInvites.map((invite) => invite.id),
[groupId],
'bob has an invite to the group'
)
t.equals(
bobInvites[0].readKeys.length,
2,
'there are 2 readKeys in the invite'
)
t.notEquals(
bobInvites[0].readKeys[0].key.toString('base64'),
bobInvites[0].readKeys[1].key.toString('base64'),
'the two readKeys are different'
)
await bob.tribes2.acceptInvite(groupId).catch(t.fail)
const bobGotFirstMsg = await p(bob.db.get)(firstPostId)
t.notEquals(
typeof bobGotFirstMsg.content,
'string',
"bob managed to decrypt alice's first message"
)
const bobGotSecondMsg = await p(bob.db.get)(secondPostId)
t.notEquals(
typeof bobGotSecondMsg.content,
'string',
"bob managed to decrypt alice's second message"
)
await p(alice.close)(true)
await p(bob.close)(true)
await p(carol.close)(true)
})
test('Can exclude a person in a group with a lot of members', async (t) => {
const alice = Testbot({
keys: ssbKeys.generate(null, 'alice'),
mfSeed: Buffer.from(
'000000000000000000000000000000000000000000000000000000000000a1ce',
'hex'
),
})
const peers = Array.from({ length: 20 }).map(() => Testbot())
async function sync() {
await Promise.all(peers.map((peer) => replicate(alice, peer)))
}
await alice.tribes2.start()
await Promise.all(peers.map((peer) => peer.tribes2.start()))
const peerRoots = await Promise.all(
peers.map((peer) => p(peer.metafeeds.findOrCreate)())
).catch((err) => t.error(err, 'Error getting root feeds for peers'))
const peerRootIds = peerRoots.map((root) => root.id)
const { id: groupId } = await alice.tribes2.create()
await sync()
await alice.tribes2.addMembers(groupId, peerRootIds.slice(0, 10))
await alice.tribes2.addMembers(groupId, peerRootIds.slice(10))
await sync()
await Promise.all(peers.map((peer) => peer.tribes2.acceptInvite(groupId)))
await alice.tribes2
.excludeMembers(groupId, [peerRootIds[0]])
.then(() =>
t.pass('alice was able to exclude bob in a group with many members')
)
.catch((err) =>
t.error(
err,
'alice was unable to exclude bob in a group with many members'
)
)
await sync()
const [bob, ...others] = peers
const bobGroup = await bob.tribes2.get(groupId)
t.deepEquals(
bobGroup,
{ id: groupId, excluded: true },
'bob is excluded from group'
)
await Promise.all(
others.map((other) => {
return (async () => {
const otherGroup = await other.tribes2.get(groupId)
if (otherGroup.excluded) throw 'got excluded'
if (otherGroup.readKeys.length !== 2) throw 'not enough readkeys'
return otherGroup
})()
})
)
.then(() => t.pass("Others didn't get excluded from the group"))
.catch(() => t.fail('Others got excluded from the group'))
await p(alice.close)(true)
await Promise.all(peers.map((peer) => p(peer.close)(true)))
})
test("restarting the client doesn't make us rejoin old stuff", async (t) => {
const alice = Testbot({
keys: ssbKeys.generate(null, 'alice'),
mfSeed: Buffer.from(
'000000000000000000000000000000000000000000000000000000000000a1ce',
'hex'
),
})
let bob = Testbot({
name: 'bobrestart',
keys: ssbKeys.generate(null, 'bob'),
mfSeed: Buffer.from(
'0000000000000000000000000000000000000000000000000000000000000b0b',
'hex'
),
})
await Promise.all([alice.tribes2.start(), bob.tribes2.start()])
const bobRoot = await p(bob.metafeeds.findOrCreate)()
await replicate(alice, bob).catch(t.error)
const { id: groupId } = await alice.tribes2
.create()
.catch((err) => t.error(err, 'alice failed to create group'))
await alice.tribes2
.addMembers(groupId, [bobRoot.id])
.then(() => t.pass('added bob'))
.catch((err) => t.error(err, 'add bob fail'))
await replicate(alice, bob).catch(t.error)
await bob.tribes2.acceptInvite(groupId)
await replicate(alice, bob).catch(t.error)
await alice.tribes2
.excludeMembers(groupId, [bobRoot.id])
.then(() => t.pass('alice excluded bob'))
.catch((err) => t.error(err, 'remove member fail'))
await replicate(alice, bob).catch(t.error)
t.deepEquals(
await bob.tribes2.get(groupId),
{ id: groupId, excluded: true },
"bob knows he's excluded from the group before restart"
)
// TODO remove probably?
await p(setTimeout)(3000)
await p(bob.close)(true).then(() => t.pass("bob's client was closed"))
bob = Testbot({
rimraf: false,
name: 'bobrestart',
keys: ssbKeys.generate(null, 'bob'),
mfSeed: Buffer.from(
'0000000000000000000000000000000000000000000000000000000000000b0b',
'hex'
),
})
t.pass('bob got a new client')
await bob.tribes2.start().then(() => t.pass('bob restarted'))
t.deepEquals(
await bob.tribes2.get(groupId),
{ id: groupId, excluded: true },
"bob knows he's excluded from the group after restart"
)
// TODO list()
// TODO listInvites()
// TODO acceptInvite()
await p(alice.close)(true)
await p(bob.close)(true)
})