]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/friends-advanced.js
Begin user quota
[github/Chocobozzz/PeerTube.git] / server / tests / api / friends-advanced.js
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 removeFriend (podNumber, podNumberToRemove, callback) {
29 const server = servers[podNumber - 1]
30 const serverToRemove = servers[podNumberToRemove - 1]
31
32 getFriendsList(podNumber, function (err, res) {
33 if (err) throw err
34
35 let friendsList = res.body.data
36 let podToRemove = friendsList.find((friend) => (friend.host === serverToRemove.host))
37
38 return podsUtils.quitOneFriend(server.url, server.accessToken, podToRemove.id, callback)
39 })
40 }
41
42 function getFriendsList (podNumber, end) {
43 const server = servers[podNumber - 1]
44 return podsUtils.getFriendsList(server.url, end)
45 }
46
47 function uploadVideo (podNumber, callback) {
48 const videoAttributes = {
49 tags: [ 'tag1', 'tag2' ]
50 }
51 const server = servers[podNumber - 1]
52
53 return videosUtils.uploadVideo(server.url, server.accessToken, videoAttributes, callback)
54 }
55
56 function getVideos (podNumber, callback) {
57 return videosUtils.getVideosList(servers[podNumber - 1].url, callback)
58 }
59
60 // ---------------------------------------------------------------
61
62 before(function (done) {
63 this.timeout(120000)
64 serversUtils.flushAndRunMultipleServers(6, function (serversRun, urlsRun) {
65 servers = serversRun
66
67 each(servers, function (server, callbackEach) {
68 loginUtils.loginAndGetAccessToken(server, function (err, accessToken) {
69 if (err) return callbackEach(err)
70
71 server.accessToken = accessToken
72 callbackEach()
73 })
74 }, done)
75 })
76 })
77
78 it('Should make friends with two pod each in a different group', function (done) {
79 this.timeout(20000)
80
81 series([
82 // Pod 3 makes friend with the first one
83 function (next) {
84 makeFriends(3, next)
85 },
86 // Pod 4 makes friend with the second one
87 function (next) {
88 makeFriends(4, next)
89 },
90 // Now if the fifth wants to make friends with the third et the first
91 function (next) {
92 makeFriends(5, next)
93 },
94 function (next) {
95 setTimeout(next, 11000)
96 }],
97 function (err) {
98 if (err) throw err
99
100 // It should have 0 friends
101 getFriendsList(5, function (err, res) {
102 if (err) throw err
103
104 expect(res.body.data.length).to.equal(0)
105
106 done()
107 })
108 }
109 )
110 })
111
112 it('Should quit all friends', function (done) {
113 this.timeout(10000)
114
115 series([
116 function (next) {
117 quitFriends(1, next)
118 },
119 function (next) {
120 quitFriends(2, next)
121 }],
122 function (err) {
123 if (err) throw err
124
125 each([ 1, 2, 3, 4, 5, 6 ], function (i, callback) {
126 getFriendsList(i, function (err, res) {
127 if (err) throw err
128
129 expect(res.body.data.length).to.equal(0)
130
131 callback()
132 })
133 }, done)
134 }
135 )
136 })
137
138 it('Should make friends with the pods 1, 2, 3', function (done) {
139 this.timeout(150000)
140
141 series([
142 // Pods 1, 2, 3 and 4 become friends
143 function (next) {
144 makeFriends(2, next)
145 },
146 function (next) {
147 makeFriends(1, next)
148 },
149 function (next) {
150 makeFriends(4, next)
151 },
152 // Check the pods 1, 2, 3 and 4 are friends
153 function (next) {
154 each([ 1, 2, 3, 4 ], function (i, callback) {
155 getFriendsList(i, function (err, res) {
156 if (err) throw err
157
158 expect(res.body.data.length).to.equal(3)
159
160 callback()
161 })
162 }, next)
163 },
164 // Kill pod 4
165 function (next) {
166 servers[3].app.kill()
167 next()
168 },
169 // Expulse pod 4 from pod 1 and 2
170 function (next) {
171 uploadVideo(1, next)
172 },
173 function (next) {
174 uploadVideo(2, next)
175 },
176 function (next) {
177 setTimeout(next, 11000)
178 },
179 function (next) {
180 uploadVideo(1, next)
181 },
182 function (next) {
183 uploadVideo(2, next)
184 },
185 function (next) {
186 setTimeout(next, 22000)
187 },
188 // Check the pods 1, 2 expulsed pod 4
189 function (next) {
190 each([ 1, 2 ], function (i, callback) {
191 getFriendsList(i, function (err, res) {
192 if (err) throw err
193
194 // Pod 4 should not be our friend
195 const result = res.body.data
196 expect(result.length).to.equal(2)
197 for (const pod of result) {
198 expect(pod.host).not.equal(servers[3].host)
199 }
200
201 callback()
202 })
203 }, next)
204 },
205 // Rerun server 4
206 function (next) {
207 serversUtils.runServer(4, function (server) {
208 servers[3].app = server.app
209 next()
210 })
211 },
212 function (next) {
213 getFriendsList(4, function (err, res) {
214 if (err) throw err
215
216 // Pod 4 didn't know pod 1 and 2 removed it
217 expect(res.body.data.length).to.equal(3)
218 next()
219 })
220 },
221 // Pod 6 asks pod 1, 2 and 3
222 function (next) {
223 makeFriends(6, next)
224 },
225 function (next) {
226 setTimeout(next, 11000)
227 }],
228 function (err) {
229 if (err) throw err
230
231 getFriendsList(6, function (err, res) {
232 if (err) throw err
233
234 // Pod 4 should not be our friend
235 const result = res.body.data
236 expect(result.length).to.equal(3)
237 for (const pod of result) {
238 expect(pod.host).not.equal(servers[3].host)
239 }
240
241 done()
242 })
243 }
244 )
245 })
246
247 it('Should pod 1 quit friends', function (done) {
248 this.timeout(25000)
249
250 series([
251 // Upload a video on server 3 for aditionnal tests
252 function (next) {
253 uploadVideo(3, next)
254 },
255 function (next) {
256 setTimeout(next, 15000)
257 },
258 function (next) {
259 quitFriends(1, next)
260 },
261 // Remove pod 1 from pod 2
262 function (next) {
263 getVideos(1, function (err, res) {
264 if (err) throw err
265
266 const videos = res.body.data
267 expect(videos).to.be.an('array')
268 expect(videos.length).to.equal(2)
269
270 next()
271 })
272 }],
273 function (err) {
274 if (err) throw err
275
276 getVideos(2, function (err, res) {
277 if (err) throw err
278
279 const videos = res.body.data
280 expect(videos).to.be.an('array')
281 expect(videos.length).to.equal(3)
282 done()
283 })
284 }
285 )
286 })
287
288 it('Should make friends between pod 1 and 2 and exchange their videos', function (done) {
289 this.timeout(20000)
290 makeFriends(1, function () {
291 setTimeout(function () {
292 getVideos(1, function (err, res) {
293 if (err) throw err
294
295 const videos = res.body.data
296 expect(videos).to.be.an('array')
297 expect(videos.length).to.equal(5)
298
299 done()
300 })
301 }, 11000)
302 })
303 })
304
305 it('Should allow pod 6 to quit pod 1 & 2 and be friend with pod 3', function (done) {
306 this.timeout(30000)
307
308 series([
309 // Pod 3 should have 4 friends
310 function (next) {
311 getFriendsList(3, function (err, res) {
312 if (err) throw err
313
314 const friendsList = res.body.data
315 expect(friendsList).to.be.an('array')
316 expect(friendsList.length).to.equal(4)
317
318 next()
319 })
320 },
321 // Pod 1, 2, 6 should have 3 friends each
322 function (next) {
323 each([ 1, 2, 6 ], function (i, callback) {
324 getFriendsList(i, function (err, res) {
325 if (err) throw err
326
327 const friendsList = res.body.data
328 expect(friendsList).to.be.an('array')
329 expect(friendsList.length).to.equal(3)
330
331 callback()
332 })
333 }, next)
334 },
335 function (next) {
336 removeFriend(6, 1, next)
337 },
338 function (next) {
339 removeFriend(6, 2, next)
340 },
341 // Pod 6 should now have only 1 friend (and it should be Pod 3)
342 function (next) {
343 getFriendsList(6, function (err, res) {
344 if (err) throw err
345
346 const friendsList = res.body.data
347 expect(friendsList).to.be.an('array')
348 expect(friendsList.length).to.equal(1)
349 expect(friendsList[0].host).to.equal(servers[2].host)
350
351 next()
352 })
353 },
354 // Pod 1 & 2 should not know friend 6 anymore
355 function (next) {
356 each([ 1, 2 ], function (i, callback) {
357 getFriendsList(i, function (err, res) {
358 if (err) throw err
359
360 const friendsList = res.body.data
361 expect(friendsList).to.be.an('array')
362 expect(friendsList.length).to.equal(2)
363
364 callback()
365 })
366 }, next)
367 },
368 // Pod 3 should know every pod
369 function (next) {
370 getFriendsList(3, function (err, res) {
371 if (err) throw err
372
373 const friendsList = res.body.data
374 expect(friendsList).to.be.an('array')
375 expect(friendsList.length).to.equal(4)
376
377 next()
378 })
379 }
380 ], done)
381 })
382
383 after(function (done) {
384 servers.forEach(function (server) {
385 process.kill(-server.app.pid)
386 })
387
388 if (this.ok) {
389 serversUtils.flushTests(done)
390 } else {
391 done()
392 }
393 })
394 })