]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params/videos.js
Add like/dislike system for videos
[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 description: 'my super description',
116 tags: [ 'tag1', 'tag2' ]
117 }
118 const attach = {
119 'videofile': pathUtils.join(__dirname, '..', 'fixtures', 'video_short.webm')
120 }
121 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
122 })
123
124 it('Should fail with a long name', function (done) {
125 const data = {
126 name: 'My very very very very very very very very very very very very very very very very long name',
127 description: 'my super description',
128 tags: [ 'tag1', 'tag2' ]
129 }
130 const attach = {
131 'videofile': pathUtils.join(__dirname, '..', 'fixtures', 'video_short.webm')
132 }
133 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
134 })
135
136 it('Should fail without description', function (done) {
137 const data = {
138 name: 'my super name',
139 tags: [ 'tag1', 'tag2' ]
140 }
141 const attach = {
142 'videofile': pathUtils.join(__dirname, '..', 'fixtures', 'video_short.webm')
143 }
144 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
145 })
146
147 it('Should fail with a long description', function (done) {
148 const data = {
149 name: 'my super name',
150 description: 'my super description which is very very very very very very very very very very very very very very' +
151 'very very very very very very very very very very very very very very very very very very very very very' +
152 'very very very very very very very very very very very very very very very long',
153 tags: [ 'tag1', 'tag2' ]
154 }
155 const attach = {
156 'videofile': pathUtils.join(__dirname, '..', 'fixtures', 'video_short.webm')
157 }
158 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
159 })
160
161 it('Should fail without tags', function (done) {
162 const data = {
163 name: 'my super name',
164 description: 'my super description'
165 }
166 const attach = {
167 'videofile': pathUtils.join(__dirname, '..', 'fixtures', 'video_short.webm')
168 }
169 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
170 })
171
172 it('Should fail with too many tags', function (done) {
173 const data = {
174 name: 'my super name',
175 description: 'my super description',
176 tags: [ 'tag1', 'tag2', 'tag3', 'tag4' ]
177 }
178 const attach = {
179 'videofile': pathUtils.join(__dirname, '..', 'fixtures', 'video_short.webm')
180 }
181 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
182 })
183
184 it('Should fail with not enough tags', function (done) {
185 const data = {
186 name: 'my super name',
187 description: 'my super description',
188 tags: [ ]
189 }
190 const attach = {
191 'videofile': pathUtils.join(__dirname, '..', 'fixtures', 'video_short.webm')
192 }
193 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
194 })
195
196 it('Should fail with a tag length too low', function (done) {
197 const data = {
198 name: 'my super name',
199 description: 'my super description',
200 tags: [ 'tag1', 't' ]
201 }
202 const attach = {
203 'videofile': pathUtils.join(__dirname, '..', 'fixtures', 'video_short.webm')
204 }
205 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
206 })
207
208 it('Should fail with a tag length too big', function (done) {
209 const data = {
210 name: 'my super name',
211 description: 'my super description',
212 tags: [ 'mysupertagtoolong', 'tag1' ]
213 }
214 const attach = {
215 'videofile': pathUtils.join(__dirname, '..', 'fixtures', 'video_short.webm')
216 }
217 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
218 })
219
220 it('Should fail with malformed tags', function (done) {
221 const data = {
222 name: 'my super name',
223 description: 'my super description',
224 tags: [ 'my tag' ]
225 }
226 const attach = {
227 'videofile': pathUtils.join(__dirname, '..', 'fixtures', 'video_short.webm')
228 }
229 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
230 })
231
232 it('Should fail without an input file', function (done) {
233 const data = {
234 name: 'my super name',
235 description: 'my super description',
236 tags: [ 'tag1', 'tag2' ]
237 }
238 const attach = {}
239 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
240 })
241
242 it('Should fail without an incorrect input file', function (done) {
243 const data = {
244 name: 'my super name',
245 description: 'my super description',
246 tags: [ 'tag1', 'tag2' ]
247 }
248 const attach = {
249 'videofile': pathUtils.join(__dirname, '..', 'fixtures', 'video_short_fake.webm')
250 }
251 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
252 })
253
254 it('Should fail with a too big duration', function (done) {
255 const data = {
256 name: 'my super name',
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 description: 'my super description',
270 tags: [ 'tag1', 'tag2' ]
271 }
272 const attach = {
273 'videofile': pathUtils.join(__dirname, '..', 'fixtures', 'video_short.webm')
274 }
275 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, function () {
276 attach.videofile = pathUtils.join(__dirname, '..', 'fixtures', 'video_short.mp4')
277 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, function () {
278 attach.videofile = pathUtils.join(__dirname, '..', 'fixtures', 'video_short.ogv')
279 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done, 204)
280 }, false)
281 }, false)
282 })
283 })
284
285 describe('When updating a video', function () {
286 let videoId
287
288 before(function (done) {
289 videosUtils.getVideosList(server.url, function (err, res) {
290 if (err) throw err
291
292 videoId = res.body.data[0].id
293
294 return done()
295 })
296 })
297
298 it('Should fail with nothing', function (done) {
299 const data = {}
300 requestsUtils.makePutBodyRequest(server.url, path, server.accessToken, data, done)
301 })
302
303 it('Should fail without a valid uuid', function (done) {
304 const data = {
305 description: 'my super description',
306 tags: [ 'tag1', 'tag2' ]
307 }
308 requestsUtils.makePutBodyRequest(server.url, path + 'blabla', server.accessToken, data, done)
309 })
310
311 it('Should fail with an unknown id', function (done) {
312 const data = {
313 description: 'my super description',
314 tags: [ 'tag1', 'tag2' ]
315 }
316 requestsUtils.makePutBodyRequest(server.url, path + '4da6fde3-88f7-4d16-b119-108df5630b06', server.accessToken, data, done, 404)
317 })
318
319 it('Should fail with a long name', function (done) {
320 const data = {
321 name: 'My very very very very very very very very very very very very very very very very long name',
322 description: 'my super description',
323 tags: [ 'tag1', 'tag2' ]
324 }
325 requestsUtils.makePutBodyRequest(server.url, path + videoId, server.accessToken, data, done)
326 })
327
328 it('Should fail with a long description', function (done) {
329 const data = {
330 name: 'my super name',
331 description: 'my super description which is very very very very very very very very very very very very very very' +
332 'very very very very very very very very very very very very very very very very very very very very very' +
333 'very very very very very very very very very very very very very very very long',
334 tags: [ 'tag1', 'tag2' ]
335 }
336 requestsUtils.makePutBodyRequest(server.url, path + videoId, server.accessToken, data, done)
337 })
338
339 it('Should fail with too many tags', function (done) {
340 const data = {
341 name: 'my super name',
342 description: 'my super description',
343 tags: [ 'tag1', 'tag2', 'tag3', 'tag4' ]
344 }
345 requestsUtils.makePutBodyRequest(server.url, path + videoId, server.accessToken, data, done)
346 })
347
348 it('Should fail with not enough tags', function (done) {
349 const data = {
350 name: 'my super name',
351 description: 'my super description',
352 tags: [ ]
353 }
354 requestsUtils.makePutBodyRequest(server.url, path + videoId, server.accessToken, data, done)
355 })
356
357 it('Should fail with a tag length too low', function (done) {
358 const data = {
359 name: 'my super name',
360 description: 'my super description',
361 tags: [ 'tag1', 't' ]
362 }
363 requestsUtils.makePutBodyRequest(server.url, path + videoId, server.accessToken, data, done)
364 })
365
366 it('Should fail with a tag length too big', function (done) {
367 const data = {
368 name: 'my super name',
369 description: 'my super description',
370 tags: [ 'mysupertagtoolong', 'tag1' ]
371 }
372 requestsUtils.makePutBodyRequest(server.url, path + videoId, server.accessToken, data, done)
373 })
374
375 it('Should fail with malformed tags', function (done) {
376 const data = {
377 name: 'my super name',
378 description: 'my super description',
379 tags: [ 'my tag' ]
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 })