git ssb

2+

ev / minsbot



Tree: 3d269c98d409d84e87895f410008a8df9e94f15d

Files: 3d269c98d409d84e87895f410008a8df9e94f15d / test / invite.js

7508 bytesRaw
1//WARNING: this test currently only passes
2//if the computer has a network.
3var sbot = require('../')
4var ssbKeys = require('ssb-keys')
5var tape = require('tape')
6var explain = require('explain-error')
7var pull = require('pull-stream')
8var u = require('../lib/util')
9var ssbClient = require('ssb-client')
10var ref = require('ssb-ref')
11
12var createSbot = require('../')
13 .use(require('../plugins/master'))
14 .use(require('../plugins/invite'))
15 .use(require('../plugins/replicate'))
16
17 .use(require('ssb-friends'))
18 .use(require('ssb-ws'))
19
20function all(stream, cb) {
21 return pull(stream, pull.collect(cb))
22}
23
24tape('test invite.accept api', function (t) {
25
26 var alice = createSbot({
27 temp: 'test-invite-alice2', timeout: 100,
28 allowPrivate: true,
29 keys: ssbKeys.generate()
30 })
31
32 var bob = createSbot({
33 temp: 'test-invite-bob2', timeout: 100,
34 keys: ssbKeys.generate()
35 })
36
37 var carol = createSbot({
38 temp: 'test-invite-carol2', timeout: 100,
39 keys: ssbKeys.generate()
40 })
41
42 //request a secret that with particular permissions.
43
44 alice.invite.create(1, function (err, invite) {
45 if(err) throw err
46 //test that invite is accepted with quotes around it.
47 console.log('INVITE', invite)
48 bob.invite.accept(JSON.stringify(invite), function (err) {
49 if(err) throw err
50 alice.friends.hops({
51 source: alice.id, dest: bob.id
52 }, function (err, hops) {
53 if(err) throw err
54 t.equal(hops[bob.id], 1, 'alice follows bob')
55 carol.invite.accept(invite, function (err) {
56 alice.friends.hops({
57 source: alice.id, dest: bob.id
58 }, function (err, hops) {
59 t.equal(hops[carol.id], undefined)
60 alice.close(true)
61 bob.close(true)
62 carol.close(true)
63 t.end()
64 })
65 })
66 })
67 })
68 })
69})
70
71tape('test invite.accept doesnt follow if already followed', function (t) {
72
73 var alice = createSbot({
74 temp: 'test-invite-alice3',
75 timeout: 100,
76 allowPrivate: true,
77 keys: ssbKeys.generate()
78 })
79
80 var bob = createSbot({
81 temp: 'test-invite-bob3',
82 timeout: 100,
83 keys: ssbKeys.generate()
84 })
85
86 //request a secret that with particular permissions.
87
88 alice.invite.create(2, function (err, invite) {
89 if(err) throw err
90 bob.invite.accept(invite, function (err) {
91 if(err) throw err
92 alice.friends.hops(alice.id, function (err, hops) {
93 if(err) throw err
94 console.log(hops)
95 t.equal(hops[bob.id], 1)
96 all(bob.messagesByType('pub'), function (err, ary) {
97 if(err) throw err
98 t.equal(ary.length, 1)
99
100 console.log(ary)
101 t.deepEqual({
102 type: 'pub',
103 address: ref.parseAddress(alice.address()),
104 }, ary[0].value.content)
105
106 all(bob.messagesByType('contact'), function (err, ary) {
107 if(err) throw err
108
109 console.log(ary)
110 t.equal(ary.length, 1)
111
112 t.deepEqual({
113 type: 'contact',
114 contact: alice.id,
115 autofollow: true,
116 following: true,
117 }, ary[0].value.content)
118
119
120 bob.invite.accept(invite, function (err) {
121 t.ok(err)
122 alice.friends.hops(alice.id, function (err, hops) {
123 console.log(hops)
124 t.equal(hops[bob.id], 1)
125 alice.close(true)
126 bob.close(true)
127 t.end()
128 })
129 })
130 })
131 })
132 })
133 })
134 })
135})
136
137if (process.env.TRAVIS === 'true') {
138 console.warn('IPv6 is unsupported under Travis CI, test skipped')
139 var skipIPv6 = true
140}
141
142tape('test invite.accept api with ipv6', { skip: skipIPv6 }, function (t) {
143
144 var alice = createSbot({
145 temp: 'test-invite-alice4', timeout: 100,
146 allowPrivate: true,
147 keys: ssbKeys.generate()
148 })
149
150 var bob = createSbot({
151 temp: 'test-invite-bob4', timeout: 100,
152 keys: ssbKeys.generate()
153 })
154
155 alice.invite.create(1, function (err, invite) {
156 if(err) throw err
157
158 // use a local ipv6 address in the invite
159
160 var inviteV6
161
162 = invite.replace(/localhost|([0-9.]*)/, '::1')
163 console.log(inviteV6, invite)
164
165 bob.invite.accept(inviteV6, function (err, msg) {
166 if(err) throw err
167 alice.friends.hops({
168 source: alice.id, dest: bob.id
169 }, function (err, hops) {
170 if(err) throw err
171 t.equal(hops[bob.id], 1, 'alice follows bob')
172 alice.close(true)
173 bob.close(true)
174 t.end()
175 })
176 })
177 })
178
179})
180
181tape('test invite.create with modern', function (t) {
182 var alice = createSbot({
183 temp: 'test-invite-alice5', timeout: 100,
184 allowPrivate: true,
185 keys: ssbKeys.generate()
186 })
187
188 var bob = createSbot({
189 temp: 'test-invite-bob5', timeout: 100,
190 keys: ssbKeys.generate()
191 })
192
193 var carol = createSbot({
194 temp: 'test-invite-carol5', timeout: 100,
195 keys: ssbKeys.generate()
196 })
197
198 //request a secret that with particular permissions.
199
200 alice.invite.create({modern: true}, function (err, invite) {
201 if(err) throw err
202 //test that invite is accepted with quotes around it.
203 t.ok(/^ws/.test(invite)) //should be over websockets
204 console.log(invite)
205 bob.invite.accept(JSON.stringify(invite), function (err, msg) {
206 if(err) throw err
207 alice.friends.hops({
208 source: alice.id, dest: bob.id
209 }, function (err, hops) {
210 if(err) throw err
211 t.equal(hops[bob.id], 1, 'alice follows bob')
212 carol.invite.accept(invite, function (err) {
213 t.ok(err)
214 alice.friends.hops({
215 source: alice.id, dest: bob.id
216 }, function (err, hops) {
217 if(err) throw err
218 t.equal(hops[carol.id], undefined)
219
220 console.log("END")
221 alice.close(true)
222 bob.close(true)
223 carol.close(true)
224
225 t.end()
226 })
227 })
228 })
229 })
230 })
231})
232
233
234tape('test invite.accept doesnt follow if already followed', function (t) {
235
236 var alice = createSbot({
237 temp: 'test-invite-alice6',
238 timeout: 100,
239 allowPrivate: true,
240 keys: ssbKeys.generate()
241 })
242
243 alice.publish({type: 'test', okay: true}, function (err, msg) {
244 if(err) throw err
245 console.log(msg)
246 alice.invite.create({modern: true}, function (err, invite) {
247 ssbClient(null, {
248 remote: invite,
249 manifest: {get: 'async', add: 'async'}
250 }, function (err, rpc) {
251 if(err) throw err
252 rpc.get(msg.key, function (err, value) {
253 t.ok(err)
254 console.log(value)
255 t.end()
256 alice.close()
257 })
258 })
259 })
260 })
261})
262
263
264tape('test invite with note', function (t) {
265
266 var alice = createSbot({
267 temp: 'test-invite-alice7', timeout: 100,
268 allowPrivate: true,
269 keys: ssbKeys.generate()
270 })
271
272 var bob = createSbot({
273 temp: 'test-invite-bob7', timeout: 100,
274 keys: ssbKeys.generate()
275 })
276
277 alice.invite.create({uses:1, note:'bob'}, function (err, invite) {
278 if(err) throw err
279 bob.invite.accept(invite, function (err) {
280 if(err) throw err
281
282 all(alice.messagesByType('contact'), function (err, ary) {
283 t.equal(ary.length, 1)
284
285 t.deepEqual({
286 type: 'contact',
287 contact: bob.id,
288 following: true,
289 pub: true,
290 note: 'bob',
291 }, ary[0].value.content)
292
293 alice.close(true)
294 bob.close(true)
295 t.end()
296 })
297 })
298 })
299})
300
301
302
303

Built with git-ssb-web