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