]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/single-pod.js
1 /* eslint-disable no-unused-expressions */
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'))()
14 const loginUtils
= require('../utils/login')
15 const miscsUtils
= require('../utils/miscs')
16 const serversUtils
= require('../utils/servers')
17 const videosUtils
= require('../utils/videos')
19 describe('Test a single pod', function () {
22 let videosListBase
= null
24 before(function (done
) {
29 serversUtils
.flushTests(next
)
32 serversUtils
.runServer(1, function (server1
) {
38 loginUtils
.loginAndGetAccessToken(server
, function (err
, token
) {
40 server
.accessToken
= token
47 it('Should list video categories', function (done
) {
48 videosUtils
.getVideoCategories(server
.url
, function (err
, res
) {
51 const categories
= res
.body
52 expect(Object
.keys(categories
)).to
.have
.length
.above(10)
54 expect(categories
[11]).to
.equal('News')
60 it('Should not have videos', function (done
) {
61 videosUtils
.getVideosList(server
.url
, function (err
, res
) {
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)
72 it('Should upload the video', function (done
) {
74 const name
= 'my super name'
75 const description
= 'my super description'
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
)
82 it('Should seed the uploaded video', function (done
) {
83 // Yes, this could be long
86 videosUtils
.getVideosList(server
.url
, function (err
, res
) {
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)
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
106 videosUtils
.testVideoImage(server
.url
, 'video_short.webm', video
.thumbnailPath
, function (err
, test
) {
108 expect(test
).to
.equal(true)
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('')
123 it('Should get the video', function (done
) {
124 // Yes, this could be long
127 videosUtils
.getVideo(server
.url
, videoId
, function (err
, res
) {
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
143 videosUtils
.testVideoImage(server
.url
, 'video_short.webm', video
.thumbnailPath
, function (err
, test
) {
145 expect(test
).to
.equal(true)
152 it('Should have the views updated', function (done
) {
153 videosUtils
.getVideo(server
.url
, videoId
, function (err
, res
) {
156 const video
= res
.body
157 expect(video
.views
).to
.equal(1)
163 it('Should search the video by name by default', function (done
) {
164 videosUtils
.searchVideo(server
.url
, 'my', function (err
, res
) {
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)
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
183 videosUtils
.testVideoImage(server
.url
, 'video_short.webm', video
.thumbnailPath
, function (err
, test
) {
185 expect(test
).to
.equal(true)
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
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)
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
211 // videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
212 // if (err) throw err
213 // expect(test).to.equal(true)
220 it('Should search the video by tag', function (done
) {
221 videosUtils
.searchVideo(server
.url
, 'tag1', 'tags', function (err
, res
) {
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)
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
240 videosUtils
.testVideoImage(server
.url
, 'video_short.webm', video
.thumbnailPath
, function (err
, test
) {
242 expect(test
).to
.equal(true)
249 it('Should not find a search by name by default', function (done
) {
250 videosUtils
.searchVideo(server
.url
, 'hello', function (err
, res
) {
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)
261 it('Should not find a search by author', function (done
) {
262 videosUtils
.searchVideo(server
.url
, 'hello', 'author', function (err
, res
) {
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)
273 it('Should not find a search by tag', function (done
) {
274 videosUtils
.searchVideo(server
.url
, 'hello', 'tags', function (err
, res
) {
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)
285 it('Should remove the video', function (done
) {
286 videosUtils
.removeVideo(server
.url
, server
.accessToken
, videoId
, function (err
) {
289 fs
.readdir(pathUtils
.join(__dirname
, '..', '..', '..', 'test1/videos/'), function (err
, files
) {
292 expect(files
.length
).to
.equal(0)
294 fs
.readdir(pathUtils
.join(__dirname
, '..', '..', '..', 'test1/thumbnails/'), function (err
, files
) {
297 expect(files
.length
).to
.equal(0)
305 it('Should not have videos', function (done
) {
306 videosUtils
.getVideosList(server
.url
, function (err
, res
) {
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)
317 it('Should upload 6 videos', function (done
) {
320 'video_short.mp4', 'video_short.ogv', 'video_short.webm',
321 'video_short1.webm', 'video_short2.webm', 'video_short3.webm'
323 each(videos
, function (video
, callbackEach
) {
324 const name
= video
+ ' name'
325 const description
= video
+ ' description'
327 const tags
= [ 'tag1', 'tag2', 'tag3' ]
329 videosUtils
.uploadVideo(server
.url
, server
.accessToken
, name
, category
, description
, tags
, video
, callbackEach
)
333 it('Should have the correct durations', function (done
) {
334 videosUtils
.getVideosList(server
.url
, function (err
, res
) {
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)
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)
354 it('Should have the correct thumbnails', function (done
) {
355 videosUtils
.getVideosList(server
.url
, function (err
, res
) {
358 const videos
= res
.body
.data
360 videosListBase
= videos
362 each(videos
, function (video
, callbackEach
) {
364 const videoName
= video
.name
.replace(' name', '')
366 videosUtils
.testVideoImage(server
.url
, videoName
, video
.thumbnailPath
, function (err
, test
) {
369 expect(test
).to
.equal(true)
376 it('Should list only the two first videos', function (done
) {
377 videosUtils
.getVideosListPagination(server
.url
, 0, 2, 'name', function (err
, res
) {
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
)
390 it('Should list only the next three videos', function (done
) {
391 videosUtils
.getVideosListPagination(server
.url
, 2, 3, 'name', function (err
, res
) {
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
)
405 it('Should list the last video', function (done
) {
406 videosUtils
.getVideosListPagination(server
.url
, 5, 6, 'name', function (err
, res
) {
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
)
418 it('Should search the first video', function (done
) {
419 videosUtils
.searchVideoWithPagination(server
.url
, 'webm', 'name', 0, 1, 'name', function (err
, res
) {
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')
431 it('Should search the last two videos', function (done
) {
432 videosUtils
.searchVideoWithPagination(server
.url
, 'webm', 'name', 2, 2, 'name', function (err
, res
) {
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')
445 it('Should search all the webm videos', function (done
) {
446 videosUtils
.searchVideoWithPagination(server
.url
, 'webm', 'name', 0, 15, function (err
, res
) {
449 const videos
= res
.body
.data
450 expect(res
.body
.total
).to
.equal(4)
451 expect(videos
.length
).to
.equal(4)
457 it('Should search all the root author videos', function (done
) {
458 videosUtils
.searchVideoWithPagination(server
.url
, 'root', 'author', 0, 15, function (err
, res
) {
461 const videos
= res
.body
.data
462 expect(res
.body
.total
).to
.equal(6)
463 expect(videos
.length
).to
.equal(6)
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
474 // const videos = res.body.data
475 // expect(res.body.total).to.equal(6)
476 // expect(videos.length).to.equal(6)
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
486 // const videos = res.body.data
487 // expect(res.body.total).to.equal(6)
488 // expect(videos.length).to.equal(6)
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
) {
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
)
508 it('Should list and sort by name in descending order', function (done
) {
509 videosUtils
.getVideosListSort(server
.url
, '-name', function (err
, res
) {
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')
526 it('Should search and sort by name in ascending order', function (done
) {
527 videosUtils
.searchVideoWithSort(server
.url
, 'webm', 'name', function (err
, res
) {
530 const videos
= res
.body
.data
531 expect(res
.body
.total
).to
.equal(4)
532 expect(videos
.length
).to
.equal(4)
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')
539 videoId
= videos
[2].id
545 it('Should update a video', function (done
) {
546 const name
= 'my super video updated'
548 const description
= 'my super description updated'
549 const tags
= [ 'tagup1', 'tagup2' ]
551 videosUtils
.updateVideo(server
.url
, server
.accessToken
, videoId
, name
, category
, description
, tags
, done
)
554 it('Should have the video updated', function (done
) {
557 videosUtils
.getVideo(server
.url
, videoId
, function (err
, res
) {
560 const video
= res
.body
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
573 videosUtils
.testVideoImage(server
.url
, 'video_short3.webm', video
.thumbnailPath
, function (err
, test
) {
575 expect(test
).to
.equal(true)
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('')
588 it('Should update only the tags of a video', function (done
) {
589 const tags
= [ 'tag1', 'tag2', 'supertag' ]
591 videosUtils
.updateVideo(server
.url
, server
.accessToken
, videoId
, null, null, null, tags
, function (err
) {
594 videosUtils
.getVideo(server
.url
, videoId
, function (err
, res
) {
597 const video
= res
.body
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
615 it('Should update only the description of a video', function (done
) {
616 const description
= 'hello everybody'
618 videosUtils
.updateVideo(server
.url
, server
.accessToken
, videoId
, null, null, description
, null, function (err
) {
621 videosUtils
.getVideo(server
.url
, videoId
, function (err
, res
) {
624 const video
= res
.body
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
642 it('Should like a video', function (done
) {
643 videosUtils
.rateVideo(server
.url
, server
.accessToken
, videoId
, 'like', function (err
) {
646 videosUtils
.getVideo(server
.url
, videoId
, function (err
, res
) {
649 const video
= res
.body
651 expect(video
.likes
).to
.equal(1)
652 expect(video
.dislikes
).to
.equal(0)
659 it('Should dislike the same video', function (done
) {
660 videosUtils
.rateVideo(server
.url
, server
.accessToken
, videoId
, 'dislike', function (err
) {
663 videosUtils
.getVideo(server
.url
, videoId
, function (err
, res
) {
666 const video
= res
.body
668 expect(video
.likes
).to
.equal(0)
669 expect(video
.dislikes
).to
.equal(1)
676 after(function (done
) {
677 process
.kill(-server
.app
.pid
)
679 // Keep the logs if the test failed
681 serversUtils
.flushTests(done
)