Files: fe51981dcffa9ecd1ade80ce5ce03f11c28229ab / test / accept.js
2755 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-friends')) |
25 | .use(require('../')) |
26 | |
27 | function all(stream, cb) { |
28 | return pull(stream, pull.collect(cb)) |
29 | } |
30 | |
31 | var alice = createSbot({ |
32 | temp: true, |
33 | timeout: 1000, |
34 | port: 12342, |
35 | keys:ssbKeys.generate(), |
36 | }) |
37 | var bob = createSbot({ |
38 | temp: true, |
39 | timeout: 1000, |
40 | port: 12343, |
41 | keys:ssbKeys.generate(), |
42 | }) |
43 | |
44 | tape('create an invite', function (t) { |
45 | |
46 | var seed = crypto.randomBytes(32) |
47 | |
48 | var content = I.createInvite(seed, alice.id, {name: 'bob'}, {text: 'welcome to ssb!'}) |
49 | alice.publish(content, function (err, msg) { |
50 | I.verifyInvitePublic(msg.value) |
51 | |
52 | createClient( |
53 | ssbKeys.generate(null, seed), |
54 | { |
55 | remote: alice.getAddress(), |
56 | caps: require('ssb-config').caps, |
57 | manifest: { |
58 | userInvites: { |
59 | getInvite: 'async', |
60 | accept: 'async' |
61 | } |
62 | } |
63 | }, |
64 | function (err, _bob) { |
65 | if(err) throw err |
66 | _bob.userInvites.getInvite(msg.key, function (err, invite) { |
67 | if(err) throw err |
68 | t.ok(invite) |
69 | t.deepEqual(invite, msg.value) |
70 | //check this invite is valid. would throw if it wasn't. |
71 | I.verifyInvitePrivate(invite, seed) |
72 | |
73 | //bob chooses to accept this invite. |
74 | var accept_content = I.createAccept(invite, seed, bob.id) |
75 | |
76 | bob.publish(accept_content, function (err, accept) { |
77 | if(err) throw err |
78 | _bob.userInvites.accept(accept.value, function (err, msg) { |
79 | if(err) throw err |
80 | t.ok(msg) |
81 | var confirm_id = '%'+ssbKeys.hash(JSON.stringify(msg, null, 2)) |
82 | alice.get(confirm_id, function (err, _msg) { |
83 | if(err) throw err |
84 | t.deepEqual(msg, _msg) |
85 | |
86 | |
87 | //calling accept again should return the previous accept message. |
88 | _bob.userInvites.accept(accept.value, function (err, msg2) { |
89 | if(err) throw err |
90 | t.deepEqual(msg2, msg) |
91 | alice.close() |
92 | bob.close() |
93 | t.end() |
94 | }) |
95 | }) |
96 | }) |
97 | }) |
98 | }) |
99 | } |
100 | ) |
101 | }) |
102 | }) |
103 | |
104 |
Built with git-ssb-web