]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/single-pod.js
Server: fix tests
[github/Chocobozzz/PeerTube.git] / server / tests / api / single-pod.js
CommitLineData
9f10b292
C
1'use strict'
2
f0f5567b 3const chai = require('chai')
1a42c9e2 4const each = require('async/each')
f0f5567b
C
5const expect = chai.expect
6const fs = require('fs')
3a8a8b51 7const keyBy = require('lodash/keyBy')
f0f5567b 8const pathUtils = require('path')
1a42c9e2 9const series = require('async/series')
9f10b292 10
8d309058
C
11const loginUtils = require('../utils/login')
12const miscsUtils = require('../utils/miscs')
13const serversUtils = require('../utils/servers')
14const videosUtils = require('../utils/videos')
f0f5567b 15const webtorrent = require(pathUtils.join(__dirname, '../../lib/webtorrent'))
9f10b292
C
16webtorrent.silent = true
17
9f10b292 18describe('Test a single pod', function () {
0c1cbbfe 19 let server = null
bc503c2a 20 let videoId = -1
fbf1134e 21 let videosListBase = null
9f10b292
C
22
23 before(function (done) {
24 this.timeout(20000)
25
1a42c9e2 26 series([
9f10b292 27 function (next) {
8d309058 28 serversUtils.flushTests(next)
9f10b292
C
29 },
30 function (next) {
8d309058 31 serversUtils.runServer(1, function (server1) {
0c1cbbfe
C
32 server = server1
33 next()
34 })
35 },
36 function (next) {
8d309058 37 loginUtils.loginAndGetAccessToken(server, function (err, token) {
0c1cbbfe 38 if (err) throw err
b6c6f935 39 server.accessToken = token
9f10b292
C
40 next()
41 })
42 },
43 function (next) {
44 webtorrent.create({ host: 'client', port: '1' }, next)
45 }
46 ], done)
47 })
8c308c2b 48
9f10b292 49 it('Should not have videos', function (done) {
8d309058 50 videosUtils.getVideosList(server.url, function (err, res) {
9f10b292 51 if (err) throw err
8c308c2b 52
68ce3ae0
C
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)
8c308c2b 56
9f10b292 57 done()
8c308c2b 58 })
9f10b292 59 })
8c308c2b 60
9f10b292
C
61 it('Should upload the video', function (done) {
62 this.timeout(5000)
be587647
C
63 const name = 'my super name'
64 const description = 'my super description'
65 const tags = [ 'tag1', 'tag2', 'tag3' ]
66 const file = 'video_short.webm'
8d309058 67 videosUtils.uploadVideo(server.url, server.accessToken, name, description, tags, file, done)
9f10b292 68 })
8c308c2b 69
9f10b292
C
70 it('Should seed the uploaded video', function (done) {
71 // Yes, this could be long
72 this.timeout(60000)
8c308c2b 73
8d309058 74 videosUtils.getVideosList(server.url, function (err, res) {
9f10b292 75 if (err) throw err
8c308c2b 76
68ce3ae0
C
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)
8c308c2b 80
68ce3ae0 81 const video = res.body.data[0]
9f10b292
C
82 expect(video.name).to.equal('my super name')
83 expect(video.description).to.equal('my super description')
68ce3ae0 84 expect(video.podUrl).to.equal('localhost:9001')
9f10b292 85 expect(video.magnetUri).to.exist
6d8ada5f
C
86 expect(video.author).to.equal('root')
87 expect(video.isLocal).to.be.true
be587647 88 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
8d309058 89 expect(miscsUtils.dateIsValid(video.createdDate)).to.be.true
8c308c2b 90
8d309058 91 videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
9e5f3740
C
92 if (err) throw err
93 expect(test).to.equal(true)
2df82d42 94
bc503c2a 95 videoId = video.id
2df82d42 96
9e5f3740
C
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('')
101
ede4db9e 102 done()
9e5f3740 103 })
2df82d42
C
104 })
105 })
106 })
107
108 it('Should get the video', function (done) {
109 // Yes, this could be long
110 this.timeout(60000)
111
8d309058 112 videosUtils.getVideo(server.url, videoId, function (err, res) {
2df82d42
C
113 if (err) throw err
114
115 const video = res.body
116 expect(video.name).to.equal('my super name')
117 expect(video.description).to.equal('my super description')
68ce3ae0 118 expect(video.podUrl).to.equal('localhost:9001')
2df82d42 119 expect(video.magnetUri).to.exist
6d8ada5f
C
120 expect(video.author).to.equal('root')
121 expect(video.isLocal).to.be.true
be587647 122 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
8d309058 123 expect(miscsUtils.dateIsValid(video.createdDate)).to.be.true
8c308c2b 124
8d309058 125 videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
9e5f3740
C
126 if (err) throw err
127 expect(test).to.equal(true)
8c308c2b 128
ede4db9e 129 done()
876d1bcf 130 })
8c308c2b 131 })
9f10b292 132 })
8c308c2b 133
46246b5f 134 it('Should search the video by name by default', function (done) {
8d309058 135 videosUtils.searchVideo(server.url, 'my', function (err, res) {
9f10b292 136 if (err) throw err
4d5f8138 137
68ce3ae0
C
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)
4d5f8138 141
68ce3ae0 142 const video = res.body.data[0]
9f10b292
C
143 expect(video.name).to.equal('my super name')
144 expect(video.description).to.equal('my super description')
68ce3ae0 145 expect(video.podUrl).to.equal('localhost:9001')
6d8ada5f
C
146 expect(video.author).to.equal('root')
147 expect(video.isLocal).to.be.true
be587647 148 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
8d309058 149 expect(miscsUtils.dateIsValid(video.createdDate)).to.be.true
4d5f8138 150
8d309058 151 videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
9e5f3740
C
152 if (err) throw err
153 expect(test).to.equal(true)
154
155 done()
156 })
4d5f8138 157 })
9f10b292 158 })
4d5f8138 159
46246b5f 160 it('Should search the video by podUrl', function (done) {
8d309058 161 videosUtils.searchVideo(server.url, '9001', 'podUrl', function (err, res) {
46246b5f
C
162 if (err) throw err
163
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)
167
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
be587647 174 expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
8d309058 175 expect(miscsUtils.dateIsValid(video.createdDate)).to.be.true
46246b5f 176
8d309058 177 videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
46246b5f
C
178 if (err) throw err
179 expect(test).to.equal(true)
180
181 done()
182 })
183 })
184 })
185
8d199cb8 186 it('Should search the video by tag', function (done) {
8d309058 187 videosUtils.searchVideo(server.url, 'tag1', 'tags', function (err, res) {
8d199cb8
C
188 if (err) throw err
189
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)
193
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' ])
8d309058 201 expect(miscsUtils.dateIsValid(video.createdDate)).to.be.true
8d199cb8 202
8d309058 203 videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
8d199cb8
C
204 if (err) throw err
205 expect(test).to.equal(true)
206
207 done()
208 })
209 })
210 })
211
46246b5f 212 it('Should not find a search by name by default', function (done) {
8d309058 213 videosUtils.searchVideo(server.url, 'hello', function (err, res) {
9f10b292 214 if (err) throw err
4d5f8138 215
68ce3ae0
C
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)
4d5f8138 219
9f10b292 220 done()
4d5f8138 221 })
9f10b292 222 })
4d5f8138 223
46246b5f 224 it('Should not find a search by author', function (done) {
8d309058 225 videosUtils.searchVideo(server.url, 'hello', 'author', function (err, res) {
46246b5f
C
226 if (err) throw err
227
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)
231
232 done()
233 })
234 })
235
8d199cb8 236 it('Should not find a search by tag', function (done) {
8d309058 237 videosUtils.searchVideo(server.url, 'tag', 'tags', function (err, res) {
8d199cb8
C
238 if (err) throw err
239
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)
243
244 done()
245 })
246 })
247
9f10b292 248 it('Should remove the video', function (done) {
8d309058 249 videosUtils.removeVideo(server.url, server.accessToken, videoId, function (err) {
9f10b292 250 if (err) throw err
f5a60a51 251
3d446a26 252 fs.readdir(pathUtils.join(__dirname, '../../../test1/uploads/'), function (err, files) {
9f10b292 253 if (err) throw err
f5a60a51 254
9f10b292
C
255 expect(files.length).to.equal(0)
256 done()
876d1bcf 257 })
8c308c2b 258 })
9f10b292 259 })
8c308c2b 260
9f10b292 261 it('Should not have videos', function (done) {
8d309058 262 videosUtils.getVideosList(server.url, function (err, res) {
9f10b292 263 if (err) throw err
8c308c2b 264
68ce3ae0
C
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)
8c308c2b 268
9f10b292 269 done()
8c308c2b 270 })
9f10b292 271 })
8c308c2b 272
3a8a8b51
C
273 it('Should upload 6 videos', function (done) {
274 this.timeout(25000)
275 const videos = [
276 'video_short.mp4', 'video_short.ogv', 'video_short.webm',
277 'video_short1.webm', 'video_short2.webm', 'video_short3.webm'
278 ]
1a42c9e2 279 each(videos, function (video, callbackEach) {
be587647
C
280 const name = video + ' name'
281 const description = video + ' description'
282 const tags = [ 'tag1', 'tag2', 'tag3' ]
283
8d309058 284 videosUtils.uploadVideo(server.url, server.accessToken, name, description, tags, video, callbackEach)
3a8a8b51
C
285 }, done)
286 })
287
288 it('Should have the correct durations', function (done) {
8d309058 289 videosUtils.getVideosList(server.url, function (err, res) {
3a8a8b51
C
290 if (err) throw err
291
68ce3ae0
C
292 expect(res.body.total).to.equal(6)
293 const videos = res.body.data
3a8a8b51
C
294 expect(videos).to.be.an('array')
295 expect(videos.length).to.equal(6)
296
bc503c2a
C
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)
3a8a8b51
C
304
305 done()
306 })
307 })
308
9e5f3740 309 it('Should have the correct thumbnails', function (done) {
8d309058 310 videosUtils.getVideosList(server.url, function (err, res) {
fbf1134e
C
311 if (err) throw err
312
68ce3ae0 313 const videos = res.body.data
fbf1134e
C
314 // For the next test
315 videosListBase = videos
9e5f3740 316
1a42c9e2 317 each(videos, function (video, callbackEach) {
9e5f3740 318 if (err) throw err
bc503c2a 319 const videoName = video.name.replace(' name', '')
9e5f3740 320
8d309058 321 videosUtils.testVideoImage(server.url, videoName, video.thumbnailPath, function (err, test) {
9e5f3740
C
322 if (err) throw err
323
324 expect(test).to.equal(true)
bc503c2a 325 callbackEach()
9e5f3740
C
326 })
327 }, done)
328 })
329 })
330
fbf1134e 331 it('Should list only the two first videos', function (done) {
8d309058 332 videosUtils.getVideosListPagination(server.url, 0, 2, function (err, res) {
fbf1134e
C
333 if (err) throw err
334
68ce3ae0
C
335 const videos = res.body.data
336 expect(res.body.total).to.equal(6)
fbf1134e
C
337 expect(videos.length).to.equal(2)
338 expect(videos[0].name === videosListBase[0].name)
339 expect(videos[1].name === videosListBase[1].name)
340
341 done()
342 })
343 })
344
345 it('Should list only the next three videos', function (done) {
8d309058 346 videosUtils.getVideosListPagination(server.url, 2, 3, function (err, res) {
fbf1134e
C
347 if (err) throw err
348
68ce3ae0
C
349 const videos = res.body.data
350 expect(res.body.total).to.equal(6)
022856f8 351 expect(videos.length).to.equal(3)
fbf1134e
C
352 expect(videos[0].name === videosListBase[2].name)
353 expect(videos[1].name === videosListBase[3].name)
354 expect(videos[2].name === videosListBase[4].name)
355
356 done()
357 })
358 })
359
360 it('Should list the last video', function (done) {
8d309058 361 videosUtils.getVideosListPagination(server.url, 5, 6, function (err, res) {
fbf1134e
C
362 if (err) throw err
363
68ce3ae0
C
364 const videos = res.body.data
365 expect(res.body.total).to.equal(6)
fbf1134e
C
366 expect(videos.length).to.equal(1)
367 expect(videos[0].name === videosListBase[5].name)
368
369 done()
370 })
371 })
372
373 it('Should search the first video', function (done) {
8d309058 374 videosUtils.searchVideoWithPagination(server.url, 'webm', 'name', 0, 1, function (err, res) {
fbf1134e
C
375 if (err) throw err
376
68ce3ae0
C
377 const videos = res.body.data
378 expect(res.body.total).to.equal(4)
fbf1134e
C
379 expect(videos.length).to.equal(1)
380 expect(videos[0].name === 'video_short.webm name')
381
382 done()
383 })
384 })
385
386 it('Should search the last two videos', function (done) {
8d309058 387 videosUtils.searchVideoWithPagination(server.url, 'webm', 'name', 2, 2, function (err, res) {
fbf1134e
C
388 if (err) throw err
389
68ce3ae0
C
390 const videos = res.body.data
391 expect(res.body.total).to.equal(4)
fbf1134e
C
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')
395
396 done()
397 })
398 })
399
46246b5f 400 it('Should search all the webm videos', function (done) {
8d309058 401 videosUtils.searchVideoWithPagination(server.url, 'webm', 'name', 0, 15, function (err, res) {
fbf1134e
C
402 if (err) throw err
403
68ce3ae0
C
404 const videos = res.body.data
405 expect(res.body.total).to.equal(4)
fbf1134e
C
406 expect(videos.length).to.equal(4)
407
408 done()
409 })
410 })
411
46246b5f 412 it('Should search all the root author videos', function (done) {
8d309058 413 videosUtils.searchVideoWithPagination(server.url, 'root', 'author', 0, 15, function (err, res) {
46246b5f
C
414 if (err) throw err
415
416 const videos = res.body.data
417 expect(res.body.total).to.equal(6)
418 expect(videos.length).to.equal(6)
419
420 done()
421 })
422 })
423
424 it('Should search all the 9001 port videos', function (done) {
8d309058 425 videosUtils.searchVideoWithPagination(server.url, '9001', 'podUrl', 0, 15, function (err, res) {
46246b5f
C
426 if (err) throw err
427
428 const videos = res.body.data
429 expect(res.body.total).to.equal(6)
430 expect(videos.length).to.equal(6)
431
432 done()
433 })
434 })
435
436 it('Should search all the localhost videos', function (done) {
8d309058 437 videosUtils.searchVideoWithPagination(server.url, 'localhost', 'podUrl', 0, 15, function (err, res) {
46246b5f
C
438 if (err) throw err
439
440 const videos = res.body.data
441 expect(res.body.total).to.equal(6)
442 expect(videos.length).to.equal(6)
443
444 done()
445 })
446 })
447
448 it('Should search the good magnetUri video', function (done) {
449 const video = videosListBase[0]
8d309058 450 videosUtils.searchVideoWithPagination(server.url, encodeURIComponent(video.magnetUri), 'magnetUri', 0, 15, function (err, res) {
46246b5f
C
451 if (err) throw err
452
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)
457
458 done()
459 })
460 })
461
a877d5ac 462 it('Should list and sort by name in descending order', function (done) {
8d309058 463 videosUtils.getVideosListSort(server.url, '-name', function (err, res) {
a877d5ac
C
464 if (err) throw err
465
68ce3ae0
C
466 const videos = res.body.data
467 expect(res.body.total).to.equal(6)
a877d5ac
C
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')
475
476 done()
477 })
478 })
479
480 it('Should search and sort by name in ascending order', function (done) {
8d309058 481 videosUtils.searchVideoWithSort(server.url, 'webm', 'name', function (err, res) {
a877d5ac
C
482 if (err) throw err
483
68ce3ae0
C
484 const videos = res.body.data
485 expect(res.body.total).to.equal(4)
a877d5ac
C
486 expect(videos.length).to.equal(4)
487
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')
492
493 done()
494 })
495 })
496
9f10b292 497 after(function (done) {
0c1cbbfe 498 process.kill(-server.app.pid)
9f10b292 499 process.kill(-webtorrent.app.pid)
8c308c2b 500
9f10b292
C
501 // Keep the logs if the test failed
502 if (this.ok) {
8d309058 503 serversUtils.flushTests(done)
9f10b292
C
504 } else {
505 done()
506 }
8c308c2b 507 })
9f10b292 508})