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