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