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