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