Merge from upstream
[oweals/peertube.git] / server / tests / api / check-params / videos.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import { omit } from 'lodash'
5 import 'mocha'
6 import { join } from 'path'
7 import { VideoPrivacy } from '../../../../shared/models/videos/video-privacy.enum'
8 import {
9   createUser, flushTests, getMyUserInformation, getVideo, getVideosList, immutableAssign, killallServers, makeDeleteRequest,
10   makeGetRequest, makeUploadRequest, makePutBodyRequest, removeVideo, runServer, ServerInfo, setAccessTokensToServers, userLogin
11 } from '../../../../shared/utils'
12 import {
13   checkBadCountPagination,
14   checkBadSortPagination,
15   checkBadStartPagination
16 } from '../../../../shared/utils/requests/check-api-params'
17 import { getAccountsList } from '../../../../shared/utils/users/accounts'
18
19 const expect = chai.expect
20
21 describe('Test videos API validator', function () {
22   const path = '/api/v1/videos/'
23   let server: ServerInfo
24   let userAccessToken = ''
25   let accountName: string
26   let channelId: number
27   let channelName: string
28   let videoId
29
30   // ---------------------------------------------------------------
31
32   before(async function () {
33     this.timeout(30000)
34
35     await flushTests()
36
37     server = await runServer(1)
38
39     await setAccessTokensToServers([ server ])
40
41     const username = 'user1'
42     const password = 'my super password'
43     await createUser(server.url, server.accessToken, username, password)
44     userAccessToken = await userLogin(server, { username, password })
45
46     {
47       const res = await getMyUserInformation(server.url, server.accessToken)
48       channelId = res.body.videoChannels[ 0 ].id
49       channelName = res.body.videoChannels[ 0 ].name
50       accountName = res.body.account.name + '@' + res.body.account.host
51     }
52   })
53
54   describe('When listing a video', function () {
55     it('Should fail with a bad start pagination', async function () {
56       await checkBadStartPagination(server.url, path)
57     })
58
59     it('Should fail with a bad count pagination', async function () {
60       await checkBadCountPagination(server.url, path)
61     })
62
63     it('Should fail with an incorrect sort', async function () {
64       await checkBadSortPagination(server.url, path)
65     })
66
67     it('Should success with the correct parameters', async function () {
68       await makeGetRequest({ url: server.url, path, statusCodeExpected: 200 })
69     })
70   })
71
72   describe('When searching a video', function () {
73
74     it('Should fail with nothing', async function () {
75       await makeGetRequest({
76         url: server.url,
77         path: join(path, 'search'),
78         statusCodeExpected: 400
79       })
80     })
81
82     it('Should fail with a bad start pagination', async function () {
83       await checkBadStartPagination(server.url, join(path, 'search', 'test'))
84     })
85
86     it('Should fail with a bad count pagination', async function () {
87       await checkBadCountPagination(server.url, join(path, 'search', 'test'))
88     })
89
90     it('Should fail with an incorrect sort', async function () {
91       await checkBadSortPagination(server.url, join(path, 'search', 'test'))
92     })
93
94     it('Should success with the correct parameters', async function () {
95       await makeGetRequest({ url: server.url, path, statusCodeExpected: 200 })
96     })
97   })
98
99   describe('When listing my videos', function () {
100     const path = '/api/v1/users/me/videos'
101
102     it('Should fail with a bad start pagination', async function () {
103       await checkBadStartPagination(server.url, path, server.accessToken)
104     })
105
106     it('Should fail with a bad count pagination', async function () {
107       await checkBadCountPagination(server.url, path, server.accessToken)
108     })
109
110     it('Should fail with an incorrect sort', async function () {
111       await checkBadSortPagination(server.url, path, server.accessToken)
112     })
113
114     it('Should success with the correct parameters', async function () {
115       await makeGetRequest({ url: server.url, token: server.accessToken, path, statusCodeExpected: 200 })
116     })
117   })
118
119   describe('When listing account videos', function () {
120     let path: string
121
122     before(async function () {
123       path = '/api/v1/accounts/' + accountName + '/videos'
124     })
125
126     it('Should fail with a bad start pagination', async function () {
127       await checkBadStartPagination(server.url, path, server.accessToken)
128     })
129
130     it('Should fail with a bad count pagination', async function () {
131       await checkBadCountPagination(server.url, path, server.accessToken)
132     })
133
134     it('Should fail with an incorrect sort', async function () {
135       await checkBadSortPagination(server.url, path, server.accessToken)
136     })
137
138     it('Should success with the correct parameters', async function () {
139       await makeGetRequest({ url: server.url, path, statusCodeExpected: 200 })
140     })
141   })
142
143   describe('When listing video channel videos', function () {
144     let path: string
145
146     before(async function () {
147       path = '/api/v1/video-channels/' + channelName + '/videos'
148     })
149
150     it('Should fail with a bad start pagination', async function () {
151       await checkBadStartPagination(server.url, path, server.accessToken)
152     })
153
154     it('Should fail with a bad count pagination', async function () {
155       await checkBadCountPagination(server.url, path, server.accessToken)
156     })
157
158     it('Should fail with an incorrect sort', async function () {
159       await checkBadSortPagination(server.url, path, server.accessToken)
160     })
161
162     it('Should success with the correct parameters', async function () {
163       await makeGetRequest({ url: server.url, path, statusCodeExpected: 200 })
164     })
165   })
166
167   describe('When adding a video', function () {
168     let baseCorrectParams
169     const baseCorrectAttaches = {
170       'videofile': join(__dirname, '..', '..', 'fixtures', 'video_short.webm')
171     }
172
173     before(function () {
174       // Put in before to have channelId
175       baseCorrectParams = {
176         name: 'my super name',
177         category: 5,
178         licence: 1,
179         language: 'pt',
180         nsfw: false,
181         commentsEnabled: true,
182         waitTranscoding: true,
183         description: 'my super description',
184         support: 'my super support text',
185         tags: [ 'tag1', 'tag2' ],
186         privacy: VideoPrivacy.PUBLIC,
187         channelId: channelId
188       }
189     })
190
191     it('Should fail with nothing', async function () {
192       const fields = {}
193       const attaches = {}
194       await makeUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
195     })
196
197     it('Should fail without name', async function () {
198       const fields = omit(baseCorrectParams, 'name')
199       const attaches = baseCorrectAttaches
200
201       await makeUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
202     })
203
204     it('Should fail with a long name', async function () {
205       const fields = immutableAssign(baseCorrectParams, { name: 'super'.repeat(65) })
206       const attaches = baseCorrectAttaches
207
208       await makeUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
209     })
210
211     it('Should fail with a bad category', async function () {
212       const fields = immutableAssign(baseCorrectParams, { category: 125 })
213       const attaches = baseCorrectAttaches
214
215       await makeUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
216     })
217
218     it('Should fail with a bad licence', async function () {
219       const fields = immutableAssign(baseCorrectParams, { licence: 125 })
220       const attaches = baseCorrectAttaches
221
222       await makeUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
223     })
224
225     it('Should fail with a bad language', async function () {
226       const fields = immutableAssign(baseCorrectParams, { language: 'a'.repeat(15) })
227       const attaches = baseCorrectAttaches
228
229       await makeUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
230     })
231
232     it('Should fail with a long description', async function () {
233       const fields = immutableAssign(baseCorrectParams, { description: 'super'.repeat(2500) })
234       const attaches = baseCorrectAttaches
235
236       await makeUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
237     })
238
239     it('Should fail with a long support text', async function () {
240       const fields = immutableAssign(baseCorrectParams, { support: 'super'.repeat(201) })
241       const attaches = baseCorrectAttaches
242
243       await makeUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
244     })
245
246     it('Should fail without a channel', async function () {
247       const fields = omit(baseCorrectParams, 'channelId')
248       const attaches = baseCorrectAttaches
249
250       await makeUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
251     })
252
253     it('Should fail with a bad channel', async function () {
254       const fields = immutableAssign(baseCorrectParams, { channelId: 545454 })
255       const attaches = baseCorrectAttaches
256
257       await makeUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
258     })
259
260     it('Should fail with another user channel', async function () {
261       const user = {
262         username: 'fake',
263         password: 'fake_password'
264       }
265       await createUser(server.url, server.accessToken, user.username, user.password)
266
267       const accessTokenUser = await userLogin(server, user)
268       const res = await getMyUserInformation(server.url, accessTokenUser)
269       const customChannelId = res.body.videoChannels[0].id
270
271       const fields = immutableAssign(baseCorrectParams, { channelId: customChannelId })
272       const attaches = baseCorrectAttaches
273
274       await makeUploadRequest({ url: server.url, path: path + '/upload', token: userAccessToken, fields, attaches })
275     })
276
277     it('Should fail with too many tags', async function () {
278       const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6' ] })
279       const attaches = baseCorrectAttaches
280
281       await makeUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
282     })
283
284     it('Should fail with a tag length too low', async function () {
285       const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 't' ] })
286       const attaches = baseCorrectAttaches
287
288       await makeUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
289     })
290
291     it('Should fail with a tag length too big', async function () {
292       const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 'my_super_tag_too_long_long_long_long_long_long' ] })
293       const attaches = baseCorrectAttaches
294
295       await makeUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
296     })
297
298     it('Should fail with a bad schedule update (miss updateAt)', async function () {
299       const fields = immutableAssign(baseCorrectParams, { 'scheduleUpdate[privacy]': VideoPrivacy.PUBLIC })
300       const attaches = baseCorrectAttaches
301
302       await makeUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
303     })
304
305     it('Should fail with a bad schedule update (wrong updateAt)', async function () {
306       const fields = immutableAssign(baseCorrectParams, {
307         'scheduleUpdate[privacy]': VideoPrivacy.PUBLIC,
308         'scheduleUpdate[updateAt]': 'toto'
309       })
310       const attaches = baseCorrectAttaches
311
312       await makeUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
313     })
314
315     it('Should fail without an input file', async function () {
316       const fields = baseCorrectParams
317       const attaches = {}
318       await makeUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
319     })
320
321     it('Should fail without an incorrect input file', async function () {
322       const fields = baseCorrectParams
323       const attaches = {
324         'videofile': join(__dirname, '..', '..', 'fixtures', 'video_short_fake.webm')
325       }
326       await makeUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
327     })
328
329     it('Should fail with an incorrect thumbnail file', async function () {
330       const fields = baseCorrectParams
331       const attaches = {
332         'thumbnailfile': join(__dirname, '..', '..', 'fixtures', 'avatar.png'),
333         'videofile': join(__dirname, '..', '..', 'fixtures', 'video_short.mp4')
334       }
335
336       await makeUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
337     })
338
339     it('Should fail with a big thumbnail file', async function () {
340       const fields = baseCorrectParams
341       const attaches = {
342         'thumbnailfile': join(__dirname, '..', '..', 'fixtures', 'avatar-big.png'),
343         'videofile': join(__dirname, '..', '..', 'fixtures', 'video_short.mp4')
344       }
345
346       await makeUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
347     })
348
349     it('Should fail with an incorrect preview file', async function () {
350       const fields = baseCorrectParams
351       const attaches = {
352         'previewfile': join(__dirname, '..', '..', 'fixtures', 'avatar.png'),
353         'videofile': join(__dirname, '..', '..', 'fixtures', 'video_short.mp4')
354       }
355
356       await makeUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
357     })
358
359     it('Should fail with a big preview file', async function () {
360       const fields = baseCorrectParams
361       const attaches = {
362         'previewfile': join(__dirname, '..', '..', 'fixtures', 'avatar-big.png'),
363         'videofile': join(__dirname, '..', '..', 'fixtures', 'video_short.mp4')
364       }
365
366       await makeUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
367     })
368
369     it('Should succeed with the correct parameters', async function () {
370       this.timeout(10000)
371
372       const fields = baseCorrectParams
373
374       {
375         const attaches = baseCorrectAttaches
376         await makeUploadRequest({
377           url: server.url,
378           path: path + '/upload',
379           token: server.accessToken,
380           fields,
381           attaches,
382           statusCodeExpected: 200
383         })
384       }
385
386       {
387         const attaches = immutableAssign(baseCorrectAttaches, {
388           videofile: join(__dirname, '..', '..', 'fixtures', 'video_short.mp4')
389         })
390
391         await makeUploadRequest({
392           url: server.url,
393           path: path + '/upload',
394           token: server.accessToken,
395           fields,
396           attaches,
397           statusCodeExpected: 200
398         })
399       }
400
401       {
402         const attaches = immutableAssign(baseCorrectAttaches, {
403           videofile: join(__dirname, '..', '..', 'fixtures', 'video_short.ogv')
404         })
405
406         await makeUploadRequest({
407           url: server.url,
408           path: path + '/upload',
409           token: server.accessToken,
410           fields,
411           attaches,
412           statusCodeExpected: 200
413         })
414       }
415     })
416   })
417
418   describe('When updating a video', function () {
419     const baseCorrectParams = {
420       name: 'my super name',
421       category: 5,
422       licence: 2,
423       language: 'pt',
424       nsfw: false,
425       commentsEnabled: false,
426       description: 'my super description',
427       privacy: VideoPrivacy.PUBLIC,
428       tags: [ 'tag1', 'tag2' ]
429     }
430
431     before(async function () {
432       const res = await getVideosList(server.url)
433       videoId = res.body.data[0].uuid
434     })
435
436     it('Should fail with nothing', async function () {
437       const fields = {}
438       await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
439     })
440
441     it('Should fail without a valid uuid', async function () {
442       const fields = baseCorrectParams
443       await makePutBodyRequest({ url: server.url, path: path + 'blabla', token: server.accessToken, fields })
444     })
445
446     it('Should fail with an unknown id', async function () {
447       const fields = baseCorrectParams
448
449       await makePutBodyRequest({
450         url: server.url,
451         path: path + '4da6fde3-88f7-4d16-b119-108df5630b06',
452         token: server.accessToken,
453         fields,
454         statusCodeExpected: 404
455       })
456     })
457
458     it('Should fail with a long name', async function () {
459       const fields = immutableAssign(baseCorrectParams, { name: 'super'.repeat(65) })
460
461       await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
462     })
463
464     it('Should fail with a bad category', async function () {
465       const fields = immutableAssign(baseCorrectParams, { category: 125 })
466
467       await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
468     })
469
470     it('Should fail with a bad licence', async function () {
471       const fields = immutableAssign(baseCorrectParams, { licence: 125 })
472
473       await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
474     })
475
476     it('Should fail with a bad language', async function () {
477       const fields = immutableAssign(baseCorrectParams, { language: 'a'.repeat(15) })
478
479       await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
480     })
481
482     it('Should fail with a long description', async function () {
483       const fields = immutableAssign(baseCorrectParams, { description: 'super'.repeat(2500) })
484
485       await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
486     })
487
488     it('Should fail with a long support text', async function () {
489       const fields = immutableAssign(baseCorrectParams, { support: 'super'.repeat(201) })
490
491       await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
492     })
493
494     it('Should fail with a bad channel', async function () {
495       const fields = immutableAssign(baseCorrectParams, { channelId: 545454 })
496
497       await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
498     })
499
500     it('Should fail with too many tags', async function () {
501       const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6' ] })
502
503       await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
504     })
505
506     it('Should fail with a tag length too low', async function () {
507       const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 't' ] })
508
509       await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
510     })
511
512     it('Should fail with a tag length too big', async function () {
513       const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 'my_super_tag_too_long_long_long_long_long_long' ] })
514
515       await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
516     })
517
518     it('Should fail with a bad schedule update (miss updateAt)', async function () {
519       const fields = immutableAssign(baseCorrectParams, { scheduleUpdate: { privacy: VideoPrivacy.PUBLIC } })
520
521       await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
522     })
523
524     it('Should fail with a bad schedule update (wrong updateAt)', async function () {
525       const fields = immutableAssign(baseCorrectParams, { scheduleUpdate: { updateAt: 'toto', privacy: VideoPrivacy.PUBLIC } })
526
527       await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
528     })
529
530     it('Should fail with an incorrect thumbnail file', async function () {
531       const fields = baseCorrectParams
532       const attaches = {
533         'thumbnailfile': join(__dirname, '..', '..', 'fixtures', 'avatar.png')
534       }
535
536       await makeUploadRequest({
537         url: server.url,
538         method: 'PUT',
539         path: path + videoId,
540         token: server.accessToken,
541         fields,
542         attaches
543       })
544     })
545
546     it('Should fail with a big thumbnail file', async function () {
547       const fields = baseCorrectParams
548       const attaches = {
549         'thumbnailfile': join(__dirname, '..', '..', 'fixtures', 'avatar-big.png')
550       }
551
552       await makeUploadRequest({
553         url: server.url,
554         method: 'PUT',
555         path: path + videoId,
556         token: server.accessToken,
557         fields,
558         attaches
559       })
560     })
561
562     it('Should fail with an incorrect preview file', async function () {
563       const fields = baseCorrectParams
564       const attaches = {
565         'previewfile': join(__dirname, '..', '..', 'fixtures', 'avatar.png')
566       }
567
568       await makeUploadRequest({
569         url: server.url,
570         method: 'PUT',
571         path: path + videoId,
572         token: server.accessToken,
573         fields,
574         attaches
575       })
576     })
577
578     it('Should fail with a big preview file', async function () {
579       const fields = baseCorrectParams
580       const attaches = {
581         'previewfile': join(__dirname, '..', '..', 'fixtures', 'avatar-big.png')
582       }
583
584       await makeUploadRequest({
585         url: server.url,
586         method: 'PUT',
587         path: path + videoId,
588         token: server.accessToken,
589         fields,
590         attaches
591       })
592     })
593
594     it('Should fail with a video of another user without the appropriate right', async function () {
595       const fields = baseCorrectParams
596
597       await makePutBodyRequest({ url: server.url, path: path + videoId, token: userAccessToken, fields, statusCodeExpected: 403 })
598     })
599
600     it('Should fail with a video of another server')
601
602     it('Should succeed with the correct parameters', async function () {
603       const fields = baseCorrectParams
604
605       await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields, statusCodeExpected: 204 })
606     })
607   })
608
609   describe('When getting a video', function () {
610     it('Should return the list of the videos with nothing', async function () {
611       const res = await makeGetRequest({
612         url: server.url,
613         path,
614         statusCodeExpected: 200
615       })
616
617       expect(res.body.data).to.be.an('array')
618       expect(res.body.data.length).to.equal(3)
619     })
620
621     it('Should fail without a correct uuid', async function () {
622       await getVideo(server.url, 'coucou', 400)
623     })
624
625     it('Should return 404 with an incorrect video', async function () {
626       await getVideo(server.url, '4da6fde3-88f7-4d16-b119-108df5630b06', 404)
627     })
628
629     it('Should succeed with the correct parameters', async function () {
630       await getVideo(server.url, videoId)
631     })
632   })
633
634   describe('When rating a video', function () {
635     let videoId
636
637     before(async function () {
638       const res = await getVideosList(server.url)
639       videoId = res.body.data[0].id
640     })
641
642     it('Should fail without a valid uuid', async function () {
643       const fields = {
644         rating: 'like'
645       }
646       await makePutBodyRequest({ url: server.url, path: path + 'blabla/rate', token: server.accessToken, fields })
647     })
648
649     it('Should fail with an unknown id', async function () {
650       const fields = {
651         rating: 'like'
652       }
653       await makePutBodyRequest({
654         url: server.url,
655         path: path + '4da6fde3-88f7-4d16-b119-108df5630b06/rate',
656         token: server.accessToken,
657         fields,
658         statusCodeExpected: 404
659       })
660     })
661
662     it('Should fail with a wrong rating', async function () {
663       const fields = {
664         rating: 'likes'
665       }
666       await makePutBodyRequest({ url: server.url, path: path + videoId + '/rate', token: server.accessToken, fields })
667     })
668
669     it('Should succeed with the correct parameters', async function () {
670       const fields = {
671         rating: 'like'
672       }
673       await makePutBodyRequest({
674         url: server.url,
675         path: path + videoId + '/rate',
676         token: server.accessToken,
677         fields,
678         statusCodeExpected: 204
679       })
680     })
681   })
682
683   describe('When removing a video', function () {
684     it('Should have 404 with nothing', async function () {
685       await makeDeleteRequest({
686         url: server.url,
687         path,
688         statusCodeExpected: 400
689       })
690     })
691
692     it('Should fail without a correct uuid', async function () {
693       await removeVideo(server.url, server.accessToken, 'hello', 400)
694     })
695
696     it('Should fail with a video which does not exist', async function () {
697       await removeVideo(server.url, server.accessToken, '4da6fde3-88f7-4d16-b119-108df5630b06', 404)
698     })
699
700     it('Should fail with a video of another user without the appropriate right', async function () {
701       await removeVideo(server.url, userAccessToken, videoId, 403)
702     })
703
704     it('Should fail with a video of another server')
705
706     it('Should succeed with the correct parameters', async function () {
707       await removeVideo(server.url, server.accessToken, videoId)
708     })
709   })
710
711   after(async function () {
712     killallServers([ server ])
713
714     // Keep the logs if the test failed
715     if (this['ok']) {
716       await flushTests()
717     }
718   })
719 })