git ssb

4+

Dominic / scuttlebot



Tree: 3f99bb4d8aafc51133acb53c3ff0b7d52f71aa1a

Files: 3f99bb4d8aafc51133acb53c3ff0b7d52f71aa1a / test / invite.js

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

Built with git-ssb-web