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