]>
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 () {
111 it('Should fail with a invalid token', function (done
) {
113 .get(path
+ '/makefriends')
114 .query({ start: 'hello' })
115 .set('Authorization', 'Bearer faketoken')
116 .set('Accept', 'application/json')
120 it('Should fail if the user is not an administrator', function (done
) {
122 .get(path
+ '/makefriends')
123 .query({ start: 'hello' })
124 .set('Authorization', 'Bearer ' + userAccessToken
)
125 .set('Accept', 'application/json')
130 describe('When quitting friends', function () {
131 it('Should fail with a invalid token', function (done
) {
133 .get(path
+ '/quitfriends')
134 .query({ start: 'hello' })
135 .set('Authorization', 'Bearer faketoken')
136 .set('Accept', 'application/json')
140 it('Should fail if the user is not an administrator', function (done
) {
142 .get(path
+ '/quitfriends')
143 .query({ start: 'hello' })
144 .set('Authorization', 'Bearer ' + userAccessToken
)
145 .set('Accept', 'application/json')
152 describe('Of the videos API', function () {
153 const path
= '/api/v1/videos/'
155 describe('When listing a video', function () {
156 it('Should fail with a bad start pagination', function (done
) {
159 .query({ start: 'hello' })
160 .set('Accept', 'application/json')
164 it('Should fail with a bad count pagination', function (done
) {
167 .query({ count: 'hello' })
168 .set('Accept', 'application/json')
172 it('Should fail with an incorrect sort', function (done
) {
175 .query({ sort: 'hello' })
176 .set('Accept', 'application/json')
181 describe('When searching a video', function () {
182 it('Should fail with nothing', function (done
) {
184 .get(pathUtils
.join(path
, 'search'))
185 .set('Accept', 'application/json')
189 it('Should fail with a bad start pagination', function (done
) {
191 .get(pathUtils
.join(path
, 'search', 'test'))
192 .query({ start: 'hello' })
193 .set('Accept', 'application/json')
197 it('Should fail with a bad count pagination', function (done
) {
199 .get(pathUtils
.join(path
, 'search', 'test'))
200 .query({ count: 'hello' })
201 .set('Accept', 'application/json')
205 it('Should fail with an incorrect sort', function (done
) {
207 .get(pathUtils
.join(path
, 'search', 'test'))
208 .query({ sort: 'hello' })
209 .set('Accept', 'application/json')
214 describe('When adding a video', function () {
215 it('Should fail with nothing', function (done
) {
218 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
221 it('Should fail without name', function (done
) {
223 description: 'my super description',
224 tags: [ 'tag1', 'tag2' ]
227 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
229 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
232 it('Should fail with a long name', function (done
) {
234 name: 'My very very very very very very very very very very very very very very very very long name',
235 description: 'my super description',
236 tags: [ 'tag1', 'tag2' ]
239 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
241 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
244 it('Should fail without description', function (done
) {
246 name: 'my super name',
247 tags: [ 'tag1', 'tag2' ]
250 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
252 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
255 it('Should fail with a long description', function (done
) {
257 name: 'my super name',
258 description: 'my super description which is very very very very very very very very very very very very very very' +
259 'very very very very very very very very very very very very very very very very very very very very very' +
260 'very very very very very very very very very very very very very very very long',
261 tags: [ 'tag1', 'tag2' ]
264 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
266 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
269 it('Should fail without tags', function (done
) {
271 name: 'my super name',
272 description: 'my super description'
275 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
277 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
280 it('Should fail with too many tags', function (done
) {
282 name: 'my super name',
283 description: 'my super description',
284 tags: [ 'tag1', 'tag2', 'tag3', 'tag4' ]
287 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
289 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
292 it('Should fail with not enough tags', function (done
) {
294 name: 'my super name',
295 description: 'my super description',
299 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
301 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
304 it('Should fail with a tag length too low', function (done
) {
306 name: 'my super name',
307 description: 'my super description',
308 tags: [ 'tag1', 't' ]
311 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
313 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
316 it('Should fail with a tag length too big', function (done
) {
318 name: 'my super name',
319 description: 'my super description',
320 tags: [ 'mysupertagtoolong', 'tag1' ]
323 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
325 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
328 it('Should fail with malformed tags', function (done
) {
330 name: 'my super name',
331 description: 'my super description',
335 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
337 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
340 it('Should fail without an input file', function (done
) {
342 name: 'my super name',
343 description: 'my super description',
344 tags: [ 'tag1', 'tag2' ]
347 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
350 it('Should fail without an incorrect input file', function (done
) {
352 name: 'my super name',
353 description: 'my super description',
354 tags: [ 'tag1', 'tag2' ]
357 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short_fake.webm')
359 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
362 it('Should fail with a too big duration', function (done
) {
364 name: 'my super name',
365 description: 'my super description',
366 tags: [ 'tag1', 'tag2' ]
369 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_too_long.webm')
371 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
)
374 it('Should succeed with the correct parameters', function (done
) {
376 name: 'my super name',
377 description: 'my super description',
378 tags: [ 'tag1', 'tag2' ]
381 'videofile': pathUtils
.join(__dirname
, 'fixtures', 'video_short.webm')
383 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, function () {
384 attach
.videofile
= pathUtils
.join(__dirname
, 'fixtures', 'video_short.mp4')
385 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, function () {
386 attach
.videofile
= pathUtils
.join(__dirname
, 'fixtures', 'video_short.ogv')
387 requestsUtils
.makePostUploadRequest(server
.url
, path
, server
.accessToken
, data
, attach
, done
, 204)
393 describe('When getting a video', function () {
394 it('Should return the list of the videos with nothing', function (done
) {
397 .set('Accept', 'application/json')
399 .expect('Content-Type', /json/)
400 .end(function (err
, res
) {
403 expect(res
.body
.data
).to
.be
.an('array')
404 expect(res
.body
.data
.length
).to
.equal(3)
410 it('Should fail without a mongodb id', function (done
) {
412 .get(path
+ 'coucou')
413 .set('Accept', 'application/json')
417 it('Should return 404 with an incorrect video', function (done
) {
419 .get(path
+ '123456789012345678901234')
420 .set('Accept', 'application/json')
424 it('Should succeed with the correct parameters')
427 describe('When removing a video', function () {
428 it('Should have 404 with nothing', function (done
) {
431 .set('Authorization', 'Bearer ' + server
.accessToken
)
435 it('Should fail without a mongodb id', function (done
) {
437 .delete(path
+ 'hello')
438 .set('Authorization', 'Bearer ' + server
.accessToken
)
442 it('Should fail with a video which does not exist', function (done
) {
444 .delete(path
+ '123456789012345678901234')
445 .set('Authorization', 'Bearer ' + server
.accessToken
)
449 it('Should fail with a video of another user')
451 it('Should fail with a video of another pod')
453 it('Should succeed with the correct parameters')
457 describe('Of the users API', function () {
458 const path
= '/api/v1/users/'
460 let userAccessToken
= null
462 describe('When adding a new user', function () {
463 it('Should fail with a too small username', function (done
) {
466 password: 'mysuperpassword'
469 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
)
472 it('Should fail with a too long username', function (done
) {
474 username: 'mysuperusernamewhichisverylong',
475 password: 'mysuperpassword'
478 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
)
481 it('Should fail with an incorrect username', function (done
) {
483 username: 'my username',
484 password: 'mysuperpassword'
487 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
)
490 it('Should fail with a too small password', function (done
) {
492 username: 'myusername',
496 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
)
499 it('Should fail with a too long password', function (done
) {
501 username: 'myusername',
502 password: 'my super long password which is very very very very very very very very very very very very very very' +
503 'very very very very very very very very very very very very very very very veryv very very very very' +
504 'very very very very very very very very very very very very very very very very very very very very long'
507 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
)
510 it('Should fail with an non authenticated user', function (done
) {
512 username: 'myusername',
513 password: 'my super password'
516 requestsUtils
.makePostBodyRequest(server
.url
, path
, 'super token', data
, done
, 401)
519 it('Should succeed with the correct params', function (done
) {
522 password: 'my super password'
525 requestsUtils
.makePostBodyRequest(server
.url
, path
, server
.accessToken
, data
, done
, 204)
528 it('Should fail with a non admin user', function (done
) {
531 password: 'my super password'
534 loginUtils
.loginAndGetAccessToken(server
, function (err
, accessToken
) {
537 userAccessToken
= accessToken
541 password: 'my super password'
544 requestsUtils
.makePostBodyRequest(server
.url
, path
, userAccessToken
, data
, done
, 403)
549 describe('When updating a user', function () {
550 before(function (done
) {
551 usersUtils
.getUsersList(server
.url
, function (err
, res
) {
554 userId
= res
.body
.data
[1].id
559 it('Should fail with a too small password', function (done
) {
564 requestsUtils
.makePutBodyRequest(server
.url
, path
+ userId
, userAccessToken
, data
, done
)
567 it('Should fail with a too long password', function (done
) {
569 password: 'my super long password which is very very very very very very very very very very very very very very' +
570 'very very very very very very very very very very very very very very very veryv very very very very' +
571 'very very very very very very very very very very very very very very very very very very very very long'
574 requestsUtils
.makePutBodyRequest(server
.url
, path
+ userId
, userAccessToken
, data
, done
)
577 it('Should fail with an non authenticated user', function (done
) {
579 password: 'my super password'
582 requestsUtils
.makePutBodyRequest(server
.url
, path
+ userId
, 'super token', data
, done
, 401)
585 it('Should succeed with the correct params', function (done
) {
587 password: 'my super password'
590 requestsUtils
.makePutBodyRequest(server
.url
, path
+ userId
, userAccessToken
, data
, done
, 204)
594 describe('When getting my information', function () {
595 it('Should fail with a non authenticated user', function (done
) {
598 .set('Authorization', 'Bearer faketoken')
599 .set('Accept', 'application/json')
603 it('Should success with the correct parameters', function (done
) {
606 .set('Authorization', 'Bearer ' + userAccessToken
)
607 .set('Accept', 'application/json')
612 describe('When removing an user', function () {
613 it('Should fail with an incorrect id', function (done
) {
615 .delete(path
+ 'bla-bla')
616 .set('Authorization', 'Bearer ' + server
.accessToken
)
620 it('Should return 404 with a non existing id', function (done
) {
622 .delete(path
+ '579f982228c99c221d8092b8')
623 .set('Authorization', 'Bearer ' + server
.accessToken
)
627 it('Should success with the correct parameters', function (done
) {
629 .delete(path
+ userId
)
630 .set('Authorization', 'Bearer ' + server
.accessToken
)
636 describe('Of the remote videos API', function () {
637 describe('When making a secure request', function () {
638 it('Should check a secure request')
641 describe('When adding a video', function () {
642 it('Should check when adding a video')
645 describe('When removing a video', function () {
646 it('Should check when removing a video')
650 after(function (done
) {
651 process
.kill(-server
.app
.pid
)
653 // Keep the logs if the test failed
655 serversUtils
.flushTests(done
)