]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/multiple-pods.js
Server: use crypto instead of ursa for pod signature
[github/Chocobozzz/PeerTube.git] / server / tests / api / multiple-pods.js
CommitLineData
9f10b292
C
1'use strict'
2
f0f5567b 3const chai = require('chai')
1a42c9e2 4const each = require('async/each')
f0f5567b 5const expect = chai.expect
1a42c9e2 6const series = require('async/series')
bf94b6f0 7const webtorrent = new (require('webtorrent'))()
9f10b292 8
8d309058
C
9const loginUtils = require('../utils/login')
10const miscsUtils = require('../utils/miscs')
11const podsUtils = require('../utils/pods')
12const serversUtils = require('../utils/servers')
13const videosUtils = require('../utils/videos')
9f10b292
C
14
15describe('Test multiple pods', function () {
0c1cbbfe 16 let servers = []
bc503c2a 17 const toRemove = []
9f10b292
C
18
19 before(function (done) {
20 this.timeout(30000)
21
1a42c9e2 22 series([
9f10b292
C
23 // Run servers
24 function (next) {
8d309058 25 serversUtils.flushAndRunMultipleServers(3, function (serversRun) {
bc503c2a 26 servers = serversRun
9f10b292
C
27 next()
28 })
29 },
0c1cbbfe
C
30 // Get the access tokens
31 function (next) {
1a42c9e2 32 each(servers, function (server, callbackEach) {
8d309058 33 loginUtils.loginAndGetAccessToken(server, function (err, accessToken) {
bc503c2a 34 if (err) return callbackEach(err)
0c1cbbfe 35
bc503c2a
C
36 server.accessToken = accessToken
37 callbackEach()
0c1cbbfe
C
38 })
39 }, next)
40 },
9f10b292
C
41 // The second pod make friend with the third
42 function (next) {
b3b92647 43 const server = servers[1]
8d309058 44 podsUtils.makeFriends(server.url, server.accessToken, next)
9f10b292
C
45 },
46 // Wait for the request between pods
47 function (next) {
48 setTimeout(next, 10000)
49 },
50 // Pod 1 make friends too
51 function (next) {
b3b92647 52 const server = servers[0]
8d309058 53 podsUtils.makeFriends(server.url, server.accessToken, next)
9f10b292
C
54 }
55 ], done)
56 })
8c308c2b 57
9f10b292 58 it('Should not have videos for all pods', function (done) {
1a42c9e2 59 each(servers, function (server, callback) {
8d309058 60 videosUtils.getVideosList(server.url, function (err, res) {
9f10b292 61 if (err) throw err
8c308c2b 62
68ce3ae0
C
63 const videos = res.body.data
64 expect(videos).to.be.an('array')
65 expect(videos.length).to.equal(0)
8c308c2b 66
9f10b292
C
67 callback()
68 })
69 }, done)
70 })
8c308c2b 71
9f10b292
C
72 describe('Should upload the video and propagate on each pod', function () {
73 it('Should upload the video on pod 1 and propagate on each pod', function (done) {
74 this.timeout(15000)
8c308c2b 75
1a42c9e2 76 series([
ee66c593 77 function (next) {
be587647
C
78 const name = 'my super name for pod 1'
79 const description = 'my super description for pod 1'
80 const tags = [ 'tag1p1', 'tag2p1' ]
81 const file = 'video_short1.webm'
8d309058 82 videosUtils.uploadVideo(servers[0].url, servers[0].accessToken, name, description, tags, file, next)
ee66c593 83 },
ee66c593 84 function (next) {
9f10b292
C
85 setTimeout(next, 11000)
86 }],
87 // All pods should have this video
88 function (err) {
89 if (err) throw err
90
1a42c9e2 91 each(servers, function (server, callback) {
bc503c2a 92 let baseMagnet = null
9f10b292 93
8d309058 94 videosUtils.getVideosList(server.url, function (err, res) {
9f10b292
C
95 if (err) throw err
96
68ce3ae0 97 const videos = res.body.data
9f10b292
C
98 expect(videos).to.be.an('array')
99 expect(videos.length).to.equal(1)
f0f5567b 100 const video = videos[0]
9f10b292
C
101 expect(video.name).to.equal('my super name for pod 1')
102 expect(video.description).to.equal('my super description for pod 1')
a4254ea1 103 expect(video.podHost).to.equal('localhost:9001')
9f10b292 104 expect(video.magnetUri).to.exist
3a8a8b51 105 expect(video.duration).to.equal(10)
be587647 106 expect(video.tags).to.deep.equal([ 'tag1p1', 'tag2p1' ])
feb4bdfd 107 expect(miscsUtils.dateIsValid(video.createdAt)).to.be.true
79066fdf 108 expect(miscsUtils.dateIsValid(video.updatedAt)).to.be.true
528a9efa 109 expect(video.author).to.equal('root')
9f10b292 110
6d8ada5f
C
111 if (server.url !== 'http://localhost:9001') {
112 expect(video.isLocal).to.be.false
113 } else {
114 expect(video.isLocal).to.be.true
115 }
116
9f10b292 117 // All pods should have the same magnet Uri
bc503c2a
C
118 if (baseMagnet === null) {
119 baseMagnet = video.magnetUri
9f10b292
C
120 } else {
121 expect(video.magnetUri).to.equal.magnetUri
122 }
123
8d309058 124 videosUtils.testVideoImage(server.url, 'video_short1.webm', video.thumbnailPath, function (err, test) {
9e5f3740
C
125 if (err) throw err
126 expect(test).to.equal(true)
127
128 callback()
129 })
9f10b292
C
130 })
131 }, done)
132 }
133 )
134 })
135
136 it('Should upload the video on pod 2 and propagate on each pod', function (done) {
137 this.timeout(15000)
138
1a42c9e2 139 series([
ee66c593 140 function (next) {
be587647
C
141 const name = 'my super name for pod 2'
142 const description = 'my super description for pod 2'
143 const tags = [ 'tag1p2', 'tag2p2', 'tag3p2' ]
144 const file = 'video_short2.webm'
8d309058 145 videosUtils.uploadVideo(servers[1].url, servers[1].accessToken, name, description, tags, file, next)
ee66c593
C
146 },
147 function (next) {
9f10b292
C
148 setTimeout(next, 11000)
149 }],
150 // All pods should have this video
151 function (err) {
152 if (err) throw err
153
1a42c9e2 154 each(servers, function (server, callback) {
bc503c2a 155 let baseMagnet = null
9f10b292 156
8d309058 157 videosUtils.getVideosList(server.url, function (err, res) {
9f10b292
C
158 if (err) throw err
159
68ce3ae0 160 const videos = res.body.data
9f10b292
C
161 expect(videos).to.be.an('array')
162 expect(videos.length).to.equal(2)
f0f5567b 163 const video = videos[1]
9f10b292
C
164 expect(video.name).to.equal('my super name for pod 2')
165 expect(video.description).to.equal('my super description for pod 2')
a4254ea1 166 expect(video.podHost).to.equal('localhost:9002')
9f10b292 167 expect(video.magnetUri).to.exist
3a8a8b51 168 expect(video.duration).to.equal(5)
be587647 169 expect(video.tags).to.deep.equal([ 'tag1p2', 'tag2p2', 'tag3p2' ])
feb4bdfd 170 expect(miscsUtils.dateIsValid(video.createdAt)).to.be.true
79066fdf 171 expect(miscsUtils.dateIsValid(video.updatedAt)).to.be.true
528a9efa 172 expect(video.author).to.equal('root')
9f10b292 173
6d8ada5f
C
174 if (server.url !== 'http://localhost:9002') {
175 expect(video.isLocal).to.be.false
176 } else {
177 expect(video.isLocal).to.be.true
178 }
179
9f10b292 180 // All pods should have the same magnet Uri
bc503c2a
C
181 if (baseMagnet === null) {
182 baseMagnet = video.magnetUri
9f10b292
C
183 } else {
184 expect(video.magnetUri).to.equal.magnetUri
185 }
186
8d309058 187 videosUtils.testVideoImage(server.url, 'video_short2.webm', video.thumbnailPath, function (err, test) {
9e5f3740
C
188 if (err) throw err
189 expect(test).to.equal(true)
190
191 callback()
192 })
9f10b292
C
193 })
194 }, done)
ee66c593 195 }
9f10b292 196 )
8c308c2b
C
197 })
198
9f10b292
C
199 it('Should upload two videos on pod 3 and propagate on each pod', function (done) {
200 this.timeout(30000)
8c308c2b 201
1a42c9e2 202 series([
9f10b292 203 function (next) {
be587647
C
204 const name = 'my super name for pod 3'
205 const description = 'my super description for pod 3'
206 const tags = [ 'tag1p3' ]
207 const file = 'video_short3.webm'
8d309058 208 videosUtils.uploadVideo(servers[2].url, servers[2].accessToken, name, description, tags, file, next)
9f10b292
C
209 },
210 function (next) {
be587647
C
211 const name = 'my super name for pod 3-2'
212 const description = 'my super description for pod 3-2'
213 const tags = [ 'tag2p3', 'tag3p3', 'tag4p3' ]
214 const file = 'video_short.webm'
8d309058 215 videosUtils.uploadVideo(servers[2].url, servers[2].accessToken, name, description, tags, file, next)
9f10b292
C
216 },
217 function (next) {
218 setTimeout(next, 22000)
219 }],
220 function (err) {
221 if (err) throw err
8c308c2b 222
bc503c2a 223 let baseMagnet = null
9f10b292 224 // All pods should have this video
1a42c9e2 225 each(servers, function (server, callback) {
8d309058 226 videosUtils.getVideosList(server.url, function (err, res) {
9f10b292
C
227 if (err) throw err
228
68ce3ae0 229 const videos = res.body.data
9f10b292
C
230 expect(videos).to.be.an('array')
231 expect(videos.length).to.equal(4)
3a8a8b51 232
cbe2f7c3
C
233 // We not sure about the order of the two last uploads
234 let video1 = null
235 let video2 = null
236 if (videos[2].name === 'my super name for pod 3') {
237 video1 = videos[2]
238 video2 = videos[3]
239 } else {
240 video1 = videos[3]
241 video2 = videos[2]
242 }
243
6d8ada5f
C
244 expect(video1.name).to.equal('my super name for pod 3')
245 expect(video1.description).to.equal('my super description for pod 3')
a4254ea1 246 expect(video1.podHost).to.equal('localhost:9003')
6d8ada5f 247 expect(video1.magnetUri).to.exist
3a8a8b51 248 expect(video1.duration).to.equal(5)
be587647 249 expect(video1.tags).to.deep.equal([ 'tag1p3' ])
528a9efa 250 expect(video1.author).to.equal('root')
feb4bdfd 251 expect(miscsUtils.dateIsValid(video1.createdAt)).to.be.true
79066fdf 252 expect(miscsUtils.dateIsValid(video1.updatedAt)).to.be.true
6d8ada5f 253
6d8ada5f
C
254 expect(video2.name).to.equal('my super name for pod 3-2')
255 expect(video2.description).to.equal('my super description for pod 3-2')
a4254ea1 256 expect(video2.podHost).to.equal('localhost:9003')
6d8ada5f 257 expect(video2.magnetUri).to.exist
3a8a8b51 258 expect(video2.duration).to.equal(5)
be587647 259 expect(video2.tags).to.deep.equal([ 'tag2p3', 'tag3p3', 'tag4p3' ])
528a9efa 260 expect(video2.author).to.equal('root')
feb4bdfd 261 expect(miscsUtils.dateIsValid(video2.createdAt)).to.be.true
79066fdf 262 expect(miscsUtils.dateIsValid(video2.updatedAt)).to.be.true
6d8ada5f
C
263
264 if (server.url !== 'http://localhost:9003') {
265 expect(video1.isLocal).to.be.false
266 expect(video2.isLocal).to.be.false
267 } else {
268 expect(video1.isLocal).to.be.true
269 expect(video2.isLocal).to.be.true
270 }
9f10b292
C
271
272 // All pods should have the same magnet Uri
bc503c2a
C
273 if (baseMagnet === null) {
274 baseMagnet = video2.magnetUri
9f10b292 275 } else {
6d8ada5f 276 expect(video2.magnetUri).to.equal.magnetUri
9f10b292
C
277 }
278
8d309058 279 videosUtils.testVideoImage(server.url, 'video_short3.webm', video1.thumbnailPath, function (err, test) {
9e5f3740
C
280 if (err) throw err
281 expect(test).to.equal(true)
282
8d309058 283 videosUtils.testVideoImage(server.url, 'video_short.webm', video2.thumbnailPath, function (err, test) {
9e5f3740
C
284 if (err) throw err
285 expect(test).to.equal(true)
286
287 callback()
288 })
289 })
9f10b292
C
290 })
291 }, done)
292 }
293 )
8c308c2b 294 })
9f10b292 295 })
8c308c2b 296
9f10b292
C
297 describe('Should seed the uploaded video', function () {
298 it('Should add the file 1 by asking pod 3', function (done) {
299 // Yes, this could be long
300 this.timeout(200000)
8c308c2b 301
8d309058 302 videosUtils.getVideosList(servers[2].url, function (err, res) {
9f10b292 303 if (err) throw err
8c308c2b 304
68ce3ae0 305 const video = res.body.data[0]
3d118fb5
C
306 toRemove.push(res.body.data[2])
307 toRemove.push(res.body.data[3])
8c308c2b 308
9f10b292
C
309 webtorrent.add(video.magnetUri, function (torrent) {
310 expect(torrent.files).to.exist
311 expect(torrent.files.length).to.equal(1)
312 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
8c308c2b 313
9f10b292
C
314 done()
315 })
8c308c2b
C
316 })
317 })
318
9f10b292
C
319 it('Should add the file 2 by asking pod 1', function (done) {
320 // Yes, this could be long
321 this.timeout(200000)
8c308c2b 322
8d309058 323 videosUtils.getVideosList(servers[0].url, function (err, res) {
9f10b292 324 if (err) throw err
8c308c2b 325
68ce3ae0 326 const video = res.body.data[1]
0b697522 327
9f10b292
C
328 webtorrent.add(video.magnetUri, function (torrent) {
329 expect(torrent.files).to.exist
330 expect(torrent.files.length).to.equal(1)
331 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
8c308c2b 332
9f10b292 333 done()
8c308c2b
C
334 })
335 })
9f10b292 336 })
8c308c2b 337
9f10b292
C
338 it('Should add the file 3 by asking pod 2', function (done) {
339 // Yes, this could be long
340 this.timeout(200000)
8c308c2b 341
8d309058 342 videosUtils.getVideosList(servers[1].url, function (err, res) {
9f10b292 343 if (err) throw err
8c308c2b 344
68ce3ae0 345 const video = res.body.data[2]
8c308c2b 346
9f10b292
C
347 webtorrent.add(video.magnetUri, function (torrent) {
348 expect(torrent.files).to.exist
349 expect(torrent.files.length).to.equal(1)
350 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
8c308c2b 351
7a2c9a8e 352 done()
8c308c2b
C
353 })
354 })
9f10b292 355 })
8c308c2b 356
9f10b292
C
357 it('Should add the file 3-2 by asking pod 1', function (done) {
358 // Yes, this could be long
359 this.timeout(200000)
8c308c2b 360
8d309058 361 videosUtils.getVideosList(servers[0].url, function (err, res) {
9f10b292 362 if (err) throw err
8c308c2b 363
68ce3ae0 364 const video = res.body.data[3]
8c308c2b 365
9f10b292
C
366 webtorrent.add(video.magnetUri, function (torrent) {
367 expect(torrent.files).to.exist
368 expect(torrent.files.length).to.equal(1)
369 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
8c308c2b 370
9f10b292 371 done()
8c308c2b
C
372 })
373 })
9f10b292 374 })
3d118fb5
C
375 })
376
377 describe('Should manipulate these videos', function () {
378 it('Should update the video 3 by asking pod 3', function (done) {
379 this.timeout(15000)
380
381 const name = 'my super video updated'
382 const description = 'my super description updated'
383 const tags = [ 'tagup1', 'tagup2' ]
384
385 videosUtils.updateVideo(servers[2].url, servers[2].accessToken, toRemove[0].id, name, description, tags, function (err) {
386 if (err) throw err
387
388 setTimeout(done, 11000)
389 })
390 })
391
392 it('Should have the video 3 updated on each pod', function (done) {
393 each(servers, function (server, callback) {
394 videosUtils.getVideosList(server.url, function (err, res) {
395 if (err) throw err
396
397 const videos = res.body.data
398 const videoUpdated = videos.find(function (video) {
399 return video.name === 'my super video updated'
400 })
401
402 expect(!!videoUpdated).to.be.true
403 expect(videoUpdated.description).to.equal('my super description updated')
404 expect(videoUpdated.tags).to.deep.equal([ 'tagup1', 'tagup2' ])
79066fdf 405 expect(miscsUtils.dateIsValid(videoUpdated.updatedAt, 20000)).to.be.true
3d118fb5
C
406
407 callback()
408 })
409 }, done)
410 })
8c308c2b 411
3d118fb5 412 it('Should remove the videos 3 and 3-2 by asking pod 3', function (done) {
9f10b292 413 this.timeout(15000)
0b697522 414
1a42c9e2 415 series([
9f10b292 416 function (next) {
3d118fb5 417 videosUtils.removeVideo(servers[2].url, servers[2].accessToken, toRemove[0].id, next)
9f10b292
C
418 },
419 function (next) {
3d118fb5 420 videosUtils.removeVideo(servers[2].url, servers[2].accessToken, toRemove[1].id, next)
9f10b292
C
421 }],
422 function (err) {
0b697522 423 if (err) throw err
9f10b292
C
424 setTimeout(done, 11000)
425 }
426 )
427 })
0b697522 428
9f10b292 429 it('Should have videos 1 and 3 on each pod', function (done) {
1a42c9e2 430 each(servers, function (server, callback) {
8d309058 431 videosUtils.getVideosList(server.url, function (err, res) {
9f10b292 432 if (err) throw err
0b697522 433
68ce3ae0 434 const videos = res.body.data
9f10b292
C
435 expect(videos).to.be.an('array')
436 expect(videos.length).to.equal(2)
3d118fb5
C
437 expect(videos[0].name).not.to.equal(videos[1].name)
438 expect(videos[0].name).not.to.equal(toRemove[0].name)
439 expect(videos[1].name).not.to.equal(toRemove[0].name)
440 expect(videos[0].name).not.to.equal(toRemove[1].name)
441 expect(videos[1].name).not.to.equal(toRemove[1].name)
0b697522 442
9f10b292 443 callback()
0b697522 444 })
9f10b292 445 }, done)
8c308c2b 446 })
9f10b292 447 })
8c308c2b 448
9f10b292 449 after(function (done) {
0c1cbbfe
C
450 servers.forEach(function (server) {
451 process.kill(-server.app.pid)
8c308c2b 452 })
9f10b292
C
453
454 // Keep the logs if the test failed
455 if (this.ok) {
8d309058 456 serversUtils.flushTests(done)
9f10b292
C
457 } else {
458 done()
459 }
8c308c2b 460 })
9f10b292 461})