git ssb

4+

Dominic / scuttlebot



Tree: a5863ed6af73f89e56c11e0019e14538eb5c7cae

Files: a5863ed6af73f89e56c11e0019e14538eb5c7cae / test / invite.js

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

Built with git-ssb-web