Files: bbe62fbce9b33aecd11d216692e3de644aa62f80 / test / invite-with-pubs.js
3509 bytesRaw
1 | var crypto = require('crypto') |
2 | var createClient = require('ssb-client') |
3 | var explain = require('explain-error') |
4 | |
5 | var ssbKeys = require('ssb-keys') |
6 | var tape = require('tape') |
7 | var pull = require('pull-stream') |
8 | var ref = require('ssb-ref') |
9 | var u = require('../util') |
10 | |
11 | var createSbot = require('scuttlebot') |
12 | .use(require('ssb-links')) |
13 | .use(require('scuttlebot/plugins/replicate')) |
14 | .use(require('scuttlebot/plugins/gossip')) |
15 | .use(require('ssb-query')) |
16 | .use(require('ssb-device-address')) |
17 | .use(require('ssb-identities')) |
18 | .use(require('ssb-friends')) |
19 | .use(require('../')) |
20 | |
21 | function all(stream, cb) { |
22 | return pull(stream, pull.collect(cb)) |
23 | } |
24 | |
25 | var caps = { |
26 | sign: crypto.randomBytes(32), |
27 | peerInvite: crypto.randomBytes(32), |
28 | shs: crypto.randomBytes(32), |
29 | } |
30 | |
31 | var alice = createSbot({ |
32 | temp: true, |
33 | timeout: 1000, |
34 | port: 12342, |
35 | keys:ssbKeys.generate(), |
36 | caps: caps |
37 | }) |
38 | |
39 | var bob = createSbot({ |
40 | temp: true, |
41 | timeout: 1000, |
42 | port: 12343, |
43 | keys:ssbKeys.generate(), |
44 | caps: caps |
45 | }) |
46 | |
47 | var carol = createSbot({ |
48 | temp: true, |
49 | timeout: 1000, |
50 | port: 12344, |
51 | keys:ssbKeys.generate(), |
52 | caps: caps |
53 | }) |
54 | |
55 | tape('setup', function (t) { |
56 | |
57 | //once alice has 3 messages (one from her, and two from carol) |
58 | //can move to next test. |
59 | var a = 3 |
60 | alice.post(function (data) { |
61 | if(--a) return |
62 | |
63 | //HACK: wait for servers to start |
64 | setTimeout(function () { t.end() }, 1000) |
65 | }) |
66 | |
67 | carol.deviceAddress.announce({ |
68 | address:carol.getAddress('device'), |
69 | availability: 1 |
70 | }, function (err, msg) { |
71 | if(err) throw err |
72 | t.ok(msg) |
73 | alice.publish({ |
74 | type: 'contact', contact: carol.id, following: true |
75 | }, function (err, msg) { |
76 | if(err) throw err |
77 | t.ok(msg) |
78 | carol.publish({ |
79 | type: 'contact', contact: alice.id, following: true |
80 | }, function (err, msg) { |
81 | if(err) throw err |
82 | t.ok(msg) |
83 | alice.connect(carol.getAddress(), function (err) { |
84 | if(err) throw err |
85 | }) |
86 | }) |
87 | }) |
88 | }) |
89 | }) |
90 | |
91 | tape('getNearbyPubs', function (t) { |
92 | alice.peerInvites.getNearbyPubs({}, function (err, pubs) { |
93 | if(err) throw err |
94 | t.ok(pubs.length) |
95 | t.end() |
96 | }) |
97 | }) |
98 | |
99 | var invite |
100 | tape('create-invite, with automatic pubs', function (t) { |
101 | var n = 1 |
102 | //wait until carol has received alice's invite |
103 | carol.post(function (data) { |
104 | if(data.value.content.type === 'peer-invite') { |
105 | console.log('invit?', data) |
106 | if(--n) return |
107 | t.end() |
108 | } |
109 | }) |
110 | |
111 | setTimeout(function () { |
112 | alice.peerInvites.create({}, function (err, _invite) { |
113 | if(err) throw err |
114 | console.log('create invite') |
115 | invite = u.parse(_invite) |
116 | console.log(_invite) |
117 | console.log(invite) |
118 | }) |
119 | }) |
120 | |
121 | }) |
122 | |
123 | tape('accept invite', function (t) { |
124 | alice.get(invite.invite, function (err, invite_msg) { |
125 | if(err) throw err |
126 | t.deepEqual(invite.pubs, [carol.getAddress('device')]) |
127 | |
128 | bob.peerInvites.openInvite(invite, function (err, _invite_msg) { |
129 | if(err) throw explain(err, 'error while opening invite') |
130 | t.deepEqual(_invite_msg, invite_msg) |
131 | bob.peerInvites.acceptInvite(invite, function (err) { |
132 | if(err) throw err |
133 | t.end() |
134 | }) |
135 | }) |
136 | }) |
137 | }) |
138 | |
139 | //there is another race here. seems flumedb |
140 | //doesn't like it if you close and immediately |
141 | //it receives a message. (should just drop that though) |
142 | //we don't need to fix that just to get peer-invites working, though. |
143 | tape('cleanup', function (t) { |
144 | setTimeout(function () { |
145 | alice.close() |
146 | carol.close() |
147 | bob.close() |
148 | t.end() |
149 | }, 1000) |
150 | }) |
151 | |
152 | |
153 | |
154 |
Built with git-ssb-web