]>
Commit | Line | Data |
---|---|---|
1 | 'use strict' | |
2 | ||
3 | const chai = require('chai') | |
4 | const each = require('async/each') | |
5 | const expect = chai.expect | |
6 | const series = require('async/series') | |
7 | ||
8 | const loginUtils = require('../utils/login') | |
9 | const miscsUtils = require('../utils/miscs') | |
10 | const podsUtils = require('../utils/pods') | |
11 | const serversUtils = require('../utils/servers') | |
12 | ||
13 | describe('Test basic friends', function () { | |
14 | let servers = [] | |
15 | ||
16 | function makeFriends (podNumber, callback) { | |
17 | const server = servers[podNumber - 1] | |
18 | return podsUtils.makeFriends(server.url, server.accessToken, callback) | |
19 | } | |
20 | ||
21 | function testMadeFriends (servers, serverToTest, callback) { | |
22 | const friends = [] | |
23 | for (let i = 0; i < servers.length; i++) { | |
24 | if (servers[i].url === serverToTest.url) continue | |
25 | friends.push(servers[i].url) | |
26 | } | |
27 | ||
28 | podsUtils.getFriendsList(serverToTest.url, function (err, res) { | |
29 | if (err) throw err | |
30 | ||
31 | const result = res.body | |
32 | expect(result).to.be.an('array') | |
33 | expect(result.length).to.equal(2) | |
34 | ||
35 | const resultUrls = [ result[0].url, result[1].url ] | |
36 | expect(resultUrls[0]).to.not.equal(resultUrls[1]) | |
37 | ||
38 | const errorString = 'Friends url do not correspond for ' + serverToTest.url | |
39 | expect(friends).to.contain(resultUrls[0], errorString) | |
40 | expect(friends).to.contain(resultUrls[1], errorString) | |
41 | callback() | |
42 | }) | |
43 | } | |
44 | ||
45 | // --------------------------------------------------------------- | |
46 | ||
47 | before(function (done) { | |
48 | this.timeout(20000) | |
49 | serversUtils.flushAndRunMultipleServers(3, function (serversRun, urlsRun) { | |
50 | servers = serversRun | |
51 | ||
52 | each(servers, function (server, callbackEach) { | |
53 | loginUtils.loginAndGetAccessToken(server, function (err, accessToken) { | |
54 | if (err) return callbackEach(err) | |
55 | ||
56 | server.accessToken = accessToken | |
57 | callbackEach() | |
58 | }) | |
59 | }, done) | |
60 | }) | |
61 | }) | |
62 | ||
63 | it('Should not have friends', function (done) { | |
64 | each(servers, function (server, callback) { | |
65 | podsUtils.getFriendsList(server.url, function (err, res) { | |
66 | if (err) throw err | |
67 | ||
68 | const result = res.body | |
69 | expect(result).to.be.an('array') | |
70 | expect(result.length).to.equal(0) | |
71 | callback() | |
72 | }) | |
73 | }, done) | |
74 | }) | |
75 | ||
76 | it('Should make friends', function (done) { | |
77 | this.timeout(10000) | |
78 | ||
79 | series([ | |
80 | // The second pod make friend with the third | |
81 | function (next) { | |
82 | makeFriends(2, next) | |
83 | }, | |
84 | // Wait for the request between pods | |
85 | function (next) { | |
86 | setTimeout(next, 1000) | |
87 | }, | |
88 | // The second pod should have the third as a friend | |
89 | function (next) { | |
90 | podsUtils.getFriendsList(servers[1].url, function (err, res) { | |
91 | if (err) throw err | |
92 | ||
93 | const result = res.body | |
94 | expect(result).to.be.an('array') | |
95 | expect(result.length).to.equal(1) | |
96 | ||
97 | const pod = result[0] | |
98 | expect(pod.url).to.equal(servers[2].url) | |
99 | expect(pod.score).to.equal(20) | |
100 | expect(miscsUtils.dateIsValid(pod.createdDate)).to.be.true | |
101 | ||
102 | next() | |
103 | }) | |
104 | }, | |
105 | // Same here, the third pod should have the second pod as a friend | |
106 | function (next) { | |
107 | podsUtils.getFriendsList(servers[2].url, function (err, res) { | |
108 | if (err) throw err | |
109 | ||
110 | const result = res.body | |
111 | expect(result).to.be.an('array') | |
112 | expect(result.length).to.equal(1) | |
113 | ||
114 | const pod = result[0] | |
115 | expect(pod.url).to.equal(servers[1].url) | |
116 | expect(pod.score).to.equal(20) | |
117 | expect(miscsUtils.dateIsValid(pod.createdDate)).to.be.true | |
118 | ||
119 | next() | |
120 | }) | |
121 | }, | |
122 | // Finally the first pod make friend with the second pod | |
123 | function (next) { | |
124 | makeFriends(1, next) | |
125 | }, | |
126 | // Wait for the request between pods | |
127 | function (next) { | |
128 | setTimeout(next, 11000) | |
129 | } | |
130 | ], | |
131 | // Now each pod should be friend with the other ones | |
132 | function (err) { | |
133 | if (err) throw err | |
134 | each(servers, function (server, callback) { | |
135 | testMadeFriends(servers, server, callback) | |
136 | }, done) | |
137 | }) | |
138 | }) | |
139 | ||
140 | it('Should not be allowed to make friend again', function (done) { | |
141 | const server = servers[1] | |
142 | podsUtils.makeFriends(server.url, server.accessToken, 409, done) | |
143 | }) | |
144 | ||
145 | it('Should quit friends of pod 2', function (done) { | |
146 | series([ | |
147 | // Pod 1 quit friends | |
148 | function (next) { | |
149 | const server = servers[1] | |
150 | podsUtils.quitFriends(server.url, server.accessToken, next) | |
151 | }, | |
152 | // Pod 1 should not have friends anymore | |
153 | function (next) { | |
154 | podsUtils.getFriendsList(servers[1].url, function (err, res) { | |
155 | if (err) throw err | |
156 | ||
157 | const result = res.body | |
158 | expect(result).to.be.an('array') | |
159 | expect(result.length).to.equal(0) | |
160 | ||
161 | next() | |
162 | }) | |
163 | }, | |
164 | // Other pods shouldn't have pod 1 too | |
165 | function (next) { | |
166 | each([ servers[0].url, servers[2].url ], function (url, callback) { | |
167 | podsUtils.getFriendsList(url, function (err, res) { | |
168 | if (err) throw err | |
169 | ||
170 | const result = res.body | |
171 | expect(result).to.be.an('array') | |
172 | expect(result.length).to.equal(1) | |
173 | expect(result[0].url).not.to.be.equal(servers[1].url) | |
174 | callback() | |
175 | }) | |
176 | }, next) | |
177 | } | |
178 | ], done) | |
179 | }) | |
180 | ||
181 | it('Should allow pod 2 to make friend again', function (done) { | |
182 | const server = servers[1] | |
183 | podsUtils.makeFriends(server.url, server.accessToken, function () { | |
184 | setTimeout(function () { | |
185 | each(servers, function (server, callback) { | |
186 | testMadeFriends(servers, server, callback) | |
187 | }, done) | |
188 | }, 11000) | |
189 | }) | |
190 | }) | |
191 | ||
192 | after(function (done) { | |
193 | servers.forEach(function (server) { | |
194 | process.kill(-server.app.pid) | |
195 | }) | |
196 | ||
197 | if (this.ok) { | |
198 | serversUtils.flushTests(done) | |
199 | } else { | |
200 | done() | |
201 | } | |
202 | }) | |
203 | }) |