Files: aefe1c57d403d6caf0ca29793121221ed00ff9cf / test / block.js
5443 bytesRaw
1 | var cont = require('cont') |
2 | var tape = require('tape') |
3 | var pull = require('pull-stream') |
4 | var ssbKeys = require('ssb-keys') |
5 | var u = require('./util') |
6 | |
7 | var createSbot = require('../') |
8 | .use(require('../plugins/friends')) |
9 | .use(require('../plugins/block')) |
10 | .use(require('../plugins/replicate')) |
11 | |
12 | var toAddress = require('../lib/util').toAddress |
13 | |
14 | // alice, bob, and carol all follow each other, |
15 | // but then bob offends alice, and she blocks him. |
16 | // this means that: |
17 | // |
18 | // 1. when bob tries to connect to alice, she refuses. |
19 | // 2. alice never tries to connect to bob. (removed from peers) |
20 | // 3. carol will not give bob any, she will not give him any data from alice. |
21 | |
22 | var alice = createSbot({ |
23 | temp:'test-block-alice', timeout: 1400, |
24 | keys:ssbKeys.generate() |
25 | }) |
26 | |
27 | var bob = createSbot({ |
28 | temp: 'test-block-bob', timeout: 600, |
29 | keys:ssbKeys.generate() |
30 | }) |
31 | |
32 | var carol = createSbot({ |
33 | temp: 'test-block-carol', timeout: 600, |
34 | keys:ssbKeys.generate() |
35 | }) |
36 | |
37 | tape('alice blocks bob, and bob cannot connect to alice', function (t) { |
38 | |
39 | //in the beginning alice and bob follow each other |
40 | cont.para([ |
41 | cont(alice.publish)(u.follow(bob.id)), |
42 | cont(bob .publish)(u.follow(alice.id)), |
43 | cont(carol.publish)(u.follow(alice.id)) |
44 | ]) (function (err) { |
45 | if(err) throw err |
46 | |
47 | var n = 3, rpc |
48 | |
49 | bob.connect(alice.getAddress(), function (err, _rpc) { |
50 | if(err) throw err |
51 | //replication will begin immediately. |
52 | rpc = _rpc |
53 | next() |
54 | }) |
55 | |
56 | //get the next messages that are replicated to alice and bob, |
57 | //and check that these are the correct follow messages. |
58 | var bobCancel = bob.post(function (op) { |
59 | console.log('BOB_POST', op) |
60 | //should be the alice's follow(bob) message. |
61 | t.equal(op.value.author, alice.id) |
62 | t.equal(op.value.content.contact, bob.id) |
63 | next() |
64 | }) |
65 | |
66 | var aliceCancel = alice.post(function (op) { |
67 | console.log('ALICE_POST', op) |
68 | //should be the bob's follow(alice) message. |
69 | t.equal(op.value.author, bob.id) |
70 | t.equal(op.value.content.contact, alice.id) |
71 | next() |
72 | }) |
73 | |
74 | function next () { |
75 | if(--n) return |
76 | |
77 | rpc.close(true, function () { |
78 | aliceCancel(); bobCancel() |
79 | console.log('ALICE BLOCKS BOB', { |
80 | source: alice.id, dest: bob.id |
81 | }) |
82 | alice.publish(u.block(bob.id)) |
83 | (function (err) { |
84 | if(err) throw err |
85 | |
86 | t.ok(alice.friends.get({source: alice.id, dest: bob.id, graph: 'flag'})) |
87 | |
88 | pull( |
89 | alice.links({ |
90 | source: alice.id, |
91 | dest: bob.id, |
92 | rel: 'contact', |
93 | values: true |
94 | }), |
95 | pull.filter(function (op) { |
96 | return op.value.content.flagged != null |
97 | }), |
98 | pull.collect(function (err, ary) { |
99 | if(err) throw err |
100 | console.log(ary) |
101 | t.ok(flagged = ary.pop().value.content.flagged, 'alice did block bob') |
102 | |
103 | //since bob is blocked, he should not be able to connect |
104 | bob.connect(alice.getAddress(), function (err, rpc) { |
105 | t.ok(err, 'bob is blocked, should fail to connect to alice') |
106 | |
107 | |
108 | carol.post(function (msg) { |
109 | console.log('CAROL RECV', msg, alice.id) |
110 | if(msg.author === alice.id) { |
111 | if(msg.sequence == 2) |
112 | t.end() |
113 | } |
114 | }) |
115 | |
116 | //but carol, should, because she is not blocked. |
117 | carol.connect(alice.getAddress(), function (err, rpc) { |
118 | if(err) throw err |
119 | console.log('CAROL CONNECTED TO ALICE', carol.id, alice.id) |
120 | // pull( |
121 | // alice.createHistoryStream({id: alice.id, seq: 0}), |
122 | // pull.collect(console.log) |
123 | // ) |
124 | |
125 | rpc.on('closed', function () { |
126 | pull( |
127 | carol.createHistoryStream({id: alice.id, seq: 0, live: false}), |
128 | pull.collect(function (err, ary) { |
129 | if(err) throw err |
130 | |
131 | t.ok(ary.length, 'carol replicated data from alice') |
132 | console.log(alice.id, carol.id, err, ary) |
133 | t.end() |
134 | }) |
135 | ) |
136 | }) |
137 | }) |
138 | // carol.once('replicate:finish', function (vclock) { |
139 | // t.equal(vclock[alice.id], 2) |
140 | // //in next test, bob connects to carol... |
141 | // t.end() |
142 | // }) |
143 | }) |
144 | }) |
145 | ) |
146 | }) |
147 | }) |
148 | } |
149 | }) |
150 | }) |
151 | |
152 | tape('carol does not let bob replicate with alice', function (t) { |
153 | //first, carol should have already replicated with alice. |
154 | //emits this event when did not allow bob to get this data. |
155 | bob.once('replicate:finish', function (vclock) { |
156 | console.log('BOB REPLICATED FROM CAROL') |
157 | t.equal(vclock[alice.id], 1) |
158 | console.log('ALICE:', alice.id) |
159 | t.end() |
160 | }) |
161 | bob.connect(carol.getAddress(), function(err) { |
162 | if(err) throw err |
163 | }) |
164 | }) |
165 | |
166 | //TODO test that bob is disconnected from alice if he is connected |
167 | // and she blocks him. |
168 | |
169 | //TODO test that blocks work in realtime. if alice blocks him |
170 | // when he is already connected to alice's friend. |
171 | |
172 | tape('cleanup!', function (t) { |
173 | alice.close(true); bob.close(true); carol.close(true) |
174 | t.end() |
175 | }) |
176 | |
177 | |
178 | |
179 | |
180 | |
181 | |
182 | |
183 | |
184 |
Built with git-ssb-web