]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params.js
d9e51770cc82200e743fdf06baef8a816b2d0a94
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')
14 describe('Test parameters validator', function () {
16 let userAccessToken
= null
18 // ---------------------------------------------------------------
20 before(function (done
) {
25 serversUtils
.flushTests(next
)
28 serversUtils
.runServer(1, function (server1
) {
35 loginUtils
.loginAndGetAccessToken(server
, function (err
, token
) {
37 server
.accessToken
= token
45 describe('Of the pods API', function () {
46 const path
= '/api/v1/pods/'
48 describe('When making friends', function () {
49 let userAccessToken
= null
51 before(function (done
) {
52 usersUtils
.createUser(server
.url
, server
.accessToken
, 'user1', 'password', function () {
58 loginUtils
.loginAndGetAccessToken(server
, function (err
, accessToken
) {
61 userAccessToken
= accessToken
68 describe('When making friends', function () {
70 hosts: [ 'localhost:9002' ]
73 it('Should fail without hosts', function (done
) {
75 .post(path
+ '/makefriends')
76 .set('Authorization', 'Bearer ' + server
.accessToken
)
77 .set('Accept', 'application/json')
81 it('Should fail if hosts is not an array', function (done
) {
83 .post(path
+ '/makefriends')
84 .send({ hosts: 'localhost:9002' })
85 .set('Authorization', 'Bearer ' + server
.accessToken
)
86 .set('Accept', 'application/json')
90 it('Should fail if the array is not composed by hosts', function (done
) {
92 .post(path
+ '/makefriends')
93 .send({ hosts: [ 'localhost:9002', 'localhost:coucou' ] })
94 .set('Authorization', 'Bearer ' + server
.accessToken
)
95 .set('Accept', 'application/json')
99 it('Should fail if the array is composed with http schemes', function (done
) {
101 .post(path
+ '/makefriends')
102 .send({ hosts: [ 'localhost:9002', 'http://localhost:9003' ] })
103 .set('Authorization', 'Bearer ' + server
.accessToken
)
104 .set('Accept', 'application/json')
108 it('Should fail if hosts are not unique', function (done
) {
110 .post(path
+ '/makefriends')
111 .send({ urls: [ 'localhost:9002', 'localhost:9002' ] })
112 .set('Authorization', 'Bearer ' + server
.accessToken
)
113 .set('Accept', 'application/json')
117 it('Should fail with a invalid token', function (done
) {
119 .post(path
+ '/makefriends')
121 .set('Authorization', 'Bearer faketoken')
122 .set('Accept', 'application/json')
126 it('Should fail if the user is not an administrator', function (done
) {
128 .post(path
+ '/makefriends')
130 .set('Authorization', 'Bearer ' + userAccessToken
)
131 .set('Accept', 'application/json')
136 describe('When quitting friends', function () {
137 it('Should fail with a invalid token', function (done
) {
139 .get(path
+ '/quitfriends')
140 .query({ start: 'hello' })
141 .set('Authorization', 'Bearer faketoken')
142 .set('Accept', 'application/json')
146 it('Should fail if the user is not an administrator', function (done
) {
148 .get(path
+ '/quitfriends')
149 .query({ start: 'hello' })
150 .set('Authorization', 'Bearer ' + userAccessToken
)
151 .set('Accept', 'application/json')
157 describe('When adding a pod', function () {
158 it('Should fail with nothing', function (done
) {
160 requestsUtils
.makePostBodyRequest(server
.url
, path
, null, data
, done
)
163 it('Should fail without public key', function (done
) {
167 requestsUtils
.makePostBodyRequest(server
.url
, path
, null, data
, done
)
170 it('Should fail without an host', function (done
) {
172 publicKey: 'mysuperpublickey'
174 requestsUtils
.makePostBodyRequest(server
.url
, path
, null, data
, done
)
177 it('Should fail with an incorrect host', function (done
) {
179 host: 'http://coucou.com',
180 publicKey: 'mysuperpublickey'
182 requestsUtils
.makePostBodyRequest(server
.url
, path
, null, data
, function () {
183 data
.host
= 'http://coucou'
184 requestsUtils
.makePostBodyRequest(server
.url
, path
, null, data
, function () {
186 requestsUtils
.makePostBodyRequest(server
.url
, path
, null, data
, done
)
191 it('Should succeed with the correct parameters', function (done
) {
194 publicKey: 'mysuperpublickey'
196 requestsUtils
.makePostBodyRequest(server
.url
, path
, null, data
, done
, 200)
201 describe('Of the videos API', function () {
202 const path
= '/api/v1/videos/'
204 describe('When listing a video', function () {
205 it('Should fail with a bad start pagination', function (done
) {
208 .query({ start: 'hello' })
209 .set('Accept', 'application/json')
213 it('Should fail with a bad count pagination', function (done
) {
216 .query({ count: 'hello' })
217 .set('Accept', 'application/json')
221 it('Should fail with an incorrect sort', function (done
) {
224 .query({ sort: 'hello' })
225 .set('Accept', 'application/json')
230 describe('When searching a video', function () {
231 it('Should fail with nothing', function (done
) {
233 .get(pathUtils
.join(path
, 'search'))
234 .set('Accept', 'application/json')
238 it('Should fail with a bad start pagination', function (done
) {
240 .get(pathUtils
.join(path
, 'search', 'test'))
241 .query({ start: 'hello' })
242 .set('Accept', 'application/json')
246 it('Should fail with a bad count pagination', function (done
) {
248 .get(pathUtils
.join(path
, 'search', 'test'))
249 .query({ count: 'hello' })
250 .set('Accept', 'application/json')
254 it('Should fail with an incorrect sort', function (done
) {
256 .get(pathUtils
.join(path
, 'search', 'test'))
257 .query({ sort: 'hello' })
258 .set('Accept', 'application/json')
263 describe('When adding a video', function () {
264 it('Should fail with nothing', function (done
) {
267 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
270 it('Should fail without name', function (done
) {
272 description: 'my super description',
273 tags: [ 'tag1', 'tag2' ]
276 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
278 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
281 it('Should fail with a long name', function (done
) {
283 name: 'My very very very very very very very very very very very very very very very very long name',
284 description: 'my super description',
285 tags: [ 'tag1', 'tag2' ]
288 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
290 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
293 it('Should fail without description', function (done
) {
295 name: 'my super name',
296 tags: [ 'tag1', 'tag2' ]
299 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
301 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
304 it('Should fail with a long description', function (done
) {
306 name: 'my super name',
307 description: 'my super description which is very very very very very very very very very very very very very very' +
308 'very very very very very very very 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 long',
310 tags: [ 'tag1', 'tag2' ]
313 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
315 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
318 it('Should fail without tags', function (done
) {
320 name: 'my super name',
321 description: 'my super description'
324 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
326 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
329 it('Should fail with too many tags', function (done
) {
331 name: 'my super name',
332 description: 'my super description',
333 tags: [ 'tag1', 'tag2', 'tag3', 'tag4' ]
336 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
338 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
341 it('Should fail with not enough tags', function (done
) {
343 name: 'my super name',
344 description: 'my super description',
348 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
350 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
353 it('Should fail with a tag length too low', function (done
) {
355 name: 'my super name',
356 description: 'my super description',
357 tags: [ 'tag1', 't' ]
360 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
362 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
365 it('Should fail with a tag length too big', function (done
) {
367 name: 'my super name',
368 description: 'my super description',
369 tags: [ 'mysupertagtoolong', 'tag1' ]
372 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
374 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
377 it('Should fail with malformed tags', function (done
) {
379 name: 'my super name',
380 description: 'my super description',
384 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
386 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
389 it('Should fail without an input file', function (done
) {
391 name: 'my super name',
392 description: 'my super description',
393 tags: [ 'tag1', 'tag2' ]
396 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
399 it('Should fail without an incorrect input file', function (done
) {
401 name: 'my super name',
402 description: 'my super description',
403 tags: [ 'tag1', 'tag2' ]
406 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short_fake.webm')
408 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
411 it('Should fail with a too big duration', function (done
) {
413 name: 'my super name',
414 description: 'my super description',
415 tags: [ 'tag1', 'tag2' ]
418 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_too_long.webm')
420 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
423 it('Should succeed with the correct parameters', function (done
) {
425 name: 'my super name',
426 description: 'my super description',
427 tags: [ 'tag1', 'tag2' ]
430 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
432 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, function () {
433 attach
.videofile
= pathUtils
.join(__dirname
, 'fixtures', 'video_short.mp4')
434 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, function () {
435 attach
.videofile
= pathUtils
.join(__dirname
, 'fixtures', 'video_short.ogv')
436 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
, 204)
442 describe('When getting a video', function () {
443 it('Should return the list of the videos with nothing', function (done
) {
446 .set('Accept', 'application/json')
448 .expect('Content-Type', /json/)
449 .end(function (err
, res
) {
452 expect(res
.body
.data
).to
.be
.an('array')
453 expect(res
.body
.data
.length
).to
.equal(3)
459 it('Should fail without a mongodb id', function (done
) {
461 .get(path
+ 'coucou')
462 .set('Accept', 'application/json')
466 it('Should return 404 with an incorrect video', function (done
) {
468 .get(path
+ '4da6fde3-88f7-4d16-b119-108df5630b06')
469 .set('Accept', 'application/json')
473 it('Should succeed with the correct parameters')
476 describe('When removing a video', function () {
477 it('Should have 404 with nothing', function (done
) {
480 .set('Authorization', 'Bearer ' + server
.accessToken
)
484 it('Should fail without a mongodb id', function (done
) {
486 .delete(path
+ 'hello')
487 .set('Authorization', 'Bearer ' + server
.accessToken
)
491 it('Should fail with a video which does not exist', function (done
) {
493 .delete(path
+ '4da6fde3-88f7-4d16-b119-108df5630b06')
494 .set('Authorization', 'Bearer ' + server
.accessToken
)
498 it('Should fail with a video of another user')
500 it('Should fail with a video of another pod')
502 it('Should succeed with the correct parameters')
506 describe('Of the users API', function () {
507 const path
= '/api/v1/users/'
511 describe('When listing users', function () {
512 it('Should fail with a bad start pagination', function (done
) {
515 .query({ start: 'hello' })
516 .set('Accept', 'application/json')
520 it('Should fail with a bad count pagination', function (done
) {
523 .query({ count: 'hello' })
524 .set('Accept', 'application/json')
528 it('Should fail with an incorrect sort', function (done
) {
531 .query({ sort: 'hello' })
532 .set('Accept', 'application/json')
537 describe('When adding a new user', function () {
538 it('Should fail with a too small username', function (done
) {
541 password: 'mysuperpassword'
544 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
)
547 it('Should fail with a too long username', function (done
) {
549 username: 'mysuperusernamewhichisverylong',
550 password: 'mysuperpassword'
553 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
)
556 it('Should fail with an incorrect username', function (done
) {
558 username: 'my username',
559 password: 'mysuperpassword'
562 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
)
565 it('Should fail with a too small password', function (done
) {
567 username: 'myusername',
571 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
)
574 it('Should fail with a too long password', function (done
) {
576 username: 'myusername',
577 password: 'my super long password which is very very very very very very very very very very very very very very' +
578 'very very very very very very very very very very very very very very very veryv very very very very' +
579 'very very very very very very very very very very very very very very very very very very very very long'
582 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
)
585 it('Should fail with an non authenticated user', function (done
) {
587 username: 'myusername',
588 password: 'my super password'
591 requestsUtils
.makePostBodyRequest(server
.url
, path
, 'super token', data
, done
, 401)
594 it('Should fail if we add a user with the same username', function (done
) {
597 password: 'my super password'
600 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
, 409)
603 it('Should succeed with the correct params', function (done
) {
606 password: 'my super password'
609 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
, 204)
612 it('Should fail with a non admin user', function (done
) {
618 loginUtils
.loginAndGetAccessToken(server
, function (err
, accessToken
) {
621 userAccessToken
= accessToken
625 password: 'my super password'
628 requestsUtils
.makePostBodyRequest(server
.url
, path
, userAccessToken
, data
, done
, 403)
633 describe('When updating a user', function () {
634 before(function (done
) {
635 usersUtils
.getUsersList(server
.url
, function (err
, res
) {
638 userId
= res
.body
.data
[1].id
639 rootId
= res
.body
.data
[2].id
644 it('Should fail with a too small password', function (done
) {
649 requestsUtils
.makePutBodyRequest(server
.url
, path
+ userId
, userAccessToken
, data
, done
)
652 it('Should fail with a too long password', function (done
) {
654 password: 'my super long password which is very very very very very very very very very very very very very very' +
655 'very very very very very very very very very very very very very very very veryv very very very very' +
656 'very very very very very very very very very very very very very very very very very very very very long'
659 requestsUtils
.makePutBodyRequest(server
.url
, path
+ userId
, userAccessToken
, data
, done
)
662 it('Should fail with an non authenticated user', function (done
) {
664 password: 'my super password'
667 requestsUtils
.makePutBodyRequest(server
.url
, path
+ userId
, 'super token', data
, done
, 401)
670 it('Should succeed with the correct params', function (done
) {
672 password: 'my super password'
675 requestsUtils
.makePutBodyRequest(server
.url
, path
+ userId
, userAccessToken
, data
, done
, 204)
679 describe('When getting my information', function () {
680 it('Should fail with a non authenticated user', function (done
) {
683 .set('Authorization', 'Bearer faketoken')
684 .set('Accept', 'application/json')
688 it('Should success with the correct parameters', function (done
) {
691 .set('Authorization', 'Bearer ' + userAccessToken
)
692 .set('Accept', 'application/json')
697 describe('When removing an user', function () {
698 it('Should fail with an incorrect id', function (done
) {
700 .delete(path
+ 'bla-bla')
701 .set('Authorization', 'Bearer ' + server
.accessToken
)
705 it('Should fail with the root user', function (done
) {
707 .delete(path
+ rootId
)
708 .set('Authorization', 'Bearer ' + server
.accessToken
)
712 it('Should return 404 with a non existing id', function (done
) {
715 .set('Authorization', 'Bearer ' + server
.accessToken
)
721 describe('Of the remote videos API', function () {
722 describe('When making a secure request', function () {
723 it('Should check a secure request')
726 describe('When adding a video', function () {
727 it('Should check when adding a video')
730 describe('When removing a video', function () {
731 it('Should check when removing a video')
735 describe('Of the requests API', function () {
736 const path
= '/api/v1/requests/stats'
738 it('Should fail with an non authenticated user', function (done
) {
741 .set('Accept', 'application/json')
745 it('Should fail with a non admin user', function (done
) {
748 .set('Authorization', 'Bearer ' + userAccessToken
)
749 .set('Accept', 'application/json')
754 after(function (done
) {
755 process
.kill(-server
.app
.pid
)
757 // Keep the logs if the test failed
759 serversUtils
.flushTests(done
)