Files: 66c545deb943d0b40249722b3d1b125249d6bcde / test / accept2.js
3154 bytesRaw
1 | //WARNING: this test currently only passes |
2 | //if the computer has a network. |
3 | var crypto = require('crypto') |
4 | var I = require('../valid') |
5 | var createClient = require('ssb-client') |
6 | |
7 | |
8 | var ssbKeys = require('ssb-keys') |
9 | var tape = require('tape') |
10 | //var explain = require('explain-error') |
11 | var pull = require('pull-stream') |
12 | //var u = require('../lib/util') |
13 | var ref = require('ssb-ref') |
14 | |
15 | var 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-query')) |
25 | .use(require('ssb-device-address')) |
26 | .use(require('ssb-identities')) |
27 | .use(require('ssb-friends')) |
28 | .use(require('../')) |
29 | |
30 | function all(stream, cb) { |
31 | return pull(stream, pull.collect(cb)) |
32 | } |
33 | |
34 | var alice = createSbot({ |
35 | temp: true, |
36 | timeout: 1000, |
37 | port: 12342, |
38 | keys:ssbKeys.generate(), |
39 | }) |
40 | var bob = createSbot({ |
41 | temp: true, |
42 | timeout: 1000, |
43 | port: 12343, |
44 | keys:ssbKeys.generate(), |
45 | }) |
46 | |
47 | function toId(msg) { |
48 | return '%'+ssbKeys.hash(JSON.stringify(msg, null, 2)) |
49 | } |
50 | |
51 | tape('create an invite', function (t) { |
52 | |
53 | var seed = crypto.randomBytes(32) |
54 | |
55 | var content = I.createInvite(seed, alice.id, {name: 'bob'}, {text: 'welcome to ssb!'}) |
56 | // alice.publish(content, function (err, msg) { |
57 | // I.verifyInvitePublic(msg.value) |
58 | |
59 | alice.userInvites.create({}, function (err, invite) { |
60 | if(err) throw err |
61 | var seed = invite.seed |
62 | var invite_id = invite.invite |
63 | |
64 | createClient( |
65 | ssbKeys.generate(null, seed), |
66 | { |
67 | remote: alice.getAddress(), |
68 | caps: require('ssb-config').caps, |
69 | manifest: { |
70 | userInvites: { |
71 | getInvite: 'async', |
72 | confirm: 'async' |
73 | } |
74 | } |
75 | }, |
76 | function (err, _bob) { |
77 | if(err) throw err |
78 | _bob.userInvites.getInvite(invite_id, function (err, invite_msg) { |
79 | if(err) throw err |
80 | t.ok(invite) |
81 | t.equal(toId(invite_msg), invite_id) |
82 | var data = I.verifyInvitePrivate(invite_msg, seed) |
83 | t.deepEqual(data, {reveal: undefined, private: undefined}) |
84 | //check this invite is valid. would throw if it wasn't. |
85 | |
86 | //bob chooses to accept this invite. |
87 | var accept_content = I.createAccept(invite_msg, seed, bob.id) |
88 | |
89 | bob.publish(accept_content, function (err, accept) { |
90 | if(err) throw err |
91 | _bob.userInvites.confirm(accept.value, function (err, msg) { |
92 | if(err) throw err |
93 | t.ok(msg) |
94 | var confirm_id = toId(msg) |
95 | alice.get(confirm_id, function (err, _msg) { |
96 | if(err) throw err |
97 | t.deepEqual(msg, _msg) |
98 | |
99 | |
100 | //calling accept again should return the previous accept message. |
101 | _bob.userInvites.confirm(accept.value, function (err, msg2) { |
102 | if(err) throw err |
103 | t.deepEqual(msg2, msg) |
104 | alice.close() |
105 | bob.close() |
106 | t.end() |
107 | }) |
108 | }) |
109 | }) |
110 | }) |
111 | }) |
112 | } |
113 | ) |
114 | }) |
115 | }) |
116 | |
117 | |
118 | |
119 | |
120 | |
121 | |
122 | |
123 | |
124 |
Built with git-ssb-web