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