]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/checkParams.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 serversUtils
= require('../utils/servers')
11 const usersUtils
= require('../utils/users')
13 describe('Test parameters validator', function () {
16 function makePostRequest (path
, token
, fields
, attaches
, done
, statusCodeExpected
) {
17 if (!statusCodeExpected
) statusCodeExpected
= 400
19 const req
= request(server
.url
)
21 .set('Accept', 'application/json')
23 if (token
) req
.set('Authorization', 'Bearer ' + token
)
25 Object
.keys(fields
).forEach(function (field
) {
26 const value
= fields
[field
]
28 if (Array
.isArray(value
)) {
29 for (let i
= 0; i
< value
.length
; i
++) {
30 req
.field(field
+ '[' + i
+ ']', value
[i
])
33 req
.field(field
, value
)
37 Object
.keys(attaches
).forEach(function (attach
) {
38 const value
= attaches
[attach
]
39 req
.attach(attach
, value
)
42 req
.expect(statusCodeExpected
, done
)
45 function makePostBodyRequest (path
, token
, fields
, done
, statusCodeExpected
) {
46 if (!statusCodeExpected
) statusCodeExpected
= 400
48 const req
= request(server
.url
)
50 .set('Accept', 'application/json')
52 if (token
) req
.set('Authorization', 'Bearer ' + token
)
54 req
.send(fields
).expect(statusCodeExpected
, done
)
57 function makePutBodyRequest (path
, token
, fields
, done
, statusCodeExpected
) {
58 if (!statusCodeExpected
) statusCodeExpected
= 400
60 const req
= request(server
.url
)
62 .set('Accept', 'application/json')
64 if (token
) req
.set('Authorization', 'Bearer ' + token
)
66 req
.send(fields
).expect(statusCodeExpected
, done
)
69 // ---------------------------------------------------------------
71 before(function (done
) {
76 serversUtils
.flushTests(next
)
79 serversUtils
.runServer(1, function (server1
) {
86 loginUtils
.loginAndGetAccessToken(server
, function (err
, token
) {
88 server
.accessToken
= token
96 describe('Of the pods API', function () {
97 const path
= '/api/v1/pods/'
99 describe('When adding a pod', function () {
100 it('Should fail with nothing', function (done
) {
102 makePostBodyRequest(path
, null, data
, done
)
105 it('Should fail without public key', function (done
) {
107 url: 'http://coucou.com'
109 makePostBodyRequest(path
, null, data
, done
)
112 it('Should fail without an url', function (done
) {
114 publicKey: 'mysuperpublickey'
116 makePostBodyRequest(path
, null, data
, done
)
119 it('Should fail with an incorrect url', function (done
) {
122 publicKey: 'mysuperpublickey'
124 makePostBodyRequest(path
, null, data
, function () {
125 data
.url
= 'http://coucou'
126 makePostBodyRequest(path
, null, data
, function () {
128 makePostBodyRequest(path
, null, data
, done
)
133 it('Should succeed with the correct parameters', function (done
) {
135 url: 'http://coucou.com',
136 publicKey: 'mysuperpublickey'
138 makePostBodyRequest(path
, null, data
, done
, 200)
142 describe('For the friends API', function () {
143 let userAccessToken
= null
145 before(function (done
) {
146 usersUtils
.createUser(server
.url
, server
.accessToken
, 'user1', 'password', function () {
152 loginUtils
.loginAndGetAccessToken(server
, function (err
, accessToken
) {
155 userAccessToken
= accessToken
162 describe('When making friends', function () {
163 it('Should fail with a invalid token', function (done
) {
165 .get(path
+ '/makefriends')
166 .query({ start: 'hello' })
167 .set('Authorization', 'Bearer faketoken')
168 .set('Accept', 'application/json')
172 it('Should fail if the user is not an administrator', function (done
) {
174 .get(path
+ '/makefriends')
175 .query({ start: 'hello' })
176 .set('Authorization', 'Bearer ' + userAccessToken
)
177 .set('Accept', 'application/json')
182 describe('When quitting friends', function () {
183 it('Should fail with a invalid token', function (done
) {
185 .get(path
+ '/quitfriends')
186 .query({ start: 'hello' })
187 .set('Authorization', 'Bearer faketoken')
188 .set('Accept', 'application/json')
192 it('Should fail if the user is not an administrator', function (done
) {
194 .get(path
+ '/quitfriends')
195 .query({ start: 'hello' })
196 .set('Authorization', 'Bearer ' + userAccessToken
)
197 .set('Accept', 'application/json')
204 describe('Of the videos API', function () {
205 const path
= '/api/v1/videos/'
207 describe('When listing a video', function () {
208 it('Should fail with a bad start pagination', function (done
) {
211 .query({ start: 'hello' })
212 .set('Accept', 'application/json')
216 it('Should fail with a bad count pagination', function (done
) {
219 .query({ count: 'hello' })
220 .set('Accept', 'application/json')
224 it('Should fail with an incorrect sort', function (done
) {
227 .query({ sort: 'hello' })
228 .set('Accept', 'application/json')
233 describe('When searching a video', function () {
234 it('Should fail with nothing', function (done
) {
236 .get(pathUtils
.join(path
, 'search'))
237 .set('Accept', 'application/json')
241 it('Should fail with a bad start pagination', function (done
) {
243 .get(pathUtils
.join(path
, 'search', 'test'))
244 .query({ start: 'hello' })
245 .set('Accept', 'application/json')
249 it('Should fail with a bad count pagination', function (done
) {
251 .get(pathUtils
.join(path
, 'search', 'test'))
252 .query({ count: 'hello' })
253 .set('Accept', 'application/json')
257 it('Should fail with an incorrect sort', function (done
) {
259 .get(pathUtils
.join(path
, 'search', 'test'))
260 .query({ sort: 'hello' })
261 .set('Accept', 'application/json')
266 describe('When adding a video', function () {
267 it('Should fail with nothing', function (done
) {
270 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
273 it('Should fail without name', function (done
) {
275 description: 'my super description',
276 tags: [ 'tag1', 'tag2' ]
279 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
281 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
284 it('Should fail with a long name', function (done
) {
286 name: 'My very very very very very very very very very very very very very very very very long name',
287 description: 'my super description',
288 tags: [ 'tag1', 'tag2' ]
291 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
293 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
296 it('Should fail without description', function (done
) {
298 name: 'my super name',
299 tags: [ 'tag1', 'tag2' ]
302 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
304 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
307 it('Should fail with a long description', function (done
) {
309 name: 'my super name',
310 description: 'my super description which is very very very very very very very very very very very very very very' +
311 'very very very very very very very very very very very very very very very very very very very very very' +
312 'very very very very very very very very very very very very very very very long',
313 tags: [ 'tag1', 'tag2' ]
316 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
318 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
321 it('Should fail without tags', function (done
) {
323 name: 'my super name',
324 description: 'my super description'
327 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
329 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
332 it('Should fail with too many tags', function (done
) {
334 name: 'my super name',
335 description: 'my super description',
336 tags: [ 'tag1', 'tag2', 'tag3', 'tag4' ]
339 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
341 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
344 it('Should fail with not enough tags', function (done
) {
346 name: 'my super name',
347 description: 'my super description',
351 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
353 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
356 it('Should fail with a tag length too low', function (done
) {
358 name: 'my super name',
359 description: 'my super description',
360 tags: [ 'tag1', 't' ]
363 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
365 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
368 it('Should fail with a tag length too big', function (done
) {
370 name: 'my super name',
371 description: 'my super description',
372 tags: [ 'mysupertagtoolong', 'tag1' ]
375 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
377 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
380 it('Should fail with malformed tags', function (done
) {
382 name: 'my super name',
383 description: 'my super description',
387 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
389 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
392 it('Should fail without an input file', function (done
) {
394 name: 'my super name',
395 description: 'my super description',
396 tags: [ 'tag1', 'tag2' ]
399 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
402 it('Should fail without an incorrect input file', function (done
) {
404 name: 'my super name',
405 description: 'my super description',
406 tags: [ 'tag1', 'tag2' ]
409 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short_fake.webm')
411 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
414 it('Should fail with a too big duration', function (done
) {
416 name: 'my super name',
417 description: 'my super description',
418 tags: [ 'tag1', 'tag2' ]
421 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_too_long.webm')
423 makePostRequest(path
, server
.accessToken
, data
, attach
, done
)
426 it('Should succeed with the correct parameters', function (done
) {
428 name: 'my super name',
429 description: 'my super description',
430 tags: [ 'tag1', 'tag2' ]
433 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
435 makePostRequest(path
, server
.accessToken
, data
, attach
, function () {
436 attach
.videofile
= pathUtils
.join(__dirname
, 'fixtures', 'video_short.mp4')
437 makePostRequest(path
, server
.accessToken
, data
, attach
, function () {
438 attach
.videofile
= pathUtils
.join(__dirname
, 'fixtures', 'video_short.ogv')
439 makePostRequest(path
, server
.accessToken
, data
, attach
, done
, 204)
445 describe('When getting a video', function () {
446 it('Should return the list of the videos with nothing', function (done
) {
449 .set('Accept', 'application/json')
451 .expect('Content-Type', /json/)
452 .end(function (err
, res
) {
455 expect(res
.body
.data
).to
.be
.an('array')
456 expect(res
.body
.data
.length
).to
.equal(3)
462 it('Should fail without a mongodb id', function (done
) {
464 .get(path
+ 'coucou')
465 .set('Accept', 'application/json')
469 it('Should return 404 with an incorrect video', function (done
) {
471 .get(path
+ '123456789012345678901234')
472 .set('Accept', 'application/json')
476 it('Should succeed with the correct parameters')
479 describe('When removing a video', function () {
480 it('Should have 404 with nothing', function (done
) {
483 .set('Authorization', 'Bearer ' + server
.accessToken
)
487 it('Should fail without a mongodb id', function (done
) {
489 .delete(path
+ 'hello')
490 .set('Authorization', 'Bearer ' + server
.accessToken
)
494 it('Should fail with a video which does not exist', function (done
) {
496 .delete(path
+ '123456789012345678901234')
497 .set('Authorization', 'Bearer ' + server
.accessToken
)
501 it('Should fail with a video of another user')
503 it('Should fail with a video of another pod')
505 it('Should succeed with the correct parameters')
509 describe('Of the users API', function () {
510 const path
= '/api/v1/users/'
512 let userAccessToken
= null
514 describe('When adding a new user', function () {
515 it('Should fail with a too small username', function (done
) {
518 password: 'mysuperpassword'
521 makePostBodyRequest(path
, server
.accessToken
, data
, done
)
524 it('Should fail with a too long username', function (done
) {
526 username: 'mysuperusernamewhichisverylong',
527 password: 'mysuperpassword'
530 makePostBodyRequest(path
, server
.accessToken
, data
, done
)
533 it('Should fail with an incorrect username', function (done
) {
535 username: 'my username',
536 password: 'mysuperpassword'
539 makePostBodyRequest(path
, server
.accessToken
, data
, done
)
542 it('Should fail with a too small password', function (done
) {
544 username: 'myusername',
548 makePostBodyRequest(path
, server
.accessToken
, data
, done
)
551 it('Should fail with a too long password', function (done
) {
553 username: 'myusername',
554 password: 'my super long password which is very very very very very very very very very very very very very very' +
555 'very very very very very very very very very very very very very very very veryv very very very very' +
556 'very very very very very very very very very very very very very very very very very very very very long'
559 makePostBodyRequest(path
, server
.accessToken
, data
, done
)
562 it('Should fail with an non authenticated user', function (done
) {
564 username: 'myusername',
565 password: 'my super password'
568 makePostBodyRequest(path
, 'super token', data
, done
, 401)
571 it('Should succeed with the correct params', function (done
) {
574 password: 'my super password'
577 makePostBodyRequest(path
, server
.accessToken
, data
, done
, 204)
580 it('Should fail with a non admin user', function (done
) {
583 password: 'my super password'
586 loginUtils
.loginAndGetAccessToken(server
, function (err
, accessToken
) {
589 userAccessToken
= accessToken
593 password: 'my super password'
596 makePostBodyRequest(path
, userAccessToken
, data
, done
, 403)
601 describe('When updating a user', function () {
602 before(function (done
) {
603 usersUtils
.getUsersList(server
.url
, function (err
, res
) {
606 userId
= res
.body
.data
[1].id
611 it('Should fail with a too small password', function (done
) {
616 makePutBodyRequest(path
+ userId
, userAccessToken
, data
, done
)
619 it('Should fail with a too long password', function (done
) {
621 password: 'my super long password which is very very very very very very very very very very very very very very' +
622 'very very very very very very very very very very very very very very very veryv very very very very' +
623 'very very very very very very very very very very very very very very very very very very very very long'
626 makePutBodyRequest(path
+ userId
, userAccessToken
, data
, done
)
629 it('Should fail with an non authenticated user', function (done
) {
631 password: 'my super password'
634 makePutBodyRequest(path
+ userId
, 'super token', data
, done
, 401)
637 it('Should succeed with the correct params', function (done
) {
639 password: 'my super password'
642 makePutBodyRequest(path
+ userId
, userAccessToken
, data
, done
, 204)
646 describe('When getting my information', function () {
647 it('Should fail with a non authenticated user', function (done
) {
650 .set('Authorization', 'Bearer faketoken')
651 .set('Accept', 'application/json')
655 it('Should success with the correct parameters', function (done
) {
658 .set('Authorization', 'Bearer ' + userAccessToken
)
659 .set('Accept', 'application/json')
664 describe('When removing an user', function () {
665 it('Should fail with an incorrect username', function (done
) {
667 .delete(path
+ 'bla-bla')
668 .set('Authorization', 'Bearer ' + server
.accessToken
)
672 it('Should return 404 with a non existing username', function (done
) {
674 .delete(path
+ 'qzzerg')
675 .set('Authorization', 'Bearer ' + server
.accessToken
)
679 it('Should success with the correct parameters', function (done
) {
681 .delete(path
+ 'user1')
682 .set('Authorization', 'Bearer ' + server
.accessToken
)
688 describe('Of the remote videos API', function () {
689 describe('When making a secure request', function () {
690 it('Should check a secure request')
693 describe('When adding a video', function () {
694 it('Should check when adding a video')
697 describe('When removing a video', function () {
698 it('Should check when removing a video')
702 after(function (done
) {
703 process
.kill(-server
.app
.pid
)
705 // Keep the logs if the test failed
707 serversUtils
.flushTests(done
)