Fix tests
[oweals/peertube.git] / server / tests / api / videos / single-server.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import { keyBy } from 'lodash'
5 import 'mocha'
6 import { join } from 'path'
7 import { VideoPrivacy } from '../../../../shared/models/videos'
8 import {
9   completeVideoCheck, flushTests, getVideo, getVideoCategories, getVideoLanguages, getVideoLicences, getVideoPrivacies,
10   getVideosList, getVideosListPagination, getVideosListSort, killallServers, rateVideo, readdirPromise, removeVideo, runServer, searchVideo,
11   searchVideoWithPagination, searchVideoWithSort, ServerInfo, setAccessTokensToServers, testVideoImage, updateVideo, uploadVideo, viewVideo
12 } from '../../utils'
13
14 const expect = chai.expect
15
16 describe('Test a single server', function () {
17   let server: ServerInfo = null
18   let videoId = -1
19   let videoUUID = ''
20   let videosListBase: any[] = null
21
22   const getCheckAttributes = {
23     name: 'my super name',
24     category: 2,
25     licence: 6,
26     language: 3,
27     nsfw: true,
28     description: 'my super description',
29     host: 'localhost:9001',
30     account: 'root',
31     isLocal: true,
32     duration: 5,
33     tags: [ 'tag1', 'tag2', 'tag3' ],
34     privacy: VideoPrivacy.PUBLIC,
35     channel: {
36       name: 'Default root channel',
37       description: '',
38       isLocal: true
39     },
40     fixture: 'video_short.webm',
41     files: [
42       {
43         resolution: 720,
44         size: 218910
45       }
46     ]
47   }
48
49   const updateCheckAttributes = {
50     name: 'my super video updated',
51     category: 4,
52     licence: 2,
53     language: 5,
54     nsfw: true,
55     description: 'my super description updated',
56     host: 'localhost:9001',
57     account: 'root',
58     isLocal: true,
59     tags: [ 'tagup1', 'tagup2' ],
60     privacy: VideoPrivacy.PUBLIC,
61     duration: 5,
62     channel: {
63       name: 'Default root channel',
64       description: '',
65       isLocal: true
66     },
67     fixture: 'video_short3.webm',
68     files: [
69       {
70         resolution: 720,
71         size: 292677
72       }
73     ]
74   }
75
76   before(async function () {
77     this.timeout(10000)
78
79     await flushTests()
80
81     server = await runServer(1)
82
83     await setAccessTokensToServers([ server ])
84   })
85
86   it('Should list video categories', async function () {
87     const res = await getVideoCategories(server.url)
88
89     const categories = res.body
90     expect(Object.keys(categories)).to.have.length.above(10)
91
92     expect(categories[11]).to.equal('News')
93   })
94
95   it('Should list video licences', async function () {
96     const res = await getVideoLicences(server.url)
97
98     const licences = res.body
99     expect(Object.keys(licences)).to.have.length.above(5)
100
101     expect(licences[3]).to.equal('Attribution - No Derivatives')
102   })
103
104   it('Should list video languages', async function () {
105     const res = await getVideoLanguages(server.url)
106
107     const languages = res.body
108     expect(Object.keys(languages)).to.have.length.above(5)
109
110     expect(languages[3]).to.equal('Mandarin')
111   })
112
113   it('Should list video privacies', async function () {
114     const res = await getVideoPrivacies(server.url)
115
116     const privacies = res.body
117     expect(Object.keys(privacies)).to.have.length.at.least(3)
118
119     expect(privacies[3]).to.equal('Private')
120   })
121
122   it('Should not have videos', async function () {
123     const res = await getVideosList(server.url)
124
125     expect(res.body.total).to.equal(0)
126     expect(res.body.data).to.be.an('array')
127     expect(res.body.data.length).to.equal(0)
128   })
129
130   it('Should upload the video', async function () {
131     const videoAttributes = {
132       name: 'my super name',
133       category: 2,
134       nsfw: true,
135       licence: 6,
136       tags: [ 'tag1', 'tag2', 'tag3' ]
137     }
138     const res = await uploadVideo(server.url, server.accessToken, videoAttributes)
139     expect(res.body.video).to.not.be.undefined
140     expect(res.body.video.id).to.equal(1)
141     expect(res.body.video.uuid).to.have.length.above(5)
142
143     videoId = res.body.video.id
144     videoUUID = res.body.video.uuid
145   })
146
147   it('Should get and seed the uploaded video', async function () {
148     // Yes, this could be long
149     this.timeout(60000)
150
151     const res = await getVideosList(server.url)
152
153     expect(res.body.total).to.equal(1)
154     expect(res.body.data).to.be.an('array')
155     expect(res.body.data.length).to.equal(1)
156
157     const video = res.body.data[0]
158     await completeVideoCheck(server.url, video, getCheckAttributes)
159   })
160
161   it('Should get the video by UUID', async function () {
162     // Yes, this could be long
163     this.timeout(60000)
164
165     const res = await getVideo(server.url, videoUUID)
166
167     const video = res.body
168     await completeVideoCheck(server.url, video, getCheckAttributes)
169   })
170
171   it('Should have the views updated', async function () {
172     await viewVideo(server.url, videoId)
173     await viewVideo(server.url, videoId)
174     await viewVideo(server.url, videoId)
175
176     const res = await getVideo(server.url, videoId)
177
178     const video = res.body
179     expect(video.views).to.equal(3)
180   })
181
182   it('Should search the video by name', async function () {
183     const res = await searchVideo(server.url, 'my')
184
185     expect(res.body.total).to.equal(1)
186     expect(res.body.data).to.be.an('array')
187     expect(res.body.data.length).to.equal(1)
188
189     const video = res.body.data[0]
190     await completeVideoCheck(server.url, video, getCheckAttributes)
191   })
192
193   // Not implemented yet
194   // it('Should search the video by serverHost', async function () {
195   //     const res = await   videosUtils.searchVideo(server.url, '9001', 'host')
196
197   //     expect(res.body.total).to.equal(1)
198   //     expect(res.body.data).to.be.an('array')
199   //     expect(res.body.data.length).to.equal(1)
200
201   //     const video = res.body.data[0]
202   //     expect(video.name).to.equal('my super name')
203   //     expect(video.description).to.equal('my super description')
204   //     expect(video.serverHost).to.equal('localhost:9001')
205   //     expect(video.author).to.equal('root')
206   //     expect(video.isLocal).to.be.true
207   //     expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
208   //     expect(dateIsValid(video.createdAt)).to.be.true
209   //     expect(dateIsValid(video.updatedAt)).to.be.true
210
211   //     const test = await testVideoImage(server.url, 'video_short.webm', video.thumbnailPath)
212   //       expect(test).to.equal(true)
213
214   //       done()
215   //     })
216   //   })
217   // })
218
219   // Not implemented yet
220   // it('Should search the video by tag', async function () {
221   //   const res = await searchVideo(server.url, 'tag1')
222   //
223   //   expect(res.body.total).to.equal(1)
224   //   expect(res.body.data).to.be.an('array')
225   //   expect(res.body.data.length).to.equal(1)
226   //
227   //   const video = res.body.data[0]
228   //   expect(video.name).to.equal('my super name')
229   //   expect(video.category).to.equal(2)
230   //   expect(video.categoryLabel).to.equal('Films')
231   //   expect(video.licence).to.equal(6)
232   //   expect(video.licenceLabel).to.equal('Attribution - Non Commercial - No Derivatives')
233   //   expect(video.language).to.equal(3)
234   //   expect(video.languageLabel).to.equal('Mandarin')
235   //   expect(video.nsfw).to.be.ok
236   //   expect(video.description).to.equal('my super description')
237   //   expect(video.serverHost).to.equal('localhost:9001')
238   //   expect(video.accountName).to.equal('root')
239   //   expect(video.isLocal).to.be.true
240   //   expect(video.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
241   //   expect(dateIsValid(video.createdAt)).to.be.true
242   //   expect(dateIsValid(video.updatedAt)).to.be.true
243   //
244   //   const test = await testVideoImage(server.url, 'video_short.webm', video.thumbnailPath)
245   //   expect(test).to.equal(true)
246   // })
247
248   it('Should not find a search by name', async function () {
249     const res = await searchVideo(server.url, 'hello')
250
251     expect(res.body.total).to.equal(0)
252     expect(res.body.data).to.be.an('array')
253     expect(res.body.data.length).to.equal(0)
254   })
255
256   // Not implemented yet
257   // it('Should not find a search by author', async function () {
258   //   const res = await searchVideo(server.url, 'hello')
259   //
260   //   expect(res.body.total).to.equal(0)
261   //   expect(res.body.data).to.be.an('array')
262   //   expect(res.body.data.length).to.equal(0)
263   // })
264   //
265   // Not implemented yet
266   // it('Should not find a search by tag', async function () {
267   //   const res = await searchVideo(server.url, 'hello')
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
274   it('Should remove the video', async function () {
275     await removeVideo(server.url, server.accessToken, videoId)
276
277     const files1 = await readdirPromise(join(__dirname, '..', '..', '..', '..', 'test1', 'videos'))
278     expect(files1).to.have.lengthOf(0)
279
280     const files2 = await readdirPromise(join(__dirname, '..', '..', '..', '..', 'test1', 'thumbnails'))
281     expect(files2).to.have.lengthOf(0)
282   })
283
284   it('Should not have videos', async function () {
285     const res = await getVideosList(server.url)
286
287     expect(res.body.total).to.equal(0)
288     expect(res.body.data).to.be.an('array')
289     expect(res.body.data).to.have.lengthOf(0)
290   })
291
292   it('Should upload 6 videos', async function () {
293     this.timeout(25000)
294
295     const videos = [
296       'video_short.mp4', 'video_short.ogv', 'video_short.webm',
297       'video_short1.webm', 'video_short2.webm', 'video_short3.webm'
298     ]
299
300     const tasks: Promise<any>[] = []
301     for (const video of videos) {
302       const videoAttributes = {
303         name: video + ' name',
304         description: video + ' description',
305         category: 2,
306         licence: 1,
307         language: 1,
308         nsfw: true,
309         tags: [ 'tag1', 'tag2', 'tag3' ],
310         fixture: video
311       }
312
313       const p = uploadVideo(server.url, server.accessToken, videoAttributes)
314       tasks.push(p)
315     }
316
317     await Promise.all(tasks)
318   })
319
320   it('Should have the correct durations', async function () {
321     const res = await getVideosList(server.url)
322
323     expect(res.body.total).to.equal(6)
324     const videos = res.body.data
325     expect(videos).to.be.an('array')
326     expect(videos).to.have.lengthOf(6)
327
328     const videosByName = keyBy<{ duration: number }>(videos, 'name')
329     expect(videosByName['video_short.mp4 name'].duration).to.equal(5)
330     expect(videosByName['video_short.ogv name'].duration).to.equal(5)
331     expect(videosByName['video_short.webm name'].duration).to.equal(5)
332     expect(videosByName['video_short1.webm name'].duration).to.equal(10)
333     expect(videosByName['video_short2.webm name'].duration).to.equal(5)
334     expect(videosByName['video_short3.webm name'].duration).to.equal(5)
335   })
336
337   it('Should have the correct thumbnails', async function () {
338     const res = await getVideosList(server.url)
339
340     const videos = res.body.data
341     // For the next test
342     videosListBase = videos
343
344     for (const video of videos) {
345       const videoName = video.name.replace(' name', '')
346       const test = await testVideoImage(server.url, videoName, video.thumbnailPath)
347
348       expect(test).to.equal(true)
349     }
350   })
351
352   it('Should list only the two first videos', async function () {
353     const res = await getVideosListPagination(server.url, 0, 2, 'name')
354
355     const videos = res.body.data
356     expect(res.body.total).to.equal(6)
357     expect(videos.length).to.equal(2)
358     expect(videos[0].name).to.equal(videosListBase[0].name)
359     expect(videos[1].name).to.equal(videosListBase[1].name)
360   })
361
362   it('Should list only the next three videos', async function () {
363     const res = await getVideosListPagination(server.url, 2, 3, 'name')
364
365     const videos = res.body.data
366     expect(res.body.total).to.equal(6)
367     expect(videos.length).to.equal(3)
368     expect(videos[0].name).to.equal(videosListBase[2].name)
369     expect(videos[1].name).to.equal(videosListBase[3].name)
370     expect(videos[2].name).to.equal(videosListBase[4].name)
371   })
372
373   it('Should list the last video', async function () {
374     const res = await getVideosListPagination(server.url, 5, 6, 'name')
375
376     const videos = res.body.data
377     expect(res.body.total).to.equal(6)
378     expect(videos.length).to.equal(1)
379     expect(videos[0].name).to.equal(videosListBase[5].name)
380   })
381
382   it('Should search the first video', async function () {
383     const res = await searchVideoWithPagination(server.url, 'webm', 0, 1, 'name')
384
385     const videos = res.body.data
386     expect(res.body.total).to.equal(4)
387     expect(videos.length).to.equal(1)
388     expect(videos[0].name).to.equal('video_short1.webm name')
389   })
390
391   it('Should search the last two videos', async function () {
392     const res = await searchVideoWithPagination(server.url, 'webm', 2, 2, 'name')
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).to.equal('video_short3.webm name')
398     expect(videos[1].name).to.equal('video_short.webm name')
399   })
400
401   it('Should search all the webm videos', async function () {
402     const res = await searchVideoWithPagination(server.url, 'webm', 0, 15)
403
404     const videos = res.body.data
405     expect(res.body.total).to.equal(4)
406     expect(videos.length).to.equal(4)
407   })
408
409   // Not implemented yet
410   // it('Should search all the root author videos', async function () {
411   //   const res = await searchVideoWithPagination(server.url, 'root', 0, 15)
412   //
413   //   const videos = res.body.data
414   //   expect(res.body.total).to.equal(6)
415   //   expect(videos.length).to.equal(6)
416   // })
417
418   // Not implemented yet
419   // it('Should search all the 9001 port videos', async function () {
420   // const res = await   videosUtils.searchVideoWithPagination(server.url, '9001', 'host', 0, 15)
421
422   //     const videos = res.body.data
423   //     expect(res.body.total).to.equal(6)
424   //     expect(videos.length).to.equal(6)
425
426   //     done()
427   //   })
428   // })
429
430   // it('Should search all the localhost videos', async function () {
431   // const res = await   videosUtils.searchVideoWithPagination(server.url, 'localhost', 'host', 0, 15)
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 list and sort by name in descending order', async function () {
442     const res = await getVideosListSort(server.url, '-name')
443
444     const videos = res.body.data
445     expect(res.body.total).to.equal(6)
446     expect(videos.length).to.equal(6)
447     expect(videos[0].name).to.equal('video_short.webm name')
448     expect(videos[1].name).to.equal('video_short.ogv name')
449     expect(videos[2].name).to.equal('video_short.mp4 name')
450     expect(videos[3].name).to.equal('video_short3.webm name')
451     expect(videos[4].name).to.equal('video_short2.webm name')
452     expect(videos[5].name).to.equal('video_short1.webm name')
453   })
454
455   it('Should search and sort by name in ascending order', async function () {
456     const res = await searchVideoWithSort(server.url, 'webm', 'name')
457
458     const videos = res.body.data
459     expect(res.body.total).to.equal(4)
460     expect(videos.length).to.equal(4)
461
462     expect(videos[0].name).to.equal('video_short1.webm name')
463     expect(videos[1].name).to.equal('video_short2.webm name')
464     expect(videos[2].name).to.equal('video_short3.webm name')
465     expect(videos[3].name).to.equal('video_short.webm name')
466
467     videoId = videos[2].id
468   })
469
470   it('Should update a video', async function () {
471     const attributes = {
472       name: 'my super video updated',
473       category: 4,
474       licence: 2,
475       language: 5,
476       nsfw: false,
477       description: 'my super description updated',
478       tags: [ 'tagup1', 'tagup2' ]
479     }
480     await updateVideo(server.url, server.accessToken, videoId, attributes)
481   })
482
483   it('Should have the video updated', async function () {
484     this.timeout(60000)
485
486     const res = await getVideo(server.url, videoId)
487     const video = res.body
488
489     await completeVideoCheck(server.url, video, updateCheckAttributes)
490   })
491
492   it('Should update only the tags of a video', async function () {
493     const attributes = {
494       tags: [ 'supertag', 'tag1', 'tag2' ]
495     }
496     await updateVideo(server.url, server.accessToken, videoId, attributes)
497
498     const res = await getVideo(server.url, videoId)
499     const video = res.body
500
501     await completeVideoCheck(server.url, video, Object.assign(updateCheckAttributes, attributes))
502   })
503
504   it('Should update only the description of a video', async function () {
505     const attributes = {
506       description: 'hello everybody'
507     }
508     await updateVideo(server.url, server.accessToken, videoId, attributes)
509
510     const res = await getVideo(server.url, videoId)
511     const video = res.body
512
513     await completeVideoCheck(server.url, video, Object.assign(updateCheckAttributes, attributes))
514   })
515
516   it('Should like a video', async function () {
517     await rateVideo(server.url, server.accessToken, videoId, 'like')
518
519     const res = await getVideo(server.url, videoId)
520     const video = res.body
521
522     expect(video.likes).to.equal(1)
523     expect(video.dislikes).to.equal(0)
524   })
525
526   it('Should dislike the same video', async function () {
527     await rateVideo(server.url, server.accessToken, videoId, 'dislike')
528
529     const res = await getVideo(server.url, videoId)
530     const video = res.body
531
532     expect(video.likes).to.equal(0)
533     expect(video.dislikes).to.equal(1)
534   })
535
536   after(async function () {
537     killallServers([ server ])
538
539     // Keep the logs if the test failed
540     if (this['ok']) {
541       await flushTests()
542     }
543   })
544 })