Server: paths refractoring
[oweals/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       expect(miscsUtils.dateIsValid(video.updatedAt)).to.be.true
87
88       videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
89         if (err) throw err
90         expect(test).to.equal(true)
91
92         videoId = video.id
93
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('')
98
99           done()
100         })
101       })
102     })
103   })
104
105   it('Should get the video', function (done) {
106     // Yes, this could be long
107     this.timeout(60000)
108
109     videosUtils.getVideo(server.url, videoId, function (err, res) {
110       if (err) throw err
111
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
122
123       videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
124         if (err) throw err
125         expect(test).to.equal(true)
126
127         done()
128       })
129     })
130   })
131
132   it('Should search the video by name by default', function (done) {
133     videosUtils.searchVideo(server.url, 'my', function (err, res) {
134       if (err) throw err
135
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)
139
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
149
150       videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
151         if (err) throw err
152         expect(test).to.equal(true)
153
154         done()
155       })
156     })
157   })
158
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
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.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
177
178   //     videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
179   //       if (err) throw err
180   //       expect(test).to.equal(true)
181
182   //       done()
183   //     })
184   //   })
185   // })
186
187   it('Should search the video by tag', function (done) {
188     videosUtils.searchVideo(server.url, 'tag1', 'tags', function (err, res) {
189       if (err) throw err
190
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)
194
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
204
205       videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
206         if (err) throw err
207         expect(test).to.equal(true)
208
209         done()
210       })
211     })
212   })
213
214   it('Should not find a search by name by default', function (done) {
215     videosUtils.searchVideo(server.url, 'hello', function (err, res) {
216       if (err) throw err
217
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)
221
222       done()
223     })
224   })
225
226   it('Should not find a search by author', function (done) {
227     videosUtils.searchVideo(server.url, 'hello', 'author', function (err, res) {
228       if (err) throw err
229
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)
233
234       done()
235     })
236   })
237
238   it('Should not find a search by tag', function (done) {
239     videosUtils.searchVideo(server.url, 'hello', 'tags', function (err, res) {
240       if (err) throw err
241
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)
245
246       done()
247     })
248   })
249
250   it('Should remove the video', function (done) {
251     videosUtils.removeVideo(server.url, server.accessToken, videoId, function (err) {
252       if (err) throw err
253
254       fs.readdir(pathUtils.join(__dirname, '..', '..', '..', 'test1/videos/'), function (err, files) {
255         if (err) throw err
256
257         expect(files.length).to.equal(0)
258
259         fs.readdir(pathUtils.join(__dirname, '..', '..', '..', 'test1/thumbnails/'), function (err, files) {
260           if (err) throw err
261
262           expect(files.length).to.equal(0)
263
264           done()
265         })
266       })
267     })
268   })
269
270   it('Should not have videos', function (done) {
271     videosUtils.getVideosList(server.url, function (err, res) {
272       if (err) throw err
273
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)
277
278       done()
279     })
280   })
281
282   it('Should upload 6 videos', function (done) {
283     this.timeout(25000)
284     const videos = [
285       'video_short.mp4', 'video_short.ogv', 'video_short.webm',
286       'video_short1.webm', 'video_short2.webm', 'video_short3.webm'
287     ]
288     each(videos, function (video, callbackEach) {
289       const name = video + ' name'
290       const description = video + ' description'
291       const tags = [ 'tag1', 'tag2', 'tag3' ]
292
293       videosUtils.uploadVideo(server.url, server.accessToken, name, description, tags, video, callbackEach)
294     }, done)
295   })
296
297   it('Should have the correct durations', function (done) {
298     videosUtils.getVideosList(server.url, function (err, res) {
299       if (err) throw err
300
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)
305
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)
313
314       done()
315     })
316   })
317
318   it('Should have the correct thumbnails', function (done) {
319     videosUtils.getVideosList(server.url, function (err, res) {
320       if (err) throw err
321
322       const videos = res.body.data
323       // For the next test
324       videosListBase = videos
325
326       each(videos, function (video, callbackEach) {
327         if (err) throw err
328         const videoName = video.name.replace(' name', '')
329
330         videosUtils.testVideoImage(server.url, videoName, video.thumbnailPath, function (err, test) {
331           if (err) throw err
332
333           expect(test).to.equal(true)
334           callbackEach()
335         })
336       }, done)
337     })
338   })
339
340   it('Should list only the two first videos', function (done) {
341     videosUtils.getVideosListPagination(server.url, 0, 2, 'name', function (err, res) {
342       if (err) throw err
343
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)
349
350       done()
351     })
352   })
353
354   it('Should list only the next three videos', function (done) {
355     videosUtils.getVideosListPagination(server.url, 2, 3, 'name', function (err, res) {
356       if (err) throw err
357
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)
364
365       done()
366     })
367   })
368
369   it('Should list the last video', function (done) {
370     videosUtils.getVideosListPagination(server.url, 5, 6, 'name', function (err, res) {
371       if (err) throw err
372
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)
377
378       done()
379     })
380   })
381
382   it('Should search the first video', function (done) {
383     videosUtils.searchVideoWithPagination(server.url, 'webm', 'name', 0, 1, 'name', function (err, res) {
384       if (err) throw err
385
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')
390
391       done()
392     })
393   })
394
395   it('Should search the last two videos', function (done) {
396     videosUtils.searchVideoWithPagination(server.url, 'webm', 'name', 2, 2, 'name', function (err, res) {
397       if (err) throw err
398
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')
404
405       done()
406     })
407   })
408
409   it('Should search all the webm videos', function (done) {
410     videosUtils.searchVideoWithPagination(server.url, 'webm', 'name', 0, 15, function (err, res) {
411       if (err) throw err
412
413       const videos = res.body.data
414       expect(res.body.total).to.equal(4)
415       expect(videos.length).to.equal(4)
416
417       done()
418     })
419   })
420
421   it('Should search all the root author videos', function (done) {
422     videosUtils.searchVideoWithPagination(server.url, 'root', 'author', 0, 15, function (err, res) {
423       if (err) throw err
424
425       const videos = res.body.data
426       expect(res.body.total).to.equal(6)
427       expect(videos.length).to.equal(6)
428
429       done()
430     })
431   })
432
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
437
438   //     const videos = res.body.data
439   //     expect(res.body.total).to.equal(6)
440   //     expect(videos.length).to.equal(6)
441
442   //     done()
443   //   })
444   // })
445
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
449
450   //     const videos = res.body.data
451   //     expect(res.body.total).to.equal(6)
452   //     expect(videos.length).to.equal(6)
453
454   //     done()
455   //   })
456   // })
457
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) {
461       if (err) throw err
462
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)
467
468       done()
469     })
470   })
471
472   it('Should list and sort by name in descending order', function (done) {
473     videosUtils.getVideosListSort(server.url, '-name', function (err, res) {
474       if (err) throw err
475
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')
485
486       done()
487     })
488   })
489
490   it('Should search and sort by name in ascending order', function (done) {
491     videosUtils.searchVideoWithSort(server.url, 'webm', 'name', function (err, res) {
492       if (err) throw err
493
494       const videos = res.body.data
495       expect(res.body.total).to.equal(4)
496       expect(videos.length).to.equal(4)
497
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')
502
503       videoId = videos[2].id
504
505       done()
506     })
507   })
508
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' ]
513
514     videosUtils.updateVideo(server.url, server.accessToken, videoId, name, description, tags, done)
515   })
516
517   it('Should have the video updated', function (done) {
518     this.timeout(60000)
519
520     videosUtils.getVideo(server.url, videoId, function (err, res) {
521       if (err) throw err
522
523       const video = res.body
524
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
533
534       videosUtils.testVideoImage(server.url, 'video_short3.webm', video.thumbnailPath, function (err, test) {
535         if (err) throw err
536         expect(test).to.equal(true)
537
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('')
542
543           done()
544         })
545       })
546     })
547   })
548
549   it('Should update only the tags of a video', function (done) {
550     const tags = [ 'tag1', 'tag2', 'supertag' ]
551
552     videosUtils.updateVideo(server.url, server.accessToken, videoId, null, null, tags, function (err) {
553       if (err) throw err
554
555       videosUtils.getVideo(server.url, videoId, function (err, res) {
556         if (err) throw err
557
558         const video = res.body
559
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
568
569         done()
570       })
571     })
572   })
573
574   it('Should update only the description of a video', function (done) {
575     const description = 'hello everybody'
576
577     videosUtils.updateVideo(server.url, server.accessToken, videoId, null, description, null, function (err) {
578       if (err) throw err
579
580       videosUtils.getVideo(server.url, videoId, function (err, res) {
581         if (err) throw err
582
583         const video = res.body
584
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
593
594         done()
595       })
596     })
597   })
598
599   after(function (done) {
600     process.kill(-server.app.pid)
601
602     // Keep the logs if the test failed
603     if (this.ok) {
604       serversUtils.flushTests(done)
605     } else {
606       done()
607     }
608   })
609 })