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