]>
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/'
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 fail if we add a user with the same username', function (done
) {
587 password: 'my super password'
590 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
, 409)
593 it('Should succeed with the correct params', function (done
) {
596 password: 'my super password'
599 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
, 204)
602 it('Should fail with a non admin user', function (done
) {
608 loginUtils
.loginAndGetAccessToken(server
, function (err
, accessToken
) {
611 userAccessToken
= accessToken
615 password: 'my super password'
618 requestsUtils
.makePostBodyRequest(server
.url
, path
, userAccessToken
, data
, done
, 403)
623 describe('When updating a user', function () {
624 before(function (done
) {
625 usersUtils
.getUsersList(server
.url
, function (err
, res
) {
628 userId
= res
.body
.data
[1].id
633 it('Should fail with a too small password', function (done
) {
638 requestsUtils
.makePutBodyRequest(server
.url
, path
+ userId
, userAccessToken
, data
, done
)
641 it('Should fail with a too long password', function (done
) {
643 password: 'my super long password which is very very very very very very very very very very very very very very' +
644 'very very very very very very very very very very very very very very very veryv very very very very' +
645 'very very very very very very very very very very very very very very very very very very very very long'
648 requestsUtils
.makePutBodyRequest(server
.url
, path
+ userId
, userAccessToken
, data
, done
)
651 it('Should fail with an non authenticated user', function (done
) {
653 password: 'my super password'
656 requestsUtils
.makePutBodyRequest(server
.url
, path
+ userId
, 'super token', data
, done
, 401)
659 it('Should succeed with the correct params', function (done
) {
661 password: 'my super password'
664 requestsUtils
.makePutBodyRequest(server
.url
, path
+ userId
, userAccessToken
, data
, done
, 204)
668 describe('When getting my information', function () {
669 it('Should fail with a non authenticated user', function (done
) {
672 .set('Authorization', 'Bearer faketoken')
673 .set('Accept', 'application/json')
677 it('Should success with the correct parameters', function (done
) {
680 .set('Authorization', 'Bearer ' + userAccessToken
)
681 .set('Accept', 'application/json')
686 describe('When removing an user', function () {
687 it('Should fail with an incorrect id', function (done
) {
689 .delete(path
+ 'bla-bla')
690 .set('Authorization', 'Bearer ' + server
.accessToken
)
694 it('Should return 404 with a non existing id', function (done
) {
696 .delete(path
+ '579f982228c99c221d8092b8')
697 .set('Authorization', 'Bearer ' + server
.accessToken
)
703 describe('Of the remote videos API', function () {
704 describe('When making a secure request', function () {
705 it('Should check a secure request')
708 describe('When adding a video', function () {
709 it('Should check when adding a video')
712 describe('When removing a video', function () {
713 it('Should check when removing a video')
717 describe('Of the requests API', function () {
718 const path
= '/api/v1/requests/stats'
720 it('Should fail with an non authenticated user', function (done
) {
723 .set('Accept', 'application/json')
727 it('Should fail with a non admin user', function (done
) {
730 .set('Authorization', 'Bearer ' + userAccessToken
)
731 .set('Accept', 'application/json')
736 after(function (done
) {
737 process
.kill(-server
.app
.pid
)
739 // Keep the logs if the test failed
741 serversUtils
.flushTests(done
)