]>
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 () {
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 urls: [ 'http://localhost:9002' ]
73 it('Should fail without urls', function (done
) {
75 .post(path
+ '/makefriends')
76 .set('Authorization', 'Bearer ' + server
.accessToken
)
77 .set('Accept', 'application/json')
81 it('Should fail with urls is not an array', function (done
) {
83 .post(path
+ '/makefriends')
84 .send({ urls: 'http://localhost:9002' })
85 .set('Authorization', 'Bearer ' + server
.accessToken
)
86 .set('Accept', 'application/json')
90 it('Should fail if the array is not composed by urls', function (done
) {
92 .post(path
+ '/makefriends')
93 .send({ urls: [ 'http://localhost:9002', 'localhost:coucou' ] })
94 .set('Authorization', 'Bearer ' + server
.accessToken
)
95 .set('Accept', 'application/json')
99 it('Should fail if urls are not unique', function (done
) {
101 .post(path
+ '/makefriends')
102 .send({ urls: [ 'http://localhost:9002', 'http://localhost:9002' ] })
103 .set('Authorization', 'Bearer ' + server
.accessToken
)
104 .set('Accept', 'application/json')
108 it('Should fail with a invalid token', function (done
) {
110 .post(path
+ '/makefriends')
112 .set('Authorization', 'Bearer faketoken')
113 .set('Accept', 'application/json')
117 it('Should fail if the user is not an administrator', function (done
) {
119 .post(path
+ '/makefriends')
121 .set('Authorization', 'Bearer ' + userAccessToken
)
122 .set('Accept', 'application/json')
127 describe('When quitting friends', function () {
128 it('Should fail with a invalid token', function (done
) {
130 .get(path
+ '/quitfriends')
131 .query({ start: 'hello' })
132 .set('Authorization', 'Bearer faketoken')
133 .set('Accept', 'application/json')
137 it('Should fail if the user is not an administrator', function (done
) {
139 .get(path
+ '/quitfriends')
140 .query({ start: 'hello' })
141 .set('Authorization', 'Bearer ' + userAccessToken
)
142 .set('Accept', 'application/json')
148 describe('When adding a pod', function () {
149 it('Should fail with nothing', function (done
) {
151 requestsUtils
.makePostBodyRequest(server
.url
, path
, null, data
, done
)
154 it('Should fail without public key', function (done
) {
156 url: 'http://coucou.com'
158 requestsUtils
.makePostBodyRequest(server
.url
, path
, null, data
, done
)
161 it('Should fail without an url', function (done
) {
163 publicKey: 'mysuperpublickey'
165 requestsUtils
.makePostBodyRequest(server
.url
, path
, null, data
, done
)
168 it('Should fail with an incorrect url', function (done
) {
171 publicKey: 'mysuperpublickey'
173 requestsUtils
.makePostBodyRequest(server
.url
, path
, null, data
, function () {
174 data
.url
= 'http://coucou'
175 requestsUtils
.makePostBodyRequest(server
.url
, path
, null, data
, function () {
177 requestsUtils
.makePostBodyRequest(server
.url
, path
, null, data
, done
)
182 it('Should succeed with the correct parameters', function (done
) {
184 url: 'http://coucou.com',
185 publicKey: 'mysuperpublickey'
187 requestsUtils
.makePostBodyRequest(server
.url
, path
, null, data
, done
, 200)
192 describe('Of the videos API', function () {
193 const path
= '/api/v1/videos/'
195 describe('When listing a video', function () {
196 it('Should fail with a bad start pagination', function (done
) {
199 .query({ start: 'hello' })
200 .set('Accept', 'application/json')
204 it('Should fail with a bad count pagination', function (done
) {
207 .query({ count: 'hello' })
208 .set('Accept', 'application/json')
212 it('Should fail with an incorrect sort', function (done
) {
215 .query({ sort: 'hello' })
216 .set('Accept', 'application/json')
221 describe('When searching a video', function () {
222 it('Should fail with nothing', function (done
) {
224 .get(pathUtils
.join(path
, 'search'))
225 .set('Accept', 'application/json')
229 it('Should fail with a bad start pagination', function (done
) {
231 .get(pathUtils
.join(path
, 'search', 'test'))
232 .query({ start: 'hello' })
233 .set('Accept', 'application/json')
237 it('Should fail with a bad count pagination', function (done
) {
239 .get(pathUtils
.join(path
, 'search', 'test'))
240 .query({ count: 'hello' })
241 .set('Accept', 'application/json')
245 it('Should fail with an incorrect sort', function (done
) {
247 .get(pathUtils
.join(path
, 'search', 'test'))
248 .query({ sort: 'hello' })
249 .set('Accept', 'application/json')
254 describe('When adding a video', function () {
255 it('Should fail with nothing', function (done
) {
258 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
261 it('Should fail without name', function (done
) {
263 description: 'my super description',
264 tags: [ 'tag1', 'tag2' ]
267 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
269 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
272 it('Should fail with a long name', function (done
) {
274 name: 'My very very very very very very very very very very very very very very very very long name',
275 description: 'my super description',
276 tags: [ 'tag1', 'tag2' ]
279 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
281 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
284 it('Should fail without description', function (done
) {
286 name: 'my super name',
287 tags: [ 'tag1', 'tag2' ]
290 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
292 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
295 it('Should fail with a long description', function (done
) {
297 name: 'my super name',
298 description: 'my super description which is 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 very very very very very very' +
300 'very very very very very very very very very very very very very very very long',
301 tags: [ 'tag1', 'tag2' ]
304 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
306 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
309 it('Should fail without tags', function (done
) {
311 name: 'my super name',
312 description: 'my super description'
315 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
317 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
320 it('Should fail with too many tags', function (done
) {
322 name: 'my super name',
323 description: 'my super description',
324 tags: [ 'tag1', 'tag2', 'tag3', 'tag4' ]
327 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
329 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
332 it('Should fail with not enough tags', function (done
) {
334 name: 'my super name',
335 description: 'my super description',
339 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
341 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
344 it('Should fail with a tag length too low', function (done
) {
346 name: 'my super name',
347 description: 'my super description',
348 tags: [ 'tag1', 't' ]
351 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
353 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
356 it('Should fail with a tag length too big', function (done
) {
358 name: 'my super name',
359 description: 'my super description',
360 tags: [ 'mysupertagtoolong', 'tag1' ]
363 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
365 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
368 it('Should fail with malformed tags', function (done
) {
370 name: 'my super name',
371 description: 'my super description',
375 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
377 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
380 it('Should fail without an input file', function (done
) {
382 name: 'my super name',
383 description: 'my super description',
384 tags: [ 'tag1', 'tag2' ]
387 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
390 it('Should fail without an incorrect input file', function (done
) {
392 name: 'my super name',
393 description: 'my super description',
394 tags: [ 'tag1', 'tag2' ]
397 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short_fake.webm')
399 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
402 it('Should fail with a too big duration', function (done
) {
404 name: 'my super name',
405 description: 'my super description',
406 tags: [ 'tag1', 'tag2' ]
409 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_too_long.webm')
411 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
414 it('Should succeed with the correct parameters', function (done
) {
416 name: 'my super name',
417 description: 'my super description',
418 tags: [ 'tag1', 'tag2' ]
421 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
423 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, function () {
424 attach
.videofile
= pathUtils
.join(__dirname
, 'fixtures', 'video_short.mp4')
425 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, function () {
426 attach
.videofile
= pathUtils
.join(__dirname
, 'fixtures', 'video_short.ogv')
427 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
, 204)
433 describe('When getting a video', function () {
434 it('Should return the list of the videos with nothing', function (done
) {
437 .set('Accept', 'application/json')
439 .expect('Content-Type', /json/)
440 .end(function (err
, res
) {
443 expect(res
.body
.data
).to
.be
.an('array')
444 expect(res
.body
.data
.length
).to
.equal(3)
450 it('Should fail without a mongodb id', function (done
) {
452 .get(path
+ 'coucou')
453 .set('Accept', 'application/json')
457 it('Should return 404 with an incorrect video', function (done
) {
459 .get(path
+ '123456789012345678901234')
460 .set('Accept', 'application/json')
464 it('Should succeed with the correct parameters')
467 describe('When removing a video', function () {
468 it('Should have 404 with nothing', function (done
) {
471 .set('Authorization', 'Bearer ' + server
.accessToken
)
475 it('Should fail without a mongodb id', function (done
) {
477 .delete(path
+ 'hello')
478 .set('Authorization', 'Bearer ' + server
.accessToken
)
482 it('Should fail with a video which does not exist', function (done
) {
484 .delete(path
+ '123456789012345678901234')
485 .set('Authorization', 'Bearer ' + server
.accessToken
)
489 it('Should fail with a video of another user')
491 it('Should fail with a video of another pod')
493 it('Should succeed with the correct parameters')
497 describe('Of the users API', function () {
498 const path
= '/api/v1/users/'
502 describe('When listing users', function () {
503 it('Should fail with a bad start pagination', function (done
) {
506 .query({ start: 'hello' })
507 .set('Accept', 'application/json')
511 it('Should fail with a bad count pagination', function (done
) {
514 .query({ count: 'hello' })
515 .set('Accept', 'application/json')
519 it('Should fail with an incorrect sort', function (done
) {
522 .query({ sort: 'hello' })
523 .set('Accept', 'application/json')
528 describe('When adding a new user', function () {
529 it('Should fail with a too small username', function (done
) {
532 password: 'mysuperpassword'
535 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
)
538 it('Should fail with a too long username', function (done
) {
540 username: 'mysuperusernamewhichisverylong',
541 password: 'mysuperpassword'
544 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
)
547 it('Should fail with an incorrect username', function (done
) {
549 username: 'my username',
550 password: 'mysuperpassword'
553 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
)
556 it('Should fail with a too small password', function (done
) {
558 username: 'myusername',
562 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
)
565 it('Should fail with a too long password', function (done
) {
567 username: 'myusername',
568 password: 'my super long password which is very very very very very very very very very very very very very very' +
569 'very very very very very very very very very very very very very very very veryv very very very very' +
570 'very very very very very very very very very very very very very very very very very very very very long'
573 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
)
576 it('Should fail with an non authenticated user', function (done
) {
578 username: 'myusername',
579 password: 'my super password'
582 requestsUtils
.makePostBodyRequest(server
.url
, path
, 'super token', data
, done
, 401)
585 it('Should fail if we add a user with the same username', function (done
) {
588 password: 'my super password'
591 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
, 409)
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
, 204)
603 it('Should fail with a non admin user', function (done
) {
609 loginUtils
.loginAndGetAccessToken(server
, function (err
, accessToken
) {
612 userAccessToken
= accessToken
616 password: 'my super password'
619 requestsUtils
.makePostBodyRequest(server
.url
, path
, userAccessToken
, data
, done
, 403)
624 describe('When updating a user', function () {
625 before(function (done
) {
626 usersUtils
.getUsersList(server
.url
, function (err
, res
) {
629 userId
= res
.body
.data
[1].id
630 rootId
= res
.body
.data
[2].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 fail with the root user', function (done
) {
698 .delete(path
+ rootId
)
699 .set('Authorization', 'Bearer ' + server
.accessToken
)
703 it('Should return 404 with a non existing id', function (done
) {
705 .delete(path
+ '579f982228c99c221d8092b8')
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 describe('Of the requests API', function () {
727 const path
= '/api/v1/requests/stats'
729 it('Should fail with an non authenticated user', function (done
) {
732 .set('Accept', 'application/json')
736 it('Should fail with a non admin user', function (done
) {
739 .set('Authorization', 'Bearer ' + userAccessToken
)
740 .set('Accept', 'application/json')
745 after(function (done
) {
746 process
.kill(-server
.app
.pid
)
748 // Keep the logs if the test failed
750 serversUtils
.flushTests(done
)