git ssb

0+

Dominic / ssb-peer-invites



Commit 2584af0f0bc7b120e352272cee3f6a5f7ef14d2c

more test coverage!

Dominic Tarr committed on 12/21/2018, 1:06:32 AM
Parent: d8e63bc4e3cc6ca4565996990f626ed2a9742fd6

Files changed

test/accept2.jschanged
test/accept3.jschanged
test/accept4.jschanged
test/accept5.jschanged
test/error-without-pubs.jsadded
test/invite-with-pubs.jsadded
test/accept2.jsView
@@ -61,9 +61,9 @@
6161 // var content = I.createInvite(seed, alice.id, {name: 'bob'}, {text: 'welcome to ssb!'})
6262 // alice.publish(content, function (err, msg) {
6363 // I.verifyInvitePublic(msg.value)
6464
65- alice.userInvites.create({}, function (err, invite) {
65 + alice.userInvites.create({allowWithoutPubs: true}, function (err, invite) {
6666 if(err) throw err
6767 var seed = invite.seed
6868 var invite_id = invite.invite
6969
test/accept3.jsView
@@ -63,9 +63,9 @@
6363 //in this test, we use a separate identity to create the invite,
6464 //to test multiple identity support, and also simulate confirmation by pub.
6565 alice.identities.create(function (err, carol_id) {
6666 if(err) throw err
67- alice.userInvites.create({id: carol_id}, function (err, invite) {
67 + alice.userInvites.create({id: carol_id, allowWithoutPubs: true}, function (err, invite) {
6868 if(err) throw err
6969 var seed = invite.seed
7070 var invite_id = invite.invite
7171
test/accept4.jsView
@@ -55,9 +55,9 @@
5555 })
5656
5757 tape('create an invite', function (t) {
5858
59- alice.userInvites.create({}, function (err, invite) {
59 + alice.userInvites.create({allowWithoutPubs: true}, function (err, invite) {
6060 if(err) throw err
6161 var seed = invite.seed
6262 var invite_id = invite.invite
6363
test/accept5.jsView
@@ -55,9 +55,9 @@
5555 })
5656
5757 tape('create an invite', function (t) {
5858
59- alice.userInvites.create({}, function (err, invite) {
59 + alice.userInvites.create({allowWithoutPubs: true}, function (err, invite) {
6060 if(err) throw err
6161 var seed = invite.seed
6262 var invite_id = invite.invite
6363
test/error-without-pubs.jsView
@@ -1,0 +1,76 @@
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 pull = require('pull-stream')
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-query'))
23 + .use(require('ssb-device-address'))
24 + .use(require('ssb-identities'))
25 + .use(require('ssb-friends'))
26 + .use(require('../'))
27 +
28 +function all(stream, cb) {
29 + return pull(stream, pull.collect(cb))
30 +}
31 +
32 +var caps = {
33 + sign: crypto.randomBytes(32),//.toString('base64'),
34 + userInvite: crypto.randomBytes(32),//.toString('base64'),
35 + shs: crypto.randomBytes(32),//.toString('base64'),
36 +}
37 +
38 +var alice = createSbot({
39 + temp: true,
40 + timeout: 1000,
41 + port: 12342,
42 + keys:ssbKeys.generate(),
43 + caps: caps
44 +})
45 +
46 +function toId(msg) {
47 + return '%'+ssbKeys.hash(JSON.stringify(msg, null, 2))
48 +}
49 +
50 +tape('create an invite', function (t) {
51 +
52 + var seed = crypto.randomBytes(32)
53 +
54 + //without the pubs option, do not allow creating
55 + //an invite.
56 + alice.userInvites.create({}, function (err, invite) {
57 + t.ok(err)
58 + alice.close()
59 + t.end()
60 + })
61 +})
62 +
63 +
64 +
65 +
66 +
67 +
68 +
69 +
70 +
71 +
72 +
73 +
74 +
75 +
76 +
test/invite-with-pubs.jsView
@@ -1,0 +1,138 @@
1 +var crypto = require('crypto')
2 +var createClient = require('ssb-client')
3 +var explain = require('explain-error')
4 +
5 +var ssbKeys = require('ssb-keys')
6 +var tape = require('tape')
7 +var pull = require('pull-stream')
8 +var ref = require('ssb-ref')
9 +
10 +var 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 +
20 +function all(stream, cb) {
21 + return pull(stream, pull.collect(cb))
22 +}
23 +
24 +var caps = {
25 + sign: crypto.randomBytes(32),
26 + userInvite: crypto.randomBytes(32),
27 + shs: crypto.randomBytes(32),
28 +}
29 +
30 +var alice = createSbot({
31 + temp: true,
32 + timeout: 1000,
33 + port: 12342,
34 + keys:ssbKeys.generate(),
35 + caps: caps
36 +})
37 +
38 +var bob = createSbot({
39 + temp: true,
40 + timeout: 1000,
41 + port: 12343,
42 + keys:ssbKeys.generate(),
43 + caps: caps
44 +})
45 +
46 +var carol = createSbot({
47 + temp: true,
48 + timeout: 1000,
49 + port: 12344,
50 + keys:ssbKeys.generate(),
51 + caps: caps
52 +})
53 +
54 +tape('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 +
88 +var invite
89 +tape('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 +
110 +tape('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.
130 +tape('cleanup', function (t) {
131 + setTimeout(function () {
132 + alice.close()
133 + carol.close()
134 + bob.close()
135 + t.end()
136 + }, 1000)
137 +})
138 +

Built with git-ssb-web