git ssb

0+

Dominic / ssb-peer-invites



Tree: 3c6e9ba6132fbd1415505276a00c24a5126918a1

Files: 3c6e9ba6132fbd1415505276a00c24a5126918a1 / test / accept.js

2973 bytesRaw
1//WARNING: this test currently only passes
2//if the computer has a network.
3var crypto = require('crypto')
4var I = require('../valid')
5var createClient = require('ssb-client')
6
7
8var ssbKeys = require('ssb-keys')
9var tape = require('tape')
10//var explain = require('explain-error')
11var pull = require('pull-stream')
12//var u = require('../lib/util')
13var ref = require('ssb-ref')
14
15var createSbot = require('scuttlebot')
16 .use(require('ssb-links'))
17 .use({
18 name: 'replicate', version: '1.0.0',
19 manifest: { request: 'sync' },
20 init: function () {
21 return { request: function () {} }
22 }
23 })
24 .use(require('ssb-friends'))
25 .use(require('../'))
26
27function all(stream, cb) {
28 return pull(stream, pull.collect(cb))
29}
30
31var caps = {
32 sign: crypto.randomBytes(32),//.toString('base64'),
33 userInvite: crypto.randomBytes(32),//.toString('base64'),
34 shs: crypto.randomBytes(32),//.toString('base64'),
35}
36
37var alice = createSbot({
38 temp: true,
39 timeout: 1000,
40 port: 12342,
41 keys:ssbKeys.generate(),
42 caps: caps
43})
44var bob = createSbot({
45 temp: true,
46 timeout: 1000,
47 port: 12343,
48 keys:ssbKeys.generate(),
49 caps: caps
50})
51
52tape('create an invite', function (t) {
53
54 var seed = crypto.randomBytes(32)
55
56 var content = I.createInvite(seed, alice.id, {name: 'bob'}, {text: 'welcome to ssb!'}, caps)
57 alice.publish(content, function (err, msg) {
58 I.verifyInvitePublic(msg.value, caps)
59
60 createClient(
61 ssbKeys.generate(null, seed),
62 {
63 remote: alice.getAddress(),
64 caps: caps,
65 manifest: {
66 userInvites: {
67 getInvite: 'async',
68 confirm: 'async'
69 }
70 }
71 },
72 function (err, _bob) {
73 if(err) throw err
74 _bob.userInvites.getInvite(msg.key, function (err, invite) {
75 if(err) throw err
76 t.ok(invite)
77 t.deepEqual(invite, msg.value)
78 //check this invite is valid. would throw if it wasn't.
79 I.verifyInvitePrivate(invite, seed, caps)
80
81 //bob chooses to accept this invite.
82 var accept_content = I.createAccept(invite, seed, bob.id, caps)
83
84 bob.publish(accept_content, function (err, accept) {
85 if(err) throw err
86 _bob.userInvites.confirm(accept.value, function (err, msg) {
87 if(err) throw err
88 t.ok(msg)
89 var confirm_id = '%'+ssbKeys.hash(JSON.stringify(msg, null, 2))
90 alice.get(confirm_id, function (err, _msg) {
91 if(err) throw err
92 t.deepEqual(msg, _msg)
93
94
95 //calling accept again should return the previous accept message.
96 _bob.userInvites.confirm(accept.value, function (err, msg2) {
97 if(err) throw err
98 t.deepEqual(msg2, msg)
99 alice.close()
100 bob.close()
101 t.end()
102 })
103 })
104 })
105 })
106 })
107 }
108 )
109 })
110})
111
112
113
114
115
116

Built with git-ssb-web