Rename downloadingEnabled property to downloadEnabled
[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 { VideoPrivacy } from '../../../../shared/models/videos'
7 import {
8   checkVideoFilesWereRemoved,
9   completeVideoCheck,
10   flushTests,
11   getVideo,
12   getVideoCategories,
13   getVideoLanguages,
14   getVideoLicences,
15   getVideoPrivacies,
16   getVideosList,
17   getVideosListPagination,
18   getVideosListSort,
19   getVideosWithFilters,
20   killallServers,
21   rateVideo,
22   removeVideo,
23   runServer,
24   ServerInfo,
25   setAccessTokensToServers,
26   testImage,
27   updateVideo,
28   uploadVideo,
29   viewVideo,
30   wait
31 } from '../../utils'
32
33 const expect = chai.expect
34
35 describe('Test a single server', function () {
36   let server: ServerInfo = null
37   let videoId = -1
38   let videoUUID = ''
39   let videosListBase: any[] = null
40
41   const getCheckAttributes = {
42     name: 'my super name',
43     category: 2,
44     licence: 6,
45     language: 'zh',
46     nsfw: true,
47     description: 'my super description',
48     support: 'my super support text',
49     account: {
50       name: 'root',
51       host: 'localhost:9001'
52     },
53     isLocal: true,
54     duration: 5,
55     tags: [ 'tag1', 'tag2', 'tag3' ],
56     privacy: VideoPrivacy.PUBLIC,
57     commentsEnabled: true,
58     downloadEnabled: true,
59     channel: {
60       displayName: 'Main root channel',
61       name: 'root_channel',
62       description: '',
63       isLocal: true
64     },
65     fixture: 'video_short.webm',
66     files: [
67       {
68         resolution: 720,
69         size: 218910
70       }
71     ]
72   }
73
74   const updateCheckAttributes = {
75     name: 'my super video updated',
76     category: 4,
77     licence: 2,
78     language: 'ar',
79     nsfw: false,
80     description: 'my super description updated',
81     support: 'my super support text updated',
82     account: {
83       name: 'root',
84       host: 'localhost:9001'
85     },
86     isLocal: true,
87     tags: [ 'tagup1', 'tagup2' ],
88     privacy: VideoPrivacy.PUBLIC,
89     duration: 5,
90     commentsEnabled: false,
91     downloadEnabled: false,
92     channel: {
93       name: 'root_channel',
94       displayName: 'Main root channel',
95       description: '',
96       isLocal: true
97     },
98     fixture: 'video_short3.webm',
99     files: [
100       {
101         resolution: 720,
102         size: 292677
103       }
104     ]
105   }
106
107   before(async function () {
108     this.timeout(30000)
109
110     await flushTests()
111
112     server = await runServer(1)
113
114     await setAccessTokensToServers([ server ])
115   })
116
117   it('Should list video categories', async function () {
118     const res = await getVideoCategories(server.url)
119
120     const categories = res.body
121     expect(Object.keys(categories)).to.have.length.above(10)
122
123     expect(categories[11]).to.equal('News')
124   })
125
126   it('Should list video licences', async function () {
127     const res = await getVideoLicences(server.url)
128
129     const licences = res.body
130     expect(Object.keys(licences)).to.have.length.above(5)
131
132     expect(licences[3]).to.equal('Attribution - No Derivatives')
133   })
134
135   it('Should list video languages', async function () {
136     const res = await getVideoLanguages(server.url)
137
138     const languages = res.body
139     expect(Object.keys(languages)).to.have.length.above(5)
140
141     expect(languages['ru']).to.equal('Russian')
142   })
143
144   it('Should list video privacies', async function () {
145     const res = await getVideoPrivacies(server.url)
146
147     const privacies = res.body
148     expect(Object.keys(privacies)).to.have.length.at.least(3)
149
150     expect(privacies[3]).to.equal('Private')
151   })
152
153   it('Should not have videos', async function () {
154     const res = await getVideosList(server.url)
155
156     expect(res.body.total).to.equal(0)
157     expect(res.body.data).to.be.an('array')
158     expect(res.body.data.length).to.equal(0)
159   })
160
161   it('Should upload the video', async function () {
162     const videoAttributes = {
163       name: 'my super name',
164       category: 2,
165       nsfw: true,
166       licence: 6,
167       tags: [ 'tag1', 'tag2', 'tag3' ]
168     }
169     const res = await uploadVideo(server.url, server.accessToken, videoAttributes)
170     expect(res.body.video).to.not.be.undefined
171     expect(res.body.video.id).to.equal(1)
172     expect(res.body.video.uuid).to.have.length.above(5)
173
174     videoId = res.body.video.id
175     videoUUID = res.body.video.uuid
176   })
177
178   it('Should get and seed the uploaded video', async function () {
179     this.timeout(5000)
180
181     const res = await getVideosList(server.url)
182
183     expect(res.body.total).to.equal(1)
184     expect(res.body.data).to.be.an('array')
185     expect(res.body.data.length).to.equal(1)
186
187     const video = res.body.data[0]
188     await completeVideoCheck(server.url, video, getCheckAttributes)
189   })
190
191   it('Should get the video by UUID', async function () {
192     this.timeout(5000)
193
194     const res = await getVideo(server.url, videoUUID)
195
196     const video = res.body
197     await completeVideoCheck(server.url, video, getCheckAttributes)
198   })
199
200   it('Should have the views updated', async function () {
201     this.timeout(20000)
202
203     await viewVideo(server.url, videoId)
204     await viewVideo(server.url, videoId)
205     await viewVideo(server.url, videoId)
206
207     await wait(1500)
208
209     await viewVideo(server.url, videoId)
210     await viewVideo(server.url, videoId)
211
212     await wait(1500)
213
214     await viewVideo(server.url, videoId)
215     await viewVideo(server.url, videoId)
216
217     // Wait the repeatable job
218     await wait(8000)
219
220     const res = await getVideo(server.url, videoId)
221
222     const video = res.body
223     expect(video.views).to.equal(3)
224   })
225
226   it('Should remove the video', async function () {
227     await removeVideo(server.url, server.accessToken, videoId)
228
229     await checkVideoFilesWereRemoved(videoUUID, 1)
230   })
231
232   it('Should not have videos', async function () {
233     const res = await getVideosList(server.url)
234
235     expect(res.body.total).to.equal(0)
236     expect(res.body.data).to.be.an('array')
237     expect(res.body.data).to.have.lengthOf(0)
238   })
239
240   it('Should upload 6 videos', async function () {
241     this.timeout(25000)
242
243     const videos = [
244       'video_short.mp4', 'video_short.ogv', 'video_short.webm',
245       'video_short1.webm', 'video_short2.webm', 'video_short3.webm'
246     ]
247
248     const tasks: Promise<any>[] = []
249     for (const video of videos) {
250       const videoAttributes = {
251         name: video + ' name',
252         description: video + ' description',
253         category: 2,
254         licence: 1,
255         language: 'en',
256         nsfw: true,
257         tags: [ 'tag1', 'tag2', 'tag3' ],
258         fixture: video
259       }
260
261       const p = uploadVideo(server.url, server.accessToken, videoAttributes)
262       tasks.push(p)
263     }
264
265     await Promise.all(tasks)
266   })
267
268   it('Should have the correct durations', async function () {
269     const res = await getVideosList(server.url)
270
271     expect(res.body.total).to.equal(6)
272     const videos = res.body.data
273     expect(videos).to.be.an('array')
274     expect(videos).to.have.lengthOf(6)
275
276     const videosByName = keyBy<{ duration: number }>(videos, 'name')
277     expect(videosByName['video_short.mp4 name'].duration).to.equal(5)
278     expect(videosByName['video_short.ogv name'].duration).to.equal(5)
279     expect(videosByName['video_short.webm name'].duration).to.equal(5)
280     expect(videosByName['video_short1.webm name'].duration).to.equal(10)
281     expect(videosByName['video_short2.webm name'].duration).to.equal(5)
282     expect(videosByName['video_short3.webm name'].duration).to.equal(5)
283   })
284
285   it('Should have the correct thumbnails', async function () {
286     const res = await getVideosList(server.url)
287
288     const videos = res.body.data
289     // For the next test
290     videosListBase = videos
291
292     for (const video of videos) {
293       const videoName = video.name.replace(' name', '')
294       await testImage(server.url, videoName, video.thumbnailPath)
295     }
296   })
297
298   it('Should list only the two first videos', async function () {
299     const res = await getVideosListPagination(server.url, 0, 2, 'name')
300
301     const videos = res.body.data
302     expect(res.body.total).to.equal(6)
303     expect(videos.length).to.equal(2)
304     expect(videos[0].name).to.equal(videosListBase[0].name)
305     expect(videos[1].name).to.equal(videosListBase[1].name)
306   })
307
308   it('Should list only the next three videos', async function () {
309     const res = await getVideosListPagination(server.url, 2, 3, 'name')
310
311     const videos = res.body.data
312     expect(res.body.total).to.equal(6)
313     expect(videos.length).to.equal(3)
314     expect(videos[0].name).to.equal(videosListBase[2].name)
315     expect(videos[1].name).to.equal(videosListBase[3].name)
316     expect(videos[2].name).to.equal(videosListBase[4].name)
317   })
318
319   it('Should list the last video', async function () {
320     const res = await getVideosListPagination(server.url, 5, 6, 'name')
321
322     const videos = res.body.data
323     expect(res.body.total).to.equal(6)
324     expect(videos.length).to.equal(1)
325     expect(videos[0].name).to.equal(videosListBase[5].name)
326   })
327
328   it('Should list and sort by name in descending order', async function () {
329     const res = await getVideosListSort(server.url, '-name')
330
331     const videos = res.body.data
332     expect(res.body.total).to.equal(6)
333     expect(videos.length).to.equal(6)
334     expect(videos[0].name).to.equal('video_short.webm name')
335     expect(videos[1].name).to.equal('video_short.ogv name')
336     expect(videos[2].name).to.equal('video_short.mp4 name')
337     expect(videos[3].name).to.equal('video_short3.webm name')
338     expect(videos[4].name).to.equal('video_short2.webm name')
339     expect(videos[5].name).to.equal('video_short1.webm name')
340
341     videoId = videos[3].uuid
342   })
343
344   it('Should list and sort by trending in descending order', async function () {
345     const res = await getVideosListPagination(server.url, 0, 2, '-trending')
346
347     const videos = res.body.data
348     expect(res.body.total).to.equal(6)
349     expect(videos.length).to.equal(2)
350   })
351
352   it('Should update a video', async function () {
353     const attributes = {
354       name: 'my super video updated',
355       category: 4,
356       licence: 2,
357       language: 'ar',
358       nsfw: false,
359       description: 'my super description updated',
360       commentsEnabled: false,
361       downloadEnabled: false,
362       tags: [ 'tagup1', 'tagup2' ]
363     }
364     await updateVideo(server.url, server.accessToken, videoId, attributes)
365   })
366
367   it('Should filter by tags and category', async function () {
368     const res1 = await getVideosWithFilters(server.url, { tagsAllOf: [ 'tagup1', 'tagup2' ], categoryOneOf: 4 })
369     expect(res1.body.total).to.equal(1)
370     expect(res1.body.data[0].name).to.equal('my super video updated')
371
372     const res2 = await getVideosWithFilters(server.url, { tagsAllOf: [ 'tagup1', 'tagup2' ], categoryOneOf: 3 })
373     expect(res2.body.total).to.equal(0)
374   })
375
376   it('Should have the video updated', async function () {
377     this.timeout(60000)
378
379     const res = await getVideo(server.url, videoId)
380     const video = res.body
381
382     await completeVideoCheck(server.url, video, updateCheckAttributes)
383   })
384
385   it('Should update only the tags of a video', async function () {
386     const attributes = {
387       tags: [ 'supertag', 'tag1', 'tag2' ]
388     }
389     await updateVideo(server.url, server.accessToken, videoId, attributes)
390
391     const res = await getVideo(server.url, videoId)
392     const video = res.body
393
394     await completeVideoCheck(server.url, video, Object.assign(updateCheckAttributes, attributes))
395   })
396
397   it('Should update only the description of a video', async function () {
398     const attributes = {
399       description: 'hello everybody'
400     }
401     await updateVideo(server.url, server.accessToken, videoId, attributes)
402
403     const res = await getVideo(server.url, videoId)
404     const video = res.body
405
406     await completeVideoCheck(server.url, video, Object.assign(updateCheckAttributes, attributes))
407   })
408
409   it('Should like a video', async function () {
410     await rateVideo(server.url, server.accessToken, videoId, 'like')
411
412     const res = await getVideo(server.url, videoId)
413     const video = res.body
414
415     expect(video.likes).to.equal(1)
416     expect(video.dislikes).to.equal(0)
417   })
418
419   it('Should dislike the same video', async function () {
420     await rateVideo(server.url, server.accessToken, videoId, 'dislike')
421
422     const res = await getVideo(server.url, videoId)
423     const video = res.body
424
425     expect(video.likes).to.equal(0)
426     expect(video.dislikes).to.equal(1)
427   })
428
429   after(async function () {
430     killallServers([ server ])
431
432     // Keep the logs if the test failed
433     if (this['ok']) {
434       await flushTests()
435     }
436   })
437 })