]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params/videos.js
03b4db3fe68d72ebd680eed9135672852dd5334a
[github/Chocobozzz/PeerTube.git] / server / tests / api / check-params / videos.js
1 /* eslint-disable no-unused-expressions */
2
3 'use strict'
4
5 const chai = require('chai')
6 const expect = chai.expect
7 const pathUtils = require('path')
8 const request = require('supertest')
9 const series = require('async/series')
10
11 const loginUtils = require('../../utils/login')
12 const requestsUtils = require('../../utils/requests')
13 const serversUtils = require('../../utils/servers')
14 const videosUtils = require('../../utils/videos')
15
16 describe('Test videos API validator', function () {
17 const path = '/api/v1/videos/'
18 let server = null
19
20 // ---------------------------------------------------------------
21
22 before(function (done) {
23 this.timeout(20000)
24
25 series([
26 function (next) {
27 serversUtils.flushTests(next)
28 },
29 function (next) {
30 serversUtils.runServer(1, function (server1) {
31 server = server1
32
33 next()
34 })
35 },
36 function (next) {
37 loginUtils.loginAndGetAccessToken(server, function (err, token) {
38 if (err) throw err
39 server.accessToken = token
40
41 next()
42 })
43 }
44 ], done)
45 })
46
47 describe('When listing a video', function () {
48 it('Should fail with a bad start pagination', function (done) {
49 request(server.url)
50 .get(path)
51 .query({ start: 'hello' })
52 .set('Accept', 'application/json')
53 .expect(400, done)
54 })
55
56 it('Should fail with a bad count pagination', function (done) {
57 request(server.url)
58 .get(path)
59 .query({ count: 'hello' })
60 .set('Accept', 'application/json')
61 .expect(400, done)
62 })
63
64 it('Should fail with an incorrect sort', function (done) {
65 request(server.url)
66 .get(path)
67 .query({ sort: 'hello' })
68 .set('Accept', 'application/json')
69 .expect(400, done)
70 })
71 })
72
73 describe('When searching a video', function () {
74 it('Should fail with nothing', function (done) {
75 request(server.url)
76 .get(pathUtils.join(path, 'search'))
77 .set('Accept', 'application/json')
78 .expect(400, done)
79 })
80
81 it('Should fail with a bad start pagination', function (done) {
82 request(server.url)
83 .get(pathUtils.join(path, 'search', 'test'))
84 .query({ start: 'hello' })
85 .set('Accept', 'application/json')
86 .expect(400, done)
87 })
88
89 it('Should fail with a bad count pagination', function (done) {
90 request(server.url)
91 .get(pathUtils.join(path, 'search', 'test'))
92 .query({ count: 'hello' })
93 .set('Accept', 'application/json')
94 .expect(400, done)
95 })
96
97 it('Should fail with an incorrect sort', function (done) {
98 request(server.url)
99 .get(pathUtils.join(path, 'search', 'test'))
100 .query({ sort: 'hello' })
101 .set('Accept', 'application/json')
102 .expect(400, done)
103 })
104 })
105
106 describe('When adding a video', function () {
107 it('Should fail with nothing', function (done) {
108 const data = {}
109 const attach = {}
110 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
111 })
112
113 it('Should fail without name', function (done) {
114 const data = {
115 category: 5,
116 description: 'my super description',
117 tags: [ 'tag1', 'tag2' ]
118 }
119 const attach = {
120 'videofile': pathUtils.join(__dirname, '..', 'fixtures', 'video_short.webm')
121 }
122 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
123 })
124
125 it('Should fail with a long name', function (done) {
126 const data = {
127 name: 'My very very very very very very very very very very very very very very very very long name',
128 category: 5,
129 description: 'my super description',
130 tags: [ 'tag1', 'tag2' ]
131 }
132 const attach = {
133 'videofile': pathUtils.join(__dirname, '..', 'fixtures', 'video_short.webm')
134 }
135 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
136 })
137
138 it('Should fail without a category', function (done) {
139 const data = {
140 name: 'my super name',
141 description: 'my super description',
142 tags: [ 'tag1', 'tag2' ]
143 }
144 const attach = {
145 'videofile': pathUtils.join(__dirname, '..', 'fixtures', 'video_short.webm')
146 }
147 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
148 })
149
150 it('Should fail with a bad category', function (done) {
151 const data = {
152 name: 'my super name',
153 category: 125,
154 description: 'my super description',
155 tags: [ 'tag1', 'tag2' ]
156 }
157 const attach = {
158 'videofile': pathUtils.join(__dirname, '..', 'fixtures', 'video_short.webm')
159 }
160 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
161 })
162
163 it('Should fail without description', function (done) {
164 const data = {
165 name: 'my super name',
166 category: 5,
167 tags: [ 'tag1', 'tag2' ]
168 }
169 const attach = {
170 'videofile': pathUtils.join(__dirname, '..', 'fixtures', 'video_short.webm')
171 }
172 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
173 })
174
175 it('Should fail with a long description', function (done) {
176 const data = {
177 name: 'my super name',
178 category: 5,
179 description: 'my super description which is very very very very very very very very very very very very very very' +
180 'very very very very very very very very very very very very very very very very very very very very very' +
181 'very very very very very very very very very very very very very very very long',
182 tags: [ 'tag1', 'tag2' ]
183 }
184 const attach = {
185 'videofile': pathUtils.join(__dirname, '..', 'fixtures', 'video_short.webm')
186 }
187 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
188 })
189
190 it('Should fail with too many tags', function (done) {
191 const data = {
192 name: 'my super name',
193 category: 5,
194 description: 'my super description',
195 tags: [ 'tag1', 'tag2', 'tag3', 'tag4' ]
196 }
197 const attach = {
198 'videofile': pathUtils.join(__dirname, '..', 'fixtures', 'video_short.webm')
199 }
200 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
201 })
202
203 it('Should fail with a tag length too low', function (done) {
204 const data = {
205 name: 'my super name',
206 category: 5,
207 description: 'my super description',
208 tags: [ 'tag1', 't' ]
209 }
210 const attach = {
211 'videofile': pathUtils.join(__dirname, '..', 'fixtures', 'video_short.webm')
212 }
213 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
214 })
215
216 it('Should fail with a tag length too big', function (done) {
217 const data = {
218 name: 'my super name',
219 category: 5,
220 description: 'my super description',
221 tags: [ 'mysupertagtoolong', 'tag1' ]
222 }
223 const attach = {
224 'videofile': pathUtils.join(__dirname, '..', 'fixtures', 'video_short.webm')
225 }
226 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
227 })
228
229 it('Should fail without an input file', function (done) {
230 const data = {
231 name: 'my super name',
232 category: 5,
233 description: 'my super description',
234 tags: [ 'tag1', 'tag2' ]
235 }
236 const attach = {}
237 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
238 })
239
240 it('Should fail without an incorrect input file', function (done) {
241 const data = {
242 name: 'my super name',
243 category: 5,
244 description: 'my super description',
245 tags: [ 'tag1', 'tag2' ]
246 }
247 const attach = {
248 'videofile': pathUtils.join(__dirname, '..', 'fixtures', 'video_short_fake.webm')
249 }
250 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
251 })
252
253 it('Should fail with a too big duration', function (done) {
254 const data = {
255 name: 'my super name',
256 category: 5,
257 description: 'my super description',
258 tags: [ 'tag1', 'tag2' ]
259 }
260 const attach = {
261 'videofile': pathUtils.join(__dirname, '..', 'fixtures', 'video_too_long.webm')
262 }
263 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
264 })
265
266 it('Should succeed with the correct parameters', function (done) {
267 const data = {
268 name: 'my super name',
269 category: 5,
270 description: 'my super description',
271 tags: [ 'tag1', 'tag2' ]
272 }
273 const attach = {
274 'videofile': pathUtils.join(__dirname, '..', 'fixtures', 'video_short.webm')
275 }
276 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, function () {
277 attach.videofile = pathUtils.join(__dirname, '..', 'fixtures', 'video_short.mp4')
278 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, function () {
279 attach.videofile = pathUtils.join(__dirname, '..', 'fixtures', 'video_short.ogv')
280 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done, 204)
281 }, false)
282 }, false)
283 })
284 })
285
286 describe('When updating a video', function () {
287 let videoId
288
289 before(function (done) {
290 videosUtils.getVideosList(server.url, function (err, res) {
291 if (err) throw err
292
293 videoId = res.body.data[0].id
294
295 return done()
296 })
297 })
298
299 it('Should fail with nothing', function (done) {
300 const data = {}
301 requestsUtils.makePutBodyRequest(server.url, path, server.accessToken, data, done)
302 })
303
304 it('Should fail without a valid uuid', function (done) {
305 const data = {
306 category: 5,
307 description: 'my super description',
308 tags: [ 'tag1', 'tag2' ]
309 }
310 requestsUtils.makePutBodyRequest(server.url, path + 'blabla', server.accessToken, data, done)
311 })
312
313 it('Should fail with an unknown id', function (done) {
314 const data = {
315 category: 5,
316 description: 'my super description',
317 tags: [ 'tag1', 'tag2' ]
318 }
319 requestsUtils.makePutBodyRequest(server.url, path + '4da6fde3-88f7-4d16-b119-108df5630b06', server.accessToken, data, done, 404)
320 })
321
322 it('Should fail with a long name', function (done) {
323 const data = {
324 name: 'My very very very very very very very very very very very very very very very very long name',
325 category: 5,
326 description: 'my super description',
327 tags: [ 'tag1', 'tag2' ]
328 }
329 requestsUtils.makePutBodyRequest(server.url, path + videoId, server.accessToken, data, done)
330 })
331
332 it('Should fail with a bad category', function (done) {
333 const data = {
334 name: 'my super name',
335 category: 128,
336 description: 'my super description',
337 tags: [ 'tag1', 'tag2' ]
338 }
339 requestsUtils.makePutBodyRequest(server.url, path + videoId, server.accessToken, data, done)
340 })
341
342 it('Should fail with a long description', function (done) {
343 const data = {
344 name: 'my super name',
345 category: 5,
346 description: 'my super description which is very very very very very very very very very very very very very very' +
347 'very very very very very very very very very very very very very very very very very very very very very' +
348 'very very very very very very very very very very very very very very very long',
349 tags: [ 'tag1', 'tag2' ]
350 }
351 requestsUtils.makePutBodyRequest(server.url, path + videoId, server.accessToken, data, done)
352 })
353
354 it('Should fail with too many tags', function (done) {
355 const data = {
356 name: 'my super name',
357 category: 5,
358 description: 'my super description',
359 tags: [ 'tag1', 'tag2', 'tag3', 'tag4' ]
360 }
361 requestsUtils.makePutBodyRequest(server.url, path + videoId, server.accessToken, data, done)
362 })
363
364 it('Should fail with a tag length too low', function (done) {
365 const data = {
366 name: 'my super name',
367 category: 5,
368 description: 'my super description',
369 tags: [ 'tag1', 't' ]
370 }
371 requestsUtils.makePutBodyRequest(server.url, path + videoId, server.accessToken, data, done)
372 })
373
374 it('Should fail with a tag length too big', function (done) {
375 const data = {
376 name: 'my super name',
377 category: 5,
378 description: 'my super description',
379 tags: [ 'mysupertagtoolong', 'tag1' ]
380 }
381 requestsUtils.makePutBodyRequest(server.url, path + videoId, server.accessToken, data, done)
382 })
383
384 it('Should fail with a video of another user')
385
386 it('Should fail with a video of another pod')
387 })
388
389 describe('When getting a video', function () {
390 it('Should return the list of the videos with nothing', function (done) {
391 request(server.url)
392 .get(path)
393 .set('Accept', 'application/json')
394 .expect(200)
395 .expect('Content-Type', /json/)
396 .end(function (err, res) {
397 if (err) throw err
398
399 expect(res.body.data).to.be.an('array')
400 expect(res.body.data.length).to.equal(3)
401
402 done()
403 })
404 })
405
406 it('Should fail without a correct uuid', function (done) {
407 request(server.url)
408 .get(path + 'coucou')
409 .set('Accept', 'application/json')
410 .expect(400, done)
411 })
412
413 it('Should return 404 with an incorrect video', function (done) {
414 request(server.url)
415 .get(path + '4da6fde3-88f7-4d16-b119-108df5630b06')
416 .set('Accept', 'application/json')
417 .expect(404, done)
418 })
419
420 it('Should succeed with the correct parameters')
421 })
422
423 describe('When rating a video', function () {
424 let videoId
425
426 before(function (done) {
427 videosUtils.getVideosList(server.url, function (err, res) {
428 if (err) throw err
429
430 videoId = res.body.data[0].id
431
432 return done()
433 })
434 })
435
436 it('Should fail without a valid uuid', function (done) {
437 const data = {
438 rating: 'like'
439 }
440 requestsUtils.makePutBodyRequest(server.url, path + 'blabla/rate', server.accessToken, data, done)
441 })
442
443 it('Should fail with an unknown id', function (done) {
444 const data = {
445 rating: 'like'
446 }
447 requestsUtils.makePutBodyRequest(server.url, path + '4da6fde3-88f7-4d16-b119-108df5630b06/rate', server.accessToken, data, done, 404)
448 })
449
450 it('Should fail with a wrong rating', function (done) {
451 const data = {
452 rating: 'likes'
453 }
454 requestsUtils.makePutBodyRequest(server.url, path + videoId + '/rate', server.accessToken, data, done)
455 })
456
457 it('Should succeed with the correct parameters', function (done) {
458 const data = {
459 rating: 'like'
460 }
461 requestsUtils.makePutBodyRequest(server.url, path + videoId + '/rate', server.accessToken, data, done, 204)
462 })
463 })
464
465 describe('When removing a video', function () {
466 it('Should have 404 with nothing', function (done) {
467 request(server.url)
468 .delete(path)
469 .set('Authorization', 'Bearer ' + server.accessToken)
470 .expect(400, done)
471 })
472
473 it('Should fail without a correct uuid', function (done) {
474 request(server.url)
475 .delete(path + 'hello')
476 .set('Authorization', 'Bearer ' + server.accessToken)
477 .expect(400, done)
478 })
479
480 it('Should fail with a video which does not exist', function (done) {
481 request(server.url)
482 .delete(path + '4da6fde3-88f7-4d16-b119-108df5630b06')
483 .set('Authorization', 'Bearer ' + server.accessToken)
484 .expect(404, done)
485 })
486
487 it('Should fail with a video of another user')
488
489 it('Should fail with a video of another pod')
490
491 it('Should succeed with the correct parameters')
492 })
493
494 after(function (done) {
495 process.kill(-server.app.pid)
496
497 // Keep the logs if the test failed
498 if (this.ok) {
499 serversUtils.flushTests(done)
500 } else {
501 done()
502 }
503 })
504 })