]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/checkParams.js
Server: update dev dependencies
[github/Chocobozzz/PeerTube.git] / server / tests / api / checkParams.js
CommitLineData
9f10b292 1'use strict'
34ca3b52 2
f0f5567b
C
3const chai = require('chai')
4const expect = chai.expect
5const pathUtils = require('path')
6const request = require('supertest')
1a42c9e2 7const series = require('async/series')
34ca3b52 8
8d309058 9const loginUtils = require('../utils/login')
25ed57f3 10const requestsUtils = require('../utils/requests')
8d309058
C
11const serversUtils = require('../utils/servers')
12const usersUtils = require('../utils/users')
34ca3b52 13
9f10b292 14describe('Test parameters validator', function () {
0c1cbbfe 15 let server = null
34ca3b52 16
9f10b292
C
17 // ---------------------------------------------------------------
18
19 before(function (done) {
20 this.timeout(20000)
21
1a42c9e2 22 series([
9f10b292 23 function (next) {
8d309058 24 serversUtils.flushTests(next)
9f10b292
C
25 },
26 function (next) {
8d309058 27 serversUtils.runServer(1, function (server1) {
0c1cbbfe
C
28 server = server1
29
30 next()
31 })
32 },
33 function (next) {
8d309058 34 loginUtils.loginAndGetAccessToken(server, function (err, token) {
0c1cbbfe 35 if (err) throw err
b6c6f935 36 server.accessToken = token
0c1cbbfe 37
9f10b292 38 next()
34ca3b52 39 })
9f10b292
C
40 }
41 ], done)
42 })
43
44 describe('Of the pods API', function () {
f0f5567b 45 const path = '/api/v1/pods/'
9f10b292
C
46
47 describe('When adding a pod', function () {
48 it('Should fail with nothing', function (done) {
f0f5567b 49 const data = {}
25ed57f3 50 requestsUtils.makePostBodyRequest(server.url, path, null, data, done)
9f10b292 51 })
34ca3b52 52
9f10b292 53 it('Should fail without public key', function (done) {
f0f5567b 54 const data = {
528a9efa 55 url: 'http://coucou.com'
9f10b292 56 }
25ed57f3 57 requestsUtils.makePostBodyRequest(server.url, path, null, data, done)
9f10b292 58 })
34ca3b52 59
9f10b292 60 it('Should fail without an url', function (done) {
f0f5567b 61 const data = {
528a9efa 62 publicKey: 'mysuperpublickey'
9f10b292 63 }
25ed57f3 64 requestsUtils.makePostBodyRequest(server.url, path, null, data, done)
9f10b292 65 })
34ca3b52 66
9f10b292 67 it('Should fail with an incorrect url', function (done) {
f0f5567b 68 const data = {
528a9efa
C
69 url: 'coucou.com',
70 publicKey: 'mysuperpublickey'
9f10b292 71 }
25ed57f3 72 requestsUtils.makePostBodyRequest(server.url, path, null, data, function () {
528a9efa 73 data.url = 'http://coucou'
25ed57f3 74 requestsUtils.makePostBodyRequest(server.url, path, null, data, function () {
528a9efa 75 data.url = 'coucou'
25ed57f3 76 requestsUtils.makePostBodyRequest(server.url, path, null, data, done)
34ca3b52
C
77 })
78 })
9f10b292 79 })
34ca3b52 80
9f10b292 81 it('Should succeed with the correct parameters', function (done) {
f0f5567b 82 const data = {
528a9efa
C
83 url: 'http://coucou.com',
84 publicKey: 'mysuperpublickey'
9f10b292 85 }
25ed57f3 86 requestsUtils.makePostBodyRequest(server.url, path, null, data, done, 200)
9bd26629
C
87 })
88 })
89
90 describe('For the friends API', function () {
91 let userAccessToken = null
92
93 before(function (done) {
8d309058 94 usersUtils.createUser(server.url, server.accessToken, 'user1', 'password', function () {
9bd26629
C
95 server.user = {
96 username: 'user1',
97 password: 'password'
98 }
99
8d309058 100 loginUtils.loginAndGetAccessToken(server, function (err, accessToken) {
9bd26629
C
101 if (err) throw err
102
103 userAccessToken = accessToken
104
105 done()
106 })
107 })
108 })
109
110 describe('When making friends', function () {
111 it('Should fail with a invalid token', function (done) {
112 request(server.url)
113 .get(path + '/makefriends')
114 .query({ start: 'hello' })
115 .set('Authorization', 'Bearer faketoken')
116 .set('Accept', 'application/json')
117 .expect(401, done)
118 })
119
120 it('Should fail if the user is not an administrator', function (done) {
121 request(server.url)
122 .get(path + '/makefriends')
123 .query({ start: 'hello' })
124 .set('Authorization', 'Bearer ' + userAccessToken)
125 .set('Accept', 'application/json')
126 .expect(403, done)
127 })
128 })
129
130 describe('When quitting friends', function () {
131 it('Should fail with a invalid token', function (done) {
132 request(server.url)
133 .get(path + '/quitfriends')
134 .query({ start: 'hello' })
135 .set('Authorization', 'Bearer faketoken')
136 .set('Accept', 'application/json')
137 .expect(401, done)
138 })
139
140 it('Should fail if the user is not an administrator', function (done) {
141 request(server.url)
142 .get(path + '/quitfriends')
143 .query({ start: 'hello' })
144 .set('Authorization', 'Bearer ' + userAccessToken)
145 .set('Accept', 'application/json')
146 .expect(403, done)
147 })
34ca3b52
C
148 })
149 })
9f10b292 150 })
34ca3b52 151
9f10b292 152 describe('Of the videos API', function () {
f0f5567b 153 const path = '/api/v1/videos/'
34ca3b52 154
a877d5ac
C
155 describe('When listing a video', function () {
156 it('Should fail with a bad start pagination', function (done) {
157 request(server.url)
158 .get(path)
159 .query({ start: 'hello' })
160 .set('Accept', 'application/json')
161 .expect(400, done)
162 })
163
164 it('Should fail with a bad count pagination', function (done) {
165 request(server.url)
166 .get(path)
167 .query({ count: 'hello' })
168 .set('Accept', 'application/json')
169 .expect(400, done)
170 })
171
172 it('Should fail with an incorrect sort', function (done) {
173 request(server.url)
174 .get(path)
175 .query({ sort: 'hello' })
176 .set('Accept', 'application/json')
177 .expect(400, done)
178 })
179 })
180
9f10b292
C
181 describe('When searching a video', function () {
182 it('Should fail with nothing', function (done) {
0c1cbbfe 183 request(server.url)
9f10b292
C
184 .get(pathUtils.join(path, 'search'))
185 .set('Accept', 'application/json')
186 .expect(400, done)
34ca3b52 187 })
a877d5ac
C
188
189 it('Should fail with a bad start pagination', function (done) {
190 request(server.url)
191 .get(pathUtils.join(path, 'search', 'test'))
192 .query({ start: 'hello' })
193 .set('Accept', 'application/json')
194 .expect(400, done)
195 })
196
197 it('Should fail with a bad count pagination', function (done) {
198 request(server.url)
199 .get(pathUtils.join(path, 'search', 'test'))
200 .query({ count: 'hello' })
201 .set('Accept', 'application/json')
202 .expect(400, done)
203 })
204
205 it('Should fail with an incorrect sort', function (done) {
206 request(server.url)
207 .get(pathUtils.join(path, 'search', 'test'))
208 .query({ sort: 'hello' })
209 .set('Accept', 'application/json')
210 .expect(400, done)
211 })
9f10b292 212 })
34ca3b52 213
9f10b292
C
214 describe('When adding a video', function () {
215 it('Should fail with nothing', function (done) {
f0f5567b
C
216 const data = {}
217 const attach = {}
25ed57f3 218 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
9f10b292 219 })
34ca3b52 220
9f10b292 221 it('Should fail without name', function (done) {
f0f5567b 222 const data = {
be587647
C
223 description: 'my super description',
224 tags: [ 'tag1', 'tag2' ]
9f10b292 225 }
f0f5567b 226 const attach = {
8c9c1942 227 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
9f10b292 228 }
25ed57f3 229 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
9f10b292 230 })
34ca3b52 231
9f10b292 232 it('Should fail with a long name', function (done) {
f0f5567b 233 const data = {
9f10b292 234 name: 'My very very very very very very very very very very very very very very very very long name',
be587647
C
235 description: 'my super description',
236 tags: [ 'tag1', 'tag2' ]
9f10b292 237 }
f0f5567b 238 const attach = {
8c9c1942 239 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
9f10b292 240 }
25ed57f3 241 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
9f10b292 242 })
34ca3b52 243
9f10b292 244 it('Should fail without description', function (done) {
f0f5567b 245 const data = {
be587647
C
246 name: 'my super name',
247 tags: [ 'tag1', 'tag2' ]
9f10b292 248 }
f0f5567b 249 const attach = {
8c9c1942 250 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
9f10b292 251 }
25ed57f3 252 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
9f10b292 253 })
34ca3b52 254
9f10b292 255 it('Should fail with a long description', function (done) {
f0f5567b 256 const data = {
9f10b292
C
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' +
be587647
C
260 'very very very very very very very very very very very very very very very long',
261 tags: [ 'tag1', 'tag2' ]
9f10b292 262 }
f0f5567b 263 const attach = {
8c9c1942 264 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
9f10b292 265 }
25ed57f3 266 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
9f10b292 267 })
34ca3b52 268
be587647 269 it('Should fail without tags', function (done) {
f0f5567b 270 const data = {
9f10b292
C
271 name: 'my super name',
272 description: 'my super description'
273 }
be587647
C
274 const attach = {
275 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
276 }
25ed57f3 277 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
be587647
C
278 })
279
280 it('Should fail with too many tags', function (done) {
281 const data = {
282 name: 'my super name',
283 description: 'my super description',
284 tags: [ 'tag1', 'tag2', 'tag3', 'tag4' ]
285 }
286 const attach = {
287 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
288 }
25ed57f3 289 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
be587647
C
290 })
291
292 it('Should fail with not enough tags', function (done) {
293 const data = {
294 name: 'my super name',
295 description: 'my super description',
296 tags: [ ]
297 }
298 const attach = {
299 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
300 }
25ed57f3 301 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
be587647
C
302 })
303
304 it('Should fail with a tag length too low', function (done) {
305 const data = {
306 name: 'my super name',
307 description: 'my super description',
308 tags: [ 'tag1', 't' ]
309 }
310 const attach = {
311 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
312 }
25ed57f3 313 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
be587647
C
314 })
315
316 it('Should fail with a tag length too big', function (done) {
317 const data = {
318 name: 'my super name',
319 description: 'my super description',
320 tags: [ 'mysupertagtoolong', 'tag1' ]
321 }
322 const attach = {
323 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
324 }
25ed57f3 325 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
be587647
C
326 })
327
328 it('Should fail with malformed tags', function (done) {
329 const data = {
330 name: 'my super name',
331 description: 'my super description',
332 tags: [ 'my tag' ]
333 }
334 const attach = {
335 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
336 }
25ed57f3 337 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
be587647
C
338 })
339
340 it('Should fail without an input file', function (done) {
341 const data = {
342 name: 'my super name',
343 description: 'my super description',
344 tags: [ 'tag1', 'tag2' ]
345 }
f0f5567b 346 const attach = {}
25ed57f3 347 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
9f10b292 348 })
34ca3b52 349
9f10b292 350 it('Should fail without an incorrect input file', function (done) {
f0f5567b 351 const data = {
9f10b292 352 name: 'my super name',
be587647
C
353 description: 'my super description',
354 tags: [ 'tag1', 'tag2' ]
9f10b292 355 }
f0f5567b 356 const attach = {
67100f1f
C
357 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short_fake.webm')
358 }
25ed57f3 359 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
67100f1f
C
360 })
361
362 it('Should fail with a too big duration', function (done) {
363 const data = {
364 name: 'my super name',
be587647
C
365 description: 'my super description',
366 tags: [ 'tag1', 'tag2' ]
67100f1f
C
367 }
368 const attach = {
369 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_too_long.webm')
9f10b292 370 }
25ed57f3 371 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
9f10b292 372 })
34ca3b52 373
9f10b292 374 it('Should succeed with the correct parameters', function (done) {
f0f5567b 375 const data = {
9f10b292 376 name: 'my super name',
be587647
C
377 description: 'my super description',
378 tags: [ 'tag1', 'tag2' ]
9f10b292 379 }
f0f5567b 380 const attach = {
8c9c1942 381 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
9f10b292 382 }
25ed57f3 383 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, function () {
8c9c1942 384 attach.videofile = pathUtils.join(__dirname, 'fixtures', 'video_short.mp4')
25ed57f3 385 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, function () {
8c9c1942 386 attach.videofile = pathUtils.join(__dirname, 'fixtures', 'video_short.ogv')
25ed57f3 387 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done, 204)
67100f1f
C
388 }, false)
389 }, false)
34ca3b52 390 })
9f10b292 391 })
34ca3b52 392
9f10b292
C
393 describe('When getting a video', function () {
394 it('Should return the list of the videos with nothing', function (done) {
0c1cbbfe 395 request(server.url)
9f10b292
C
396 .get(path)
397 .set('Accept', 'application/json')
398 .expect(200)
399 .expect('Content-Type', /json/)
400 .end(function (err, res) {
401 if (err) throw err
34ca3b52 402
68ce3ae0
C
403 expect(res.body.data).to.be.an('array')
404 expect(res.body.data.length).to.equal(3)
34ca3b52 405
9f10b292
C
406 done()
407 })
408 })
34ca3b52 409
9f10b292 410 it('Should fail without a mongodb id', function (done) {
0c1cbbfe 411 request(server.url)
9f10b292
C
412 .get(path + 'coucou')
413 .set('Accept', 'application/json')
414 .expect(400, done)
34ca3b52
C
415 })
416
9f10b292 417 it('Should return 404 with an incorrect video', function (done) {
0c1cbbfe 418 request(server.url)
9f10b292
C
419 .get(path + '123456789012345678901234')
420 .set('Accept', 'application/json')
34ca3b52 421 .expect(404, done)
34ca3b52 422 })
9f10b292
C
423
424 it('Should succeed with the correct parameters')
34ca3b52
C
425 })
426
9f10b292
C
427 describe('When removing a video', function () {
428 it('Should have 404 with nothing', function (done) {
0c1cbbfe
C
429 request(server.url)
430 .delete(path)
b6c6f935 431 .set('Authorization', 'Bearer ' + server.accessToken)
0c1cbbfe 432 .expect(400, done)
34ca3b52
C
433 })
434
9f10b292 435 it('Should fail without a mongodb id', function (done) {
0c1cbbfe 436 request(server.url)
9f10b292 437 .delete(path + 'hello')
b6c6f935 438 .set('Authorization', 'Bearer ' + server.accessToken)
9f10b292 439 .expect(400, done)
34ca3b52
C
440 })
441
9f10b292 442 it('Should fail with a video which does not exist', function (done) {
0c1cbbfe 443 request(server.url)
9f10b292 444 .delete(path + '123456789012345678901234')
b6c6f935 445 .set('Authorization', 'Bearer ' + server.accessToken)
9f10b292 446 .expect(404, done)
34ca3b52 447 })
9f10b292 448
58b2ba55
C
449 it('Should fail with a video of another user')
450
9f10b292
C
451 it('Should fail with a video of another pod')
452
453 it('Should succeed with the correct parameters')
34ca3b52 454 })
9f10b292 455 })
34ca3b52 456
9bd26629
C
457 describe('Of the users API', function () {
458 const path = '/api/v1/users/'
99a64bfe
C
459 let userId = null
460 let userAccessToken = null
9bd26629
C
461
462 describe('When adding a new user', function () {
463 it('Should fail with a too small username', function (done) {
464 const data = {
465 username: 'ji',
466 password: 'mysuperpassword'
467 }
468
25ed57f3 469 requestsUtils.makePostBodyRequest(server.url, path, server.accessToken, data, done)
9bd26629
C
470 })
471
472 it('Should fail with a too long username', function (done) {
473 const data = {
474 username: 'mysuperusernamewhichisverylong',
475 password: 'mysuperpassword'
476 }
477
25ed57f3 478 requestsUtils.makePostBodyRequest(server.url, path, server.accessToken, data, done)
9bd26629
C
479 })
480
481 it('Should fail with an incorrect username', function (done) {
482 const data = {
483 username: 'my username',
484 password: 'mysuperpassword'
485 }
486
25ed57f3 487 requestsUtils.makePostBodyRequest(server.url, path, server.accessToken, data, done)
9bd26629
C
488 })
489
490 it('Should fail with a too small password', function (done) {
491 const data = {
492 username: 'myusername',
493 password: 'bla'
494 }
495
25ed57f3 496 requestsUtils.makePostBodyRequest(server.url, path, server.accessToken, data, done)
9bd26629
C
497 })
498
499 it('Should fail with a too long password', function (done) {
500 const data = {
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'
505 }
506
25ed57f3 507 requestsUtils.makePostBodyRequest(server.url, path, server.accessToken, data, done)
9bd26629
C
508 })
509
510 it('Should fail with an non authenticated user', function (done) {
511 const data = {
512 username: 'myusername',
513 password: 'my super password'
514 }
515
25ed57f3 516 requestsUtils.makePostBodyRequest(server.url, path, 'super token', data, done, 401)
9bd26629
C
517 })
518
519 it('Should succeed with the correct params', function (done) {
520 const data = {
521 username: 'user1',
522 password: 'my super password'
523 }
524
25ed57f3 525 requestsUtils.makePostBodyRequest(server.url, path, server.accessToken, data, done, 204)
9bd26629
C
526 })
527
528 it('Should fail with a non admin user', function (done) {
529 server.user = {
530 username: 'user1',
531 password: 'my super password'
532 }
533
8d309058 534 loginUtils.loginAndGetAccessToken(server, function (err, accessToken) {
9bd26629
C
535 if (err) throw err
536
99a64bfe
C
537 userAccessToken = accessToken
538
9bd26629
C
539 const data = {
540 username: 'user2',
541 password: 'my super password'
542 }
543
25ed57f3 544 requestsUtils.makePostBodyRequest(server.url, path, userAccessToken, data, done, 403)
9bd26629
C
545 })
546 })
547 })
548
549 describe('When updating a user', function () {
9bd26629 550 before(function (done) {
8d309058 551 usersUtils.getUsersList(server.url, function (err, res) {
9bd26629
C
552 if (err) throw err
553
554 userId = res.body.data[1].id
555 done()
556 })
557 })
558
559 it('Should fail with a too small password', function (done) {
560 const data = {
561 password: 'bla'
562 }
563
25ed57f3 564 requestsUtils.makePutBodyRequest(server.url, path + userId, userAccessToken, data, done)
9bd26629
C
565 })
566
567 it('Should fail with a too long password', function (done) {
568 const data = {
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'
572 }
573
25ed57f3 574 requestsUtils.makePutBodyRequest(server.url, path + userId, userAccessToken, data, done)
9bd26629
C
575 })
576
577 it('Should fail with an non authenticated user', function (done) {
578 const data = {
579 password: 'my super password'
580 }
581
25ed57f3 582 requestsUtils.makePutBodyRequest(server.url, path + userId, 'super token', data, done, 401)
9bd26629
C
583 })
584
585 it('Should succeed with the correct params', function (done) {
586 const data = {
587 password: 'my super password'
588 }
589
25ed57f3 590 requestsUtils.makePutBodyRequest(server.url, path + userId, userAccessToken, data, done, 204)
99a64bfe
C
591 })
592 })
593
594 describe('When getting my information', function () {
595 it('Should fail with a non authenticated user', function (done) {
596 request(server.url)
597 .get(path + 'me')
598 .set('Authorization', 'Bearer faketoken')
599 .set('Accept', 'application/json')
600 .expect(401, done)
601 })
602
603 it('Should success with the correct parameters', function (done) {
604 request(server.url)
605 .get(path + 'me')
606 .set('Authorization', 'Bearer ' + userAccessToken)
607 .set('Accept', 'application/json')
608 .expect(200, done)
9bd26629
C
609 })
610 })
611
612 describe('When removing an user', function () {
613 it('Should fail with an incorrect username', function (done) {
614 request(server.url)
615 .delete(path + 'bla-bla')
616 .set('Authorization', 'Bearer ' + server.accessToken)
617 .expect(400, done)
618 })
619
620 it('Should return 404 with a non existing username', function (done) {
621 request(server.url)
622 .delete(path + 'qzzerg')
623 .set('Authorization', 'Bearer ' + server.accessToken)
624 .expect(404, done)
625 })
626
627 it('Should success with the correct parameters', function (done) {
628 request(server.url)
629 .delete(path + 'user1')
630 .set('Authorization', 'Bearer ' + server.accessToken)
631 .expect(204, done)
632 })
633 })
634 })
635
9f10b292
C
636 describe('Of the remote videos API', function () {
637 describe('When making a secure request', function () {
638 it('Should check a secure request')
639 })
34ca3b52 640
9f10b292
C
641 describe('When adding a video', function () {
642 it('Should check when adding a video')
34ca3b52 643 })
9f10b292
C
644
645 describe('When removing a video', function () {
646 it('Should check when removing a video')
647 })
648 })
649
650 after(function (done) {
0c1cbbfe 651 process.kill(-server.app.pid)
9f10b292
C
652
653 // Keep the logs if the test failed
654 if (this.ok) {
8d309058 655 serversUtils.flushTests(done)
9f10b292
C
656 } else {
657 done()
658 }
34ca3b52 659 })
9f10b292 660})