]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/checkParams.js
Server: split tests utils in multiple files
[github/Chocobozzz/PeerTube.git] / server / tests / api / checkParams.js
1 'use strict'
2
3 const chai = require('chai')
4 const expect = chai.expect
5 const pathUtils = require('path')
6 const request = require('supertest')
7 const series = require('async/series')
8
9 const loginUtils = require('../utils/login')
10 const serversUtils = require('../utils/servers')
11 const usersUtils = require('../utils/users')
12
13 describe('Test parameters validator', function () {
14 let server = null
15
16 function makePostRequest (path, token, fields, attaches, done, statusCodeExpected) {
17 if (!statusCodeExpected) statusCodeExpected = 400
18
19 const req = request(server.url)
20 .post(path)
21 .set('Accept', 'application/json')
22
23 if (token) req.set('Authorization', 'Bearer ' + token)
24
25 Object.keys(fields).forEach(function (field) {
26 const value = fields[field]
27
28 if (Array.isArray(value)) {
29 for (let i = 0; i < value.length; i++) {
30 req.field(field + '[' + i + ']', value[i])
31 }
32 } else {
33 req.field(field, value)
34 }
35 })
36
37 Object.keys(attaches).forEach(function (attach) {
38 const value = attaches[attach]
39 req.attach(attach, value)
40 })
41
42 req.expect(statusCodeExpected, done)
43 }
44
45 function makePostBodyRequest (path, token, fields, done, statusCodeExpected) {
46 if (!statusCodeExpected) statusCodeExpected = 400
47
48 const req = request(server.url)
49 .post(path)
50 .set('Accept', 'application/json')
51
52 if (token) req.set('Authorization', 'Bearer ' + token)
53
54 req.send(fields).expect(statusCodeExpected, done)
55 }
56
57 function makePutBodyRequest (path, token, fields, done, statusCodeExpected) {
58 if (!statusCodeExpected) statusCodeExpected = 400
59
60 const req = request(server.url)
61 .put(path)
62 .set('Accept', 'application/json')
63
64 if (token) req.set('Authorization', 'Bearer ' + token)
65
66 req.send(fields).expect(statusCodeExpected, done)
67 }
68
69 // ---------------------------------------------------------------
70
71 before(function (done) {
72 this.timeout(20000)
73
74 series([
75 function (next) {
76 serversUtils.flushTests(next)
77 },
78 function (next) {
79 serversUtils.runServer(1, function (server1) {
80 server = server1
81
82 next()
83 })
84 },
85 function (next) {
86 loginUtils.loginAndGetAccessToken(server, function (err, token) {
87 if (err) throw err
88 server.accessToken = token
89
90 next()
91 })
92 }
93 ], done)
94 })
95
96 describe('Of the pods API', function () {
97 const path = '/api/v1/pods/'
98
99 describe('When adding a pod', function () {
100 it('Should fail with nothing', function (done) {
101 const data = {}
102 makePostBodyRequest(path, null, data, done)
103 })
104
105 it('Should fail without public key', function (done) {
106 const data = {
107 url: 'http://coucou.com'
108 }
109 makePostBodyRequest(path, null, data, done)
110 })
111
112 it('Should fail without an url', function (done) {
113 const data = {
114 publicKey: 'mysuperpublickey'
115 }
116 makePostBodyRequest(path, null, data, done)
117 })
118
119 it('Should fail with an incorrect url', function (done) {
120 const data = {
121 url: 'coucou.com',
122 publicKey: 'mysuperpublickey'
123 }
124 makePostBodyRequest(path, null, data, function () {
125 data.url = 'http://coucou'
126 makePostBodyRequest(path, null, data, function () {
127 data.url = 'coucou'
128 makePostBodyRequest(path, null, data, done)
129 })
130 })
131 })
132
133 it('Should succeed with the correct parameters', function (done) {
134 const data = {
135 url: 'http://coucou.com',
136 publicKey: 'mysuperpublickey'
137 }
138 makePostBodyRequest(path, null, data, done, 200)
139 })
140 })
141
142 describe('For the friends API', function () {
143 let userAccessToken = null
144
145 before(function (done) {
146 usersUtils.createUser(server.url, server.accessToken, 'user1', 'password', function () {
147 server.user = {
148 username: 'user1',
149 password: 'password'
150 }
151
152 loginUtils.loginAndGetAccessToken(server, function (err, accessToken) {
153 if (err) throw err
154
155 userAccessToken = accessToken
156
157 done()
158 })
159 })
160 })
161
162 describe('When making friends', function () {
163 it('Should fail with a invalid token', function (done) {
164 request(server.url)
165 .get(path + '/makefriends')
166 .query({ start: 'hello' })
167 .set('Authorization', 'Bearer faketoken')
168 .set('Accept', 'application/json')
169 .expect(401, done)
170 })
171
172 it('Should fail if the user is not an administrator', function (done) {
173 request(server.url)
174 .get(path + '/makefriends')
175 .query({ start: 'hello' })
176 .set('Authorization', 'Bearer ' + userAccessToken)
177 .set('Accept', 'application/json')
178 .expect(403, done)
179 })
180 })
181
182 describe('When quitting friends', function () {
183 it('Should fail with a invalid token', function (done) {
184 request(server.url)
185 .get(path + '/quitfriends')
186 .query({ start: 'hello' })
187 .set('Authorization', 'Bearer faketoken')
188 .set('Accept', 'application/json')
189 .expect(401, done)
190 })
191
192 it('Should fail if the user is not an administrator', function (done) {
193 request(server.url)
194 .get(path + '/quitfriends')
195 .query({ start: 'hello' })
196 .set('Authorization', 'Bearer ' + userAccessToken)
197 .set('Accept', 'application/json')
198 .expect(403, done)
199 })
200 })
201 })
202 })
203
204 describe('Of the videos API', function () {
205 const path = '/api/v1/videos/'
206
207 describe('When listing a video', function () {
208 it('Should fail with a bad start pagination', function (done) {
209 request(server.url)
210 .get(path)
211 .query({ start: 'hello' })
212 .set('Accept', 'application/json')
213 .expect(400, done)
214 })
215
216 it('Should fail with a bad count pagination', function (done) {
217 request(server.url)
218 .get(path)
219 .query({ count: 'hello' })
220 .set('Accept', 'application/json')
221 .expect(400, done)
222 })
223
224 it('Should fail with an incorrect sort', function (done) {
225 request(server.url)
226 .get(path)
227 .query({ sort: 'hello' })
228 .set('Accept', 'application/json')
229 .expect(400, done)
230 })
231 })
232
233 describe('When searching a video', function () {
234 it('Should fail with nothing', function (done) {
235 request(server.url)
236 .get(pathUtils.join(path, 'search'))
237 .set('Accept', 'application/json')
238 .expect(400, done)
239 })
240
241 it('Should fail with a bad start pagination', function (done) {
242 request(server.url)
243 .get(pathUtils.join(path, 'search', 'test'))
244 .query({ start: 'hello' })
245 .set('Accept', 'application/json')
246 .expect(400, done)
247 })
248
249 it('Should fail with a bad count pagination', function (done) {
250 request(server.url)
251 .get(pathUtils.join(path, 'search', 'test'))
252 .query({ count: 'hello' })
253 .set('Accept', 'application/json')
254 .expect(400, done)
255 })
256
257 it('Should fail with an incorrect sort', function (done) {
258 request(server.url)
259 .get(pathUtils.join(path, 'search', 'test'))
260 .query({ sort: 'hello' })
261 .set('Accept', 'application/json')
262 .expect(400, done)
263 })
264 })
265
266 describe('When adding a video', function () {
267 it('Should fail with nothing', function (done) {
268 const data = {}
269 const attach = {}
270 makePostRequest(path, server.accessToken, data, attach, done)
271 })
272
273 it('Should fail without name', function (done) {
274 const data = {
275 description: 'my super description',
276 tags: [ 'tag1', 'tag2' ]
277 }
278 const attach = {
279 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
280 }
281 makePostRequest(path, server.accessToken, data, attach, done)
282 })
283
284 it('Should fail with a long name', function (done) {
285 const data = {
286 name: 'My very very very very very very very very very very very very very very very very long name',
287 description: 'my super description',
288 tags: [ 'tag1', 'tag2' ]
289 }
290 const attach = {
291 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
292 }
293 makePostRequest(path, server.accessToken, data, attach, done)
294 })
295
296 it('Should fail without description', function (done) {
297 const data = {
298 name: 'my super name',
299 tags: [ 'tag1', 'tag2' ]
300 }
301 const attach = {
302 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
303 }
304 makePostRequest(path, server.accessToken, data, attach, done)
305 })
306
307 it('Should fail with a long description', function (done) {
308 const data = {
309 name: 'my super name',
310 description: 'my super description which is very very very very very very very very very very very very very very' +
311 'very very very very very very very very very very very very very very very very very very very very very' +
312 'very very very very very very very very very very very very very very very long',
313 tags: [ 'tag1', 'tag2' ]
314 }
315 const attach = {
316 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
317 }
318 makePostRequest(path, server.accessToken, data, attach, done)
319 })
320
321 it('Should fail without tags', function (done) {
322 const data = {
323 name: 'my super name',
324 description: 'my super description'
325 }
326 const attach = {
327 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
328 }
329 makePostRequest(path, server.accessToken, data, attach, done)
330 })
331
332 it('Should fail with too many tags', function (done) {
333 const data = {
334 name: 'my super name',
335 description: 'my super description',
336 tags: [ 'tag1', 'tag2', 'tag3', 'tag4' ]
337 }
338 const attach = {
339 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
340 }
341 makePostRequest(path, server.accessToken, data, attach, done)
342 })
343
344 it('Should fail with not enough tags', function (done) {
345 const data = {
346 name: 'my super name',
347 description: 'my super description',
348 tags: [ ]
349 }
350 const attach = {
351 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
352 }
353 makePostRequest(path, server.accessToken, data, attach, done)
354 })
355
356 it('Should fail with a tag length too low', function (done) {
357 const data = {
358 name: 'my super name',
359 description: 'my super description',
360 tags: [ 'tag1', 't' ]
361 }
362 const attach = {
363 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
364 }
365 makePostRequest(path, server.accessToken, data, attach, done)
366 })
367
368 it('Should fail with a tag length too big', function (done) {
369 const data = {
370 name: 'my super name',
371 description: 'my super description',
372 tags: [ 'mysupertagtoolong', 'tag1' ]
373 }
374 const attach = {
375 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
376 }
377 makePostRequest(path, server.accessToken, data, attach, done)
378 })
379
380 it('Should fail with malformed tags', function (done) {
381 const data = {
382 name: 'my super name',
383 description: 'my super description',
384 tags: [ 'my tag' ]
385 }
386 const attach = {
387 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
388 }
389 makePostRequest(path, server.accessToken, data, attach, done)
390 })
391
392 it('Should fail without an input file', function (done) {
393 const data = {
394 name: 'my super name',
395 description: 'my super description',
396 tags: [ 'tag1', 'tag2' ]
397 }
398 const attach = {}
399 makePostRequest(path, server.accessToken, data, attach, done)
400 })
401
402 it('Should fail without an incorrect input file', function (done) {
403 const data = {
404 name: 'my super name',
405 description: 'my super description',
406 tags: [ 'tag1', 'tag2' ]
407 }
408 const attach = {
409 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short_fake.webm')
410 }
411 makePostRequest(path, server.accessToken, data, attach, done)
412 })
413
414 it('Should fail with a too big duration', function (done) {
415 const data = {
416 name: 'my super name',
417 description: 'my super description',
418 tags: [ 'tag1', 'tag2' ]
419 }
420 const attach = {
421 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_too_long.webm')
422 }
423 makePostRequest(path, server.accessToken, data, attach, done)
424 })
425
426 it('Should succeed with the correct parameters', function (done) {
427 const data = {
428 name: 'my super name',
429 description: 'my super description',
430 tags: [ 'tag1', 'tag2' ]
431 }
432 const attach = {
433 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
434 }
435 makePostRequest(path, server.accessToken, data, attach, function () {
436 attach.videofile = pathUtils.join(__dirname, 'fixtures', 'video_short.mp4')
437 makePostRequest(path, server.accessToken, data, attach, function () {
438 attach.videofile = pathUtils.join(__dirname, 'fixtures', 'video_short.ogv')
439 makePostRequest(path, server.accessToken, data, attach, done, 204)
440 }, false)
441 }, false)
442 })
443 })
444
445 describe('When getting a video', function () {
446 it('Should return the list of the videos with nothing', function (done) {
447 request(server.url)
448 .get(path)
449 .set('Accept', 'application/json')
450 .expect(200)
451 .expect('Content-Type', /json/)
452 .end(function (err, res) {
453 if (err) throw err
454
455 expect(res.body.data).to.be.an('array')
456 expect(res.body.data.length).to.equal(3)
457
458 done()
459 })
460 })
461
462 it('Should fail without a mongodb id', function (done) {
463 request(server.url)
464 .get(path + 'coucou')
465 .set('Accept', 'application/json')
466 .expect(400, done)
467 })
468
469 it('Should return 404 with an incorrect video', function (done) {
470 request(server.url)
471 .get(path + '123456789012345678901234')
472 .set('Accept', 'application/json')
473 .expect(404, done)
474 })
475
476 it('Should succeed with the correct parameters')
477 })
478
479 describe('When removing a video', function () {
480 it('Should have 404 with nothing', function (done) {
481 request(server.url)
482 .delete(path)
483 .set('Authorization', 'Bearer ' + server.accessToken)
484 .expect(400, done)
485 })
486
487 it('Should fail without a mongodb id', function (done) {
488 request(server.url)
489 .delete(path + 'hello')
490 .set('Authorization', 'Bearer ' + server.accessToken)
491 .expect(400, done)
492 })
493
494 it('Should fail with a video which does not exist', function (done) {
495 request(server.url)
496 .delete(path + '123456789012345678901234')
497 .set('Authorization', 'Bearer ' + server.accessToken)
498 .expect(404, done)
499 })
500
501 it('Should fail with a video of another user')
502
503 it('Should fail with a video of another pod')
504
505 it('Should succeed with the correct parameters')
506 })
507 })
508
509 describe('Of the users API', function () {
510 const path = '/api/v1/users/'
511 let userId = null
512 let userAccessToken = null
513
514 describe('When adding a new user', function () {
515 it('Should fail with a too small username', function (done) {
516 const data = {
517 username: 'ji',
518 password: 'mysuperpassword'
519 }
520
521 makePostBodyRequest(path, server.accessToken, data, done)
522 })
523
524 it('Should fail with a too long username', function (done) {
525 const data = {
526 username: 'mysuperusernamewhichisverylong',
527 password: 'mysuperpassword'
528 }
529
530 makePostBodyRequest(path, server.accessToken, data, done)
531 })
532
533 it('Should fail with an incorrect username', function (done) {
534 const data = {
535 username: 'my username',
536 password: 'mysuperpassword'
537 }
538
539 makePostBodyRequest(path, server.accessToken, data, done)
540 })
541
542 it('Should fail with a too small password', function (done) {
543 const data = {
544 username: 'myusername',
545 password: 'bla'
546 }
547
548 makePostBodyRequest(path, server.accessToken, data, done)
549 })
550
551 it('Should fail with a too long password', function (done) {
552 const data = {
553 username: 'myusername',
554 password: 'my super long password which is very very very very very very very very very very very very very very' +
555 'very very very very very very very very very very very very very very very veryv very very very very' +
556 'very very very very very very very very very very very very very very very very very very very very long'
557 }
558
559 makePostBodyRequest(path, server.accessToken, data, done)
560 })
561
562 it('Should fail with an non authenticated user', function (done) {
563 const data = {
564 username: 'myusername',
565 password: 'my super password'
566 }
567
568 makePostBodyRequest(path, 'super token', data, done, 401)
569 })
570
571 it('Should succeed with the correct params', function (done) {
572 const data = {
573 username: 'user1',
574 password: 'my super password'
575 }
576
577 makePostBodyRequest(path, server.accessToken, data, done, 204)
578 })
579
580 it('Should fail with a non admin user', function (done) {
581 server.user = {
582 username: 'user1',
583 password: 'my super password'
584 }
585
586 loginUtils.loginAndGetAccessToken(server, function (err, accessToken) {
587 if (err) throw err
588
589 userAccessToken = accessToken
590
591 const data = {
592 username: 'user2',
593 password: 'my super password'
594 }
595
596 makePostBodyRequest(path, userAccessToken, data, done, 403)
597 })
598 })
599 })
600
601 describe('When updating a user', function () {
602 before(function (done) {
603 usersUtils.getUsersList(server.url, function (err, res) {
604 if (err) throw err
605
606 userId = res.body.data[1].id
607 done()
608 })
609 })
610
611 it('Should fail with a too small password', function (done) {
612 const data = {
613 password: 'bla'
614 }
615
616 makePutBodyRequest(path + userId, userAccessToken, data, done)
617 })
618
619 it('Should fail with a too long password', function (done) {
620 const data = {
621 password: 'my super long password which is very very very very very very very very very very very very very very' +
622 'very very very very very very very very very very very very very very very veryv very very very very' +
623 'very very very very very very very very very very very very very very very very very very very very long'
624 }
625
626 makePutBodyRequest(path + userId, userAccessToken, data, done)
627 })
628
629 it('Should fail with an non authenticated user', function (done) {
630 const data = {
631 password: 'my super password'
632 }
633
634 makePutBodyRequest(path + userId, 'super token', data, done, 401)
635 })
636
637 it('Should succeed with the correct params', function (done) {
638 const data = {
639 password: 'my super password'
640 }
641
642 makePutBodyRequest(path + userId, userAccessToken, data, done, 204)
643 })
644 })
645
646 describe('When getting my information', function () {
647 it('Should fail with a non authenticated user', function (done) {
648 request(server.url)
649 .get(path + 'me')
650 .set('Authorization', 'Bearer faketoken')
651 .set('Accept', 'application/json')
652 .expect(401, done)
653 })
654
655 it('Should success with the correct parameters', function (done) {
656 request(server.url)
657 .get(path + 'me')
658 .set('Authorization', 'Bearer ' + userAccessToken)
659 .set('Accept', 'application/json')
660 .expect(200, done)
661 })
662 })
663
664 describe('When removing an user', function () {
665 it('Should fail with an incorrect username', function (done) {
666 request(server.url)
667 .delete(path + 'bla-bla')
668 .set('Authorization', 'Bearer ' + server.accessToken)
669 .expect(400, done)
670 })
671
672 it('Should return 404 with a non existing username', function (done) {
673 request(server.url)
674 .delete(path + 'qzzerg')
675 .set('Authorization', 'Bearer ' + server.accessToken)
676 .expect(404, done)
677 })
678
679 it('Should success with the correct parameters', function (done) {
680 request(server.url)
681 .delete(path + 'user1')
682 .set('Authorization', 'Bearer ' + server.accessToken)
683 .expect(204, done)
684 })
685 })
686 })
687
688 describe('Of the remote videos API', function () {
689 describe('When making a secure request', function () {
690 it('Should check a secure request')
691 })
692
693 describe('When adding a video', function () {
694 it('Should check when adding a video')
695 })
696
697 describe('When removing a video', function () {
698 it('Should check when removing a video')
699 })
700 })
701
702 after(function (done) {
703 process.kill(-server.app.pid)
704
705 // Keep the logs if the test failed
706 if (this.ok) {
707 serversUtils.flushTests(done)
708 } else {
709 done()
710 }
711 })
712 })