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