]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/single-pod.js
d583592d8755583f1b05ee00914ffccfd1610dbf
[github/Chocobozzz/PeerTube.git] / server / tests / api / single-pod.js
1 /* eslint-disable no-unused-expressions */
2
3 'use strict'
4
5 const chai = require('chai')
6 const each = require('async/each')
7 const expect = chai.expect
8 const fs = require('fs')
9 const keyBy = require('lodash/keyBy')
10 const pathUtils = require('path')
11 const series = require('async/series')
12 const webtorrent = new (require('webtorrent'))()
13
14 const loginUtils = require('../utils/login')
15 const miscsUtils = require('../utils/miscs')
16 const serversUtils = require('../utils/servers')
17 const videosUtils = require('../utils/videos')
18
19 describe('Test a single pod', function () {
20 let server = null
21 let videoId = -1
22 let videosListBase = null
23
24 before(function (done) {
25 this.timeout(20000)
26
27 series([
28 function (next) {
29 serversUtils.flushTests(next)
30 },
31 function (next) {
32 serversUtils.runServer(1, function (server1) {
33 server = server1
34 next()
35 })
36 },
37 function (next) {
38 loginUtils.loginAndGetAccessToken(server, function (err, token) {
39 if (err) throw err
40 server.accessToken = token
41 next()
42 })
43 }
44 ], done)
45 })
46
47 it('Should list video categories', function (done) {
48 videosUtils.getVideoCategories(server.url, function (err, res) {
49 if (err) throw err
50
51 const categories = res.body
52 expect(Object.keys(categories)).to.have.length.above(10)
53
54 expect(categories[11]).to.equal('News')
55
56 done()
57 })
58 })
59
60 it('Should not have videos', function (done) {
61 videosUtils.getVideosList(server.url, function (err, res) {
62 if (err) throw err
63
64 expect(res.body.total).to.equal(0)
65 expect(res.body.data).to.be.an('array')
66 expect(res.body.data.length).to.equal(0)
67
68 done()
69 })
70 })
71
72 it('Should upload the video', function (done) {
73 this.timeout(5000)
74 const name = 'my super name'
75 const description = 'my super description'
76 const category = 2
77 const tags = [ 'tag1', 'tag2', 'tag3' ]
78 const file = 'video_short.webm'
79 videosUtils.uploadVideo(server.url, server.accessToken, name, category, description, tags, file, done)
80 })
81
82 it('Should seed the uploaded video', function (done) {
83 // Yes, this could be long
84 this.timeout(60000)
85
86 videosUtils.getVideosList(server.url, function (err, res) {
87 if (err) throw err
88
89 expect(res.body.total).to.equal(1)
90 expect(res.body.data).to.be.an('array')
91 expect(res.body.data.length).to.equal(1)
92
93 const video = res.body.data[0]
94 expect(video.name).to.equal('my super name')
95 expect(video.category).to.equal(2)
96 expect(video.categoryLabel).to.equal('Films')
97 expect(video.description).to.equal('my super description')
98 expect(video.podHost).to.equal('localhost:9001')
99 expect(video.magnetUri).to.exist
100 expect(video.author).to.equal('root')
101 expect(video.isLocal).to.be.true
102 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
103 expect(miscsUtils.dateIsValid(video.createdAt)).to.be.true
104 expect(miscsUtils.dateIsValid(video.updatedAt)).to.be.true
105
106 videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
107 if (err) throw err
108 expect(test).to.equal(true)
109
110 videoId = video.id
111
112 webtorrent.add(video.magnetUri, function (torrent) {
113 expect(torrent.files).to.exist
114 expect(torrent.files.length).to.equal(1)
115 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
116
117 done()
118 })
119 })
120 })
121 })
122
123 it('Should get the video', function (done) {
124 // Yes, this could be long
125 this.timeout(60000)
126
127 videosUtils.getVideo(server.url, videoId, function (err, res) {
128 if (err) throw err
129
130 const video = res.body
131 expect(video.name).to.equal('my super name')
132 expect(video.category).to.equal(2)
133 expect(video.categoryLabel).to.equal('Films')
134 expect(video.description).to.equal('my super description')
135 expect(video.podHost).to.equal('localhost:9001')
136 expect(video.magnetUri).to.exist
137 expect(video.author).to.equal('root')
138 expect(video.isLocal).to.be.true
139 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
140 expect(miscsUtils.dateIsValid(video.createdAt)).to.be.true
141 expect(miscsUtils.dateIsValid(video.updatedAt)).to.be.true
142
143 videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
144 if (err) throw err
145 expect(test).to.equal(true)
146
147 done()
148 })
149 })
150 })
151
152 it('Should have the views updated', function (done) {
153 videosUtils.getVideo(server.url, videoId, function (err, res) {
154 if (err) throw err
155
156 const video = res.body
157 expect(video.views).to.equal(1)
158
159 done()
160 })
161 })
162
163 it('Should search the video by name by default', function (done) {
164 videosUtils.searchVideo(server.url, 'my', function (err, res) {
165 if (err) throw err
166
167 expect(res.body.total).to.equal(1)
168 expect(res.body.data).to.be.an('array')
169 expect(res.body.data.length).to.equal(1)
170
171 const video = res.body.data[0]
172 expect(video.name).to.equal('my super name')
173 expect(video.category).to.equal(2)
174 expect(video.categoryLabel).to.equal('Films')
175 expect(video.description).to.equal('my super description')
176 expect(video.podHost).to.equal('localhost:9001')
177 expect(video.author).to.equal('root')
178 expect(video.isLocal).to.be.true
179 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
180 expect(miscsUtils.dateIsValid(video.createdAt)).to.be.true
181 expect(miscsUtils.dateIsValid(video.updatedAt)).to.be.true
182
183 videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
184 if (err) throw err
185 expect(test).to.equal(true)
186
187 done()
188 })
189 })
190 })
191
192 // Not implemented yet
193 // it('Should search the video by podHost', function (done) {
194 // videosUtils.searchVideo(server.url, '9001', 'host', function (err, res) {
195 // if (err) throw err
196
197 // expect(res.body.total).to.equal(1)
198 // expect(res.body.data).to.be.an('array')
199 // expect(res.body.data.length).to.equal(1)
200
201 // const video = res.body.data[0]
202 // expect(video.name).to.equal('my super name')
203 // expect(video.description).to.equal('my super description')
204 // expect(video.podHost).to.equal('localhost:9001')
205 // expect(video.author).to.equal('root')
206 // expect(video.isLocal).to.be.true
207 // expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
208 // expect(miscsUtils.dateIsValid(video.createdAt)).to.be.true
209 // expect(miscsUtils.dateIsValid(video.updatedAt)).to.be.true
210
211 // videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
212 // if (err) throw err
213 // expect(test).to.equal(true)
214
215 // done()
216 // })
217 // })
218 // })
219
220 it('Should search the video by tag', function (done) {
221 videosUtils.searchVideo(server.url, 'tag1', 'tags', function (err, res) {
222 if (err) throw err
223
224 expect(res.body.total).to.equal(1)
225 expect(res.body.data).to.be.an('array')
226 expect(res.body.data.length).to.equal(1)
227
228 const video = res.body.data[0]
229 expect(video.name).to.equal('my super name')
230 expect(video.category).to.equal(2)
231 expect(video.categoryLabel).to.equal('Films')
232 expect(video.description).to.equal('my super description')
233 expect(video.podHost).to.equal('localhost:9001')
234 expect(video.author).to.equal('root')
235 expect(video.isLocal).to.be.true
236 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
237 expect(miscsUtils.dateIsValid(video.createdAt)).to.be.true
238 expect(miscsUtils.dateIsValid(video.updatedAt)).to.be.true
239
240 videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
241 if (err) throw err
242 expect(test).to.equal(true)
243
244 done()
245 })
246 })
247 })
248
249 it('Should not find a search by name by default', function (done) {
250 videosUtils.searchVideo(server.url, 'hello', function (err, res) {
251 if (err) throw err
252
253 expect(res.body.total).to.equal(0)
254 expect(res.body.data).to.be.an('array')
255 expect(res.body.data.length).to.equal(0)
256
257 done()
258 })
259 })
260
261 it('Should not find a search by author', function (done) {
262 videosUtils.searchVideo(server.url, 'hello', 'author', function (err, res) {
263 if (err) throw err
264
265 expect(res.body.total).to.equal(0)
266 expect(res.body.data).to.be.an('array')
267 expect(res.body.data.length).to.equal(0)
268
269 done()
270 })
271 })
272
273 it('Should not find a search by tag', function (done) {
274 videosUtils.searchVideo(server.url, 'hello', 'tags', function (err, res) {
275 if (err) throw err
276
277 expect(res.body.total).to.equal(0)
278 expect(res.body.data).to.be.an('array')
279 expect(res.body.data.length).to.equal(0)
280
281 done()
282 })
283 })
284
285 it('Should remove the video', function (done) {
286 videosUtils.removeVideo(server.url, server.accessToken, videoId, function (err) {
287 if (err) throw err
288
289 fs.readdir(pathUtils.join(__dirname, '..', '..', '..', 'test1/videos/'), function (err, files) {
290 if (err) throw err
291
292 expect(files.length).to.equal(0)
293
294 fs.readdir(pathUtils.join(__dirname, '..', '..', '..', 'test1/thumbnails/'), function (err, files) {
295 if (err) throw err
296
297 expect(files.length).to.equal(0)
298
299 done()
300 })
301 })
302 })
303 })
304
305 it('Should not have videos', function (done) {
306 videosUtils.getVideosList(server.url, function (err, res) {
307 if (err) throw err
308
309 expect(res.body.total).to.equal(0)
310 expect(res.body.data).to.be.an('array')
311 expect(res.body.data.length).to.equal(0)
312
313 done()
314 })
315 })
316
317 it('Should upload 6 videos', function (done) {
318 this.timeout(25000)
319 const videos = [
320 'video_short.mp4', 'video_short.ogv', 'video_short.webm',
321 'video_short1.webm', 'video_short2.webm', 'video_short3.webm'
322 ]
323 each(videos, function (video, callbackEach) {
324 const name = video + ' name'
325 const description = video + ' description'
326 const category = 2
327 const tags = [ 'tag1', 'tag2', 'tag3' ]
328
329 videosUtils.uploadVideo(server.url, server.accessToken, name, category, description, tags, video, callbackEach)
330 }, done)
331 })
332
333 it('Should have the correct durations', function (done) {
334 videosUtils.getVideosList(server.url, function (err, res) {
335 if (err) throw err
336
337 expect(res.body.total).to.equal(6)
338 const videos = res.body.data
339 expect(videos).to.be.an('array')
340 expect(videos.length).to.equal(6)
341
342 const videosByName = keyBy(videos, 'name')
343 expect(videosByName['video_short.mp4 name'].duration).to.equal(5)
344 expect(videosByName['video_short.ogv name'].duration).to.equal(5)
345 expect(videosByName['video_short.webm name'].duration).to.equal(5)
346 expect(videosByName['video_short1.webm name'].duration).to.equal(10)
347 expect(videosByName['video_short2.webm name'].duration).to.equal(5)
348 expect(videosByName['video_short3.webm name'].duration).to.equal(5)
349
350 done()
351 })
352 })
353
354 it('Should have the correct thumbnails', function (done) {
355 videosUtils.getVideosList(server.url, function (err, res) {
356 if (err) throw err
357
358 const videos = res.body.data
359 // For the next test
360 videosListBase = videos
361
362 each(videos, function (video, callbackEach) {
363 if (err) throw err
364 const videoName = video.name.replace(' name', '')
365
366 videosUtils.testVideoImage(server.url, videoName, video.thumbnailPath, function (err, test) {
367 if (err) throw err
368
369 expect(test).to.equal(true)
370 callbackEach()
371 })
372 }, done)
373 })
374 })
375
376 it('Should list only the two first videos', function (done) {
377 videosUtils.getVideosListPagination(server.url, 0, 2, 'name', function (err, res) {
378 if (err) throw err
379
380 const videos = res.body.data
381 expect(res.body.total).to.equal(6)
382 expect(videos.length).to.equal(2)
383 expect(videos[0].name).to.equal(videosListBase[0].name)
384 expect(videos[1].name).to.equal(videosListBase[1].name)
385
386 done()
387 })
388 })
389
390 it('Should list only the next three videos', function (done) {
391 videosUtils.getVideosListPagination(server.url, 2, 3, 'name', function (err, res) {
392 if (err) throw err
393
394 const videos = res.body.data
395 expect(res.body.total).to.equal(6)
396 expect(videos.length).to.equal(3)
397 expect(videos[0].name).to.equal(videosListBase[2].name)
398 expect(videos[1].name).to.equal(videosListBase[3].name)
399 expect(videos[2].name).to.equal(videosListBase[4].name)
400
401 done()
402 })
403 })
404
405 it('Should list the last video', function (done) {
406 videosUtils.getVideosListPagination(server.url, 5, 6, 'name', function (err, res) {
407 if (err) throw err
408
409 const videos = res.body.data
410 expect(res.body.total).to.equal(6)
411 expect(videos.length).to.equal(1)
412 expect(videos[0].name).to.equal(videosListBase[5].name)
413
414 done()
415 })
416 })
417
418 it('Should search the first video', function (done) {
419 videosUtils.searchVideoWithPagination(server.url, 'webm', 'name', 0, 1, 'name', function (err, res) {
420 if (err) throw err
421
422 const videos = res.body.data
423 expect(res.body.total).to.equal(4)
424 expect(videos.length).to.equal(1)
425 expect(videos[0].name).to.equal('video_short1.webm name')
426
427 done()
428 })
429 })
430
431 it('Should search the last two videos', function (done) {
432 videosUtils.searchVideoWithPagination(server.url, 'webm', 'name', 2, 2, 'name', function (err, res) {
433 if (err) throw err
434
435 const videos = res.body.data
436 expect(res.body.total).to.equal(4)
437 expect(videos.length).to.equal(2)
438 expect(videos[0].name).to.equal('video_short3.webm name')
439 expect(videos[1].name).to.equal('video_short.webm name')
440
441 done()
442 })
443 })
444
445 it('Should search all the webm videos', function (done) {
446 videosUtils.searchVideoWithPagination(server.url, 'webm', 'name', 0, 15, function (err, res) {
447 if (err) throw err
448
449 const videos = res.body.data
450 expect(res.body.total).to.equal(4)
451 expect(videos.length).to.equal(4)
452
453 done()
454 })
455 })
456
457 it('Should search all the root author videos', function (done) {
458 videosUtils.searchVideoWithPagination(server.url, 'root', 'author', 0, 15, function (err, res) {
459 if (err) throw err
460
461 const videos = res.body.data
462 expect(res.body.total).to.equal(6)
463 expect(videos.length).to.equal(6)
464
465 done()
466 })
467 })
468
469 // Not implemented yet
470 // it('Should search all the 9001 port videos', function (done) {
471 // videosUtils.searchVideoWithPagination(server.url, '9001', 'host', 0, 15, function (err, res) {
472 // if (err) throw err
473
474 // const videos = res.body.data
475 // expect(res.body.total).to.equal(6)
476 // expect(videos.length).to.equal(6)
477
478 // done()
479 // })
480 // })
481
482 // it('Should search all the localhost videos', function (done) {
483 // videosUtils.searchVideoWithPagination(server.url, 'localhost', 'host', 0, 15, function (err, res) {
484 // if (err) throw err
485
486 // const videos = res.body.data
487 // expect(res.body.total).to.equal(6)
488 // expect(videos.length).to.equal(6)
489
490 // done()
491 // })
492 // })
493
494 it('Should search the right magnetUri video', function (done) {
495 const video = videosListBase[0]
496 videosUtils.searchVideoWithPagination(server.url, encodeURIComponent(video.magnetUri), 'magnetUri', 0, 15, function (err, res) {
497 if (err) throw err
498
499 const videos = res.body.data
500 expect(res.body.total).to.equal(1)
501 expect(videos.length).to.equal(1)
502 expect(videos[0].name).to.equal(video.name)
503
504 done()
505 })
506 })
507
508 it('Should list and sort by name in descending order', function (done) {
509 videosUtils.getVideosListSort(server.url, '-name', function (err, res) {
510 if (err) throw err
511
512 const videos = res.body.data
513 expect(res.body.total).to.equal(6)
514 expect(videos.length).to.equal(6)
515 expect(videos[0].name).to.equal('video_short.webm name')
516 expect(videos[1].name).to.equal('video_short.ogv name')
517 expect(videos[2].name).to.equal('video_short.mp4 name')
518 expect(videos[3].name).to.equal('video_short3.webm name')
519 expect(videos[4].name).to.equal('video_short2.webm name')
520 expect(videos[5].name).to.equal('video_short1.webm name')
521
522 done()
523 })
524 })
525
526 it('Should search and sort by name in ascending order', function (done) {
527 videosUtils.searchVideoWithSort(server.url, 'webm', 'name', function (err, res) {
528 if (err) throw err
529
530 const videos = res.body.data
531 expect(res.body.total).to.equal(4)
532 expect(videos.length).to.equal(4)
533
534 expect(videos[0].name).to.equal('video_short1.webm name')
535 expect(videos[1].name).to.equal('video_short2.webm name')
536 expect(videos[2].name).to.equal('video_short3.webm name')
537 expect(videos[3].name).to.equal('video_short.webm name')
538
539 videoId = videos[2].id
540
541 done()
542 })
543 })
544
545 it('Should update a video', function (done) {
546 const name = 'my super video updated'
547 const category = 4
548 const description = 'my super description updated'
549 const tags = [ 'tagup1', 'tagup2' ]
550
551 videosUtils.updateVideo(server.url, server.accessToken, videoId, name, category, description, tags, done)
552 })
553
554 it('Should have the video updated', function (done) {
555 this.timeout(60000)
556
557 videosUtils.getVideo(server.url, videoId, function (err, res) {
558 if (err) throw err
559
560 const video = res.body
561
562 expect(video.name).to.equal('my super video updated')
563 expect(video.category).to.equal(4)
564 expect(video.categoryLabel).to.equal('Art')
565 expect(video.description).to.equal('my super description updated')
566 expect(video.podHost).to.equal('localhost:9001')
567 expect(video.author).to.equal('root')
568 expect(video.isLocal).to.be.true
569 expect(video.tags).to.deep.equal([ 'tagup1', 'tagup2' ])
570 expect(miscsUtils.dateIsValid(video.createdAt)).to.be.true
571 expect(miscsUtils.dateIsValid(video.updatedAt)).to.be.true
572
573 videosUtils.testVideoImage(server.url, 'video_short3.webm', video.thumbnailPath, function (err, test) {
574 if (err) throw err
575 expect(test).to.equal(true)
576
577 webtorrent.add(video.magnetUri, function (torrent) {
578 expect(torrent.files).to.exist
579 expect(torrent.files.length).to.equal(1)
580 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
581
582 done()
583 })
584 })
585 })
586 })
587
588 it('Should update only the tags of a video', function (done) {
589 const tags = [ 'tag1', 'tag2', 'supertag' ]
590
591 videosUtils.updateVideo(server.url, server.accessToken, videoId, null, null, null, tags, function (err) {
592 if (err) throw err
593
594 videosUtils.getVideo(server.url, videoId, function (err, res) {
595 if (err) throw err
596
597 const video = res.body
598
599 expect(video.name).to.equal('my super video updated')
600 expect(video.category).to.equal(4)
601 expect(video.categoryLabel).to.equal('Art')
602 expect(video.description).to.equal('my super description updated')
603 expect(video.podHost).to.equal('localhost:9001')
604 expect(video.author).to.equal('root')
605 expect(video.isLocal).to.be.true
606 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'supertag' ])
607 expect(miscsUtils.dateIsValid(video.createdAt)).to.be.true
608 expect(miscsUtils.dateIsValid(video.updatedAt)).to.be.true
609
610 done()
611 })
612 })
613 })
614
615 it('Should update only the description of a video', function (done) {
616 const description = 'hello everybody'
617
618 videosUtils.updateVideo(server.url, server.accessToken, videoId, null, null, description, null, function (err) {
619 if (err) throw err
620
621 videosUtils.getVideo(server.url, videoId, function (err, res) {
622 if (err) throw err
623
624 const video = res.body
625
626 expect(video.name).to.equal('my super video updated')
627 expect(video.category).to.equal(4)
628 expect(video.categoryLabel).to.equal('Art')
629 expect(video.description).to.equal('hello everybody')
630 expect(video.podHost).to.equal('localhost:9001')
631 expect(video.author).to.equal('root')
632 expect(video.isLocal).to.be.true
633 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'supertag' ])
634 expect(miscsUtils.dateIsValid(video.createdAt)).to.be.true
635 expect(miscsUtils.dateIsValid(video.updatedAt)).to.be.true
636
637 done()
638 })
639 })
640 })
641
642 it('Should like a video', function (done) {
643 videosUtils.rateVideo(server.url, server.accessToken, videoId, 'like', function (err) {
644 if (err) throw err
645
646 videosUtils.getVideo(server.url, videoId, function (err, res) {
647 if (err) throw err
648
649 const video = res.body
650
651 expect(video.likes).to.equal(1)
652 expect(video.dislikes).to.equal(0)
653
654 done()
655 })
656 })
657 })
658
659 it('Should dislike the same video', function (done) {
660 videosUtils.rateVideo(server.url, server.accessToken, videoId, 'dislike', function (err) {
661 if (err) throw err
662
663 videosUtils.getVideo(server.url, videoId, function (err, res) {
664 if (err) throw err
665
666 const video = res.body
667
668 expect(video.likes).to.equal(0)
669 expect(video.dislikes).to.equal(1)
670
671 done()
672 })
673 })
674 })
675
676 after(function (done) {
677 process.kill(-server.app.pid)
678
679 // Keep the logs if the test failed
680 if (this.ok) {
681 serversUtils.flushTests(done)
682 } else {
683 done()
684 }
685 })
686 })