git ssb

0+

Dominic / ssb-peer-invites



Tree: 6868da9f177810f436109565b2ebf80b78fe44da

Files: 6868da9f177810f436109565b2ebf80b78fe44da / test / accept5.js

2838 bytesRaw
1var I = require('../valid')
2var u = require('../util')
3
4var ssbKeys = require('ssb-keys')
5var tape = require('tape')
6
7var createSbot = require('ssb-server')
8 .use(require('ssb-links'))
9 .use({
10 name: 'replicate', version: '1.0.0',
11 manifest: { request: 'sync' },
12 init: function () {
13 return { request: function () {} }
14 }
15 })
16 .use(require('ssb-query'))
17 .use(require('ssb-device-address'))
18 .use(require('ssb-identities'))
19 .use(require('ssb-friends'))
20 .use(require('../'))
21
22function toId(msg) {
23 return '%'+ssbKeys.hash(JSON.stringify(msg, null, 2))
24}
25
26var caps = require('./randcaps')()
27
28var alice = createSbot({
29 temp: true,
30 timeout: 1000,
31 port: 12342,
32 keys:ssbKeys.generate(),
33 caps: caps
34})
35var bob = createSbot({
36 temp: true,
37 timeout: 1000,
38 port: 12343,
39 keys:ssbKeys.generate(),
40 caps: caps
41})
42
43tape('create an invite (accept5)', function (t) {
44
45 alice.peerInvites.create({allowWithoutPubs: true}, function (err, _invite) {
46 if(err) throw err
47 var invite = u.parse(_invite)
48 var seed = invite.seed
49 var invite_id = invite.invite
50
51 //use device address, just for tests
52 invite.pubs.push(alice.getAddress('device'))
53 t.ok(invite)
54
55 bob.peerInvites.openInvite(u.stringify(invite), function (err, data) {
56 if(err) throw err
57 var invite_msg = data.value
58 var opened = data.opened
59 t.equal(toId(invite_msg), invite_id)
60 t.deepEqual(opened, {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

Built with git-ssb-web