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