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