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