]>
Commit | Line | Data |
---|---|---|
1 | /* eslint-disable no-unused-expressions */ | |
2 | ||
3 | 'use strict' | |
4 | ||
5 | const chai = require('chai') | |
6 | const each = require('async/each') | |
7 | const expect = chai.expect | |
8 | const series = require('async/series') | |
9 | ||
10 | const loginUtils = require('../utils/login') | |
11 | const podsUtils = require('../utils/pods') | |
12 | const serversUtils = require('../utils/servers') | |
13 | const videosUtils = require('../utils/videos') | |
14 | ||
15 | describe('Test advanced friends', function () { | |
16 | let servers = [] | |
17 | ||
18 | function makeFriends (podNumber, callback) { | |
19 | const server = servers[podNumber - 1] | |
20 | return podsUtils.makeFriends(server.url, server.accessToken, callback) | |
21 | } | |
22 | ||
23 | function quitFriends (podNumber, callback) { | |
24 | const server = servers[podNumber - 1] | |
25 | return podsUtils.quitFriends(server.url, server.accessToken, callback) | |
26 | } | |
27 | ||
28 | function getFriendsList (podNumber, end) { | |
29 | const server = servers[podNumber - 1] | |
30 | return podsUtils.getFriendsList(server.url, end) | |
31 | } | |
32 | ||
33 | function uploadVideo (podNumber, callback) { | |
34 | const videoAttributes = { | |
35 | tags: [ 'tag1', 'tag2' ] | |
36 | } | |
37 | const server = servers[podNumber - 1] | |
38 | ||
39 | return videosUtils.uploadVideo(server.url, server.accessToken, videoAttributes, callback) | |
40 | } | |
41 | ||
42 | function getVideos (podNumber, callback) { | |
43 | return videosUtils.getVideosList(servers[podNumber - 1].url, callback) | |
44 | } | |
45 | ||
46 | // --------------------------------------------------------------- | |
47 | ||
48 | before(function (done) { | |
49 | this.timeout(30000) | |
50 | serversUtils.flushAndRunMultipleServers(6, function (serversRun, urlsRun) { | |
51 | servers = serversRun | |
52 | ||
53 | each(servers, function (server, callbackEach) { | |
54 | loginUtils.loginAndGetAccessToken(server, function (err, accessToken) { | |
55 | if (err) return callbackEach(err) | |
56 | ||
57 | server.accessToken = accessToken | |
58 | callbackEach() | |
59 | }) | |
60 | }, done) | |
61 | }) | |
62 | }) | |
63 | ||
64 | it('Should make friends with two pod each in a different group', function (done) { | |
65 | this.timeout(20000) | |
66 | ||
67 | series([ | |
68 | // Pod 3 makes friend with the first one | |
69 | function (next) { | |
70 | makeFriends(3, next) | |
71 | }, | |
72 | // Pod 4 makes friend with the second one | |
73 | function (next) { | |
74 | makeFriends(4, next) | |
75 | }, | |
76 | // Now if the fifth wants to make friends with the third et the first | |
77 | function (next) { | |
78 | makeFriends(5, next) | |
79 | }, | |
80 | function (next) { | |
81 | setTimeout(next, 11000) | |
82 | }], | |
83 | function (err) { | |
84 | if (err) throw err | |
85 | ||
86 | // It should have 0 friends | |
87 | getFriendsList(5, function (err, res) { | |
88 | if (err) throw err | |
89 | ||
90 | expect(res.body.data.length).to.equal(0) | |
91 | ||
92 | done() | |
93 | }) | |
94 | } | |
95 | ) | |
96 | }) | |
97 | ||
98 | it('Should quit all friends', function (done) { | |
99 | this.timeout(10000) | |
100 | ||
101 | series([ | |
102 | function (next) { | |
103 | quitFriends(1, next) | |
104 | }, | |
105 | function (next) { | |
106 | quitFriends(2, next) | |
107 | }], | |
108 | function (err) { | |
109 | if (err) throw err | |
110 | ||
111 | each([ 1, 2, 3, 4, 5, 6 ], function (i, callback) { | |
112 | getFriendsList(i, function (err, res) { | |
113 | if (err) throw err | |
114 | ||
115 | expect(res.body.data.length).to.equal(0) | |
116 | ||
117 | callback() | |
118 | }) | |
119 | }, done) | |
120 | } | |
121 | ) | |
122 | }) | |
123 | ||
124 | it('Should make friends with the pods 1, 2, 3', function (done) { | |
125 | this.timeout(150000) | |
126 | ||
127 | series([ | |
128 | // Pods 1, 2, 3 and 4 become friends | |
129 | function (next) { | |
130 | makeFriends(2, next) | |
131 | }, | |
132 | function (next) { | |
133 | makeFriends(1, next) | |
134 | }, | |
135 | function (next) { | |
136 | makeFriends(4, next) | |
137 | }, | |
138 | // Check the pods 1, 2, 3 and 4 are friends | |
139 | function (next) { | |
140 | each([ 1, 2, 3, 4 ], function (i, callback) { | |
141 | getFriendsList(i, function (err, res) { | |
142 | if (err) throw err | |
143 | ||
144 | expect(res.body.data.length).to.equal(3) | |
145 | ||
146 | callback() | |
147 | }) | |
148 | }, next) | |
149 | }, | |
150 | // Kill pod 4 | |
151 | function (next) { | |
152 | servers[3].app.kill() | |
153 | next() | |
154 | }, | |
155 | // Expulse pod 4 from pod 1 and 2 | |
156 | function (next) { | |
157 | uploadVideo(1, next) | |
158 | }, | |
159 | function (next) { | |
160 | uploadVideo(2, next) | |
161 | }, | |
162 | function (next) { | |
163 | setTimeout(next, 11000) | |
164 | }, | |
165 | function (next) { | |
166 | uploadVideo(1, next) | |
167 | }, | |
168 | function (next) { | |
169 | uploadVideo(2, next) | |
170 | }, | |
171 | function (next) { | |
172 | setTimeout(next, 22000) | |
173 | }, | |
174 | // Rerun server 4 | |
175 | function (next) { | |
176 | serversUtils.runServer(4, function (server) { | |
177 | servers[3].app = server.app | |
178 | next() | |
179 | }) | |
180 | }, | |
181 | function (next) { | |
182 | getFriendsList(4, function (err, res) { | |
183 | if (err) throw err | |
184 | ||
185 | // Pod 4 didn't know pod 1 and 2 removed it | |
186 | expect(res.body.data.length).to.equal(3) | |
187 | next() | |
188 | }) | |
189 | }, | |
190 | // Pod 6 ask pod 1, 2 and 3 | |
191 | function (next) { | |
192 | makeFriends(6, next) | |
193 | }, | |
194 | function (next) { | |
195 | setTimeout(next, 11000) | |
196 | }], | |
197 | function (err) { | |
198 | if (err) throw err | |
199 | ||
200 | getFriendsList(6, function (err, res) { | |
201 | if (err) throw err | |
202 | ||
203 | // Pod 4 should not be our friend | |
204 | const result = res.body.data | |
205 | expect(result.length).to.equal(3) | |
206 | for (const pod of result) { | |
207 | expect(pod.host).not.equal(servers[3].host) | |
208 | } | |
209 | ||
210 | done() | |
211 | }) | |
212 | } | |
213 | ) | |
214 | }) | |
215 | ||
216 | it('Should pod 1 quit friends', function (done) { | |
217 | this.timeout(25000) | |
218 | ||
219 | series([ | |
220 | // Upload a video on server 3 for aditionnal tests | |
221 | function (next) { | |
222 | uploadVideo(3, next) | |
223 | }, | |
224 | function (next) { | |
225 | setTimeout(next, 15000) | |
226 | }, | |
227 | function (next) { | |
228 | quitFriends(1, next) | |
229 | }, | |
230 | // Remove pod 1 from pod 2 | |
231 | function (next) { | |
232 | getVideos(1, function (err, res) { | |
233 | if (err) throw err | |
234 | ||
235 | const videos = res.body.data | |
236 | expect(videos).to.be.an('array') | |
237 | expect(videos.length).to.equal(2) | |
238 | ||
239 | next() | |
240 | }) | |
241 | }], | |
242 | function (err) { | |
243 | if (err) throw err | |
244 | ||
245 | getVideos(2, function (err, res) { | |
246 | if (err) throw err | |
247 | ||
248 | const videos = res.body.data | |
249 | expect(videos).to.be.an('array') | |
250 | expect(videos.length).to.equal(3) | |
251 | done() | |
252 | }) | |
253 | } | |
254 | ) | |
255 | }) | |
256 | ||
257 | it('Should make friends between pod 1 and 2 and exchange their videos', function (done) { | |
258 | this.timeout(20000) | |
259 | makeFriends(1, function () { | |
260 | setTimeout(function () { | |
261 | getVideos(1, function (err, res) { | |
262 | if (err) throw err | |
263 | ||
264 | const videos = res.body.data | |
265 | expect(videos).to.be.an('array') | |
266 | expect(videos.length).to.equal(5) | |
267 | ||
268 | done() | |
269 | }) | |
270 | }, 11000) | |
271 | }) | |
272 | }) | |
273 | ||
274 | after(function (done) { | |
275 | servers.forEach(function (server) { | |
276 | process.kill(-server.app.pid) | |
277 | }) | |
278 | ||
279 | if (this.ok) { | |
280 | serversUtils.flushTests(done) | |
281 | } else { | |
282 | done() | |
283 | } | |
284 | }) | |
285 | }) |