]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/checkParams.js
Implement user API (create, update, remove, list)
[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/'
507
508 describe('When adding a new user', function () {
509 it('Should fail with a too small username', function (done) {
510 const data = {
511 username: 'ji',
512 password: 'mysuperpassword'
513 }
514
515 makePostBodyRequest(path, server.accessToken, data, done)
516 })
517
518 it('Should fail with a too long username', function (done) {
519 const data = {
520 username: 'mysuperusernamewhichisverylong',
521 password: 'mysuperpassword'
522 }
523
524 makePostBodyRequest(path, server.accessToken, data, done)
525 })
526
527 it('Should fail with an incorrect username', function (done) {
528 const data = {
529 username: 'my username',
530 password: 'mysuperpassword'
531 }
532
533 makePostBodyRequest(path, server.accessToken, data, done)
534 })
535
536 it('Should fail with a too small password', function (done) {
537 const data = {
538 username: 'myusername',
539 password: 'bla'
540 }
541
542 makePostBodyRequest(path, server.accessToken, data, done)
543 })
544
545 it('Should fail with a too long password', function (done) {
546 const data = {
547 username: 'myusername',
548 password: 'my super long password which is very very very very very very very very very very very very very very' +
549 'very very very very very very very very very very very very very very very veryv very very very very' +
550 'very very very very very very very very very very very very very very very very very very very very long'
551 }
552
553 makePostBodyRequest(path, server.accessToken, data, done)
554 })
555
556 it('Should fail with an non authenticated user', function (done) {
557 const data = {
558 username: 'myusername',
559 password: 'my super password'
560 }
561
562 makePostBodyRequest(path, 'super token', data, done, 401)
563 })
564
565 it('Should succeed with the correct params', function (done) {
566 const data = {
567 username: 'user1',
568 password: 'my super password'
569 }
570
571 makePostBodyRequest(path, server.accessToken, data, done, 204)
572 })
573
574 it('Should fail with a non admin user', function (done) {
575 server.user = {
576 username: 'user1',
577 password: 'my super password'
578 }
579
580 utils.loginAndGetAccessToken(server, function (err, accessToken) {
581 if (err) throw err
582
583 const data = {
584 username: 'user2',
585 password: 'my super password'
586 }
587
588 makePostBodyRequest(path, accessToken, data, done, 403)
589 })
590 })
591 })
592
593 describe('When updating a user', function () {
594 let userId = null
595
596 before(function (done) {
597 utils.getUsersList(server.url, function (err, res) {
598 if (err) throw err
599
600 userId = res.body.data[1].id
601 done()
602 })
603 })
604
605 it('Should fail with a too small password', function (done) {
606 const data = {
607 password: 'bla'
608 }
609
610 makePutBodyRequest(path + '/' + userId, server.accessToken, data, done)
611 })
612
613 it('Should fail with a too long password', function (done) {
614 const data = {
615 password: 'my super long password which is very very very very very very very very very very very very very very' +
616 'very very very very very very very very very very very very very very very veryv very very very very' +
617 'very very very very very very very very very very very very very very very very very very very very long'
618 }
619
620 makePutBodyRequest(path + '/' + userId, server.accessToken, data, done)
621 })
622
623 it('Should fail with an non authenticated user', function (done) {
624 const data = {
625 password: 'my super password'
626 }
627
628 makePutBodyRequest(path + '/' + userId, 'super token', data, done, 401)
629 })
630
631 it('Should succeed with the correct params', function (done) {
632 const data = {
633 password: 'my super password'
634 }
635
636 makePutBodyRequest(path + '/' + userId, server.accessToken, data, done, 204)
637 })
638 })
639
640 describe('When removing an user', function () {
641 it('Should fail with an incorrect username', function (done) {
642 request(server.url)
643 .delete(path + 'bla-bla')
644 .set('Authorization', 'Bearer ' + server.accessToken)
645 .expect(400, done)
646 })
647
648 it('Should return 404 with a non existing username', function (done) {
649 request(server.url)
650 .delete(path + 'qzzerg')
651 .set('Authorization', 'Bearer ' + server.accessToken)
652 .expect(404, done)
653 })
654
655 it('Should success with the correct parameters', function (done) {
656 request(server.url)
657 .delete(path + 'user1')
658 .set('Authorization', 'Bearer ' + server.accessToken)
659 .expect(204, done)
660 })
661 })
662 })
663
9f10b292
C
664 describe('Of the remote videos API', function () {
665 describe('When making a secure request', function () {
666 it('Should check a secure request')
667 })
34ca3b52 668
9f10b292
C
669 describe('When adding a video', function () {
670 it('Should check when adding a video')
34ca3b52 671 })
9f10b292
C
672
673 describe('When removing a video', function () {
674 it('Should check when removing a video')
675 })
676 })
677
678 after(function (done) {
0c1cbbfe 679 process.kill(-server.app.pid)
9f10b292
C
680
681 // Keep the logs if the test failed
682 if (this.ok) {
683 utils.flushTests(done)
684 } else {
685 done()
686 }
34ca3b52 687 })
9f10b292 688})