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