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