]>
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 adding a pod', function () {
48 it('Should fail with nothing', function (done
) {
50 requestsUtils
.makePostBodyRequest(server
.url
, path
, null, data
, done
)
53 it('Should fail without public key', function (done
) {
55 url: 'http://coucou.com'
57 requestsUtils
.makePostBodyRequest(server
.url
, path
, null, data
, done
)
60 it('Should fail without an url', function (done
) {
62 publicKey: 'mysuperpublickey'
64 requestsUtils
.makePostBodyRequest(server
.url
, path
, null, data
, done
)
67 it('Should fail with an incorrect url', function (done
) {
70 publicKey: 'mysuperpublickey'
72 requestsUtils
.makePostBodyRequest(server
.url
, path
, null, data
, function () {
73 data
.url
= 'http://coucou'
74 requestsUtils
.makePostBodyRequest(server
.url
, path
, null, data
, function () {
76 requestsUtils
.makePostBodyRequest(server
.url
, path
, null, data
, done
)
81 it('Should succeed with the correct parameters', function (done
) {
83 url: 'http://coucou.com',
84 publicKey: 'mysuperpublickey'
86 requestsUtils
.makePostBodyRequest(server
.url
, path
, null, data
, done
, 200)
90 describe('For the friends API', function () {
91 let userAccessToken
= null
93 before(function (done
) {
94 usersUtils
.createUser(server
.url
, server
.accessToken
, 'user1', 'password', function () {
100 loginUtils
.loginAndGetAccessToken(server
, function (err
, accessToken
) {
103 userAccessToken
= accessToken
110 describe('When making friends', function () {
112 urls: [ 'http://localhost:9002' ]
115 it('Should fail without urls', function (done
) {
117 .post(path
+ '/makefriends')
118 .set('Authorization', 'Bearer faketoken')
119 .set('Accept', 'application/json')
123 it('Should fail with urls is not an array', function (done
) {
125 .post(path
+ '/makefriends')
126 .send({ urls: 'http://localhost:9002' })
127 .set('Authorization', 'Bearer faketoken')
128 .set('Accept', 'application/json')
132 it('Should fail if the array is not composed by urls', function (done
) {
134 .post(path
+ '/makefriends')
135 .send({ urls: [ 'http://localhost:9002', 'localhost:coucou' ] })
136 .set('Authorization', 'Bearer faketoken')
137 .set('Accept', 'application/json')
141 it('Should fail with a invalid token', function (done
) {
143 .post(path
+ '/makefriends')
145 .set('Authorization', 'Bearer faketoken')
146 .set('Accept', 'application/json')
150 it('Should fail if the user is not an administrator', function (done
) {
152 .post(path
+ '/makefriends')
154 .set('Authorization', 'Bearer ' + userAccessToken
)
155 .set('Accept', 'application/json')
160 describe('When quitting friends', function () {
161 it('Should fail with a invalid token', function (done
) {
163 .get(path
+ '/quitfriends')
164 .query({ start: 'hello' })
165 .set('Authorization', 'Bearer faketoken')
166 .set('Accept', 'application/json')
170 it('Should fail if the user is not an administrator', function (done
) {
172 .get(path
+ '/quitfriends')
173 .query({ start: 'hello' })
174 .set('Authorization', 'Bearer ' + userAccessToken
)
175 .set('Accept', 'application/json')
182 describe('Of the videos API', function () {
183 const path
= '/api/v1/videos/'
185 describe('When listing a video', function () {
186 it('Should fail with a bad start pagination', function (done
) {
189 .query({ start: 'hello' })
190 .set('Accept', 'application/json')
194 it('Should fail with a bad count pagination', function (done
) {
197 .query({ count: 'hello' })
198 .set('Accept', 'application/json')
202 it('Should fail with an incorrect sort', function (done
) {
205 .query({ sort: 'hello' })
206 .set('Accept', 'application/json')
211 describe('When searching a video', function () {
212 it('Should fail with nothing', function (done
) {
214 .get(pathUtils
.join(path
, 'search'))
215 .set('Accept', 'application/json')
219 it('Should fail with a bad start pagination', function (done
) {
221 .get(pathUtils
.join(path
, 'search', 'test'))
222 .query({ start: 'hello' })
223 .set('Accept', 'application/json')
227 it('Should fail with a bad count pagination', function (done
) {
229 .get(pathUtils
.join(path
, 'search', 'test'))
230 .query({ count: 'hello' })
231 .set('Accept', 'application/json')
235 it('Should fail with an incorrect sort', function (done
) {
237 .get(pathUtils
.join(path
, 'search', 'test'))
238 .query({ sort: 'hello' })
239 .set('Accept', 'application/json')
244 describe('When adding a video', function () {
245 it('Should fail with nothing', function (done
) {
248 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
251 it('Should fail without name', function (done
) {
253 description: 'my super description',
254 tags: [ 'tag1', 'tag2' ]
257 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
259 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
262 it('Should fail with a long name', function (done
) {
264 name: 'My very very very very very very very very very very very very very very very very long name',
265 description: 'my super description',
266 tags: [ 'tag1', 'tag2' ]
269 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
271 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
274 it('Should fail without description', function (done
) {
276 name: 'my super name',
277 tags: [ 'tag1', 'tag2' ]
280 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
282 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
285 it('Should fail with a long description', function (done
) {
287 name: 'my super name',
288 description: 'my super description which is very very very very very very very very very very very very very very' +
289 'very very very very very very very very very very very very very very very very very very very very very' +
290 'very very very very very very very very very very very very very very very long',
291 tags: [ 'tag1', 'tag2' ]
294 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
296 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
299 it('Should fail without tags', function (done
) {
301 name: 'my super name',
302 description: 'my super description'
305 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
307 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
310 it('Should fail with too many tags', function (done
) {
312 name: 'my super name',
313 description: 'my super description',
314 tags: [ 'tag1', 'tag2', 'tag3', 'tag4' ]
317 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
319 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
322 it('Should fail with not enough tags', function (done
) {
324 name: 'my super name',
325 description: 'my super description',
329 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
331 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
334 it('Should fail with a tag length too low', function (done
) {
336 name: 'my super name',
337 description: 'my super description',
338 tags: [ 'tag1', 't' ]
341 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
343 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
346 it('Should fail with a tag length too big', function (done
) {
348 name: 'my super name',
349 description: 'my super description',
350 tags: [ 'mysupertagtoolong', 'tag1' ]
353 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
355 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
358 it('Should fail with malformed tags', function (done
) {
360 name: 'my super name',
361 description: 'my super description',
365 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
367 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
370 it('Should fail without an input file', function (done
) {
372 name: 'my super name',
373 description: 'my super description',
374 tags: [ 'tag1', 'tag2' ]
377 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
380 it('Should fail without an incorrect input file', function (done
) {
382 name: 'my super name',
383 description: 'my super description',
384 tags: [ 'tag1', 'tag2' ]
387 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short_fake.webm')
389 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
392 it('Should fail with a too big duration', function (done
) {
394 name: 'my super name',
395 description: 'my super description',
396 tags: [ 'tag1', 'tag2' ]
399 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_too_long.webm')
401 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
404 it('Should succeed with the correct parameters', function (done
) {
406 name: 'my super name',
407 description: 'my super description',
408 tags: [ 'tag1', 'tag2' ]
411 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
413 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, function () {
414 attach
.videofile
= pathUtils
.join(__dirname
, 'fixtures', 'video_short.mp4')
415 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, function () {
416 attach
.videofile
= pathUtils
.join(__dirname
, 'fixtures', 'video_short.ogv')
417 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
, 204)
423 describe('When getting a video', function () {
424 it('Should return the list of the videos with nothing', function (done
) {
427 .set('Accept', 'application/json')
429 .expect('Content-Type', /json/)
430 .end(function (err
, res
) {
433 expect(res
.body
.data
).to
.be
.an('array')
434 expect(res
.body
.data
.length
).to
.equal(3)
440 it('Should fail without a mongodb id', function (done
) {
442 .get(path
+ 'coucou')
443 .set('Accept', 'application/json')
447 it('Should return 404 with an incorrect video', function (done
) {
449 .get(path
+ '123456789012345678901234')
450 .set('Accept', 'application/json')
454 it('Should succeed with the correct parameters')
457 describe('When removing a video', function () {
458 it('Should have 404 with nothing', function (done
) {
461 .set('Authorization', 'Bearer ' + server
.accessToken
)
465 it('Should fail without a mongodb id', function (done
) {
467 .delete(path
+ 'hello')
468 .set('Authorization', 'Bearer ' + server
.accessToken
)
472 it('Should fail with a video which does not exist', function (done
) {
474 .delete(path
+ '123456789012345678901234')
475 .set('Authorization', 'Bearer ' + server
.accessToken
)
479 it('Should fail with a video of another user')
481 it('Should fail with a video of another pod')
483 it('Should succeed with the correct parameters')
487 describe('Of the users API', function () {
488 const path
= '/api/v1/users/'
490 let userAccessToken
= null
492 describe('When listing users', function () {
493 it('Should fail with a bad start pagination', function (done
) {
496 .query({ start: 'hello' })
497 .set('Accept', 'application/json')
501 it('Should fail with a bad count pagination', function (done
) {
504 .query({ count: 'hello' })
505 .set('Accept', 'application/json')
509 it('Should fail with an incorrect sort', function (done
) {
512 .query({ sort: 'hello' })
513 .set('Accept', 'application/json')
518 describe('When adding a new user', function () {
519 it('Should fail with a too small username', function (done
) {
522 password: 'mysuperpassword'
525 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
)
528 it('Should fail with a too long username', function (done
) {
530 username: 'mysuperusernamewhichisverylong',
531 password: 'mysuperpassword'
534 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
)
537 it('Should fail with an incorrect username', function (done
) {
539 username: 'my username',
540 password: 'mysuperpassword'
543 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
)
546 it('Should fail with a too small password', function (done
) {
548 username: 'myusername',
552 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
)
555 it('Should fail with a too long password', function (done
) {
557 username: 'myusername',
558 password: 'my super long password which is very very very very very very very very very very very very very very' +
559 'very very very very very very very very very very very very very very very veryv very very very very' +
560 'very very very very very very very very very very very very very very very very very very very very long'
563 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
)
566 it('Should fail with an non authenticated user', function (done
) {
568 username: 'myusername',
569 password: 'my super password'
572 requestsUtils
.makePostBodyRequest(server
.url
, path
, 'super token', data
, done
, 401)
575 it('Should succeed with the correct params', function (done
) {
578 password: 'my super password'
581 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
, 204)
584 it('Should fail with a non admin user', function (done
) {
587 password: 'my super password'
590 loginUtils
.loginAndGetAccessToken(server
, function (err
, accessToken
) {
593 userAccessToken
= accessToken
597 password: 'my super password'
600 requestsUtils
.makePostBodyRequest(server
.url
, path
, userAccessToken
, data
, done
, 403)
605 describe('When updating a user', function () {
606 before(function (done
) {
607 usersUtils
.getUsersList(server
.url
, function (err
, res
) {
610 userId
= res
.body
.data
[1].id
615 it('Should fail with a too small password', function (done
) {
620 requestsUtils
.makePutBodyRequest(server
.url
, path
+ userId
, userAccessToken
, data
, done
)
623 it('Should fail with a too long password', function (done
) {
625 password: 'my super long password which is very very very very very very very very very very very very very very' +
626 'very very very very very very very very very very very very very very very veryv very very very very' +
627 'very very very very very very very very very very very very very very very very very very very very long'
630 requestsUtils
.makePutBodyRequest(server
.url
, path
+ userId
, userAccessToken
, data
, done
)
633 it('Should fail with an non authenticated user', function (done
) {
635 password: 'my super password'
638 requestsUtils
.makePutBodyRequest(server
.url
, path
+ userId
, 'super token', data
, done
, 401)
641 it('Should succeed with the correct params', function (done
) {
643 password: 'my super password'
646 requestsUtils
.makePutBodyRequest(server
.url
, path
+ userId
, userAccessToken
, data
, done
, 204)
650 describe('When getting my information', function () {
651 it('Should fail with a non authenticated user', function (done
) {
654 .set('Authorization', 'Bearer faketoken')
655 .set('Accept', 'application/json')
659 it('Should success with the correct parameters', function (done
) {
662 .set('Authorization', 'Bearer ' + userAccessToken
)
663 .set('Accept', 'application/json')
668 describe('When removing an user', function () {
669 it('Should fail with an incorrect id', function (done
) {
671 .delete(path
+ 'bla-bla')
672 .set('Authorization', 'Bearer ' + server
.accessToken
)
676 it('Should return 404 with a non existing id', function (done
) {
678 .delete(path
+ '579f982228c99c221d8092b8')
679 .set('Authorization', 'Bearer ' + server
.accessToken
)
683 it('Should success with the correct parameters', function (done
) {
685 .delete(path
+ userId
)
686 .set('Authorization', 'Bearer ' + server
.accessToken
)
692 describe('Of the remote videos API', function () {
693 describe('When making a secure request', function () {
694 it('Should check a secure request')
697 describe('When adding a video', function () {
698 it('Should check when adding a video')
701 describe('When removing a video', function () {
702 it('Should check when removing a video')
706 after(function (done
) {
707 process
.kill(-server
.app
.pid
)
709 // Keep the logs if the test failed
711 serversUtils
.flushTests(done
)