3 const chai = require('chai')
4 const each = require('async/each')
5 const expect = chai.expect
6 const fs = require('fs')
7 const keyBy = require('lodash/keyBy')
8 const pathUtils = require('path')
9 const series = require('async/series')
10 const webtorrent = new (require('webtorrent'))()
12 const loginUtils = require('../utils/login')
13 const miscsUtils = require('../utils/miscs')
14 const serversUtils = require('../utils/servers')
15 const videosUtils = require('../utils/videos')
17 describe('Test a single pod', function () {
20 let videosListBase = null
22 before(function (done) {
27 serversUtils.flushTests(next)
30 serversUtils.runServer(1, function (server1) {
36 loginUtils.loginAndGetAccessToken(server, function (err, token) {
38 server.accessToken = token
45 it('Should not have videos', function (done) {
46 videosUtils.getVideosList(server.url, function (err, res) {
49 expect(res.body.total).to.equal(0)
50 expect(res.body.data).to.be.an('array')
51 expect(res.body.data.length).to.equal(0)
57 it('Should upload the video', function (done) {
59 const name = 'my super name'
60 const description = 'my super description'
61 const tags = [ 'tag1', 'tag2', 'tag3' ]
62 const file = 'video_short.webm'
63 videosUtils.uploadVideo(server.url, server.accessToken, name, description, tags, file, done)
66 it('Should seed the uploaded video', function (done) {
67 // Yes, this could be long
70 videosUtils.getVideosList(server.url, function (err, res) {
73 expect(res.body.total).to.equal(1)
74 expect(res.body.data).to.be.an('array')
75 expect(res.body.data.length).to.equal(1)
77 const video = res.body.data[0]
78 expect(video.name).to.equal('my super name')
79 expect(video.description).to.equal('my super description')
80 expect(video.podHost).to.equal('localhost:9001')
81 expect(video.magnetUri).to.exist
82 expect(video.author).to.equal('root')
83 expect(video.isLocal).to.be.true
84 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
85 expect(miscsUtils.dateIsValid(video.createdAt)).to.be.true
86 expect(miscsUtils.dateIsValid(video.updatedAt)).to.be.true
88 videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
90 expect(test).to.equal(true)
94 webtorrent.add(video.magnetUri, function (torrent) {
95 expect(torrent.files).to.exist
96 expect(torrent.files.length).to.equal(1)
97 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
105 it('Should get the video', function (done) {
106 // Yes, this could be long
109 videosUtils.getVideo(server.url, videoId, function (err, res) {
112 const video = res.body
113 expect(video.name).to.equal('my super name')
114 expect(video.description).to.equal('my super description')
115 expect(video.podHost).to.equal('localhost:9001')
116 expect(video.magnetUri).to.exist
117 expect(video.author).to.equal('root')
118 expect(video.isLocal).to.be.true
119 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
120 expect(miscsUtils.dateIsValid(video.createdAt)).to.be.true
121 expect(miscsUtils.dateIsValid(video.updatedAt)).to.be.true
123 videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
125 expect(test).to.equal(true)
132 it('Should search the video by name by default', function (done) {
133 videosUtils.searchVideo(server.url, 'my', function (err, res) {
136 expect(res.body.total).to.equal(1)
137 expect(res.body.data).to.be.an('array')
138 expect(res.body.data.length).to.equal(1)
140 const video = res.body.data[0]
141 expect(video.name).to.equal('my super name')
142 expect(video.description).to.equal('my super description')
143 expect(video.podHost).to.equal('localhost:9001')
144 expect(video.author).to.equal('root')
145 expect(video.isLocal).to.be.true
146 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
147 expect(miscsUtils.dateIsValid(video.createdAt)).to.be.true
148 expect(miscsUtils.dateIsValid(video.updatedAt)).to.be.true
150 videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
152 expect(test).to.equal(true)
159 // Not implemented yet
160 // it('Should search the video by podHost', function (done) {
161 // videosUtils.searchVideo(server.url, '9001', 'host', function (err, res) {
162 // if (err) throw err
164 // expect(res.body.total).to.equal(1)
165 // expect(res.body.data).to.be.an('array')
166 // expect(res.body.data.length).to.equal(1)
168 // const video = res.body.data[0]
169 // expect(video.name).to.equal('my super name')
170 // expect(video.description).to.equal('my super description')
171 // expect(video.podHost).to.equal('localhost:9001')
172 // expect(video.author).to.equal('root')
173 // expect(video.isLocal).to.be.true
174 // expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
175 // expect(miscsUtils.dateIsValid(video.createdAt)).to.be.true
176 // expect(miscsUtils.dateIsValid(video.updatedAt)).to.be.true
178 // videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
179 // if (err) throw err
180 // expect(test).to.equal(true)
187 it('Should search the video by tag', function (done) {
188 videosUtils.searchVideo(server.url, 'tag1', 'tags', function (err, res) {
191 expect(res.body.total).to.equal(1)
192 expect(res.body.data).to.be.an('array')
193 expect(res.body.data.length).to.equal(1)
195 const video = res.body.data[0]
196 expect(video.name).to.equal('my super name')
197 expect(video.description).to.equal('my super description')
198 expect(video.podHost).to.equal('localhost:9001')
199 expect(video.author).to.equal('root')
200 expect(video.isLocal).to.be.true
201 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
202 expect(miscsUtils.dateIsValid(video.createdAt)).to.be.true
203 expect(miscsUtils.dateIsValid(video.updatedAt)).to.be.true
205 videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
207 expect(test).to.equal(true)
214 it('Should not find a search by name by default', function (done) {
215 videosUtils.searchVideo(server.url, 'hello', function (err, res) {
218 expect(res.body.total).to.equal(0)
219 expect(res.body.data).to.be.an('array')
220 expect(res.body.data.length).to.equal(0)
226 it('Should not find a search by author', function (done) {
227 videosUtils.searchVideo(server.url, 'hello', 'author', function (err, res) {
230 expect(res.body.total).to.equal(0)
231 expect(res.body.data).to.be.an('array')
232 expect(res.body.data.length).to.equal(0)
238 it('Should not find a search by tag', function (done) {
239 videosUtils.searchVideo(server.url, 'hello', 'tags', function (err, res) {
242 expect(res.body.total).to.equal(0)
243 expect(res.body.data).to.be.an('array')
244 expect(res.body.data.length).to.equal(0)
250 it('Should remove the video', function (done) {
251 videosUtils.removeVideo(server.url, server.accessToken, videoId, function (err) {
254 fs.readdir(pathUtils.join(__dirname, '../../../test1/videos/'), function (err, files) {
257 expect(files.length).to.equal(0)
259 fs.readdir(pathUtils.join(__dirname, '../../../test1/thumbnails/'), function (err, files) {
262 expect(files.length).to.equal(0)
270 it('Should not have videos', function (done) {
271 videosUtils.getVideosList(server.url, function (err, res) {
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)
282 it('Should upload 6 videos', function (done) {
285 'video_short.mp4', 'video_short.ogv', 'video_short.webm',
286 'video_short1.webm', 'video_short2.webm', 'video_short3.webm'
288 each(videos, function (video, callbackEach) {
289 const name = video + ' name'
290 const description = video + ' description'
291 const tags = [ 'tag1', 'tag2', 'tag3' ]
293 videosUtils.uploadVideo(server.url, server.accessToken, name, description, tags, video, callbackEach)
297 it('Should have the correct durations', function (done) {
298 videosUtils.getVideosList(server.url, function (err, res) {
301 expect(res.body.total).to.equal(6)
302 const videos = res.body.data
303 expect(videos).to.be.an('array')
304 expect(videos.length).to.equal(6)
306 const videosByName = keyBy(videos, 'name')
307 expect(videosByName['video_short.mp4 name'].duration).to.equal(5)
308 expect(videosByName['video_short.ogv name'].duration).to.equal(5)
309 expect(videosByName['video_short.webm name'].duration).to.equal(5)
310 expect(videosByName['video_short1.webm name'].duration).to.equal(10)
311 expect(videosByName['video_short2.webm name'].duration).to.equal(5)
312 expect(videosByName['video_short3.webm name'].duration).to.equal(5)
318 it('Should have the correct thumbnails', function (done) {
319 videosUtils.getVideosList(server.url, function (err, res) {
322 const videos = res.body.data
324 videosListBase = videos
326 each(videos, function (video, callbackEach) {
328 const videoName = video.name.replace(' name', '')
330 videosUtils.testVideoImage(server.url, videoName, video.thumbnailPath, function (err, test) {
333 expect(test).to.equal(true)
340 it('Should list only the two first videos', function (done) {
341 videosUtils.getVideosListPagination(server.url, 0, 2, 'name', function (err, res) {
344 const videos = res.body.data
345 expect(res.body.total).to.equal(6)
346 expect(videos.length).to.equal(2)
347 expect(videos[0].name).to.equal(videosListBase[0].name)
348 expect(videos[1].name).to.equal(videosListBase[1].name)
354 it('Should list only the next three videos', function (done) {
355 videosUtils.getVideosListPagination(server.url, 2, 3, 'name', function (err, res) {
358 const videos = res.body.data
359 expect(res.body.total).to.equal(6)
360 expect(videos.length).to.equal(3)
361 expect(videos[0].name).to.equal(videosListBase[2].name)
362 expect(videos[1].name).to.equal(videosListBase[3].name)
363 expect(videos[2].name).to.equal(videosListBase[4].name)
369 it('Should list the last video', function (done) {
370 videosUtils.getVideosListPagination(server.url, 5, 6, 'name', function (err, res) {
373 const videos = res.body.data
374 expect(res.body.total).to.equal(6)
375 expect(videos.length).to.equal(1)
376 expect(videos[0].name).to.equal(videosListBase[5].name)
382 it('Should search the first video', function (done) {
383 videosUtils.searchVideoWithPagination(server.url, 'webm', 'name', 0, 1, 'name', function (err, res) {
386 const videos = res.body.data
387 expect(res.body.total).to.equal(4)
388 expect(videos.length).to.equal(1)
389 expect(videos[0].name).to.equal('video_short1.webm name')
395 it('Should search the last two videos', function (done) {
396 videosUtils.searchVideoWithPagination(server.url, 'webm', 'name', 2, 2, 'name', function (err, res) {
399 const videos = res.body.data
400 expect(res.body.total).to.equal(4)
401 expect(videos.length).to.equal(2)
402 expect(videos[0].name).to.equal('video_short3.webm name')
403 expect(videos[1].name).to.equal('video_short.webm name')
409 it('Should search all the webm videos', function (done) {
410 videosUtils.searchVideoWithPagination(server.url, 'webm', 'name', 0, 15, function (err, res) {
413 const videos = res.body.data
414 expect(res.body.total).to.equal(4)
415 expect(videos.length).to.equal(4)
421 it('Should search all the root author videos', function (done) {
422 videosUtils.searchVideoWithPagination(server.url, 'root', 'author', 0, 15, function (err, res) {
425 const videos = res.body.data
426 expect(res.body.total).to.equal(6)
427 expect(videos.length).to.equal(6)
433 // Not implemented yet
434 // it('Should search all the 9001 port videos', function (done) {
435 // videosUtils.searchVideoWithPagination(server.url, '9001', 'host', 0, 15, function (err, res) {
436 // if (err) throw err
438 // const videos = res.body.data
439 // expect(res.body.total).to.equal(6)
440 // expect(videos.length).to.equal(6)
446 // it('Should search all the localhost videos', function (done) {
447 // videosUtils.searchVideoWithPagination(server.url, 'localhost', 'host', 0, 15, function (err, res) {
448 // if (err) throw err
450 // const videos = res.body.data
451 // expect(res.body.total).to.equal(6)
452 // expect(videos.length).to.equal(6)
458 it('Should search the good magnetUri video', function (done) {
459 const video = videosListBase[0]
460 videosUtils.searchVideoWithPagination(server.url, encodeURIComponent(video.magnetUri), 'magnetUri', 0, 15, function (err, res) {
463 const videos = res.body.data
464 expect(res.body.total).to.equal(1)
465 expect(videos.length).to.equal(1)
466 expect(videos[0].name).to.equal(video.name)
472 it('Should list and sort by name in descending order', function (done) {
473 videosUtils.getVideosListSort(server.url, '-name', function (err, res) {
476 const videos = res.body.data
477 expect(res.body.total).to.equal(6)
478 expect(videos.length).to.equal(6)
479 expect(videos[0].name).to.equal('video_short.webm name')
480 expect(videos[1].name).to.equal('video_short.ogv name')
481 expect(videos[2].name).to.equal('video_short.mp4 name')
482 expect(videos[3].name).to.equal('video_short3.webm name')
483 expect(videos[4].name).to.equal('video_short2.webm name')
484 expect(videos[5].name).to.equal('video_short1.webm name')
490 it('Should search and sort by name in ascending order', function (done) {
491 videosUtils.searchVideoWithSort(server.url, 'webm', 'name', function (err, res) {
494 const videos = res.body.data
495 expect(res.body.total).to.equal(4)
496 expect(videos.length).to.equal(4)
498 expect(videos[0].name).to.equal('video_short1.webm name')
499 expect(videos[1].name).to.equal('video_short2.webm name')
500 expect(videos[2].name).to.equal('video_short3.webm name')
501 expect(videos[3].name).to.equal('video_short.webm name')
503 videoId = videos[2].id
509 it('Should update a video', function (done) {
510 const name = 'my super video updated'
511 const description = 'my super description updated'
512 const tags = [ 'tagup1', 'tagup2' ]
514 videosUtils.updateVideo(server.url, server.accessToken, videoId, name, description, tags, done)
517 it('Should have the video updated', function (done) {
520 videosUtils.getVideo(server.url, videoId, function (err, res) {
523 const video = res.body
525 expect(video.name).to.equal('my super video updated')
526 expect(video.description).to.equal('my super description updated')
527 expect(video.podHost).to.equal('localhost:9001')
528 expect(video.author).to.equal('root')
529 expect(video.isLocal).to.be.true
530 expect(video.tags).to.deep.equal([ 'tagup1', 'tagup2' ])
531 expect(miscsUtils.dateIsValid(video.createdAt)).to.be.true
532 expect(miscsUtils.dateIsValid(video.updatedAt)).to.be.true
534 videosUtils.testVideoImage(server.url, 'video_short3.webm', video.thumbnailPath, function (err, test) {
536 expect(test).to.equal(true)
538 webtorrent.add(video.magnetUri, function (torrent) {
539 expect(torrent.files).to.exist
540 expect(torrent.files.length).to.equal(1)
541 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
549 it('Should update only the tags of a video', function (done) {
550 const tags = [ 'tag1', 'tag2', 'supertag' ]
552 videosUtils.updateVideo(server.url, server.accessToken, videoId, null, null, tags, function (err) {
555 videosUtils.getVideo(server.url, videoId, function (err, res) {
558 const video = res.body
560 expect(video.name).to.equal('my super video updated')
561 expect(video.description).to.equal('my super description updated')
562 expect(video.podHost).to.equal('localhost:9001')
563 expect(video.author).to.equal('root')
564 expect(video.isLocal).to.be.true
565 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'supertag' ])
566 expect(miscsUtils.dateIsValid(video.createdAt)).to.be.true
567 expect(miscsUtils.dateIsValid(video.updatedAt)).to.be.true
574 it('Should update only the description of a video', function (done) {
575 const description = 'hello everybody'
577 videosUtils.updateVideo(server.url, server.accessToken, videoId, null, description, null, function (err) {
580 videosUtils.getVideo(server.url, videoId, function (err, res) {
583 const video = res.body
585 expect(video.name).to.equal('my super video updated')
586 expect(video.description).to.equal('hello everybody')
587 expect(video.podHost).to.equal('localhost:9001')
588 expect(video.author).to.equal('root')
589 expect(video.isLocal).to.be.true
590 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'supertag' ])
591 expect(miscsUtils.dateIsValid(video.createdAt)).to.be.true
592 expect(miscsUtils.dateIsValid(video.updatedAt)).to.be.true
599 after(function (done) {
600 process.kill(-server.app.pid)
602 // Keep the logs if the test failed
604 serversUtils.flushTests(done)