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