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