]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/singlePod.js
Server: add script that try to simulate a real world situation
[github/Chocobozzz/PeerTube.git] / server / tests / api / singlePod.js
CommitLineData
9f10b292
C
1'use strict'
2
f0f5567b
C
3const async = require('async')
4const chai = require('chai')
5const expect = chai.expect
6const fs = require('fs')
3a8a8b51 7const keyBy = require('lodash/keyBy')
f0f5567b 8const pathUtils = require('path')
9f10b292 9
f0f5567b 10const webtorrent = require(pathUtils.join(__dirname, '../../lib/webtorrent'))
9f10b292
C
11webtorrent.silent = true
12
f0f5567b 13const utils = require('./utils')
9f10b292
C
14
15describe('Test a single pod', function () {
0c1cbbfe 16 let server = null
bc503c2a 17 let videoId = -1
fbf1134e 18 let videosListBase = null
9f10b292
C
19
20 before(function (done) {
21 this.timeout(20000)
22
23 async.series([
24 function (next) {
25 utils.flushTests(next)
26 },
27 function (next) {
0c1cbbfe
C
28 utils.runServer(1, function (server1) {
29 server = server1
30 next()
31 })
32 },
33 function (next) {
34 utils.loginAndGetAccessToken(server, function (err, token) {
35 if (err) throw err
b6c6f935 36 server.accessToken = token
9f10b292
C
37 next()
38 })
39 },
40 function (next) {
41 webtorrent.create({ host: 'client', port: '1' }, next)
42 }
43 ], done)
44 })
8c308c2b 45
9f10b292 46 it('Should not have videos', function (done) {
0c1cbbfe 47 utils.getVideosList(server.url, function (err, res) {
9f10b292 48 if (err) throw err
8c308c2b 49
68ce3ae0
C
50 expect(res.body.total).to.equal(0)
51 expect(res.body.data).to.be.an('array')
52 expect(res.body.data.length).to.equal(0)
8c308c2b 53
9f10b292 54 done()
8c308c2b 55 })
9f10b292 56 })
8c308c2b 57
9f10b292
C
58 it('Should upload the video', function (done) {
59 this.timeout(5000)
be587647
C
60 const name = 'my super name'
61 const description = 'my super description'
62 const tags = [ 'tag1', 'tag2', 'tag3' ]
63 const file = 'video_short.webm'
64 utils.uploadVideo(server.url, server.accessToken, name, description, tags, file, done)
9f10b292 65 })
8c308c2b 66
9f10b292
C
67 it('Should seed the uploaded video', function (done) {
68 // Yes, this could be long
69 this.timeout(60000)
8c308c2b 70
0c1cbbfe 71 utils.getVideosList(server.url, function (err, res) {
9f10b292 72 if (err) throw err
8c308c2b 73
68ce3ae0
C
74 expect(res.body.total).to.equal(1)
75 expect(res.body.data).to.be.an('array')
76 expect(res.body.data.length).to.equal(1)
8c308c2b 77
68ce3ae0 78 const video = res.body.data[0]
9f10b292
C
79 expect(video.name).to.equal('my super name')
80 expect(video.description).to.equal('my super description')
68ce3ae0 81 expect(video.podUrl).to.equal('localhost:9001')
9f10b292 82 expect(video.magnetUri).to.exist
6d8ada5f
C
83 expect(video.author).to.equal('root')
84 expect(video.isLocal).to.be.true
be587647 85 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
bb10240e 86 expect(utils.dateIsValid(video.createdDate)).to.be.true
8c308c2b 87
36d56024 88 utils.testImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
9e5f3740
C
89 if (err) throw err
90 expect(test).to.equal(true)
2df82d42 91
bc503c2a 92 videoId = video.id
2df82d42 93
9e5f3740
C
94 webtorrent.add(video.magnetUri, function (torrent) {
95 expect(torrent.files).to.exist
96 expect(torrent.files.length).to.equal(1)
97 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
98
b6c6f935
C
99 // We remove it because we'll add it again
100 webtorrent.remove(video.magnetUri, done)
9e5f3740 101 })
2df82d42
C
102 })
103 })
104 })
105
106 it('Should get the video', function (done) {
107 // Yes, this could be long
108 this.timeout(60000)
109
bc503c2a 110 utils.getVideo(server.url, videoId, function (err, res) {
2df82d42
C
111 if (err) throw err
112
113 const video = res.body
114 expect(video.name).to.equal('my super name')
115 expect(video.description).to.equal('my super description')
68ce3ae0 116 expect(video.podUrl).to.equal('localhost:9001')
2df82d42 117 expect(video.magnetUri).to.exist
6d8ada5f
C
118 expect(video.author).to.equal('root')
119 expect(video.isLocal).to.be.true
be587647 120 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
bb10240e 121 expect(utils.dateIsValid(video.createdDate)).to.be.true
8c308c2b 122
36d56024 123 utils.testImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
9e5f3740
C
124 if (err) throw err
125 expect(test).to.equal(true)
8c308c2b 126
9e5f3740
C
127 webtorrent.add(video.magnetUri, function (torrent) {
128 expect(torrent.files).to.exist
129 expect(torrent.files.length).to.equal(1)
130 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
131
132 done()
133 })
876d1bcf 134 })
8c308c2b 135 })
9f10b292 136 })
8c308c2b 137
46246b5f 138 it('Should search the video by name by default', function (done) {
0c1cbbfe 139 utils.searchVideo(server.url, 'my', function (err, res) {
9f10b292 140 if (err) throw err
4d5f8138 141
68ce3ae0
C
142 expect(res.body.total).to.equal(1)
143 expect(res.body.data).to.be.an('array')
144 expect(res.body.data.length).to.equal(1)
4d5f8138 145
68ce3ae0 146 const video = res.body.data[0]
9f10b292
C
147 expect(video.name).to.equal('my super name')
148 expect(video.description).to.equal('my super description')
68ce3ae0 149 expect(video.podUrl).to.equal('localhost:9001')
6d8ada5f
C
150 expect(video.author).to.equal('root')
151 expect(video.isLocal).to.be.true
be587647 152 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
bb10240e 153 expect(utils.dateIsValid(video.createdDate)).to.be.true
4d5f8138 154
36d56024 155 utils.testImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
9e5f3740
C
156 if (err) throw err
157 expect(test).to.equal(true)
158
159 done()
160 })
4d5f8138 161 })
9f10b292 162 })
4d5f8138 163
46246b5f
C
164 it('Should search the video by podUrl', function (done) {
165 utils.searchVideo(server.url, '9001', 'podUrl', function (err, res) {
166 if (err) throw err
167
168 expect(res.body.total).to.equal(1)
169 expect(res.body.data).to.be.an('array')
170 expect(res.body.data.length).to.equal(1)
171
172 const video = res.body.data[0]
173 expect(video.name).to.equal('my super name')
174 expect(video.description).to.equal('my super description')
175 expect(video.podUrl).to.equal('localhost:9001')
176 expect(video.author).to.equal('root')
177 expect(video.isLocal).to.be.true
be587647 178 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
46246b5f
C
179 expect(utils.dateIsValid(video.createdDate)).to.be.true
180
181 utils.testImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
182 if (err) throw err
183 expect(test).to.equal(true)
184
185 done()
186 })
187 })
188 })
189
8d199cb8
C
190 it('Should search the video by tag', function (done) {
191 utils.searchVideo(server.url, 'tag1', 'tags', function (err, res) {
192 if (err) throw err
193
194 expect(res.body.total).to.equal(1)
195 expect(res.body.data).to.be.an('array')
196 expect(res.body.data.length).to.equal(1)
197
198 const video = res.body.data[0]
199 expect(video.name).to.equal('my super name')
200 expect(video.description).to.equal('my super description')
201 expect(video.podUrl).to.equal('localhost:9001')
202 expect(video.author).to.equal('root')
203 expect(video.isLocal).to.be.true
204 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
205 expect(utils.dateIsValid(video.createdDate)).to.be.true
206
207 utils.testImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
208 if (err) throw err
209 expect(test).to.equal(true)
210
211 done()
212 })
213 })
214 })
215
46246b5f 216 it('Should not find a search by name by default', function (done) {
0c1cbbfe 217 utils.searchVideo(server.url, 'hello', function (err, res) {
9f10b292 218 if (err) throw err
4d5f8138 219
68ce3ae0
C
220 expect(res.body.total).to.equal(0)
221 expect(res.body.data).to.be.an('array')
222 expect(res.body.data.length).to.equal(0)
4d5f8138 223
9f10b292 224 done()
4d5f8138 225 })
9f10b292 226 })
4d5f8138 227
46246b5f
C
228 it('Should not find a search by author', function (done) {
229 utils.searchVideo(server.url, 'hello', 'author', function (err, res) {
230 if (err) throw err
231
232 expect(res.body.total).to.equal(0)
233 expect(res.body.data).to.be.an('array')
234 expect(res.body.data.length).to.equal(0)
235
236 done()
237 })
238 })
239
8d199cb8
C
240 it('Should not find a search by tag', function (done) {
241 utils.searchVideo(server.url, 'tag', 'tags', function (err, res) {
242 if (err) throw err
243
244 expect(res.body.total).to.equal(0)
245 expect(res.body.data).to.be.an('array')
246 expect(res.body.data.length).to.equal(0)
247
248 done()
249 })
250 })
251
9f10b292 252 it('Should remove the video', function (done) {
bc503c2a 253 utils.removeVideo(server.url, server.accessToken, videoId, function (err) {
9f10b292 254 if (err) throw err
f5a60a51 255
3d446a26 256 fs.readdir(pathUtils.join(__dirname, '../../../test1/uploads/'), function (err, files) {
9f10b292 257 if (err) throw err
f5a60a51 258
9f10b292
C
259 expect(files.length).to.equal(0)
260 done()
876d1bcf 261 })
8c308c2b 262 })
9f10b292 263 })
8c308c2b 264
9f10b292 265 it('Should not have videos', function (done) {
0c1cbbfe 266 utils.getVideosList(server.url, function (err, res) {
9f10b292 267 if (err) throw err
8c308c2b 268
68ce3ae0
C
269 expect(res.body.total).to.equal(0)
270 expect(res.body.data).to.be.an('array')
271 expect(res.body.data.length).to.equal(0)
8c308c2b 272
9f10b292 273 done()
8c308c2b 274 })
9f10b292 275 })
8c308c2b 276
3a8a8b51
C
277 it('Should upload 6 videos', function (done) {
278 this.timeout(25000)
279 const videos = [
280 'video_short.mp4', 'video_short.ogv', 'video_short.webm',
281 'video_short1.webm', 'video_short2.webm', 'video_short3.webm'
282 ]
bc503c2a 283 async.each(videos, function (video, callbackEach) {
be587647
C
284 const name = video + ' name'
285 const description = video + ' description'
286 const tags = [ 'tag1', 'tag2', 'tag3' ]
287
288 utils.uploadVideo(server.url, server.accessToken, name, description, tags, video, callbackEach)
3a8a8b51
C
289 }, done)
290 })
291
292 it('Should have the correct durations', function (done) {
293 utils.getVideosList(server.url, function (err, res) {
294 if (err) throw err
295
68ce3ae0
C
296 expect(res.body.total).to.equal(6)
297 const videos = res.body.data
3a8a8b51
C
298 expect(videos).to.be.an('array')
299 expect(videos.length).to.equal(6)
300
bc503c2a
C
301 const videosByName = keyBy(videos, 'name')
302 expect(videosByName['video_short.mp4 name'].duration).to.equal(5)
303 expect(videosByName['video_short.ogv name'].duration).to.equal(5)
304 expect(videosByName['video_short.webm name'].duration).to.equal(5)
305 expect(videosByName['video_short1.webm name'].duration).to.equal(10)
306 expect(videosByName['video_short2.webm name'].duration).to.equal(5)
307 expect(videosByName['video_short3.webm name'].duration).to.equal(5)
3a8a8b51
C
308
309 done()
310 })
311 })
312
9e5f3740
C
313 it('Should have the correct thumbnails', function (done) {
314 utils.getVideosList(server.url, function (err, res) {
fbf1134e
C
315 if (err) throw err
316
68ce3ae0 317 const videos = res.body.data
fbf1134e
C
318 // For the next test
319 videosListBase = videos
9e5f3740 320
bc503c2a 321 async.each(videos, function (video, callbackEach) {
9e5f3740 322 if (err) throw err
bc503c2a 323 const videoName = video.name.replace(' name', '')
9e5f3740 324
bc503c2a 325 utils.testImage(server.url, videoName, video.thumbnailPath, function (err, test) {
9e5f3740
C
326 if (err) throw err
327
328 expect(test).to.equal(true)
bc503c2a 329 callbackEach()
9e5f3740
C
330 })
331 }, done)
332 })
333 })
334
fbf1134e
C
335 it('Should list only the two first videos', function (done) {
336 utils.getVideosListPagination(server.url, 0, 2, function (err, res) {
337 if (err) throw err
338
68ce3ae0
C
339 const videos = res.body.data
340 expect(res.body.total).to.equal(6)
fbf1134e
C
341 expect(videos.length).to.equal(2)
342 expect(videos[0].name === videosListBase[0].name)
343 expect(videos[1].name === videosListBase[1].name)
344
345 done()
346 })
347 })
348
349 it('Should list only the next three videos', function (done) {
350 utils.getVideosListPagination(server.url, 2, 3, function (err, res) {
351 if (err) throw err
352
68ce3ae0
C
353 const videos = res.body.data
354 expect(res.body.total).to.equal(6)
fbf1134e
C
355 expect(videos.length).to.equal(4)
356 expect(videos[0].name === videosListBase[2].name)
357 expect(videos[1].name === videosListBase[3].name)
358 expect(videos[2].name === videosListBase[4].name)
359
360 done()
361 })
362 })
363
364 it('Should list the last video', function (done) {
365 utils.getVideosListPagination(server.url, 5, 6, function (err, res) {
366 if (err) throw err
367
68ce3ae0
C
368 const videos = res.body.data
369 expect(res.body.total).to.equal(6)
fbf1134e
C
370 expect(videos.length).to.equal(1)
371 expect(videos[0].name === videosListBase[5].name)
372
373 done()
374 })
375 })
376
377 it('Should search the first video', function (done) {
46246b5f 378 utils.searchVideoWithPagination(server.url, 'webm', 'name', 0, 1, function (err, res) {
fbf1134e
C
379 if (err) throw err
380
68ce3ae0
C
381 const videos = res.body.data
382 expect(res.body.total).to.equal(4)
fbf1134e
C
383 expect(videos.length).to.equal(1)
384 expect(videos[0].name === 'video_short.webm name')
385
386 done()
387 })
388 })
389
390 it('Should search the last two videos', function (done) {
46246b5f 391 utils.searchVideoWithPagination(server.url, 'webm', 'name', 2, 2, function (err, res) {
fbf1134e
C
392 if (err) throw err
393
68ce3ae0
C
394 const videos = res.body.data
395 expect(res.body.total).to.equal(4)
fbf1134e
C
396 expect(videos.length).to.equal(2)
397 expect(videos[0].name === 'video_short2.webm name')
398 expect(videos[1].name === 'video_short3.webm name')
399
400 done()
401 })
402 })
403
46246b5f
C
404 it('Should search all the webm videos', function (done) {
405 utils.searchVideoWithPagination(server.url, 'webm', 'name', 0, 15, function (err, res) {
fbf1134e
C
406 if (err) throw err
407
68ce3ae0
C
408 const videos = res.body.data
409 expect(res.body.total).to.equal(4)
fbf1134e
C
410 expect(videos.length).to.equal(4)
411
412 done()
413 })
414 })
415
46246b5f
C
416 it('Should search all the root author videos', function (done) {
417 utils.searchVideoWithPagination(server.url, 'root', 'author', 0, 15, function (err, res) {
418 if (err) throw err
419
420 const videos = res.body.data
421 expect(res.body.total).to.equal(6)
422 expect(videos.length).to.equal(6)
423
424 done()
425 })
426 })
427
428 it('Should search all the 9001 port videos', function (done) {
429 utils.searchVideoWithPagination(server.url, '9001', 'podUrl', 0, 15, function (err, res) {
430 if (err) throw err
431
432 const videos = res.body.data
433 expect(res.body.total).to.equal(6)
434 expect(videos.length).to.equal(6)
435
436 done()
437 })
438 })
439
440 it('Should search all the localhost videos', function (done) {
441 utils.searchVideoWithPagination(server.url, 'localhost', 'podUrl', 0, 15, function (err, res) {
442 if (err) throw err
443
444 const videos = res.body.data
445 expect(res.body.total).to.equal(6)
446 expect(videos.length).to.equal(6)
447
448 done()
449 })
450 })
451
452 it('Should search the good magnetUri video', function (done) {
453 const video = videosListBase[0]
454 utils.searchVideoWithPagination(server.url, encodeURIComponent(video.magnetUri), 'magnetUri', 0, 15, function (err, res) {
455 if (err) throw err
456
457 const videos = res.body.data
458 expect(res.body.total).to.equal(1)
459 expect(videos.length).to.equal(1)
460 expect(videos[0].name).to.equal(video.name)
461
462 done()
463 })
464 })
465
a877d5ac
C
466 it('Should list and sort by name in descending order', function (done) {
467 utils.getVideosListSort(server.url, '-name', function (err, res) {
468 if (err) throw err
469
68ce3ae0
C
470 const videos = res.body.data
471 expect(res.body.total).to.equal(6)
a877d5ac
C
472 expect(videos.length).to.equal(6)
473 expect(videos[5].name === 'video_short.mp4 name')
474 expect(videos[4].name === 'video_short.ogv name')
475 expect(videos[3].name === 'video_short.webm name')
476 expect(videos[2].name === 'video_short1.webm name')
477 expect(videos[1].name === 'video_short2.webm name')
478 expect(videos[0].name === 'video_short3.webm name')
479
480 done()
481 })
482 })
483
484 it('Should search and sort by name in ascending order', function (done) {
485 utils.searchVideoWithSort(server.url, 'webm', 'name', function (err, res) {
486 if (err) throw err
487
68ce3ae0
C
488 const videos = res.body.data
489 expect(res.body.total).to.equal(4)
a877d5ac
C
490 expect(videos.length).to.equal(4)
491
492 expect(videos[0].name === 'video_short.webm name')
493 expect(videos[1].name === 'video_short1.webm name')
494 expect(videos[2].name === 'video_short2.webm name')
495 expect(videos[3].name === 'video_short3.webm name')
496
497 done()
498 })
499 })
500
9f10b292 501 after(function (done) {
0c1cbbfe 502 process.kill(-server.app.pid)
9f10b292 503 process.kill(-webtorrent.app.pid)
8c308c2b 504
9f10b292
C
505 // Keep the logs if the test failed
506 if (this.ok) {
507 utils.flushTests(done)
508 } else {
509 done()
510 }
8c308c2b 511 })
9f10b292 512})