git ssb

0+

Dominic / ssb-peer-invites



Tree: 2584af0f0bc7b120e352272cee3f6a5f7ef14d2c

Files: 2584af0f0bc7b120e352272cee3f6a5f7ef14d2c / test / invite-with-pubs.js

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

Built with git-ssb-web