]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params.js
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')
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 const videosUtils
= require('../utils/videos')
15 describe('Test parameters validator', function () {
17 let userAccessToken
= null
19 // ---------------------------------------------------------------
21 before(function (done
) {
26 serversUtils
.flushTests(next
)
29 serversUtils
.runServer(1, function (server1
) {
36 loginUtils
.loginAndGetAccessToken(server
, function (err
, token
) {
38 server
.accessToken
= token
46 describe('Of the pods API', function () {
47 const path
= '/api/v1/pods/'
49 describe('When making friends', function () {
50 let userAccessToken
= null
52 before(function (done
) {
53 usersUtils
.createUser(server
.url
, server
.accessToken
, 'user1', 'password', function () {
59 loginUtils
.loginAndGetAccessToken(server
, function (err
, accessToken
) {
62 userAccessToken
= accessToken
69 describe('When making friends', function () {
71 hosts: [ 'localhost:9002' ]
74 it('Should fail without hosts', function (done
) {
76 .post(path
+ '/makefriends')
77 .set('Authorization', 'Bearer ' + server
.accessToken
)
78 .set('Accept', 'application/json')
82 it('Should fail if hosts is not an array', function (done
) {
84 .post(path
+ '/makefriends')
85 .send({ hosts: 'localhost:9002' })
86 .set('Authorization', 'Bearer ' + server
.accessToken
)
87 .set('Accept', 'application/json')
91 it('Should fail if the array is not composed by hosts', function (done
) {
93 .post(path
+ '/makefriends')
94 .send({ hosts: [ 'localhost:9002', 'localhost:coucou' ] })
95 .set('Authorization', 'Bearer ' + server
.accessToken
)
96 .set('Accept', 'application/json')
100 it('Should fail if the array is composed with http schemes', function (done
) {
102 .post(path
+ '/makefriends')
103 .send({ hosts: [ 'localhost:9002', 'http://localhost:9003' ] })
104 .set('Authorization', 'Bearer ' + server
.accessToken
)
105 .set('Accept', 'application/json')
109 it('Should fail if hosts are not unique', function (done
) {
111 .post(path
+ '/makefriends')
112 .send({ urls: [ 'localhost:9002', 'localhost:9002' ] })
113 .set('Authorization', 'Bearer ' + server
.accessToken
)
114 .set('Accept', 'application/json')
118 it('Should fail with a invalid token', function (done
) {
120 .post(path
+ '/makefriends')
122 .set('Authorization', 'Bearer faketoken')
123 .set('Accept', 'application/json')
127 it('Should fail if the user is not an administrator', function (done
) {
129 .post(path
+ '/makefriends')
131 .set('Authorization', 'Bearer ' + userAccessToken
)
132 .set('Accept', 'application/json')
137 describe('When quitting friends', function () {
138 it('Should fail with a invalid token', function (done
) {
140 .get(path
+ '/quitfriends')
141 .query({ start: 'hello' })
142 .set('Authorization', 'Bearer faketoken')
143 .set('Accept', 'application/json')
147 it('Should fail if the user is not an administrator', function (done
) {
149 .get(path
+ '/quitfriends')
150 .query({ start: 'hello' })
151 .set('Authorization', 'Bearer ' + userAccessToken
)
152 .set('Accept', 'application/json')
158 describe('When adding a pod', function () {
159 it('Should fail with nothing', function (done
) {
161 requestsUtils
.makePostBodyRequest(server
.url
, path
, null, data
, done
)
164 it('Should fail without public key', function (done
) {
168 requestsUtils
.makePostBodyRequest(server
.url
, path
, null, data
, done
)
171 it('Should fail without an host', function (done
) {
173 publicKey: 'mysuperpublickey'
175 requestsUtils
.makePostBodyRequest(server
.url
, path
, null, data
, done
)
178 it('Should fail with an incorrect host', function (done
) {
180 host: 'http://coucou.com',
181 publicKey: 'mysuperpublickey'
183 requestsUtils
.makePostBodyRequest(server
.url
, path
, null, data
, function () {
184 data
.host
= 'http://coucou'
185 requestsUtils
.makePostBodyRequest(server
.url
, path
, null, data
, function () {
187 requestsUtils
.makePostBodyRequest(server
.url
, path
, null, data
, done
)
192 it('Should succeed with the correct parameters', function (done
) {
195 publicKey: 'mysuperpublickey'
197 requestsUtils
.makePostBodyRequest(server
.url
, path
, null, data
, done
, 200)
202 describe('Of the videos API', function () {
203 const path
= '/api/v1/videos/'
205 describe('When listing a video', function () {
206 it('Should fail with a bad start pagination', function (done
) {
209 .query({ start: 'hello' })
210 .set('Accept', 'application/json')
214 it('Should fail with a bad count pagination', function (done
) {
217 .query({ count: 'hello' })
218 .set('Accept', 'application/json')
222 it('Should fail with an incorrect sort', function (done
) {
225 .query({ sort: 'hello' })
226 .set('Accept', 'application/json')
231 describe('When searching a video', function () {
232 it('Should fail with nothing', function (done
) {
234 .get(pathUtils
.join(path
, 'search'))
235 .set('Accept', 'application/json')
239 it('Should fail with a bad start pagination', function (done
) {
241 .get(pathUtils
.join(path
, 'search', 'test'))
242 .query({ start: 'hello' })
243 .set('Accept', 'application/json')
247 it('Should fail with a bad count pagination', function (done
) {
249 .get(pathUtils
.join(path
, 'search', 'test'))
250 .query({ count: 'hello' })
251 .set('Accept', 'application/json')
255 it('Should fail with an incorrect sort', function (done
) {
257 .get(pathUtils
.join(path
, 'search', 'test'))
258 .query({ sort: 'hello' })
259 .set('Accept', 'application/json')
264 describe('When adding a video', function () {
265 it('Should fail with nothing', function (done
) {
268 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
271 it('Should fail without name', function (done
) {
273 description: 'my super description',
274 tags: [ 'tag1', 'tag2' ]
277 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
279 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
282 it('Should fail with a long name', function (done
) {
284 name: 'My very very very very very very very very very very very very very very very very long name',
285 description: 'my super description',
286 tags: [ 'tag1', 'tag2' ]
289 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
291 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
294 it('Should fail without description', function (done
) {
296 name: 'my super name',
297 tags: [ 'tag1', 'tag2' ]
300 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
302 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
305 it('Should fail with a long description', function (done
) {
307 name: 'my super name',
308 description: 'my super description which is very very very very very very very very very very very very very very' +
309 'very very very very very very very very very very very very very very very very very very very very very' +
310 'very very very very very very very very very very very very very very very long',
311 tags: [ 'tag1', 'tag2' ]
314 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
316 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
319 it('Should fail without tags', function (done
) {
321 name: 'my super name',
322 description: 'my super description'
325 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
327 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
330 it('Should fail with too many tags', function (done
) {
332 name: 'my super name',
333 description: 'my super description',
334 tags: [ 'tag1', 'tag2', 'tag3', 'tag4' ]
337 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
339 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
342 it('Should fail with not enough tags', function (done
) {
344 name: 'my super name',
345 description: 'my super description',
349 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
351 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
354 it('Should fail with a tag length too low', function (done
) {
356 name: 'my super name',
357 description: 'my super description',
358 tags: [ 'tag1', 't' ]
361 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
363 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
366 it('Should fail with a tag length too big', function (done
) {
368 name: 'my super name',
369 description: 'my super description',
370 tags: [ 'mysupertagtoolong', 'tag1' ]
373 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
375 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
378 it('Should fail with malformed tags', function (done
) {
380 name: 'my super name',
381 description: 'my super description',
385 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
387 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
390 it('Should fail without an input file', function (done
) {
392 name: 'my super name',
393 description: 'my super description',
394 tags: [ 'tag1', 'tag2' ]
397 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
400 it('Should fail without an incorrect input file', function (done
) {
402 name: 'my super name',
403 description: 'my super description',
404 tags: [ 'tag1', 'tag2' ]
407 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short_fake.webm')
409 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
412 it('Should fail with a too big duration', function (done
) {
414 name: 'my super name',
415 description: 'my super description',
416 tags: [ 'tag1', 'tag2' ]
419 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_too_long.webm')
421 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
424 it('Should succeed with the correct parameters', function (done
) {
426 name: 'my super name',
427 description: 'my super description',
428 tags: [ 'tag1', 'tag2' ]
431 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
433 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, function () {
434 attach
.videofile
= pathUtils
.join(__dirname
, 'fixtures', 'video_short.mp4')
435 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, function () {
436 attach
.videofile
= pathUtils
.join(__dirname
, 'fixtures', 'video_short.ogv')
437 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
, 204)
443 describe('When updating a video', function () {
446 before(function (done
) {
447 videosUtils
.getVideosList(server
.url
, function (err
, res
) {
450 videoId
= res
.body
.data
[0].id
456 it('Should fail with nothing', function (done
) {
458 requestsUtils
.makePutBodyRequest(server
.url
, path
+ videoId
, server
.accessToken
, data
, done
)
461 it('Should fail without a valid uuid', function (done
) {
463 description: 'my super description',
464 tags: [ 'tag1', 'tag2' ]
466 requestsUtils
.makePutBodyRequest(server
.url
, path
+ 'blabla', server
.accessToken
, data
, done
)
469 it('Should fail with an unknown id', function (done
) {
471 description: 'my super description',
472 tags: [ 'tag1', 'tag2' ]
474 requestsUtils
.makePutBodyRequest(server
.url
, path
+ '4da6fde3-88f7-4d16-b119-108df5630b06', server
.accessToken
, data
, done
)
477 it('Should fail with a long name', function (done
) {
479 name: 'My very very very very very very very very very very very very very very very very long name',
480 description: 'my super description',
481 tags: [ 'tag1', 'tag2' ]
483 requestsUtils
.makePutBodyRequest(server
.url
, path
+ videoId
, server
.accessToken
, data
, done
)
486 it('Should fail with a long description', function (done
) {
488 name: 'my super name',
489 description: 'my super description which is very very very very very very very very very very very very very very' +
490 'very very very very very very very very very very very very very very very very very very very very very' +
491 'very very very very very very very very very very very very very very very long',
492 tags: [ 'tag1', 'tag2' ]
494 requestsUtils
.makePutBodyRequest(server
.url
, path
+ videoId
, server
.accessToken
, data
, done
)
497 it('Should fail with too many tags', function (done
) {
499 name: 'my super name',
500 description: 'my super description',
501 tags: [ 'tag1', 'tag2', 'tag3', 'tag4' ]
503 requestsUtils
.makePutBodyRequest(server
.url
, path
+ videoId
, server
.accessToken
, data
, done
)
506 it('Should fail with not enough tags', function (done
) {
508 name: 'my super name',
509 description: 'my super description',
512 requestsUtils
.makePutBodyRequest(server
.url
, path
+ videoId
, server
.accessToken
, data
, done
)
515 it('Should fail with a tag length too low', function (done
) {
517 name: 'my super name',
518 description: 'my super description',
519 tags: [ 'tag1', 't' ]
521 requestsUtils
.makePutBodyRequest(server
.url
, path
+ videoId
, server
.accessToken
, data
, done
)
524 it('Should fail with a tag length too big', function (done
) {
526 name: 'my super name',
527 description: 'my super description',
528 tags: [ 'mysupertagtoolong', 'tag1' ]
530 requestsUtils
.makePutBodyRequest(server
.url
, path
+ videoId
, server
.accessToken
, data
, done
)
533 it('Should fail with malformed tags', function (done
) {
535 name: 'my super name',
536 description: 'my super description',
539 requestsUtils
.makePutBodyRequest(server
.url
, path
+ videoId
, server
.accessToken
, data
, done
)
543 describe('When getting a video', function () {
544 it('Should return the list of the videos with nothing', function (done
) {
547 .set('Accept', 'application/json')
549 .expect('Content-Type', /json/)
550 .end(function (err
, res
) {
553 expect(res
.body
.data
).to
.be
.an('array')
554 expect(res
.body
.data
.length
).to
.equal(3)
560 it('Should fail without a correct uuid', function (done
) {
562 .get(path
+ 'coucou')
563 .set('Accept', 'application/json')
567 it('Should return 404 with an incorrect video', function (done
) {
569 .get(path
+ '4da6fde3-88f7-4d16-b119-108df5630b06')
570 .set('Accept', 'application/json')
574 it('Should succeed with the correct parameters')
577 describe('When removing a video', function () {
578 it('Should have 404 with nothing', function (done
) {
581 .set('Authorization', 'Bearer ' + server
.accessToken
)
585 it('Should fail without a correct uuid', function (done
) {
587 .delete(path
+ 'hello')
588 .set('Authorization', 'Bearer ' + server
.accessToken
)
592 it('Should fail with a video which does not exist', function (done
) {
594 .delete(path
+ '4da6fde3-88f7-4d16-b119-108df5630b06')
595 .set('Authorization', 'Bearer ' + server
.accessToken
)
599 it('Should fail with a video of another user')
601 it('Should fail with a video of another pod')
603 it('Should succeed with the correct parameters')
607 describe('Of the users API', function () {
608 const path
= '/api/v1/users/'
612 describe('When listing users', function () {
613 it('Should fail with a bad start pagination', function (done
) {
616 .query({ start: 'hello' })
617 .set('Accept', 'application/json')
621 it('Should fail with a bad count pagination', function (done
) {
624 .query({ count: 'hello' })
625 .set('Accept', 'application/json')
629 it('Should fail with an incorrect sort', function (done
) {
632 .query({ sort: 'hello' })
633 .set('Accept', 'application/json')
638 describe('When adding a new user', function () {
639 it('Should fail with a too small username', function (done
) {
642 password: 'mysuperpassword'
645 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
)
648 it('Should fail with a too long username', function (done
) {
650 username: 'mysuperusernamewhichisverylong',
651 password: 'mysuperpassword'
654 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
)
657 it('Should fail with an incorrect username', function (done
) {
659 username: 'my username',
660 password: 'mysuperpassword'
663 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
)
666 it('Should fail with a too small password', function (done
) {
668 username: 'myusername',
672 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
)
675 it('Should fail with a too long password', function (done
) {
677 username: 'myusername',
678 password: 'my super long password which is very very very very very very very very very very very very very very' +
679 'very very very very very very very very very very very very very very very veryv very very very very' +
680 'very very very very very very very very very very very very very very very very very very very very long'
683 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
)
686 it('Should fail with an non authenticated user', function (done
) {
688 username: 'myusername',
689 password: 'my super password'
692 requestsUtils
.makePostBodyRequest(server
.url
, path
, 'super token', data
, done
, 401)
695 it('Should fail if we add a user with the same username', function (done
) {
698 password: 'my super password'
701 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
, 409)
704 it('Should succeed with the correct params', function (done
) {
707 password: 'my super password'
710 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
, 204)
713 it('Should fail with a non admin user', function (done
) {
719 loginUtils
.loginAndGetAccessToken(server
, function (err
, accessToken
) {
722 userAccessToken
= accessToken
726 password: 'my super password'
729 requestsUtils
.makePostBodyRequest(server
.url
, path
, userAccessToken
, data
, done
, 403)
734 describe('When updating a user', function () {
735 before(function (done
) {
736 usersUtils
.getUsersList(server
.url
, function (err
, res
) {
739 userId
= res
.body
.data
[1].id
740 rootId
= res
.body
.data
[2].id
745 it('Should fail with a too small password', function (done
) {
750 requestsUtils
.makePutBodyRequest(server
.url
, path
+ userId
, userAccessToken
, data
, done
)
753 it('Should fail with a too long password', function (done
) {
755 password: 'my super long password which is very very very very very very very very very very very very very very' +
756 'very very very very very very very very very very very very very very very veryv very very very very' +
757 'very very very very very very very very very very very very very very very very very very very very long'
760 requestsUtils
.makePutBodyRequest(server
.url
, path
+ userId
, userAccessToken
, data
, done
)
763 it('Should fail with an non authenticated user', function (done
) {
765 password: 'my super password'
768 requestsUtils
.makePutBodyRequest(server
.url
, path
+ userId
, 'super token', data
, done
, 401)
771 it('Should succeed with the correct params', function (done
) {
773 password: 'my super password'
776 requestsUtils
.makePutBodyRequest(server
.url
, path
+ userId
, userAccessToken
, data
, done
, 204)
780 describe('When getting my information', function () {
781 it('Should fail with a non authenticated user', function (done
) {
784 .set('Authorization', 'Bearer faketoken')
785 .set('Accept', 'application/json')
789 it('Should success with the correct parameters', function (done
) {
792 .set('Authorization', 'Bearer ' + userAccessToken
)
793 .set('Accept', 'application/json')
798 describe('When removing an user', function () {
799 it('Should fail with an incorrect id', function (done
) {
801 .delete(path
+ 'bla-bla')
802 .set('Authorization', 'Bearer ' + server
.accessToken
)
806 it('Should fail with the root user', function (done
) {
808 .delete(path
+ rootId
)
809 .set('Authorization', 'Bearer ' + server
.accessToken
)
813 it('Should return 404 with a non existing id', function (done
) {
816 .set('Authorization', 'Bearer ' + server
.accessToken
)
822 describe('Of the remote videos API', function () {
823 describe('When making a secure request', function () {
824 it('Should check a secure request')
827 describe('When adding a video', function () {
828 it('Should check when adding a video')
831 describe('When removing a video', function () {
832 it('Should check when removing a video')
836 describe('Of the requests API', function () {
837 const path
= '/api/v1/requests/stats'
839 it('Should fail with an non authenticated user', function (done
) {
842 .set('Accept', 'application/json')
846 it('Should fail with a non admin user', function (done
) {
849 .set('Authorization', 'Bearer ' + userAccessToken
)
850 .set('Accept', 'application/json')
855 after(function (done
) {
856 process
.kill(-server
.app
.pid
)
858 // Keep the logs if the test failed
860 serversUtils
.flushTests(done
)