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