]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/friends-advanced.js
Move to promises
[github/Chocobozzz/PeerTube.git] / server / tests / api / friends-advanced.js
CommitLineData
72329aaa
C
1/* eslint-disable no-unused-expressions */
2
9f10b292 3'use strict'
3bcb78b3 4
f0f5567b 5const chai = require('chai')
1a42c9e2 6const each = require('async/each')
f0f5567b 7const expect = chai.expect
1a42c9e2 8const series = require('async/series')
3bcb78b3 9
8d309058
C
10const loginUtils = require('../utils/login')
11const podsUtils = require('../utils/pods')
12const serversUtils = require('../utils/servers')
13const videosUtils = require('../utils/videos')
3bcb78b3 14
9f10b292 15describe('Test advanced friends', function () {
0c1cbbfe 16 let servers = []
3bcb78b3 17
bc503c2a 18 function makeFriends (podNumber, callback) {
b3b92647 19 const server = servers[podNumber - 1]
8d309058 20 return podsUtils.makeFriends(server.url, server.accessToken, callback)
9f10b292 21 }
45239549 22
bc503c2a 23 function quitFriends (podNumber, callback) {
b3b92647 24 const server = servers[podNumber - 1]
8d309058 25 return podsUtils.quitFriends(server.url, server.accessToken, callback)
9f10b292 26 }
3bcb78b3 27
bc503c2a 28 function getFriendsList (podNumber, end) {
b3b92647 29 const server = servers[podNumber - 1]
8d309058 30 return podsUtils.getFriendsList(server.url, end)
9f10b292 31 }
3bcb78b3 32
bc503c2a 33 function uploadVideo (podNumber, callback) {
b4c5ac97
C
34 const videoAttributes = {
35 tags: [ 'tag1', 'tag2' ]
36 }
bc503c2a 37 const server = servers[podNumber - 1]
3bcb78b3 38
b4c5ac97 39 return videosUtils.uploadVideo(server.url, server.accessToken, videoAttributes, callback)
9f10b292 40 }
3bcb78b3 41
bc503c2a 42 function getVideos (podNumber, callback) {
8d309058 43 return videosUtils.getVideosList(servers[podNumber - 1].url, callback)
9f10b292 44 }
45239549 45
9f10b292 46 // ---------------------------------------------------------------
ee66c593 47
9f10b292 48 before(function (done) {
5fe7e898 49 this.timeout(120000)
8d309058 50 serversUtils.flushAndRunMultipleServers(6, function (serversRun, urlsRun) {
bc503c2a 51 servers = serversRun
0c1cbbfe 52
1a42c9e2 53 each(servers, function (server, callbackEach) {
8d309058 54 loginUtils.loginAndGetAccessToken(server, function (err, accessToken) {
bc503c2a 55 if (err) return callbackEach(err)
0c1cbbfe 56
bc503c2a
C
57 server.accessToken = accessToken
58 callbackEach()
0c1cbbfe
C
59 })
60 }, done)
3bcb78b3 61 })
9f10b292 62 })
3bcb78b3 63
9f10b292
C
64 it('Should make friends with two pod each in a different group', function (done) {
65 this.timeout(20000)
66
1a42c9e2 67 series([
9f10b292
C
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) {
ee66c593
C
88 if (err) throw err
89
55fa55a9 90 expect(res.body.data.length).to.equal(0)
9f10b292
C
91
92 done()
93 })
94 }
95 )
96 })
97
98 it('Should quit all friends', function (done) {
99 this.timeout(10000)
100
1a42c9e2 101 series([
9f10b292
C
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
1a42c9e2 111 each([ 1, 2, 3, 4, 5, 6 ], function (i, callback) {
9f10b292 112 getFriendsList(i, function (err, res) {
ee66c593
C
113 if (err) throw err
114
55fa55a9 115 expect(res.body.data.length).to.equal(0)
ee66c593 116
9f10b292 117 callback()
3bcb78b3 118 })
9f10b292
C
119 }, done)
120 }
121 )
122 })
3bcb78b3 123
9f10b292
C
124 it('Should make friends with the pods 1, 2, 3', function (done) {
125 this.timeout(150000)
126
1a42c9e2 127 series([
9f10b292
C
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 },
528a9efa
C
138 // Check the pods 1, 2, 3 and 4 are friends
139 function (next) {
1a42c9e2 140 each([ 1, 2, 3, 4 ], function (i, callback) {
528a9efa
C
141 getFriendsList(i, function (err, res) {
142 if (err) throw err
143
55fa55a9 144 expect(res.body.data.length).to.equal(3)
528a9efa
C
145
146 callback()
147 })
148 }, next)
149 },
9f10b292
C
150 // Kill pod 4
151 function (next) {
0c1cbbfe 152 servers[3].app.kill()
9f10b292
C
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) {
b1ed2217 172 setTimeout(next, 22000)
9f10b292 173 },
6fcd19ba
C
174 // Check the pods 1, 2 expulsed pod 4
175 function (next) {
176 each([ 1, 2 ], function (i, callback) {
177 getFriendsList(i, function (err, res) {
178 if (err) throw err
179
180 // Pod 4 should not be our friend
181 const result = res.body.data
182 expect(result.length).to.equal(2)
183 for (const pod of result) {
184 expect(pod.host).not.equal(servers[3].host)
185 }
186
187 callback()
188 })
189 }, next)
190 },
9f10b292
C
191 // Rerun server 4
192 function (next) {
8d309058 193 serversUtils.runServer(4, function (server) {
0c1cbbfe 194 servers[3].app = server.app
9f10b292
C
195 next()
196 })
197 },
198 function (next) {
199 getFriendsList(4, function (err, res) {
ee66c593
C
200 if (err) throw err
201
9f10b292 202 // Pod 4 didn't know pod 1 and 2 removed it
55fa55a9 203 expect(res.body.data.length).to.equal(3)
ee66c593 204 next()
9f10b292
C
205 })
206 },
6fcd19ba 207 // Pod 6 asks pod 1, 2 and 3
9f10b292
C
208 function (next) {
209 makeFriends(6, next)
528a9efa
C
210 },
211 function (next) {
212 setTimeout(next, 11000)
9f10b292
C
213 }],
214 function (err) {
215 if (err) throw err
216
217 getFriendsList(6, function (err, res) {
ee66c593
C
218 if (err) throw err
219
9f10b292 220 // Pod 4 should not be our friend
55fa55a9 221 const result = res.body.data
9f10b292 222 expect(result.length).to.equal(3)
f0f5567b 223 for (const pod of result) {
a4254ea1 224 expect(pod.host).not.equal(servers[3].host)
9f10b292 225 }
ee66c593 226
9f10b292
C
227 done()
228 })
229 }
230 )
231 })
ee66c593 232
9f10b292
C
233 it('Should pod 1 quit friends', function (done) {
234 this.timeout(25000)
235
1a42c9e2 236 series([
9f10b292
C
237 // Upload a video on server 3 for aditionnal tests
238 function (next) {
239 uploadVideo(3, next)
240 },
241 function (next) {
242 setTimeout(next, 15000)
243 },
244 function (next) {
245 quitFriends(1, next)
246 },
247 // Remove pod 1 from pod 2
248 function (next) {
249 getVideos(1, function (err, res) {
250 if (err) throw err
68ce3ae0
C
251
252 const videos = res.body.data
253 expect(videos).to.be.an('array')
254 expect(videos.length).to.equal(2)
45239549 255
9f10b292
C
256 next()
257 })
258 }],
259 function (err) {
260 if (err) throw err
ee66c593 261
9f10b292 262 getVideos(2, function (err, res) {
ee66c593 263 if (err) throw err
68ce3ae0
C
264
265 const videos = res.body.data
266 expect(videos).to.be.an('array')
267 expect(videos.length).to.equal(3)
9f10b292
C
268 done()
269 })
270 }
271 )
272 })
ee66c593 273
9f10b292
C
274 it('Should make friends between pod 1 and 2 and exchange their videos', function (done) {
275 this.timeout(20000)
276 makeFriends(1, function () {
277 setTimeout(function () {
278 getVideos(1, function (err, res) {
279 if (err) throw err
45239549 280
68ce3ae0
C
281 const videos = res.body.data
282 expect(videos).to.be.an('array')
283 expect(videos.length).to.equal(5)
45239549 284
9f10b292
C
285 done()
286 })
528a9efa 287 }, 11000)
45239549 288 })
9f10b292 289 })
ee66c593 290
9f10b292 291 after(function (done) {
0c1cbbfe
C
292 servers.forEach(function (server) {
293 process.kill(-server.app.pid)
ee66c593 294 })
9f10b292
C
295
296 if (this.ok) {
8d309058 297 serversUtils.flushTests(done)
9f10b292
C
298 } else {
299 done()
300 }
3bcb78b3 301 })
9f10b292 302})