UNPKG

11.8 kBJavaScriptView Raw
1var request = require('superagent');
2var assert = require('assert');
3var expect = require('expect.js');
4var uuid = require('uuid');
5var async = require('async');
6
7/*
8user one -> user two, user two accept, then user one delete
9user one -> user three, user one cancel
10user one -> user four, user four decline
11user two -> user three, user three accept, then user 2 block
12user three -> user four, user four accept, then user four delete
13*/
14
15describe('friends', function () {
16 this.timeout(50000);
17
18 var client1 = request.agent();
19 var client2 = request.agent();
20 var client3 = request.agent();
21 var client4 = request.agent();
22 var client5 = request.agent();
23
24 var userTwoId;
25
26 var endpoint1 = 'http://127.0.0.1:3000/antisocial/';
27 var endpoint2 = 'http://127.0.0.1:3000/antisocial/';
28 var endpoint3 = 'http://127.0.0.1:3000/antisocial/';
29 var endpoint4 = 'http://127.0.0.1:3000/antisocial/';
30 var endpoint5 = 'http://127.0.0.1:3000/antisocial/';
31 var endpointBad = 'http://127.0.0.1:3000/antisocial/bad';
32
33 var app = require('../app');
34
35 before(function (done) {
36 app.start(3000);
37 done();
38 });
39
40 after(function (done) {
41 setTimeout(function () {
42 //console.log('users: %j', app.db.collections.users);
43 //console.log('invitations: %j', app.db.collections.invitations);
44 //console.log('friends: %j', app.db.collections.friends);
45 //console.log('blocks: %j', app.db.collections.blocks);
46 //console.log('postIdMap: %j highwaterMap: %j', app.postIdMap, app.highwaterMap);
47 app.stop();
48 done();
49 }, 10000);
50 });
51
52 it('should be able to create account 1', function (done) {
53 client1.post('http://127.0.0.1:3000/register')
54 .type('form')
55 .send({
56 'name': 'user one',
57 'username': 'user-one'
58 })
59 .end(function (err, res) {
60 expect(err).to.be(null);
61 expect(res.status).to.equal(200);
62 var accessToken = getCookie(res.headers['set-cookie'], 'access_token');
63 expect(accessToken).to.be.a('string');
64 endpoint1 += res.body.result.username;
65 done();
66 });
67 });
68
69 it('should be able to create account 2', function (done) {
70 client2.post('http://127.0.0.1:3000/register')
71 .type('form')
72 .send({
73 'name': 'user two',
74 'username': 'user-two'
75 })
76 .end(function (err, res) {
77 expect(err).to.be(null);
78 expect(res.status).to.equal(200);
79 var accessToken = getCookie(res.headers['set-cookie'], 'access_token');
80 expect(accessToken).to.be.a('string');
81 endpoint2 += res.body.result.username;
82 userTwoId = res.body.result.id;
83 done();
84 });
85 });
86
87 it('should be able to create account 3', function (done) {
88 client3.post('http://127.0.0.1:3000/register')
89 .type('form')
90 .send({
91 'name': 'user three',
92 'username': 'user-three'
93 })
94 .end(function (err, res) {
95 expect(err).to.be(null);
96 expect(res.status).to.equal(200);
97 var accessToken = getCookie(res.headers['set-cookie'], 'access_token');
98 expect(accessToken).to.be.a('string');
99 endpoint3 += res.body.result.username;
100 done();
101 });
102 });
103
104 it('should be able to create account 4', function (done) {
105 client4.post('http://127.0.0.1:3000/register')
106 .type('form')
107 .send({
108 'name': 'user four',
109 'username': 'user-four'
110 })
111 .end(function (err, res) {
112 expect(err).to.be(null);
113 expect(res.status).to.equal(200);
114 var accessToken = getCookie(res.headers['set-cookie'], 'access_token');
115 expect(accessToken).to.be.a('string');
116 endpoint4 += res.body.result.username;
117 done();
118 });
119 });
120
121 it('should be able to create account 5 (community)', function (done) {
122 client5.post('http://127.0.0.1:3000/register')
123 .type('form')
124 .send({
125 'name': 'test community',
126 'username': 'test-community',
127 'community': true
128 })
129 .end(function (err, res) {
130 expect(err).to.be(null);
131 expect(res.status).to.equal(200);
132 var accessToken = getCookie(res.headers['set-cookie'], 'access_token');
133 expect(accessToken).to.be.a('string');
134 endpoint5 += res.body.result.username;
135 done();
136 });
137 });
138
139
140 it('user1 should be able to request friend user2', function (done) {
141 client1.get('http://127.0.0.1:3000/antisocial/user-one/request-friend?endpoint=' + endpoint2).end(function (err, res) {
142 expect(res.status).to.be(200);
143 expect(res.body.status).to.equal('ok');
144 done();
145 });
146 });
147
148 it('user1 should be able to request friend user3', function (done) {
149 client1.get('http://127.0.0.1:3000/antisocial/user-one/request-friend?endpoint=' + endpoint3).end(function (err, res) {
150 expect(res.status).to.be(200);
151 expect(res.body.status).to.equal('ok');
152 done();
153 });
154 });
155
156 it('user1 should be able to request friend user4', function (done) {
157 client1.get('http://127.0.0.1:3000/antisocial/user-one/request-friend?endpoint=' + endpoint4).end(function (err, res) {
158 expect(res.status).to.be(200);
159 expect(res.body.status).to.equal('ok');
160 done();
161 });
162 });
163
164 it('user2 should be able to request friend user3', function (done) {
165 client2.get('http://127.0.0.1:3000/antisocial/user-two/request-friend?endpoint=' + endpoint3).end(function (err, res) {
166 expect(res.status).to.be(200);
167 expect(res.body.status).to.equal('ok');
168 done();
169 });
170 });
171
172 it('user3 should be able to request friend user4', function (done) {
173 client3.get('http://127.0.0.1:3000/antisocial/user-three/request-friend?endpoint=' + endpoint4).end(function (err, res) {
174 expect(res.status).to.be(200);
175 expect(res.body.status).to.equal('ok');
176 done();
177 });
178 });
179
180 it('user1 should not be able to request friend user2 again', function (done) {
181 client1.get('http://127.0.0.1:3000/antisocial/user-one/request-friend?endpoint=' + endpoint2).end(function (err, res) {
182 expect(res.status).to.be(200);
183 expect(res.body.status).to.equal('error');
184 done();
185 });
186 });
187
188 it('user1 should not be able to request friend unknown user', function (done) {
189 client1.get('http://127.0.0.1:3000/antisocial/user-one/request-friend?endpoint=' + encodeURIComponent(endpointBad)).end(function (err, res) {
190 expect(res.status).to.be(200);
191 expect(res.body.status).to.not.equal('ok');
192 done();
193 });
194 });
195
196 it('user2 should be able to accept friend request from user1', function (done) {
197 client2.post('http://127.0.0.1:3000/antisocial/user-two/friend-request-accept')
198 .type('form')
199 .send({
200 'endpoint': endpoint1
201 }).end(function (err, res) {
202 expect(res.status).to.be(200);
203 expect(res.body.status).to.equal('ok');
204 done();
205 });
206 });
207
208 it('user2 should not be able to accept friend request that is already accepted', function (done) {
209 client2.post('http://127.0.0.1:3000/antisocial/user-two/friend-request-accept')
210 .type('form')
211 .send({
212 'endpoint': endpoint1
213 }).end(function (err, res) {
214 expect(res.status).to.be(200);
215 expect(res.body.status).to.equal('error');
216 done();
217 });
218 });
219
220 it('user3 should be able to accept friend request from user2', function (done) {
221 client3.post('http://127.0.0.1:3000/antisocial/user-three/friend-request-accept')
222 .type('form')
223 .send({
224 'endpoint': endpoint2
225 }).end(function (err, res) {
226 expect(res.status).to.be(200);
227 expect(res.body.status).to.equal('ok');
228 done();
229 });
230 });
231
232 it('user4 should be able to accept friend request from user3', function (done) {
233 client4.post('http://127.0.0.1:3000/antisocial/user-four/friend-request-accept')
234 .type('form')
235 .send({
236 'endpoint': endpoint3
237 }).end(function (err, res) {
238 expect(res.status).to.be(200);
239 expect(res.body.status).to.equal('ok');
240 done();
241 });
242 });
243
244 it('user1 should be able to cancel friend request to user3', function (done) {
245 client1.post('http://127.0.0.1:3000/antisocial/user-one/request-friend-cancel')
246 .type('form')
247 .send({
248 'endpoint': endpoint3
249 }).end(function (err, res) {
250 expect(res.status).to.be(200);
251 expect(res.body.status).to.equal('ok');
252 done();
253 });
254 });
255
256 it('user4 should be able to decline friend request from user1', function (done) {
257 client4.post('http://127.0.0.1:3000/antisocial/user-four/friend-request-decline')
258 .type('form')
259 .send({
260 'endpoint': endpoint1
261 }).end(function (err, res) {
262 expect(res.status).to.be(200);
263 expect(res.body.status).to.equal('ok');
264 done();
265 });
266 });
267
268 it('user1 should be able to delete friend user2 (delete as originator)', function (done) {
269 client1.post('http://127.0.0.1:3000/antisocial/user-one/friend-update')
270 .type('form')
271 .send({
272 'endpoint': endpoint2,
273 'status': 'delete'
274 }).end(function (err, res) {
275 expect(res.status).to.be(200);
276 expect(res.body.status).to.equal('ok');
277 done();
278 });
279 });
280
281 it('user4 should be able to delete friend user3 (delete as non-originator)', function (done) {
282 client4.post('http://127.0.0.1:3000/antisocial/user-four/friend-update')
283 .type('form')
284 .send({
285 'endpoint': endpoint3,
286 'status': 'delete'
287 }).end(function (err, res) {
288 expect(res.status).to.be(200);
289 expect(res.body.status).to.equal('ok');
290 done();
291 });
292 });
293
294 it('user2 should be able to block friend user3', function (done) {
295 client2.post('http://127.0.0.1:3000/antisocial/user-two/friend-update')
296 .type('form')
297 .send({
298 'endpoint': endpoint3,
299 'status': 'block'
300 }).end(function (err, res) {
301 expect(res.status).to.be(200);
302 expect(res.body.status).to.equal('ok');
303 done();
304 });
305 });
306
307 it('user3 should be blocked by user2', function (done) {
308 client3.get('http://127.0.0.1:3000/antisocial/user-three/request-friend?endpoint=' + endpoint2).end(function (err, res) {
309 expect(res.status).to.be(200);
310 expect(res.body.status).to.equal('error');
311 expect(res.body.details).to.equal('/request-friend makeFriendRequest failed (reason: blocked)');
312 done();
313 });
314 });
315
316 it('create an invite', function (done) {
317 app.db.newInstance('invitations', {
318 'token': 'testinvite',
319 'userId': userTwoId
320 }, function (err, invite) {
321 expect(err).to.be(null);
322 done();
323 });
324 });
325
326 it('user1 should be able to request friend user2 again with invite', function (done) {
327 client1.get('http://127.0.0.1:3000/antisocial/user-one/request-friend?endpoint=' + encodeURIComponent(endpoint2) + '&invite=testinvite').end(function (err, res) {
328 expect(res.status).to.be(200);
329 expect(res.body.status).to.equal('ok');
330 done();
331 });
332 });
333
334 it('user1 should be able to request join community', function (done) {
335 client1.get('http://127.0.0.1:3000/antisocial/user-one/request-friend?endpoint=' + encodeURIComponent(endpoint5)).end(function (err, res) {
336 expect(res.status).to.be(200);
337 expect(res.body.status).to.equal('ok');
338 done();
339 });
340 });
341
342 var friend;
343 var user;
344
345 it('user1 should be able disconnect', function (done) {
346 app.db.getInstances('users', [{
347 'property': 'username',
348 'value': 'user-one'
349 }], function (err, instances) {
350 user = instances[0];
351 app.db.getInstances('friends', [{
352 'property': 'userId',
353 'value': user.id
354 }], function (err, instances) {
355 friend = instances[0];
356 //wait 2 seconds then disconnect
357 setTimeout(function () {
358 app.antisocial.activityFeed.disconnect(user, friend, function (err) {
359 expect(err).to.be(null);
360 //wait 2 seconds then continue
361 setTimeout(function () {
362 done();
363 }, 2000);
364 });
365 }, 2000);
366 });
367 });
368 });
369
370 it('user1 should be able increment lastPost to simulate backfill behavior', function (done) {
371 app.postIdMap[user.id] = 14;
372 done();
373 });
374
375 it('user1 should be able connect', function (done) {
376 app.antisocial.activityFeed.connect(user, friend);
377 done();
378 });
379
380});
381
382function getCookie(headers, id) {
383 for (var i = 0; i < headers.length; i++) {
384 var kv = headers[i].split(';')[0].split('=');
385 if (kv[0] === id) {
386 return kv[1];
387 }
388 }
389 return null;
390}