]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params/videos.ts
2962f5640a3d5a4aff0f6fa5aa4a619c1beda671
[github/Chocobozzz/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 without a category', async function () {
193 const fields = getCompleteVideoUploadAttributes()
194 delete fields.category
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 category', async function () {
201 const fields = getCompleteVideoUploadAttributes()
202 fields.category = 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 without a licence', async function () {
209 const fields = getCompleteVideoUploadAttributes()
210 delete fields.licence
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 with a bad licence', async function () {
217 const fields = getCompleteVideoUploadAttributes()
218 fields.licence = 125
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 language', async function () {
225 const fields = getCompleteVideoUploadAttributes()
226 fields.language = 563
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 without nsfw attribute', async function () {
233 const fields = getCompleteVideoUploadAttributes()
234 delete fields.nsfw
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 with a bad nsfw attribute', async function () {
241 const fields = getCompleteVideoUploadAttributes()
242 fields.nsfw = 2 as any
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 without description', async function () {
249 const fields = getCompleteVideoUploadAttributes()
250 delete fields.description
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 a long description', async function () {
257 const fields = getCompleteVideoUploadAttributes()
258 fields.description = 'my super description which is very very very very very very very very very very very very long'.repeat(35)
259
260 const attaches = getVideoUploadAttaches()
261 await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
262 })
263
264 it('Should fail without a channel', async function () {
265 const fields = getCompleteVideoUploadAttributes()
266 delete fields.channelId
267
268 const attaches = getVideoUploadAttaches()
269 await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
270 })
271
272 it('Should fail with a bad channel', async function () {
273 const fields = getCompleteVideoUploadAttributes()
274 fields.channelId = 545454
275
276 const attaches = getVideoUploadAttaches()
277 await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
278 })
279
280 it('Should fail with another user channel', async function () {
281 const user = {
282 username: 'fake',
283 password: 'fake_password'
284 }
285 await createUser(server.url, server.accessToken, user.username, user.password)
286
287 const accessTokenUser = await getUserAccessToken(server, user)
288 const res = await getMyUserInformation(server.url, accessTokenUser)
289 const customChannelId = res.body.videoChannels[0].id
290
291 const fields = getCompleteVideoUploadAttributes()
292 fields.channelId = customChannelId
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 with too many tags', async function () {
299 const fields = getCompleteVideoUploadAttributes()
300 fields.tags = [ 'tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6' ]
301
302 const attaches = getVideoUploadAttaches()
303 await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
304 })
305
306 it('Should fail with a tag length too low', async function () {
307 const fields = getCompleteVideoUploadAttributes()
308 fields.tags = [ 'tag1', 't' ]
309
310 const attaches = getVideoUploadAttaches()
311 await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
312 })
313
314 it('Should fail with a tag length too big', async function () {
315 const fields = getCompleteVideoUploadAttributes()
316 fields.tags = [ 'my_super_tag_too_long_long_long_long_long_long', 'tag1' ]
317
318 const attaches = getVideoUploadAttaches()
319 await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
320 })
321
322 it('Should fail without an input file', async function () {
323 const fields = getCompleteVideoUploadAttributes()
324 const attaches = {}
325 await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
326 })
327
328 it('Should fail without an incorrect input file', async function () {
329 const fields = getCompleteVideoUploadAttributes()
330 const attaches = {
331 'videofile': join(__dirname, '..', 'fixtures', 'video_short_fake.webm')
332 }
333 await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
334 })
335
336 it('Should succeed with the correct parameters', async function () {
337 this.timeout(10000)
338
339 const fields = getCompleteVideoUploadAttributes()
340 const attaches = getVideoUploadAttaches()
341
342 await makePostUploadRequest({
343 url: server.url,
344 path: path + '/upload',
345 token: server.accessToken,
346 fields,
347 attaches,
348 statusCodeExpected: 204
349 })
350
351 attaches.videofile = join(__dirname, '..', 'fixtures', 'video_short.mp4')
352 await makePostUploadRequest({
353 url: server.url,
354 path: path + '/upload',
355 token: server.accessToken,
356 fields,
357 attaches,
358 statusCodeExpected: 204
359 })
360
361 attaches.videofile = join(__dirname, '..', 'fixtures', 'video_short.ogv')
362 await makePostUploadRequest({
363 url: server.url,
364 path: path + '/upload',
365 token: server.accessToken,
366 fields,
367 attaches,
368 statusCodeExpected: 204
369 })
370 })
371 })
372
373 describe('When updating a video', function () {
374 let videoId
375
376 before(async function () {
377 const res = await getVideosList(server.url)
378 videoId = res.body.data[0].id
379 })
380
381 it('Should fail with nothing', async function () {
382 const fields = {}
383 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
384 })
385
386 it('Should fail without a valid uuid', async function () {
387 const fields = getCompleteVideoUpdateAttributes()
388 await makePutBodyRequest({ url: server.url, path: path + 'blabla', token: server.accessToken, fields })
389 })
390
391 it('Should fail with an unknown id', async function () {
392 const fields = getCompleteVideoUpdateAttributes()
393
394 await makePutBodyRequest({
395 url: server.url,
396 path: path + '4da6fde3-88f7-4d16-b119-108df5630b06',
397 token: server.accessToken,
398 fields,
399 statusCodeExpected: 404
400 })
401 })
402
403 it('Should fail with a long name', async function () {
404 const fields = getCompleteVideoUpdateAttributes()
405 fields.name = 'My very very very very very very very very very very very very very very very very long'.repeat(3)
406
407 await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
408 })
409
410 it('Should fail with a bad category', async function () {
411 const fields = getCompleteVideoUpdateAttributes()
412 fields.category = 128
413
414 await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
415 })
416
417 it('Should fail with a bad licence', async function () {
418 const fields = getCompleteVideoUpdateAttributes()
419 fields.licence = 128
420
421 await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
422 })
423
424 it('Should fail with a bad language', async function () {
425 const fields = getCompleteVideoUpdateAttributes()
426 fields.language = 896
427
428 await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
429 })
430
431 it('Should fail with a bad nsfw attribute', async function () {
432 const fields = getCompleteVideoUpdateAttributes()
433 fields.nsfw = (-4 as any)
434
435 await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
436 })
437
438 it('Should fail with a long description', async function () {
439 const fields = getCompleteVideoUpdateAttributes()
440 fields.description = 'my super description which is very very very very very very very very very very very very very long'.repeat(35)
441
442 await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
443 })
444
445 it('Should fail with too many tags', async function () {
446 const fields = getCompleteVideoUpdateAttributes()
447 fields.tags = [ 'tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6' ]
448
449 await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
450 })
451
452 it('Should fail with a tag length too low', async function () {
453 const fields = getCompleteVideoUpdateAttributes()
454 fields.tags = [ 'tag1', 't' ]
455
456 await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
457 })
458
459 it('Should fail with a tag length too big', async function () {
460 const fields = getCompleteVideoUpdateAttributes()
461 fields.tags = [ 'my_super_tag_too_long_long_long_long', 'tag1' ]
462
463 await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
464 })
465
466 it('Should fail with a video of another user')
467
468 it('Should fail with a video of another server')
469
470 it('Should succeed with the correct parameters', async function () {
471 const fields = getCompleteVideoUpdateAttributes()
472
473 await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields, statusCodeExpected: 204 })
474 })
475 })
476
477 describe('When getting a video', function () {
478 it('Should return the list of the videos with nothing', async function () {
479 const res = await request(server.url)
480 .get(path)
481 .set('Accept', 'application/json')
482 .expect(200)
483 .expect('Content-Type', /json/)
484
485 expect(res.body.data).to.be.an('array')
486 expect(res.body.data.length).to.equal(3)
487 })
488
489 it('Should fail without a correct uuid', async function () {
490 await request(server.url)
491 .get(path + 'coucou')
492 .set('Accept', 'application/json')
493 .expect(400)
494 })
495
496 it('Should return 404 with an incorrect video', async function () {
497 await request(server.url)
498 .get(path + '4da6fde3-88f7-4d16-b119-108df5630b06')
499 .set('Accept', 'application/json')
500 .expect(404)
501 })
502
503 it('Should succeed with the correct parameters')
504 })
505
506 describe('When rating a video', function () {
507 let videoId
508
509 before(async function () {
510 const res = await getVideosList(server.url)
511 videoId = res.body.data[0].id
512 })
513
514 it('Should fail without a valid uuid', async function () {
515 const fields = {
516 rating: 'like'
517 }
518 await makePutBodyRequest({ url: server.url, path: path + 'blabla/rate', token: server.accessToken, fields })
519 })
520
521 it('Should fail with an unknown id', async function () {
522 const fields = {
523 rating: 'like'
524 }
525 await makePutBodyRequest({
526 url: server.url,
527 path: path + '4da6fde3-88f7-4d16-b119-108df5630b06/rate',
528 token: server.accessToken,
529 fields,
530 statusCodeExpected: 404
531 })
532 })
533
534 it('Should fail with a wrong rating', async function () {
535 const fields = {
536 rating: 'likes'
537 }
538 await makePutBodyRequest({ url: server.url, path: path + videoId + '/rate', token: server.accessToken, fields })
539 })
540
541 it('Should succeed with the correct parameters', async function () {
542 const fields = {
543 rating: 'like'
544 }
545 await makePutBodyRequest({
546 url: server.url,
547 path: path + videoId + '/rate',
548 token: server.accessToken,
549 fields,
550 statusCodeExpected: 204
551 })
552 })
553 })
554
555 describe('When removing a video', function () {
556 it('Should have 404 with nothing', async function () {
557 await request(server.url)
558 .delete(path)
559 .set('Authorization', 'Bearer ' + server.accessToken)
560 .expect(400)
561 })
562
563 it('Should fail without a correct uuid', async function () {
564 await request(server.url)
565 .delete(path + 'hello')
566 .set('Authorization', 'Bearer ' + server.accessToken)
567 .expect(400)
568 })
569
570 it('Should fail with a video which does not exist', async function () {
571 await request(server.url)
572 .delete(path + '4da6fde3-88f7-4d16-b119-108df5630b06')
573 .set('Authorization', 'Bearer ' + server.accessToken)
574 .expect(404)
575 })
576
577 it('Should fail with a video of another user')
578
579 it('Should fail with a video of another server')
580
581 it('Should succeed with the correct parameters')
582 })
583
584 after(async function () {
585 killallServers([ server ])
586
587 // Keep the logs if the test failed
588 if (this['ok']) {
589 await flushTests()
590 }
591 })
592 })