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