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