Server: fix tests
[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
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
17
18 describe('Test a single pod', function () {
19   let server = null
20   let videoId = -1
21   let videosListBase = null
22
23   before(function (done) {
24     this.timeout(20000)
25
26     series([
27       function (next) {
28         serversUtils.flushTests(next)
29       },
30       function (next) {
31         serversUtils.runServer(1, function (server1) {
32           server = server1
33           next()
34         })
35       },
36       function (next) {
37         loginUtils.loginAndGetAccessToken(server, function (err, token) {
38           if (err) throw err
39           server.accessToken = token
40           next()
41         })
42       },
43       function (next) {
44         webtorrent.create({ host: 'client', port: '1' }, next)
45       }
46     ], done)
47   })
48
49   it('Should not have videos', function (done) {
50     videosUtils.getVideosList(server.url, function (err, res) {
51       if (err) throw err
52
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)
56
57       done()
58     })
59   })
60
61   it('Should upload the video', function (done) {
62     this.timeout(5000)
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)
68   })
69
70   it('Should seed the uploaded video', function (done) {
71     // Yes, this could be long
72     this.timeout(60000)
73
74     videosUtils.getVideosList(server.url, function (err, res) {
75       if (err) throw err
76
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)
80
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
90
91       videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
92         if (err) throw err
93         expect(test).to.equal(true)
94
95         videoId = video.id
96
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
102           done()
103         })
104       })
105     })
106   })
107
108   it('Should get the video', function (done) {
109     // Yes, this could be long
110     this.timeout(60000)
111
112     videosUtils.getVideo(server.url, videoId, function (err, res) {
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')
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
124
125       videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
126         if (err) throw err
127         expect(test).to.equal(true)
128
129         done()
130       })
131     })
132   })
133
134   it('Should search the video by name by default', function (done) {
135     videosUtils.searchVideo(server.url, 'my', function (err, res) {
136       if (err) throw err
137
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)
141
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
150
151       videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
152         if (err) throw err
153         expect(test).to.equal(true)
154
155         done()
156       })
157     })
158   })
159
160   it('Should search the video by podUrl', function (done) {
161     videosUtils.searchVideo(server.url, '9001', 'podUrl', 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.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
176
177       videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
178         if (err) throw err
179         expect(test).to.equal(true)
180
181         done()
182       })
183     })
184   })
185
186   it('Should search the video by tag', function (done) {
187     videosUtils.searchVideo(server.url, 'tag1', 'tags', function (err, res) {
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' ])
201       expect(miscsUtils.dateIsValid(video.createdDate)).to.be.true
202
203       videosUtils.testVideoImage(server.url, 'video_short.webm', video.thumbnailPath, function (err, test) {
204         if (err) throw err
205         expect(test).to.equal(true)
206
207         done()
208       })
209     })
210   })
211
212   it('Should not find a search by name by default', function (done) {
213     videosUtils.searchVideo(server.url, 'hello', function (err, res) {
214       if (err) throw err
215
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)
219
220       done()
221     })
222   })
223
224   it('Should not find a search by author', function (done) {
225     videosUtils.searchVideo(server.url, 'hello', 'author', function (err, res) {
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
236   it('Should not find a search by tag', function (done) {
237     videosUtils.searchVideo(server.url, 'tag', 'tags', function (err, res) {
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
248   it('Should remove the video', function (done) {
249     videosUtils.removeVideo(server.url, server.accessToken, videoId, function (err) {
250       if (err) throw err
251
252       fs.readdir(pathUtils.join(__dirname, '../../../test1/uploads/'), function (err, files) {
253         if (err) throw err
254
255         expect(files.length).to.equal(0)
256         done()
257       })
258     })
259   })
260
261   it('Should not have videos', function (done) {
262     videosUtils.getVideosList(server.url, function (err, res) {
263       if (err) throw err
264
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)
268
269       done()
270     })
271   })
272
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     ]
279     each(videos, function (video, callbackEach) {
280       const name = video + ' name'
281       const description = video + ' description'
282       const tags = [ 'tag1', 'tag2', 'tag3' ]
283
284       videosUtils.uploadVideo(server.url, server.accessToken, name, description, tags, video, callbackEach)
285     }, done)
286   })
287
288   it('Should have the correct durations', function (done) {
289     videosUtils.getVideosList(server.url, function (err, res) {
290       if (err) throw err
291
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)
296
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)
304
305       done()
306     })
307   })
308
309   it('Should have the correct thumbnails', function (done) {
310     videosUtils.getVideosList(server.url, function (err, res) {
311       if (err) throw err
312
313       const videos = res.body.data
314       // For the next test
315       videosListBase = videos
316
317       each(videos, function (video, callbackEach) {
318         if (err) throw err
319         const videoName = video.name.replace(' name', '')
320
321         videosUtils.testVideoImage(server.url, videoName, video.thumbnailPath, function (err, test) {
322           if (err) throw err
323
324           expect(test).to.equal(true)
325           callbackEach()
326         })
327       }, done)
328     })
329   })
330
331   it('Should list only the two first videos', function (done) {
332     videosUtils.getVideosListPagination(server.url, 0, 2, function (err, res) {
333       if (err) throw err
334
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)
340
341       done()
342     })
343   })
344
345   it('Should list only the next three videos', function (done) {
346     videosUtils.getVideosListPagination(server.url, 2, 3, function (err, res) {
347       if (err) throw err
348
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)
355
356       done()
357     })
358   })
359
360   it('Should list the last video', function (done) {
361     videosUtils.getVideosListPagination(server.url, 5, 6, function (err, res) {
362       if (err) throw err
363
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)
368
369       done()
370     })
371   })
372
373   it('Should search the first video', function (done) {
374     videosUtils.searchVideoWithPagination(server.url, 'webm', 'name', 0, 1, function (err, res) {
375       if (err) throw err
376
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')
381
382       done()
383     })
384   })
385
386   it('Should search the last two videos', function (done) {
387     videosUtils.searchVideoWithPagination(server.url, 'webm', 'name', 2, 2, function (err, res) {
388       if (err) throw err
389
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')
395
396       done()
397     })
398   })
399
400   it('Should search all the webm videos', function (done) {
401     videosUtils.searchVideoWithPagination(server.url, 'webm', 'name', 0, 15, function (err, res) {
402       if (err) throw err
403
404       const videos = res.body.data
405       expect(res.body.total).to.equal(4)
406       expect(videos.length).to.equal(4)
407
408       done()
409     })
410   })
411
412   it('Should search all the root author videos', function (done) {
413     videosUtils.searchVideoWithPagination(server.url, 'root', 'author', 0, 15, function (err, res) {
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) {
425     videosUtils.searchVideoWithPagination(server.url, '9001', 'podUrl', 0, 15, function (err, res) {
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) {
437     videosUtils.searchVideoWithPagination(server.url, 'localhost', 'podUrl', 0, 15, function (err, res) {
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]
450     videosUtils.searchVideoWithPagination(server.url, encodeURIComponent(video.magnetUri), 'magnetUri', 0, 15, function (err, res) {
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
462   it('Should list and sort by name in descending order', function (done) {
463     videosUtils.getVideosListSort(server.url, '-name', function (err, res) {
464       if (err) throw err
465
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')
475
476       done()
477     })
478   })
479
480   it('Should search and sort by name in ascending order', function (done) {
481     videosUtils.searchVideoWithSort(server.url, 'webm', 'name', function (err, res) {
482       if (err) throw err
483
484       const videos = res.body.data
485       expect(res.body.total).to.equal(4)
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
497   after(function (done) {
498     process.kill(-server.app.pid)
499     process.kill(-webtorrent.app.pid)
500
501     // Keep the logs if the test failed
502     if (this.ok) {
503       serversUtils.flushTests(done)
504     } else {
505       done()
506     }
507   })
508 })