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