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')
11 const loginUtils = require('../utils/login')
12 const miscsUtils = require('../utils/miscs')
13 const serversUtils = require('../utils/servers')
14 const videosUtils = require('../utils/videos')
15 const webtorrent = require(pathUtils.join(__dirname, '../../lib/webtorrent'))
16 webtorrent.silent = true
18 describe('Test a single pod', function () {
21 let videosListBase = null
23 before(function (done) {
28 serversUtils.flushTests(next)
31 serversUtils.runServer(1, function (server1) {
37 loginUtils.loginAndGetAccessToken(server, function (err, token) {
39 server.accessToken = token
44 webtorrent.create({ host: 'client', port: '1' }, next)
49 it('Should not have videos', function (done) {
50 videosUtils.getVideosList(server.url, function (err, res) {
53 expect(res.body.total).to.equal(0)
54 expect(res.body.data).to.be.an('array')
55 expect(res.body.data.length).to.equal(0)
61 it('Should upload the video', function (done) {
63 const name = 'my super name'
64 const description = 'my super description'
65 const tags = [ 'tag1', 'tag2', 'tag3' ]
66 const file = 'video_short.webm'
67 videosUtils.uploadVideo(server.url, server.accessToken, name, description, tags, file, done)
70 it('Should seed the uploaded video', function (done) {
71 // Yes, this could be long
74 videosUtils.getVideosList(server.url, function (err, res) {
77 expect(res.body.total).to.equal(1)
78 expect(res.body.data).to.be.an('array')
79 expect(res.body.data.length).to.equal(1)
81 const video = res.body.data[0]
82 expect(video.name).to.equal('my super name')
83 expect(video.description).to.equal('my super description')
84 expect(video.podUrl).to.equal('localhost:9001')
85 expect(video.magnetUri).to.exist
86 expect(video.author).to.equal('root')
87 expect(video.isLocal).to.be.true
88 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
89 expect(miscsUtils.dateIsValid(video.createdDate)).to.be.true
91 videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
93 expect(test).to.equal(true)
97 webtorrent.add(video.magnetUri, function (torrent) {
98 expect(torrent.files).to.exist
99 expect(torrent.files.length).to.equal(1)
100 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
108 it('Should get the video', function (done) {
109 // Yes, this could be long
112 videosUtils.getVideo(server.url, videoId, function (err, res) {
115 const video = res.body
116 expect(video.name).to.equal('my super name')
117 expect(video.description).to.equal('my super description')
118 expect(video.podUrl).to.equal('localhost:9001')
119 expect(video.magnetUri).to.exist
120 expect(video.author).to.equal('root')
121 expect(video.isLocal).to.be.true
122 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
123 expect(miscsUtils.dateIsValid(video.createdDate)).to.be.true
125 videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
127 expect(test).to.equal(true)
134 it('Should search the video by name by default', function (done) {
135 videosUtils.searchVideo(server.url, 'my', function (err, res) {
138 expect(res.body.total).to.equal(1)
139 expect(res.body.data).to.be.an('array')
140 expect(res.body.data.length).to.equal(1)
142 const video = res.body.data[0]
143 expect(video.name).to.equal('my super name')
144 expect(video.description).to.equal('my super description')
145 expect(video.podUrl).to.equal('localhost:9001')
146 expect(video.author).to.equal('root')
147 expect(video.isLocal).to.be.true
148 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
149 expect(miscsUtils.dateIsValid(video.createdDate)).to.be.true
151 videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
153 expect(test).to.equal(true)
160 it('Should search the video by podUrl', function (done) {
161 videosUtils.searchVideo(server.url, '9001', 'podUrl', function (err, res) {
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.podUrl).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.createdDate)).to.be.true
177 videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
179 expect(test).to.equal(true)
186 it('Should search the video by tag', function (done) {
187 videosUtils.searchVideo(server.url, 'tag1', 'tags', function (err, res) {
190 expect(res.body.total).to.equal(1)
191 expect(res.body.data).to.be.an('array')
192 expect(res.body.data.length).to.equal(1)
194 const video = res.body.data[0]
195 expect(video.name).to.equal('my super name')
196 expect(video.description).to.equal('my super description')
197 expect(video.podUrl).to.equal('localhost:9001')
198 expect(video.author).to.equal('root')
199 expect(video.isLocal).to.be.true
200 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
201 expect(miscsUtils.dateIsValid(video.createdDate)).to.be.true
203 videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
205 expect(test).to.equal(true)
212 it('Should not find a search by name by default', function (done) {
213 videosUtils.searchVideo(server.url, 'hello', function (err, res) {
216 expect(res.body.total).to.equal(0)
217 expect(res.body.data).to.be.an('array')
218 expect(res.body.data.length).to.equal(0)
224 it('Should not find a search by author', function (done) {
225 videosUtils.searchVideo(server.url, 'hello', 'author', function (err, res) {
228 expect(res.body.total).to.equal(0)
229 expect(res.body.data).to.be.an('array')
230 expect(res.body.data.length).to.equal(0)
236 it('Should not find a search by tag', function (done) {
237 videosUtils.searchVideo(server.url, 'tag', 'tags', function (err, res) {
240 expect(res.body.total).to.equal(0)
241 expect(res.body.data).to.be.an('array')
242 expect(res.body.data.length).to.equal(0)
248 it('Should remove the video', function (done) {
249 videosUtils.removeVideo(server.url, server.accessToken, videoId, function (err) {
252 fs.readdir(pathUtils.join(__dirname, '../../../test1/uploads/'), function (err, files) {
255 expect(files.length).to.equal(0)
261 it('Should not have videos', function (done) {
262 videosUtils.getVideosList(server.url, 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 upload 6 videos', function (done) {
276 'video_short.mp4', 'video_short.ogv', 'video_short.webm',
277 'video_short1.webm', 'video_short2.webm', 'video_short3.webm'
279 each(videos, function (video, callbackEach) {
280 const name = video + ' name'
281 const description = video + ' description'
282 const tags = [ 'tag1', 'tag2', 'tag3' ]
284 videosUtils.uploadVideo(server.url, server.accessToken, name, description, tags, video, callbackEach)
288 it('Should have the correct durations', function (done) {
289 videosUtils.getVideosList(server.url, function (err, res) {
292 expect(res.body.total).to.equal(6)
293 const videos = res.body.data
294 expect(videos).to.be.an('array')
295 expect(videos.length).to.equal(6)
297 const videosByName = keyBy(videos, 'name')
298 expect(videosByName['video_short.mp4 name'].duration).to.equal(5)
299 expect(videosByName['video_short.ogv name'].duration).to.equal(5)
300 expect(videosByName['video_short.webm name'].duration).to.equal(5)
301 expect(videosByName['video_short1.webm name'].duration).to.equal(10)
302 expect(videosByName['video_short2.webm name'].duration).to.equal(5)
303 expect(videosByName['video_short3.webm name'].duration).to.equal(5)
309 it('Should have the correct thumbnails', function (done) {
310 videosUtils.getVideosList(server.url, function (err, res) {
313 const videos = res.body.data
315 videosListBase = videos
317 each(videos, function (video, callbackEach) {
319 const videoName = video.name.replace(' name', '')
321 videosUtils.testVideoImage(server.url, videoName, video.thumbnailPath, function (err, test) {
324 expect(test).to.equal(true)
331 it('Should list only the two first videos', function (done) {
332 videosUtils.getVideosListPagination(server.url, 0, 2, function (err, res) {
335 const videos = res.body.data
336 expect(res.body.total).to.equal(6)
337 expect(videos.length).to.equal(2)
338 expect(videos[0].name === videosListBase[0].name)
339 expect(videos[1].name === videosListBase[1].name)
345 it('Should list only the next three videos', function (done) {
346 videosUtils.getVideosListPagination(server.url, 2, 3, function (err, res) {
349 const videos = res.body.data
350 expect(res.body.total).to.equal(6)
351 expect(videos.length).to.equal(3)
352 expect(videos[0].name === videosListBase[2].name)
353 expect(videos[1].name === videosListBase[3].name)
354 expect(videos[2].name === videosListBase[4].name)
360 it('Should list the last video', function (done) {
361 videosUtils.getVideosListPagination(server.url, 5, 6, function (err, res) {
364 const videos = res.body.data
365 expect(res.body.total).to.equal(6)
366 expect(videos.length).to.equal(1)
367 expect(videos[0].name === videosListBase[5].name)
373 it('Should search the first video', function (done) {
374 videosUtils.searchVideoWithPagination(server.url, 'webm', 'name', 0, 1, function (err, res) {
377 const videos = res.body.data
378 expect(res.body.total).to.equal(4)
379 expect(videos.length).to.equal(1)
380 expect(videos[0].name === 'video_short.webm name')
386 it('Should search the last two videos', function (done) {
387 videosUtils.searchVideoWithPagination(server.url, 'webm', 'name', 2, 2, function (err, res) {
390 const videos = res.body.data
391 expect(res.body.total).to.equal(4)
392 expect(videos.length).to.equal(2)
393 expect(videos[0].name === 'video_short2.webm name')
394 expect(videos[1].name === 'video_short3.webm name')
400 it('Should search all the webm videos', function (done) {
401 videosUtils.searchVideoWithPagination(server.url, 'webm', 'name', 0, 15, function (err, res) {
404 const videos = res.body.data
405 expect(res.body.total).to.equal(4)
406 expect(videos.length).to.equal(4)
412 it('Should search all the root author videos', function (done) {
413 videosUtils.searchVideoWithPagination(server.url, 'root', 'author', 0, 15, function (err, res) {
416 const videos = res.body.data
417 expect(res.body.total).to.equal(6)
418 expect(videos.length).to.equal(6)
424 it('Should search all the 9001 port videos', function (done) {
425 videosUtils.searchVideoWithPagination(server.url, '9001', 'podUrl', 0, 15, function (err, res) {
428 const videos = res.body.data
429 expect(res.body.total).to.equal(6)
430 expect(videos.length).to.equal(6)
436 it('Should search all the localhost videos', function (done) {
437 videosUtils.searchVideoWithPagination(server.url, 'localhost', 'podUrl', 0, 15, function (err, res) {
440 const videos = res.body.data
441 expect(res.body.total).to.equal(6)
442 expect(videos.length).to.equal(6)
448 it('Should search the good magnetUri video', function (done) {
449 const video = videosListBase[0]
450 videosUtils.searchVideoWithPagination(server.url, encodeURIComponent(video.magnetUri), 'magnetUri', 0, 15, function (err, res) {
453 const videos = res.body.data
454 expect(res.body.total).to.equal(1)
455 expect(videos.length).to.equal(1)
456 expect(videos[0].name).to.equal(video.name)
462 it('Should list and sort by name in descending order', function (done) {
463 videosUtils.getVideosListSort(server.url, '-name', function (err, res) {
466 const videos = res.body.data
467 expect(res.body.total).to.equal(6)
468 expect(videos.length).to.equal(6)
469 expect(videos[5].name === 'video_short.mp4 name')
470 expect(videos[4].name === 'video_short.ogv name')
471 expect(videos[3].name === 'video_short.webm name')
472 expect(videos[2].name === 'video_short1.webm name')
473 expect(videos[1].name === 'video_short2.webm name')
474 expect(videos[0].name === 'video_short3.webm name')
480 it('Should search and sort by name in ascending order', function (done) {
481 videosUtils.searchVideoWithSort(server.url, 'webm', 'name', function (err, res) {
484 const videos = res.body.data
485 expect(res.body.total).to.equal(4)
486 expect(videos.length).to.equal(4)
488 expect(videos[0].name === 'video_short.webm name')
489 expect(videos[1].name === 'video_short1.webm name')
490 expect(videos[2].name === 'video_short2.webm name')
491 expect(videos[3].name === 'video_short3.webm name')
497 after(function (done) {
498 process.kill(-server.app.pid)
499 process.kill(-webtorrent.app.pid)
501 // Keep the logs if the test failed
503 serversUtils.flushTests(done)