]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/checkParams.js
Server: fix tests
[github/Chocobozzz/PeerTube.git] / server / tests / api / checkParams.js
1 'use strict'
2
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')
8
9 const utils = require('./utils')
10
11 describe('Test parameters validator', function () {
12 let server = null
13
14 function makePostRequest (path, token, fields, attaches, done, statusCodeExpected) {
15 if (!statusCodeExpected) statusCodeExpected = 400
16
17 const req = request(server.url)
18 .post(path)
19 .set('Accept', 'application/json')
20
21 if (token) req.set('Authorization', 'Bearer ' + token)
22
23 Object.keys(fields).forEach(function (field) {
24 const value = fields[field]
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 }
33 })
34
35 Object.keys(attaches).forEach(function (attach) {
36 const value = attaches[attach]
37 req.attach(attach, value)
38 })
39
40 req.expect(statusCodeExpected, done)
41 }
42
43 function makePostBodyRequest (path, token, fields, done, statusCodeExpected) {
44 if (!statusCodeExpected) statusCodeExpected = 400
45
46 const req = request(server.url)
47 .post(path)
48 .set('Accept', 'application/json')
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)
65 }
66
67 // ---------------------------------------------------------------
68
69 before(function (done) {
70 this.timeout(20000)
71
72 series([
73 function (next) {
74 utils.flushTests(next)
75 },
76 function (next) {
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
86 server.accessToken = token
87
88 next()
89 })
90 }
91 ], done)
92 })
93
94 describe('Of the pods API', function () {
95 const path = '/api/v1/pods/'
96
97 describe('When adding a pod', function () {
98 it('Should fail with nothing', function (done) {
99 const data = {}
100 makePostBodyRequest(path, null, data, done)
101 })
102
103 it('Should fail without public key', function (done) {
104 const data = {
105 url: 'http://coucou.com'
106 }
107 makePostBodyRequest(path, null, data, done)
108 })
109
110 it('Should fail without an url', function (done) {
111 const data = {
112 publicKey: 'mysuperpublickey'
113 }
114 makePostBodyRequest(path, null, data, done)
115 })
116
117 it('Should fail with an incorrect url', function (done) {
118 const data = {
119 url: 'coucou.com',
120 publicKey: 'mysuperpublickey'
121 }
122 makePostBodyRequest(path, null, data, function () {
123 data.url = 'http://coucou'
124 makePostBodyRequest(path, null, data, function () {
125 data.url = 'coucou'
126 makePostBodyRequest(path, null, data, done)
127 })
128 })
129 })
130
131 it('Should succeed with the correct parameters', function (done) {
132 const data = {
133 url: 'http://coucou.com',
134 publicKey: 'mysuperpublickey'
135 }
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 })
198 })
199 })
200 })
201
202 describe('Of the videos API', function () {
203 const path = '/api/v1/videos/'
204
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
231 describe('When searching a video', function () {
232 it('Should fail with nothing', function (done) {
233 request(server.url)
234 .get(pathUtils.join(path, 'search'))
235 .set('Accept', 'application/json')
236 .expect(400, done)
237 })
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 })
262 })
263
264 describe('When adding a video', function () {
265 it('Should fail with nothing', function (done) {
266 const data = {}
267 const attach = {}
268 makePostRequest(path, server.accessToken, data, attach, done)
269 })
270
271 it('Should fail without name', function (done) {
272 const data = {
273 description: 'my super description',
274 tags: [ 'tag1', 'tag2' ]
275 }
276 const attach = {
277 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
278 }
279 makePostRequest(path, server.accessToken, data, attach, done)
280 })
281
282 it('Should fail with a long name', function (done) {
283 const data = {
284 name: 'My very very very very very very very very very very very very very very very very long name',
285 description: 'my super description',
286 tags: [ 'tag1', 'tag2' ]
287 }
288 const attach = {
289 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
290 }
291 makePostRequest(path, server.accessToken, data, attach, done)
292 })
293
294 it('Should fail without description', function (done) {
295 const data = {
296 name: 'my super name',
297 tags: [ 'tag1', 'tag2' ]
298 }
299 const attach = {
300 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
301 }
302 makePostRequest(path, server.accessToken, data, attach, done)
303 })
304
305 it('Should fail with a long description', function (done) {
306 const data = {
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' +
310 'very very very very very very very very very very very very very very very long',
311 tags: [ 'tag1', 'tag2' ]
312 }
313 const attach = {
314 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
315 }
316 makePostRequest(path, server.accessToken, data, attach, done)
317 })
318
319 it('Should fail without tags', function (done) {
320 const data = {
321 name: 'my super name',
322 description: 'my super description'
323 }
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 }
396 const attach = {}
397 makePostRequest(path, server.accessToken, data, attach, done)
398 })
399
400 it('Should fail without an incorrect input file', function (done) {
401 const data = {
402 name: 'my super name',
403 description: 'my super description',
404 tags: [ 'tag1', 'tag2' ]
405 }
406 const attach = {
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',
415 description: 'my super description',
416 tags: [ 'tag1', 'tag2' ]
417 }
418 const attach = {
419 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_too_long.webm')
420 }
421 makePostRequest(path, server.accessToken, data, attach, done)
422 })
423
424 it('Should succeed with the correct parameters', function (done) {
425 const data = {
426 name: 'my super name',
427 description: 'my super description',
428 tags: [ 'tag1', 'tag2' ]
429 }
430 const attach = {
431 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
432 }
433 makePostRequest(path, server.accessToken, data, attach, function () {
434 attach.videofile = pathUtils.join(__dirname, 'fixtures', 'video_short.mp4')
435 makePostRequest(path, server.accessToken, data, attach, function () {
436 attach.videofile = pathUtils.join(__dirname, 'fixtures', 'video_short.ogv')
437 makePostRequest(path, server.accessToken, data, attach, done, 204)
438 }, false)
439 }, false)
440 })
441 })
442
443 describe('When getting a video', function () {
444 it('Should return the list of the videos with nothing', function (done) {
445 request(server.url)
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
452
453 expect(res.body.data).to.be.an('array')
454 expect(res.body.data.length).to.equal(3)
455
456 done()
457 })
458 })
459
460 it('Should fail without a mongodb id', function (done) {
461 request(server.url)
462 .get(path + 'coucou')
463 .set('Accept', 'application/json')
464 .expect(400, done)
465 })
466
467 it('Should return 404 with an incorrect video', function (done) {
468 request(server.url)
469 .get(path + '123456789012345678901234')
470 .set('Accept', 'application/json')
471 .expect(404, done)
472 })
473
474 it('Should succeed with the correct parameters')
475 })
476
477 describe('When removing a video', function () {
478 it('Should have 404 with nothing', function (done) {
479 request(server.url)
480 .delete(path)
481 .set('Authorization', 'Bearer ' + server.accessToken)
482 .expect(400, done)
483 })
484
485 it('Should fail without a mongodb id', function (done) {
486 request(server.url)
487 .delete(path + 'hello')
488 .set('Authorization', 'Bearer ' + server.accessToken)
489 .expect(400, done)
490 })
491
492 it('Should fail with a video which does not exist', function (done) {
493 request(server.url)
494 .delete(path + '123456789012345678901234')
495 .set('Authorization', 'Bearer ' + server.accessToken)
496 .expect(404, done)
497 })
498
499 it('Should fail with a video of another pod')
500
501 it('Should succeed with the correct parameters')
502 })
503 })
504
505 describe('Of the users API', function () {
506 const path = '/api/v1/users/'
507 let userId = null
508 let userAccessToken = null
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
585 userAccessToken = accessToken
586
587 const data = {
588 username: 'user2',
589 password: 'my super password'
590 }
591
592 makePostBodyRequest(path, userAccessToken, data, done, 403)
593 })
594 })
595 })
596
597 describe('When updating a user', function () {
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
612 makePutBodyRequest(path + userId, userAccessToken, data, done)
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
622 makePutBodyRequest(path + userId, userAccessToken, data, done)
623 })
624
625 it('Should fail with an non authenticated user', function (done) {
626 const data = {
627 password: 'my super password'
628 }
629
630 makePutBodyRequest(path + userId, 'super token', data, done, 401)
631 })
632
633 it('Should succeed with the correct params', function (done) {
634 const data = {
635 password: 'my super password'
636 }
637
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)
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
684 describe('Of the remote videos API', function () {
685 describe('When making a secure request', function () {
686 it('Should check a secure request')
687 })
688
689 describe('When adding a video', function () {
690 it('Should check when adding a video')
691 })
692
693 describe('When removing a video', function () {
694 it('Should check when removing a video')
695 })
696 })
697
698 after(function (done) {
699 process.kill(-server.app.pid)
700
701 // Keep the logs if the test failed
702 if (this.ok) {
703 utils.flushTests(done)
704 } else {
705 done()
706 }
707 })
708 })