git ssb

0+

Dominic / ssb-peer-invites



Tree: 0e94992aa8bedac43906183f616bff699ec2484b

Files: 0e94992aa8bedac43906183f616bff699ec2484b / test / accept5.js

2841 bytesRaw
1var crypto = require('crypto')
2var I = require('../valid')
3var u = require('../util')
4
5var ssbKeys = require('ssb-keys')
6var tape = require('tape')
7var pull = require('pull-stream')
8
9var createSbot = require('ssb-server')
10 .use(require('ssb-links'))
11 .use({
12 name: 'replicate', version: '1.0.0',
13 manifest: { request: 'sync' },
14 init: function () {
15 return { request: function () {} }
16 }
17 })
18 .use(require('ssb-query'))
19 .use(require('ssb-device-address'))
20 .use(require('ssb-identities'))
21 .use(require('ssb-friends'))
22 .use(require('../'))
23
24function toId(msg) {
25 return '%'+ssbKeys.hash(JSON.stringify(msg, null, 2))
26}
27
28var caps = require('./randcaps')()
29
30var alice = createSbot({
31 temp: true,
32 timeout: 1000,
33 port: 12342,
34 keys:ssbKeys.generate(),
35 caps: caps
36})
37var bob = createSbot({
38 temp: true,
39 timeout: 1000,
40 port: 12343,
41 keys:ssbKeys.generate(),
42 caps: caps
43})
44
45tape('create an invite', function (t) {
46
47 alice.peerInvites.create({allowWithoutPubs: true}, function (err, _invite) {
48 if(err) throw err
49 var invite = u.parse(_invite)
50 var seed = invite.seed
51 var invite_id = invite.invite
52
53 //use device address, just for tests
54 invite.pubs.push(alice.getAddress('device'))
55
56 bob.peerInvites.openInvite(u.stringify(invite), function (err, invite_msg, data) {
57 if(err) throw err
58 t.ok(invite)
59 t.equal(toId(invite_msg), invite_id)
60 t.deepEqual(data, {reveal: undefined, private: undefined})
61
62 //bob publishes accept_content manually. simulates that he crashed
63 //before causing confirm.
64 var accept_content = I.createAccept(invite_msg, seed, bob.id, caps)
65 bob.publish(accept_content, function (err, accept) {
66 if(err) throw err
67
68 //alice manually creates confrim, to simulate receiving it, but crashing
69 //before bob receives it back, and so he calls again.
70
71 alice.publish(I.createConfirm(accept.value), function (err, _confirm) {
72 if(err) throw err
73 bob.peerInvites.acceptInvite(invite, function (err, confirm) {
74 if(err) throw err
75 //alice returns the same confirm message, does not create a new one
76 t.equal(toId(confirm), toId(_confirm.value), 'id is equal')
77 t.deepEqual(confirm, _confirm.value)
78
79 //check that alice and bob both understand the other to be following them.
80 bob.friends.hops({reverse: true}, function (err, hops) {
81 if(err) throw err
82 t.equal(hops[alice.id], 1)
83 alice.friends.hops({reverse: true}, function (err, hops) {
84 if(err) throw err
85 t.equal(hops[bob.id], 1)
86 alice.close()
87 bob.close()
88 t.end()
89 })
90 })
91 })
92 })
93 })
94 })
95 })
96})
97
98

Built with git-ssb-web