Use lodash v4
[oweals/peertube.git] / server / tests / api / multiplePods.js
1 'use strict'
2
3 const async = require('async')
4 const chai = require('chai')
5 const expect = chai.expect
6 const pathUtils = require('path')
7
8 const utils = require('./utils')
9 const webtorrent = require(pathUtils.join(__dirname, '../../lib/webtorrent'))
10 webtorrent.silent = true
11
12 describe('Test multiple pods', function () {
13   let servers = []
14   const to_remove = []
15
16   before(function (done) {
17     this.timeout(30000)
18
19     async.series([
20       // Run servers
21       function (next) {
22         utils.flushAndRunMultipleServers(3, function (servers_run) {
23           servers = servers_run
24           next()
25         })
26       },
27       // Get the access tokens
28       function (next) {
29         async.each(servers, function (server, callback_each) {
30           utils.loginAndGetAccessToken(server, function (err, access_token) {
31             if (err) return callback_each(err)
32
33             server.access_token = access_token
34             callback_each()
35           })
36         }, next)
37       },
38       // The second pod make friend with the third
39       function (next) {
40         utils.makeFriends(servers[1].url, next)
41       },
42       // Wait for the request between pods
43       function (next) {
44         setTimeout(next, 10000)
45       },
46       // Pod 1 make friends too
47       function (next) {
48         utils.makeFriends(servers[0].url, next)
49       },
50       function (next) {
51         webtorrent.create({ host: 'client', port: '1' }, next)
52       }
53     ], done)
54   })
55
56   it('Should not have videos for all pods', function (done) {
57     async.each(servers, function (server, callback) {
58       utils.getVideosList(server.url, function (err, res) {
59         if (err) throw err
60
61         expect(res.body).to.be.an('array')
62         expect(res.body.length).to.equal(0)
63
64         callback()
65       })
66     }, done)
67   })
68
69   describe('Should upload the video and propagate on each pod', function () {
70     it('Should upload the video on pod 1 and propagate on each pod', function (done) {
71       this.timeout(15000)
72
73       async.series([
74         function (next) {
75           utils.uploadVideo(servers[0].url, servers[0].access_token, 'my super name for pod 1', 'my super description for pod 1', 'video_short1.webm', next)
76         },
77         function (next) {
78           setTimeout(next, 11000)
79         }],
80         // All pods should have this video
81         function (err) {
82           if (err) throw err
83
84           async.each(servers, function (server, callback) {
85             let base_magnet = null
86
87             utils.getVideosList(server.url, function (err, res) {
88               if (err) throw err
89
90               const videos = res.body
91               expect(videos).to.be.an('array')
92               expect(videos.length).to.equal(1)
93               const video = videos[0]
94               expect(video.name).to.equal('my super name for pod 1')
95               expect(video.description).to.equal('my super description for pod 1')
96               expect(video.podUrl).to.equal('http://localhost:9001')
97               expect(video.magnetUri).to.exist
98
99               // All pods should have the same magnet Uri
100               if (base_magnet === null) {
101                 base_magnet = video.magnetUri
102               } else {
103                 expect(video.magnetUri).to.equal.magnetUri
104               }
105
106               callback()
107             })
108           }, done)
109         }
110       )
111     })
112
113     it('Should upload the video on pod 2 and propagate on each pod', function (done) {
114       this.timeout(15000)
115
116       async.series([
117         function (next) {
118           utils.uploadVideo(servers[1].url, servers[1].access_token, 'my super name for pod 2', 'my super description for pod 2', 'video_short2.webm', next)
119         },
120         function (next) {
121           setTimeout(next, 11000)
122         }],
123         // All pods should have this video
124         function (err) {
125           if (err) throw err
126
127           async.each(servers, function (server, callback) {
128             let base_magnet = null
129
130             utils.getVideosList(server.url, function (err, res) {
131               if (err) throw err
132
133               const videos = res.body
134               expect(videos).to.be.an('array')
135               expect(videos.length).to.equal(2)
136               const video = videos[1]
137               expect(video.name).to.equal('my super name for pod 2')
138               expect(video.description).to.equal('my super description for pod 2')
139               expect(video.podUrl).to.equal('http://localhost:9002')
140               expect(video.magnetUri).to.exist
141
142               // All pods should have the same magnet Uri
143               if (base_magnet === null) {
144                 base_magnet = video.magnetUri
145               } else {
146                 expect(video.magnetUri).to.equal.magnetUri
147               }
148
149               callback()
150             })
151           }, done)
152         }
153       )
154     })
155
156     it('Should upload two videos on pod 3 and propagate on each pod', function (done) {
157       this.timeout(30000)
158
159       async.series([
160         function (next) {
161           utils.uploadVideo(servers[2].url, servers[2].access_token, 'my super name for pod 3', 'my super description for pod 3', 'video_short3.webm', next)
162         },
163         function (next) {
164           utils.uploadVideo(servers[2].url, servers[2].access_token, 'my super name for pod 3-2', 'my super description for pod 3-2', 'video_short.webm', next)
165         },
166         function (next) {
167           setTimeout(next, 22000)
168         }],
169         function (err) {
170           if (err) throw err
171
172           let base_magnet = null
173           // All pods should have this video
174           async.each(servers, function (server, callback) {
175             utils.getVideosList(server.url, function (err, res) {
176               if (err) throw err
177
178               const videos = res.body
179               expect(videos).to.be.an('array')
180               expect(videos.length).to.equal(4)
181               let video = videos[2]
182               expect(video.name).to.equal('my super name for pod 3')
183               expect(video.description).to.equal('my super description for pod 3')
184               expect(video.podUrl).to.equal('http://localhost:9003')
185               expect(video.magnetUri).to.exist
186
187               video = videos[3]
188               expect(video.name).to.equal('my super name for pod 3-2')
189               expect(video.description).to.equal('my super description for pod 3-2')
190               expect(video.podUrl).to.equal('http://localhost:9003')
191               expect(video.magnetUri).to.exist
192
193               // All pods should have the same magnet Uri
194               if (base_magnet === null) {
195                 base_magnet = video.magnetUri
196               } else {
197                 expect(video.magnetUri).to.equal.magnetUri
198               }
199
200               callback()
201             })
202           }, done)
203         }
204       )
205     })
206   })
207
208   describe('Should seed the uploaded video', function () {
209     it('Should add the file 1 by asking pod 3', function (done) {
210       // Yes, this could be long
211       this.timeout(200000)
212
213       utils.getVideosList(servers[2].url, function (err, res) {
214         if (err) throw err
215
216         const video = res.body[0]
217         to_remove.push(res.body[2].id)
218         to_remove.push(res.body[3].id)
219
220         webtorrent.add(video.magnetUri, function (torrent) {
221           expect(torrent.files).to.exist
222           expect(torrent.files.length).to.equal(1)
223           expect(torrent.files[0].path).to.exist.and.to.not.equal('')
224
225           done()
226         })
227       })
228     })
229
230     it('Should add the file 2 by asking pod 1', function (done) {
231       // Yes, this could be long
232       this.timeout(200000)
233
234       utils.getVideosList(servers[0].url, function (err, res) {
235         if (err) throw err
236
237         const video = res.body[1]
238
239         webtorrent.add(video.magnetUri, function (torrent) {
240           expect(torrent.files).to.exist
241           expect(torrent.files.length).to.equal(1)
242           expect(torrent.files[0].path).to.exist.and.to.not.equal('')
243
244           done()
245         })
246       })
247     })
248
249     it('Should add the file 3 by asking pod 2', function (done) {
250       // Yes, this could be long
251       this.timeout(200000)
252
253       utils.getVideosList(servers[1].url, function (err, res) {
254         if (err) throw err
255
256         const video = res.body[2]
257
258         webtorrent.add(video.magnetUri, function (torrent) {
259           expect(torrent.files).to.exist
260           expect(torrent.files.length).to.equal(1)
261           expect(torrent.files[0].path).to.exist.and.to.not.equal('')
262
263           done()
264         })
265       })
266     })
267
268     it('Should add the file 3-2 by asking pod 1', function (done) {
269       // Yes, this could be long
270       this.timeout(200000)
271
272       utils.getVideosList(servers[0].url, function (err, res) {
273         if (err) throw err
274
275         const video = res.body[3]
276
277         webtorrent.add(video.magnetUri, function (torrent) {
278           expect(torrent.files).to.exist
279           expect(torrent.files.length).to.equal(1)
280           expect(torrent.files[0].path).to.exist.and.to.not.equal('')
281
282           done()
283         })
284       })
285     })
286
287     it('Should remove the file 3 and 3-2 by asking pod 3', function (done) {
288       this.timeout(15000)
289
290       async.series([
291         function (next) {
292           utils.removeVideo(servers[2].url, servers[2].access_token, to_remove[0], next)
293         },
294         function (next) {
295           utils.removeVideo(servers[2].url, servers[2].access_token, to_remove[1], next)
296         }],
297         function (err) {
298           if (err) throw err
299           setTimeout(done, 11000)
300         }
301       )
302     })
303
304     it('Should have videos 1 and 3 on each pod', function (done) {
305       async.each(servers, function (server, callback) {
306         utils.getVideosList(server.url, function (err, res) {
307           if (err) throw err
308
309           const videos = res.body
310           expect(videos).to.be.an('array')
311           expect(videos.length).to.equal(2)
312           expect(videos[0].id).not.to.equal(videos[1].id)
313           expect(videos[0].id).not.to.equal(to_remove[0])
314           expect(videos[1].id).not.to.equal(to_remove[0])
315           expect(videos[0].id).not.to.equal(to_remove[1])
316           expect(videos[1].id).not.to.equal(to_remove[1])
317
318           callback()
319         })
320       }, done)
321     })
322   })
323
324   after(function (done) {
325     servers.forEach(function (server) {
326       process.kill(-server.app.pid)
327     })
328     process.kill(-webtorrent.app.pid)
329
330     // Keep the logs if the test failed
331     if (this.ok) {
332       utils.flushTests(done)
333     } else {
334       done()
335     }
336   })
337 })