1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
4 import { omit } from 'lodash'
6 import { join } from 'path'
7 import { VideoPrivacy } from '../../../../shared/models/videos/video-privacy.enum'
9 createUser, flushTests, getMyUserInformation, getVideo, getVideosList, immutableAssign, killallServers, makeDeleteRequest,
10 makeGetRequest, makePostUploadRequest, makePutBodyRequest, removeVideo, runServer, ServerInfo, setAccessTokensToServers, userLogin
12 import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '../../utils/requests/check-api-params'
14 const expect = chai.expect
16 describe('Test videos API validator', function () {
17 const path = '/api/v1/videos/'
18 let server: ServerInfo
21 // ---------------------------------------------------------------
23 before(async function () {
28 server = await runServer(1)
30 await setAccessTokensToServers([ server ])
32 const res = await getMyUserInformation(server.url, server.accessToken)
33 channelId = res.body.videoChannels[0].id
36 describe('When listing a video', function () {
37 it('Should fail with a bad start pagination', async function () {
38 await checkBadStartPagination(server.url, path)
41 it('Should fail with a bad count pagination', async function () {
42 await checkBadCountPagination(server.url, path)
45 it('Should fail with an incorrect sort', async function () {
46 await checkBadSortPagination(server.url, path)
50 describe('When searching a video', function () {
52 it('Should fail with nothing', async function () {
53 await makeGetRequest({
55 path: join(path, 'search'),
56 statusCodeExpected: 400
60 it('Should fail with a bad start pagination', async function () {
61 await checkBadStartPagination(server.url, join(path, 'search', 'test'))
64 it('Should fail with a bad count pagination', async function () {
65 await checkBadCountPagination(server.url, join(path, 'search', 'test'))
68 it('Should fail with an incorrect sort', async function () {
69 await checkBadSortPagination(server.url, join(path, 'search', 'test'))
73 describe('When listing my videos', function () {
74 const path = '/api/v1/users/me/videos'
76 it('Should fail with a bad start pagination', async function () {
77 await checkBadStartPagination(server.url, path, server.accessToken)
80 it('Should fail with a bad count pagination', async function () {
81 await checkBadCountPagination(server.url, path, server.accessToken)
84 it('Should fail with an incorrect sort', async function () {
85 await checkBadSortPagination(server.url, path, server.accessToken)
89 describe('When adding a video', function () {
91 const baseCorrectAttaches = {
92 'videofile': join(__dirname, '..', 'fixtures', 'video_short.webm')
96 // Put in before to have channelId
98 name: 'my super name',
103 commentsEnabled: true,
104 description: 'my super description',
105 tags: [ 'tag1', 'tag2' ],
106 privacy: VideoPrivacy.PUBLIC,
111 it('Should fail with nothing', async function () {
114 await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
117 it('Should fail without name', async function () {
118 const fields = omit(baseCorrectParams, 'name')
119 const attaches = baseCorrectAttaches
121 await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
124 it('Should fail with a long name', async function () {
125 const fields = immutableAssign(baseCorrectParams, { name: 'super'.repeat(65) })
126 const attaches = baseCorrectAttaches
128 await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
131 it('Should fail with a bad category', async function () {
132 const fields = immutableAssign(baseCorrectParams, { category: 125 })
133 const attaches = baseCorrectAttaches
135 await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
138 it('Should fail with a bad licence', async function () {
139 const fields = immutableAssign(baseCorrectParams, { licence: 125 })
140 const attaches = baseCorrectAttaches
142 await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
145 it('Should fail with a bad language', async function () {
146 const fields = immutableAssign(baseCorrectParams, { language: 125 })
147 const attaches = baseCorrectAttaches
149 await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
152 it('Should fail without nsfw attribute', async function () {
153 const fields = omit(baseCorrectParams, 'nsfw')
154 const attaches = baseCorrectAttaches
156 await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
159 it('Should fail with a bad nsfw attribute', async function () {
160 const fields = immutableAssign(baseCorrectParams, { nsfw: 2 })
161 const attaches = baseCorrectAttaches
163 await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
166 it('Should fail without commentsEnabled attribute', async function () {
167 const fields = omit(baseCorrectParams, 'commentsEnabled')
168 const attaches = baseCorrectAttaches
170 await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
173 it('Should fail with a bad commentsEnabled attribute', async function () {
174 const fields = immutableAssign(baseCorrectParams, { commentsEnabled: 2 })
175 const attaches = baseCorrectAttaches
177 await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
180 it('Should fail with a long description', async function () {
181 const fields = immutableAssign(baseCorrectParams, { description: 'super'.repeat(1500) })
182 const attaches = baseCorrectAttaches
184 await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
187 it('Should fail without a channel', async function () {
188 const fields = omit(baseCorrectParams, 'channelId')
189 const attaches = baseCorrectAttaches
191 await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
194 it('Should fail with a bad channel', async function () {
195 const fields = immutableAssign(baseCorrectParams, { channelId: 545454 })
196 const attaches = baseCorrectAttaches
198 await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
201 it('Should fail with another user channel', async function () {
204 password: 'fake_password'
206 await createUser(server.url, server.accessToken, user.username, user.password)
208 const accessTokenUser = await userLogin(server, user)
209 const res = await getMyUserInformation(server.url, accessTokenUser)
210 const customChannelId = res.body.videoChannels[0].id
212 const fields = immutableAssign(baseCorrectParams, { channelId: customChannelId })
213 const attaches = baseCorrectAttaches
215 await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
218 it('Should fail with too many tags', async function () {
219 const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6' ] })
220 const attaches = baseCorrectAttaches
222 await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
225 it('Should fail with a tag length too low', async function () {
226 const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 't' ] })
227 const attaches = baseCorrectAttaches
229 await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
232 it('Should fail with a tag length too big', async function () {
233 const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 'my_super_tag_too_long_long_long_long_long_long' ] })
234 const attaches = baseCorrectAttaches
236 await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
239 it('Should fail without an input file', async function () {
240 const fields = baseCorrectParams
242 await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
245 it('Should fail without an incorrect input file', async function () {
246 const fields = baseCorrectParams
248 'videofile': join(__dirname, '..', 'fixtures', 'video_short_fake.webm')
250 await makePostUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches })
253 it('Should succeed with the correct parameters', async function () {
256 const fields = baseCorrectParams
259 const attaches = baseCorrectAttaches
260 await makePostUploadRequest({
262 path: path + '/upload',
263 token: server.accessToken,
266 statusCodeExpected: 200
271 const attaches = immutableAssign(baseCorrectAttaches, {
272 videofile: join(__dirname, '..', 'fixtures', 'video_short.mp4')
275 await makePostUploadRequest({
277 path: path + '/upload',
278 token: server.accessToken,
281 statusCodeExpected: 200
286 const attaches = immutableAssign(baseCorrectAttaches, {
287 videofile: join(__dirname, '..', 'fixtures', 'video_short.ogv')
290 await makePostUploadRequest({
292 path: path + '/upload',
293 token: server.accessToken,
296 statusCodeExpected: 200
302 describe('When updating a video', function () {
303 const baseCorrectParams = {
304 name: 'my super name',
309 commentsEnabled: false,
310 description: 'my super description',
311 privacy: VideoPrivacy.PUBLIC,
312 tags: [ 'tag1', 'tag2' ]
316 before(async function () {
317 const res = await getVideosList(server.url)
318 videoId = res.body.data[0].id
321 it('Should fail with nothing', async function () {
323 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
326 it('Should fail without a valid uuid', async function () {
327 const fields = baseCorrectParams
328 await makePutBodyRequest({ url: server.url, path: path + 'blabla', token: server.accessToken, fields })
331 it('Should fail with an unknown id', async function () {
332 const fields = baseCorrectParams
334 await makePutBodyRequest({
336 path: path + '4da6fde3-88f7-4d16-b119-108df5630b06',
337 token: server.accessToken,
339 statusCodeExpected: 404
343 it('Should fail with a long name', async function () {
344 const fields = immutableAssign(baseCorrectParams, { name: 'super'.repeat(65) })
346 await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
349 it('Should fail with a bad category', async function () {
350 const fields = immutableAssign(baseCorrectParams, { category: 125 })
352 await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
355 it('Should fail with a bad licence', async function () {
356 const fields = immutableAssign(baseCorrectParams, { licence: 125 })
358 await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
361 it('Should fail with a bad language', async function () {
362 const fields = immutableAssign(baseCorrectParams, { language: 125 })
364 await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
367 it('Should fail with a bad nsfw attribute', async function () {
368 const fields = immutableAssign(baseCorrectParams, { nsfw: 2 })
370 await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
373 it('Should fail with a bad commentsEnabled attribute', async function () {
374 const fields = immutableAssign(baseCorrectParams, { commentsEnabled: 2 })
376 await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
379 it('Should fail with a long description', async function () {
380 const fields = immutableAssign(baseCorrectParams, { description: 'super'.repeat(1500) })
382 await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
385 it('Should fail with too many tags', async function () {
386 const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6' ] })
388 await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
391 it('Should fail with a tag length too low', async function () {
392 const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 't' ] })
394 await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
397 it('Should fail with a tag length too big', async function () {
398 const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 'my_super_tag_too_long_long_long_long_long_long' ] })
400 await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields })
403 it('Should fail with a video of another user')
405 it('Should fail with a video of another server')
407 it('Should succeed with the correct parameters', async function () {
408 const fields = baseCorrectParams
410 await makePutBodyRequest({ url: server.url, path: path + videoId, token: server.accessToken, fields, statusCodeExpected: 204 })
414 describe('When getting a video', function () {
415 it('Should return the list of the videos with nothing', async function () {
416 const res = await makeGetRequest({
419 statusCodeExpected: 200
422 expect(res.body.data).to.be.an('array')
423 expect(res.body.data.length).to.equal(3)
426 it('Should fail without a correct uuid', async function () {
427 await getVideo(server.url, 'coucou', 400)
430 it('Should return 404 with an incorrect video', async function () {
431 await getVideo(server.url, '4da6fde3-88f7-4d16-b119-108df5630b06', 404)
434 it('Should succeed with the correct parameters')
437 describe('When rating a video', function () {
440 before(async function () {
441 const res = await getVideosList(server.url)
442 videoId = res.body.data[0].id
445 it('Should fail without a valid uuid', async function () {
449 await makePutBodyRequest({ url: server.url, path: path + 'blabla/rate', token: server.accessToken, fields })
452 it('Should fail with an unknown id', async function () {
456 await makePutBodyRequest({
458 path: path + '4da6fde3-88f7-4d16-b119-108df5630b06/rate',
459 token: server.accessToken,
461 statusCodeExpected: 404
465 it('Should fail with a wrong rating', async function () {
469 await makePutBodyRequest({ url: server.url, path: path + videoId + '/rate', token: server.accessToken, fields })
472 it('Should succeed with the correct parameters', async function () {
476 await makePutBodyRequest({
478 path: path + videoId + '/rate',
479 token: server.accessToken,
481 statusCodeExpected: 204
486 describe('When removing a video', function () {
487 it('Should have 404 with nothing', async function () {
488 await makeDeleteRequest({
491 statusCodeExpected: 400
495 it('Should fail without a correct uuid', async function () {
496 await removeVideo(server.url, server.accessToken, 'hello', 400)
499 it('Should fail with a video which does not exist', async function () {
500 await removeVideo(server.url, server.accessToken, '4da6fde3-88f7-4d16-b119-108df5630b06', 404)
503 it('Should fail with a video of another user')
505 it('Should fail with a video of another server')
507 it('Should succeed with the correct parameters')
510 after(async function () {
511 killallServers([ server ])
513 // Keep the logs if the test failed