Improve check videos parameters tests
[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, makePostUploadRequest, 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(20000)
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         description: 'my super description',
104         tags: [ 'tag1', 'tag2' ],
105         privacy: VideoPrivacy.PUBLIC,
106         channelId
107       }
108     })
109
110     it('Should fail with nothing', async function () {
111       const fields = {}
112       const attaches = {}
113       await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
114     })
115
116     it('Should fail without name', async function () {
117       const fields = omit(baseCorrectParams, 'name')
118       const attaches = baseCorrectAttaches
119
120       await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
121     })
122
123     it('Should fail with a long name', async function () {
124       const fields = immutableAssign(baseCorrectParams, { name: 'super'.repeat(65) })
125       const attaches = baseCorrectAttaches
126
127       await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
128     })
129
130     it('Should fail with a bad category', async function () {
131       const fields = immutableAssign(baseCorrectParams, { category: 125 })
132       const attaches = baseCorrectAttaches
133
134       await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
135     })
136
137     it('Should fail with a bad licence', async function () {
138       const fields = immutableAssign(baseCorrectParams, { licence: 125 })
139       const attaches = baseCorrectAttaches
140
141       await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
142     })
143
144     it('Should fail with a bad language', async function () {
145       const fields = immutableAssign(baseCorrectParams, { language: 125 })
146       const attaches = baseCorrectAttaches
147
148       await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
149     })
150
151     it('Should fail without nsfw attribute', async function () {
152       const fields = omit(baseCorrectParams, 'nsfw')
153       const attaches = baseCorrectAttaches
154
155       await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
156     })
157
158     it('Should fail with a bad nsfw attribute', async function () {
159       const fields = immutableAssign(baseCorrectParams, { nsfw: 2 })
160       const attaches = baseCorrectAttaches
161
162       await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
163     })
164
165     it('Should fail with a long description', async function () {
166       const fields = immutableAssign(baseCorrectParams, { description: 'super'.repeat(1500) })
167       const attaches = baseCorrectAttaches
168
169       await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
170     })
171
172     it('Should fail without a channel', async function () {
173       const fields = omit(baseCorrectParams, 'channelId')
174       const attaches = baseCorrectAttaches
175
176       await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
177     })
178
179     it('Should fail with a bad channel', async function () {
180       const fields = immutableAssign(baseCorrectParams, { channelId: 545454 })
181       const attaches = baseCorrectAttaches
182
183       await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
184     })
185
186     it('Should fail with another user channel', async function () {
187       const user = {
188         username: 'fake',
189         password: 'fake_password'
190       }
191       await createUser(server.url, server.accessToken, user.username, user.password)
192
193       const accessTokenUser = await userLogin(server, user)
194       const res = await getMyUserInformation(server.url, accessTokenUser)
195       const customChannelId = res.body.videoChannels[0].id
196
197       const fields = immutableAssign(baseCorrectParams, { channelId: customChannelId })
198       const attaches = baseCorrectAttaches
199
200       await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
201     })
202
203     it('Should fail with too many tags', async function () {
204       const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6' ] })
205       const attaches = baseCorrectAttaches
206
207       await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
208     })
209
210     it('Should fail with a tag length too low', async function () {
211       const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 't' ] })
212       const attaches = baseCorrectAttaches
213
214       await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
215     })
216
217     it('Should fail with a tag length too big', async function () {
218       const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 'my_super_tag_too_long_long_long_long_long_long' ] })
219       const attaches = baseCorrectAttaches
220
221       await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
222     })
223
224     it('Should fail without an input file', async function () {
225       const fields = baseCorrectParams
226       const attaches = {}
227       await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
228     })
229
230     it('Should fail without an incorrect input file', async function () {
231       const fields = baseCorrectParams
232       const attaches = {
233         'videofile': join(__dirname, '..', 'fixtures', 'video_short_fake.webm')
234       }
235       await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
236     })
237
238     it('Should succeed with the correct parameters', async function () {
239       this.timeout(10000)
240
241       const fields = baseCorrectParams
242
243       {
244         const attaches = baseCorrectAttaches
245         await makePostUploadRequest({
246           url: server.url,
247           path: path + '/upload',
248           token: server.accessToken,
249           fields,
250           attaches,
251           statusCodeExpected: 200
252         })
253       }
254
255       {
256         const attaches = immutableAssign(baseCorrectAttaches, {
257           videofile: join(__dirname, '..', 'fixtures', 'video_short.mp4')
258         })
259
260         await makePostUploadRequest({
261           url: server.url,
262           path: path + '/upload',
263           token: server.accessToken,
264           fields,
265           attaches,
266           statusCodeExpected: 200
267         })
268       }
269
270       {
271         const attaches = immutableAssign(baseCorrectAttaches, {
272           videofile: join(__dirname, '..', 'fixtures', 'video_short.ogv')
273         })
274
275         await makePostUploadRequest({
276           url: server.url,
277           path: path + '/upload',
278           token: server.accessToken,
279           fields,
280           attaches,
281           statusCodeExpected: 200
282         })
283       }
284     })
285   })
286
287   describe('When updating a video', function () {
288     const baseCorrectParams = {
289       name: 'my super name',
290       category: 5,
291       licence: 2,
292       language: 6,
293       nsfw: false,
294       description: 'my super description',
295       privacy: VideoPrivacy.PUBLIC,
296       tags: [ 'tag1', 'tag2' ]
297     }
298     let videoId
299
300     before(async function () {
301       const res = await getVideosList(server.url)
302       videoId = res.body.data[0].id
303     })
304
305     it('Should fail with nothing', async function () {
306       const fields = {}
307       await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
308     })
309
310     it('Should fail without a valid uuid', async function () {
311       const fields = baseCorrectParams
312       await makePutBodyRequest({ url: server.url, path: path + 'blabla', token: server.accessToken, fields })
313     })
314
315     it('Should fail with an unknown id', async function () {
316       const fields = baseCorrectParams
317
318       await makePutBodyRequest({
319         url: server.url,
320         path: path + '4da6fde3-88f7-4d16-b119-108df5630b06',
321         token: server.accessToken,
322         fields,
323         statusCodeExpected: 404
324       })
325     })
326
327     it('Should fail with a long name', async function () {
328       const fields = immutableAssign(baseCorrectParams, { name: 'super'.repeat(65) })
329
330       await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
331     })
332
333     it('Should fail with a bad category', async function () {
334       const fields = immutableAssign(baseCorrectParams, { category: 125 })
335
336       await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
337     })
338
339     it('Should fail with a bad licence', async function () {
340       const fields = immutableAssign(baseCorrectParams, { licence: 125 })
341
342       await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
343     })
344
345     it('Should fail with a bad language', async function () {
346       const fields = immutableAssign(baseCorrectParams, { language: 125 })
347
348       await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
349     })
350
351     it('Should fail with a bad nsfw attribute', async function () {
352       const fields = immutableAssign(baseCorrectParams, { nsfw: 2 })
353
354       await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
355     })
356
357     it('Should fail with a long description', async function () {
358       const fields = immutableAssign(baseCorrectParams, { description: 'super'.repeat(1500) })
359
360       await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
361     })
362
363     it('Should fail with too many tags', async function () {
364       const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6' ] })
365
366       await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
367     })
368
369     it('Should fail with a tag length too low', async function () {
370       const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 't' ] })
371
372       await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
373     })
374
375     it('Should fail with a tag length too big', async function () {
376       const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 'my_super_tag_too_long_long_long_long_long_long' ] })
377
378       await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
379     })
380
381     it('Should fail with a video of another user')
382
383     it('Should fail with a video of another server')
384
385     it('Should succeed with the correct parameters', async function () {
386       const fields = baseCorrectParams
387
388       await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields, statusCodeExpected: 204 })
389     })
390   })
391
392   describe('When getting a video', function () {
393     it('Should return the list of the videos with nothing', async function () {
394       const res = await makeGetRequest({
395         url: server.url,
396         path,
397         statusCodeExpected: 200
398       })
399
400       expect(res.body.data).to.be.an('array')
401       expect(res.body.data.length).to.equal(3)
402     })
403
404     it('Should fail without a correct uuid', async function () {
405       await getVideo(server.url, 'coucou', 400)
406     })
407
408     it('Should return 404 with an incorrect video', async function () {
409       await getVideo(server.url, '4da6fde3-88f7-4d16-b119-108df5630b06', 404)
410     })
411
412     it('Should succeed with the correct parameters')
413   })
414
415   describe('When rating a video', function () {
416     let videoId
417
418     before(async function () {
419       const res = await getVideosList(server.url)
420       videoId = res.body.data[0].id
421     })
422
423     it('Should fail without a valid uuid', async function () {
424       const fields = {
425         rating: 'like'
426       }
427       await makePutBodyRequest({ url: server.url, path: path + 'blabla/rate', token: server.accessToken, fields })
428     })
429
430     it('Should fail with an unknown id', async function () {
431       const fields = {
432         rating: 'like'
433       }
434       await makePutBodyRequest({
435         url: server.url,
436         path: path + '4da6fde3-88f7-4d16-b119-108df5630b06/rate',
437         token: server.accessToken,
438         fields,
439         statusCodeExpected: 404
440       })
441     })
442
443     it('Should fail with a wrong rating', async function () {
444       const fields = {
445         rating: 'likes'
446       }
447       await makePutBodyRequest({ url: server.url, path: path + videoId + '/rate', token: server.accessToken, fields })
448     })
449
450     it('Should succeed with the correct parameters', async function () {
451       const fields = {
452         rating: 'like'
453       }
454       await makePutBodyRequest({
455         url: server.url,
456         path: path + videoId + '/rate',
457         token: server.accessToken,
458         fields,
459         statusCodeExpected: 204
460       })
461     })
462   })
463
464   describe('When removing a video', function () {
465     it('Should have 404 with nothing', async function () {
466       await makeDeleteRequest({
467         url: server.url,
468         path,
469         statusCodeExpected: 400
470       })
471     })
472
473     it('Should fail without a correct uuid', async function () {
474       await removeVideo(server.url, server.accessToken, 'hello', 400)
475     })
476
477     it('Should fail with a video which does not exist', async function () {
478       await removeVideo(server.url, server.accessToken, '4da6fde3-88f7-4d16-b119-108df5630b06', 404)
479     })
480
481     it('Should fail with a video of another user')
482
483     it('Should fail with a video of another server')
484
485     it('Should succeed with the correct parameters')
486   })
487
488   after(async function () {
489     killallServers([ server ])
490
491     // Keep the logs if the test failed
492     if (this['ok']) {
493       await flushTests()
494     }
495   })
496 })