]>
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')
14 describe('Test parameters validator', function () {
17 // ---------------------------------------------------------------
19 before(function (done
) {
24 serversUtils
.flushTests(next
)
27 serversUtils
.runServer(1, function (server1
) {
34 loginUtils
.loginAndGetAccessToken(server
, function (err
, token
) {
36 server
.accessToken
= token
44 describe('Of the pods API', function () {
45 const path
= '/api/v1/pods/'
47 describe('When making friends', function () {
48 let userAccessToken
= null
50 before(function (done
) {
51 usersUtils
.createUser(server
.url
, server
.accessToken
, 'user1', 'password', function () {
57 loginUtils
.loginAndGetAccessToken(server
, function (err
, accessToken
) {
60 userAccessToken
= accessToken
67 describe('When making friends', function () {
69 urls: [ 'http://localhost:9002' ]
72 it('Should fail without urls', function (done
) {
74 .post(path
+ '/makefriends')
75 .set('Authorization', 'Bearer ' + server
.accessToken
)
76 .set('Accept', 'application/json')
80 it('Should fail with urls is not an array', function (done
) {
82 .post(path
+ '/makefriends')
83 .send({ urls: 'http://localhost:9002' })
84 .set('Authorization', 'Bearer ' + server
.accessToken
)
85 .set('Accept', 'application/json')
89 it('Should fail if the array is not composed by urls', function (done
) {
91 .post(path
+ '/makefriends')
92 .send({ urls: [ 'http://localhost:9002', 'localhost:coucou' ] })
93 .set('Authorization', 'Bearer ' + server
.accessToken
)
94 .set('Accept', 'application/json')
98 it('Should fail if urls are not unique', function (done
) {
100 .post(path
+ '/makefriends')
101 .send({ urls: [ 'http://localhost:9002', 'http://localhost:9002' ] })
102 .set('Authorization', 'Bearer ' + server
.accessToken
)
103 .set('Accept', 'application/json')
107 it('Should fail with a invalid token', function (done
) {
109 .post(path
+ '/makefriends')
111 .set('Authorization', 'Bearer faketoken')
112 .set('Accept', 'application/json')
116 it('Should fail if the user is not an administrator', function (done
) {
118 .post(path
+ '/makefriends')
120 .set('Authorization', 'Bearer ' + userAccessToken
)
121 .set('Accept', 'application/json')
126 describe('When quitting friends', function () {
127 it('Should fail with a invalid token', function (done
) {
129 .get(path
+ '/quitfriends')
130 .query({ start: 'hello' })
131 .set('Authorization', 'Bearer faketoken')
132 .set('Accept', 'application/json')
136 it('Should fail if the user is not an administrator', function (done
) {
138 .get(path
+ '/quitfriends')
139 .query({ start: 'hello' })
140 .set('Authorization', 'Bearer ' + userAccessToken
)
141 .set('Accept', 'application/json')
147 describe('When adding a pod', function () {
148 it('Should fail with nothing', function (done
) {
150 requestsUtils
.makePostBodyRequest(server
.url
, path
, null, data
, done
)
153 it('Should fail without public key', function (done
) {
155 url: 'http://coucou.com'
157 requestsUtils
.makePostBodyRequest(server
.url
, path
, null, data
, done
)
160 it('Should fail without an url', function (done
) {
162 publicKey: 'mysuperpublickey'
164 requestsUtils
.makePostBodyRequest(server
.url
, path
, null, data
, done
)
167 it('Should fail with an incorrect url', function (done
) {
170 publicKey: 'mysuperpublickey'
172 requestsUtils
.makePostBodyRequest(server
.url
, path
, null, data
, function () {
173 data
.url
= 'http://coucou'
174 requestsUtils
.makePostBodyRequest(server
.url
, path
, null, data
, function () {
176 requestsUtils
.makePostBodyRequest(server
.url
, path
, null, data
, done
)
181 it('Should succeed with the correct parameters', function (done
) {
183 url: 'http://coucou.com',
184 publicKey: 'mysuperpublickey'
186 requestsUtils
.makePostBodyRequest(server
.url
, path
, null, data
, done
, 200)
191 describe('Of the videos API', function () {
192 const path
= '/api/v1/videos/'
194 describe('When listing a video', function () {
195 it('Should fail with a bad start pagination', function (done
) {
198 .query({ start: 'hello' })
199 .set('Accept', 'application/json')
203 it('Should fail with a bad count pagination', function (done
) {
206 .query({ count: 'hello' })
207 .set('Accept', 'application/json')
211 it('Should fail with an incorrect sort', function (done
) {
214 .query({ sort: 'hello' })
215 .set('Accept', 'application/json')
220 describe('When searching a video', function () {
221 it('Should fail with nothing', function (done
) {
223 .get(pathUtils
.join(path
, 'search'))
224 .set('Accept', 'application/json')
228 it('Should fail with a bad start pagination', function (done
) {
230 .get(pathUtils
.join(path
, 'search', 'test'))
231 .query({ start: 'hello' })
232 .set('Accept', 'application/json')
236 it('Should fail with a bad count pagination', function (done
) {
238 .get(pathUtils
.join(path
, 'search', 'test'))
239 .query({ count: 'hello' })
240 .set('Accept', 'application/json')
244 it('Should fail with an incorrect sort', function (done
) {
246 .get(pathUtils
.join(path
, 'search', 'test'))
247 .query({ sort: 'hello' })
248 .set('Accept', 'application/json')
253 describe('When adding a video', function () {
254 it('Should fail with nothing', function (done
) {
257 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
260 it('Should fail without name', function (done
) {
262 description: 'my super description',
263 tags: [ 'tag1', 'tag2' ]
266 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
268 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
271 it('Should fail with a long name', function (done
) {
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' ]
278 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
280 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
283 it('Should fail without description', function (done
) {
285 name: 'my super name',
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 with a long description', function (done
) {
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' ]
303 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
305 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
308 it('Should fail without tags', function (done
) {
310 name: 'my super name',
311 description: 'my super description'
314 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
316 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
319 it('Should fail with too many tags', function (done
) {
321 name: 'my super name',
322 description: 'my super description',
323 tags: [ 'tag1', 'tag2', 'tag3', 'tag4' ]
326 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
328 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
331 it('Should fail with not enough tags', function (done
) {
333 name: 'my super name',
334 description: 'my super description',
338 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
340 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
343 it('Should fail with a tag length too low', function (done
) {
345 name: 'my super name',
346 description: 'my super description',
347 tags: [ 'tag1', 't' ]
350 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
352 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
355 it('Should fail with a tag length too big', function (done
) {
357 name: 'my super name',
358 description: 'my super description',
359 tags: [ 'mysupertagtoolong', 'tag1' ]
362 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
364 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
367 it('Should fail with malformed tags', function (done
) {
369 name: 'my super name',
370 description: 'my super description',
374 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
376 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
379 it('Should fail without an input file', function (done
) {
381 name: 'my super name',
382 description: 'my super description',
383 tags: [ 'tag1', 'tag2' ]
386 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
389 it('Should fail without an incorrect input file', function (done
) {
391 name: 'my super name',
392 description: 'my super description',
393 tags: [ 'tag1', 'tag2' ]
396 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short_fake.webm')
398 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
401 it('Should fail with a too big duration', function (done
) {
403 name: 'my super name',
404 description: 'my super description',
405 tags: [ 'tag1', 'tag2' ]
408 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_too_long.webm')
410 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
413 it('Should succeed with the correct parameters', function (done
) {
415 name: 'my super name',
416 description: 'my super description',
417 tags: [ 'tag1', 'tag2' ]
420 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
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)
432 describe('When getting a video', function () {
433 it('Should return the list of the videos with nothing', function (done
) {
436 .set('Accept', 'application/json')
438 .expect('Content-Type', /json/)
439 .end(function (err
, res
) {
442 expect(res
.body
.data
).to
.be
.an('array')
443 expect(res
.body
.data
.length
).to
.equal(3)
449 it('Should fail without a mongodb id', function (done
) {
451 .get(path
+ 'coucou')
452 .set('Accept', 'application/json')
456 it('Should return 404 with an incorrect video', function (done
) {
458 .get(path
+ '123456789012345678901234')
459 .set('Accept', 'application/json')
463 it('Should succeed with the correct parameters')
466 describe('When removing a video', function () {
467 it('Should have 404 with nothing', function (done
) {
470 .set('Authorization', 'Bearer ' + server
.accessToken
)
474 it('Should fail without a mongodb id', function (done
) {
476 .delete(path
+ 'hello')
477 .set('Authorization', 'Bearer ' + server
.accessToken
)
481 it('Should fail with a video which does not exist', function (done
) {
483 .delete(path
+ '123456789012345678901234')
484 .set('Authorization', 'Bearer ' + server
.accessToken
)
488 it('Should fail with a video of another user')
490 it('Should fail with a video of another pod')
492 it('Should succeed with the correct parameters')
496 describe('Of the users API', function () {
497 const path
= '/api/v1/users/'
499 let userAccessToken
= null
501 describe('When listing users', function () {
502 it('Should fail with a bad start pagination', function (done
) {
505 .query({ start: 'hello' })
506 .set('Accept', 'application/json')
510 it('Should fail with a bad count pagination', function (done
) {
513 .query({ count: 'hello' })
514 .set('Accept', 'application/json')
518 it('Should fail with an incorrect sort', function (done
) {
521 .query({ sort: 'hello' })
522 .set('Accept', 'application/json')
527 describe('When adding a new user', function () {
528 it('Should fail with a too small username', function (done
) {
531 password: 'mysuperpassword'
534 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
)
537 it('Should fail with a too long username', function (done
) {
539 username: 'mysuperusernamewhichisverylong',
540 password: 'mysuperpassword'
543 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
)
546 it('Should fail with an incorrect username', function (done
) {
548 username: 'my username',
549 password: 'mysuperpassword'
552 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
)
555 it('Should fail with a too small password', function (done
) {
557 username: 'myusername',
561 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
)
564 it('Should fail with a too long password', function (done
) {
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'
572 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
)
575 it('Should fail with an non authenticated user', function (done
) {
577 username: 'myusername',
578 password: 'my super password'
581 requestsUtils
.makePostBodyRequest(server
.url
, path
, 'super token', data
, done
, 401)
584 it('Should succeed with the correct params', function (done
) {
587 password: 'my super password'
590 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
, 204)
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
) {
597 password: 'my super password'
600 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
, 409)
604 it('Should fail with a non admin user', function (done
) {
607 password: 'my super password'
610 loginUtils
.loginAndGetAccessToken(server
, function (err
, accessToken
) {
613 userAccessToken
= accessToken
617 password: 'my super password'
620 requestsUtils
.makePostBodyRequest(server
.url
, path
, userAccessToken
, data
, done
, 403)
625 describe('When updating a user', function () {
626 before(function (done
) {
627 usersUtils
.getUsersList(server
.url
, function (err
, res
) {
630 userId
= res
.body
.data
[1].id
635 it('Should fail with a too small password', function (done
) {
640 requestsUtils
.makePutBodyRequest(server
.url
, path
+ userId
, userAccessToken
, data
, done
)
643 it('Should fail with a too long password', function (done
) {
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'
650 requestsUtils
.makePutBodyRequest(server
.url
, path
+ userId
, userAccessToken
, data
, done
)
653 it('Should fail with an non authenticated user', function (done
) {
655 password: 'my super password'
658 requestsUtils
.makePutBodyRequest(server
.url
, path
+ userId
, 'super token', data
, done
, 401)
661 it('Should succeed with the correct params', function (done
) {
663 password: 'my super password'
666 requestsUtils
.makePutBodyRequest(server
.url
, path
+ userId
, userAccessToken
, data
, done
, 204)
670 describe('When getting my information', function () {
671 it('Should fail with a non authenticated user', function (done
) {
674 .set('Authorization', 'Bearer faketoken')
675 .set('Accept', 'application/json')
679 it('Should success with the correct parameters', function (done
) {
682 .set('Authorization', 'Bearer ' + userAccessToken
)
683 .set('Accept', 'application/json')
688 describe('When removing an user', function () {
689 it('Should fail with an incorrect id', function (done
) {
691 .delete(path
+ 'bla-bla')
692 .set('Authorization', 'Bearer ' + server
.accessToken
)
696 it('Should return 404 with a non existing id', function (done
) {
698 .delete(path
+ '579f982228c99c221d8092b8')
699 .set('Authorization', 'Bearer ' + server
.accessToken
)
703 it('Should success with the correct parameters', function (done
) {
705 .delete(path
+ userId
)
706 .set('Authorization', 'Bearer ' + server
.accessToken
)
712 describe('Of the remote videos API', function () {
713 describe('When making a secure request', function () {
714 it('Should check a secure request')
717 describe('When adding a video', function () {
718 it('Should check when adding a video')
721 describe('When removing a video', function () {
722 it('Should check when removing a video')
726 after(function (done
) {
727 process
.kill(-server
.app
.pid
)
729 // Keep the logs if the test failed
731 serversUtils
.flushTests(done
)