aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-08-07 22:18:14 +0200
committerChocobozzz <florian.bigard@gmail.com>2016-08-07 22:18:14 +0200
commit25ed57f3db6301a5e87020b66d33671598e61df1 (patch)
tree04f2264d0ec1fe5aec37dba208586bdf1bb2df5f /server
parent8d30905858245f12a42fc327d2d57cbfe062d548 (diff)
downloadPeerTube-25ed57f3db6301a5e87020b66d33671598e61df1.tar.gz
PeerTube-25ed57f3db6301a5e87020b66d33671598e61df1.tar.zst
PeerTube-25ed57f3db6301a5e87020b66d33671598e61df1.zip
Server: create requests utils module
Diffstat (limited to 'server')
-rw-r--r--server/tests/api/checkParams.js126
-rw-r--r--server/tests/utils/requests.js68
2 files changed, 105 insertions, 89 deletions
diff --git a/server/tests/api/checkParams.js b/server/tests/api/checkParams.js
index 675dc19e6..128b07c4a 100644
--- a/server/tests/api/checkParams.js
+++ b/server/tests/api/checkParams.js
@@ -7,65 +7,13 @@ const request = require('supertest')
7const series = require('async/series') 7const series = require('async/series')
8 8
9const loginUtils = require('../utils/login') 9const loginUtils = require('../utils/login')
10const requestsUtils = require('../utils/requests')
10const serversUtils = require('../utils/servers') 11const serversUtils = require('../utils/servers')
11const usersUtils = require('../utils/users') 12const usersUtils = require('../utils/users')
12 13
13describe('Test parameters validator', function () { 14describe('Test parameters validator', function () {
14 let server = null 15 let server = null
15 16
16 function makePostRequest (path, token, fields, attaches, done, statusCodeExpected) {
17 if (!statusCodeExpected) statusCodeExpected = 400
18
19 const req = request(server.url)
20 .post(path)
21 .set('Accept', 'application/json')
22
23 if (token) req.set('Authorization', 'Bearer ' + token)
24
25 Object.keys(fields).forEach(function (field) {
26 const value = fields[field]
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 }
35 })
36
37 Object.keys(attaches).forEach(function (attach) {
38 const value = attaches[attach]
39 req.attach(attach, value)
40 })
41
42 req.expect(statusCodeExpected, done)
43 }
44
45 function makePostBodyRequest (path, token, fields, done, statusCodeExpected) {
46 if (!statusCodeExpected) statusCodeExpected = 400
47
48 const req = request(server.url)
49 .post(path)
50 .set('Accept', 'application/json')
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)
67 }
68
69 // --------------------------------------------------------------- 17 // ---------------------------------------------------------------
70 18
71 before(function (done) { 19 before(function (done) {
@@ -99,21 +47,21 @@ describe('Test parameters validator', function () {
99 describe('When adding a pod', function () { 47 describe('When adding a pod', function () {
100 it('Should fail with nothing', function (done) { 48 it('Should fail with nothing', function (done) {
101 const data = {} 49 const data = {}
102 makePostBodyRequest(path, null, data, done) 50 requestsUtils.makePostBodyRequest(server.url, path, null, data, done)
103 }) 51 })
104 52
105 it('Should fail without public key', function (done) { 53 it('Should fail without public key', function (done) {
106 const data = { 54 const data = {
107 url: 'http://coucou.com' 55 url: 'http://coucou.com'
108 } 56 }
109 makePostBodyRequest(path, null, data, done) 57 requestsUtils.makePostBodyRequest(server.url, path, null, data, done)
110 }) 58 })
111 59
112 it('Should fail without an url', function (done) { 60 it('Should fail without an url', function (done) {
113 const data = { 61 const data = {
114 publicKey: 'mysuperpublickey' 62 publicKey: 'mysuperpublickey'
115 } 63 }
116 makePostBodyRequest(path, null, data, done) 64 requestsUtils.makePostBodyRequest(server.url, path, null, data, done)
117 }) 65 })
118 66
119 it('Should fail with an incorrect url', function (done) { 67 it('Should fail with an incorrect url', function (done) {
@@ -121,11 +69,11 @@ describe('Test parameters validator', function () {
121 url: 'coucou.com', 69 url: 'coucou.com',
122 publicKey: 'mysuperpublickey' 70 publicKey: 'mysuperpublickey'
123 } 71 }
124 makePostBodyRequest(path, null, data, function () { 72 requestsUtils.makePostBodyRequest(server.url, path, null, data, function () {
125 data.url = 'http://coucou' 73 data.url = 'http://coucou'
126 makePostBodyRequest(path, null, data, function () { 74 requestsUtils.makePostBodyRequest(server.url, path, null, data, function () {
127 data.url = 'coucou' 75 data.url = 'coucou'
128 makePostBodyRequest(path, null, data, done) 76 requestsUtils.makePostBodyRequest(server.url, path, null, data, done)
129 }) 77 })
130 }) 78 })
131 }) 79 })
@@ -135,7 +83,7 @@ describe('Test parameters validator', function () {
135 url: 'http://coucou.com', 83 url: 'http://coucou.com',
136 publicKey: 'mysuperpublickey' 84 publicKey: 'mysuperpublickey'
137 } 85 }
138 makePostBodyRequest(path, null, data, done, 200) 86 requestsUtils.makePostBodyRequest(server.url, path, null, data, done, 200)
139 }) 87 })
140 }) 88 })
141 89
@@ -267,7 +215,7 @@ describe('Test parameters validator', function () {
267 it('Should fail with nothing', function (done) { 215 it('Should fail with nothing', function (done) {
268 const data = {} 216 const data = {}
269 const attach = {} 217 const attach = {}
270 makePostRequest(path, server.accessToken, data, attach, done) 218 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
271 }) 219 })
272 220
273 it('Should fail without name', function (done) { 221 it('Should fail without name', function (done) {
@@ -278,7 +226,7 @@ describe('Test parameters validator', function () {
278 const attach = { 226 const attach = {
279 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') 227 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
280 } 228 }
281 makePostRequest(path, server.accessToken, data, attach, done) 229 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
282 }) 230 })
283 231
284 it('Should fail with a long name', function (done) { 232 it('Should fail with a long name', function (done) {
@@ -290,7 +238,7 @@ describe('Test parameters validator', function () {
290 const attach = { 238 const attach = {
291 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') 239 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
292 } 240 }
293 makePostRequest(path, server.accessToken, data, attach, done) 241 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
294 }) 242 })
295 243
296 it('Should fail without description', function (done) { 244 it('Should fail without description', function (done) {
@@ -301,7 +249,7 @@ describe('Test parameters validator', function () {
301 const attach = { 249 const attach = {
302 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') 250 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
303 } 251 }
304 makePostRequest(path, server.accessToken, data, attach, done) 252 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
305 }) 253 })
306 254
307 it('Should fail with a long description', function (done) { 255 it('Should fail with a long description', function (done) {
@@ -315,7 +263,7 @@ describe('Test parameters validator', function () {
315 const attach = { 263 const attach = {
316 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') 264 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
317 } 265 }
318 makePostRequest(path, server.accessToken, data, attach, done) 266 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
319 }) 267 })
320 268
321 it('Should fail without tags', function (done) { 269 it('Should fail without tags', function (done) {
@@ -326,7 +274,7 @@ describe('Test parameters validator', function () {
326 const attach = { 274 const attach = {
327 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') 275 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
328 } 276 }
329 makePostRequest(path, server.accessToken, data, attach, done) 277 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
330 }) 278 })
331 279
332 it('Should fail with too many tags', function (done) { 280 it('Should fail with too many tags', function (done) {
@@ -338,7 +286,7 @@ describe('Test parameters validator', function () {
338 const attach = { 286 const attach = {
339 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') 287 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
340 } 288 }
341 makePostRequest(path, server.accessToken, data, attach, done) 289 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
342 }) 290 })
343 291
344 it('Should fail with not enough tags', function (done) { 292 it('Should fail with not enough tags', function (done) {
@@ -350,7 +298,7 @@ describe('Test parameters validator', function () {
350 const attach = { 298 const attach = {
351 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') 299 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
352 } 300 }
353 makePostRequest(path, server.accessToken, data, attach, done) 301 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
354 }) 302 })
355 303
356 it('Should fail with a tag length too low', function (done) { 304 it('Should fail with a tag length too low', function (done) {
@@ -362,7 +310,7 @@ describe('Test parameters validator', function () {
362 const attach = { 310 const attach = {
363 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') 311 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
364 } 312 }
365 makePostRequest(path, server.accessToken, data, attach, done) 313 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
366 }) 314 })
367 315
368 it('Should fail with a tag length too big', function (done) { 316 it('Should fail with a tag length too big', function (done) {
@@ -374,7 +322,7 @@ describe('Test parameters validator', function () {
374 const attach = { 322 const attach = {
375 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') 323 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
376 } 324 }
377 makePostRequest(path, server.accessToken, data, attach, done) 325 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
378 }) 326 })
379 327
380 it('Should fail with malformed tags', function (done) { 328 it('Should fail with malformed tags', function (done) {
@@ -386,7 +334,7 @@ describe('Test parameters validator', function () {
386 const attach = { 334 const attach = {
387 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') 335 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
388 } 336 }
389 makePostRequest(path, server.accessToken, data, attach, done) 337 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
390 }) 338 })
391 339
392 it('Should fail without an input file', function (done) { 340 it('Should fail without an input file', function (done) {
@@ -396,7 +344,7 @@ describe('Test parameters validator', function () {
396 tags: [ 'tag1', 'tag2' ] 344 tags: [ 'tag1', 'tag2' ]
397 } 345 }
398 const attach = {} 346 const attach = {}
399 makePostRequest(path, server.accessToken, data, attach, done) 347 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
400 }) 348 })
401 349
402 it('Should fail without an incorrect input file', function (done) { 350 it('Should fail without an incorrect input file', function (done) {
@@ -408,7 +356,7 @@ describe('Test parameters validator', function () {
408 const attach = { 356 const attach = {
409 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short_fake.webm') 357 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short_fake.webm')
410 } 358 }
411 makePostRequest(path, server.accessToken, data, attach, done) 359 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
412 }) 360 })
413 361
414 it('Should fail with a too big duration', function (done) { 362 it('Should fail with a too big duration', function (done) {
@@ -420,7 +368,7 @@ describe('Test parameters validator', function () {
420 const attach = { 368 const attach = {
421 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_too_long.webm') 369 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_too_long.webm')
422 } 370 }
423 makePostRequest(path, server.accessToken, data, attach, done) 371 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done)
424 }) 372 })
425 373
426 it('Should succeed with the correct parameters', function (done) { 374 it('Should succeed with the correct parameters', function (done) {
@@ -432,11 +380,11 @@ describe('Test parameters validator', function () {
432 const attach = { 380 const attach = {
433 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') 381 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm')
434 } 382 }
435 makePostRequest(path, server.accessToken, data, attach, function () { 383 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, function () {
436 attach.videofile = pathUtils.join(__dirname, 'fixtures', 'video_short.mp4') 384 attach.videofile = pathUtils.join(__dirname, 'fixtures', 'video_short.mp4')
437 makePostRequest(path, server.accessToken, data, attach, function () { 385 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, function () {
438 attach.videofile = pathUtils.join(__dirname, 'fixtures', 'video_short.ogv') 386 attach.videofile = pathUtils.join(__dirname, 'fixtures', 'video_short.ogv')
439 makePostRequest(path, server.accessToken, data, attach, done, 204) 387 requestsUtils.makePostUploadRequest(server.url, path, server.accessToken, data, attach, done, 204)
440 }, false) 388 }, false)
441 }, false) 389 }, false)
442 }) 390 })
@@ -518,7 +466,7 @@ describe('Test parameters validator', function () {
518 password: 'mysuperpassword' 466 password: 'mysuperpassword'
519 } 467 }
520 468
521 makePostBodyRequest(path, server.accessToken, data, done) 469 requestsUtils.makePostBodyRequest(server.url, path, server.accessToken, data, done)
522 }) 470 })
523 471
524 it('Should fail with a too long username', function (done) { 472 it('Should fail with a too long username', function (done) {
@@ -527,7 +475,7 @@ describe('Test parameters validator', function () {
527 password: 'mysuperpassword' 475 password: 'mysuperpassword'
528 } 476 }
529 477
530 makePostBodyRequest(path, server.accessToken, data, done) 478 requestsUtils.makePostBodyRequest(server.url, path, server.accessToken, data, done)
531 }) 479 })
532 480
533 it('Should fail with an incorrect username', function (done) { 481 it('Should fail with an incorrect username', function (done) {
@@ -536,7 +484,7 @@ describe('Test parameters validator', function () {
536 password: 'mysuperpassword' 484 password: 'mysuperpassword'
537 } 485 }
538 486
539 makePostBodyRequest(path, server.accessToken, data, done) 487 requestsUtils.makePostBodyRequest(server.url, path, server.accessToken, data, done)
540 }) 488 })
541 489
542 it('Should fail with a too small password', function (done) { 490 it('Should fail with a too small password', function (done) {
@@ -545,7 +493,7 @@ describe('Test parameters validator', function () {
545 password: 'bla' 493 password: 'bla'
546 } 494 }
547 495
548 makePostBodyRequest(path, server.accessToken, data, done) 496 requestsUtils.makePostBodyRequest(server.url, path, server.accessToken, data, done)
549 }) 497 })
550 498
551 it('Should fail with a too long password', function (done) { 499 it('Should fail with a too long password', function (done) {
@@ -556,7 +504,7 @@ describe('Test parameters validator', function () {
556 'very very very very very very very very very very very very very very very very very very very very long' 504 'very very very very very very very very very very very very very very very very very very very very long'
557 } 505 }
558 506
559 makePostBodyRequest(path, server.accessToken, data, done) 507 requestsUtils.makePostBodyRequest(server.url, path, server.accessToken, data, done)
560 }) 508 })
561 509
562 it('Should fail with an non authenticated user', function (done) { 510 it('Should fail with an non authenticated user', function (done) {
@@ -565,7 +513,7 @@ describe('Test parameters validator', function () {
565 password: 'my super password' 513 password: 'my super password'
566 } 514 }
567 515
568 makePostBodyRequest(path, 'super token', data, done, 401) 516 requestsUtils.makePostBodyRequest(server.url, path, 'super token', data, done, 401)
569 }) 517 })
570 518
571 it('Should succeed with the correct params', function (done) { 519 it('Should succeed with the correct params', function (done) {
@@ -574,7 +522,7 @@ describe('Test parameters validator', function () {
574 password: 'my super password' 522 password: 'my super password'
575 } 523 }
576 524
577 makePostBodyRequest(path, server.accessToken, data, done, 204) 525 requestsUtils.makePostBodyRequest(server.url, path, server.accessToken, data, done, 204)
578 }) 526 })
579 527
580 it('Should fail with a non admin user', function (done) { 528 it('Should fail with a non admin user', function (done) {
@@ -593,7 +541,7 @@ describe('Test parameters validator', function () {
593 password: 'my super password' 541 password: 'my super password'
594 } 542 }
595 543
596 makePostBodyRequest(path, userAccessToken, data, done, 403) 544 requestsUtils.makePostBodyRequest(server.url, path, userAccessToken, data, done, 403)
597 }) 545 })
598 }) 546 })
599 }) 547 })
@@ -613,7 +561,7 @@ describe('Test parameters validator', function () {
613 password: 'bla' 561 password: 'bla'
614 } 562 }
615 563
616 makePutBodyRequest(path + userId, userAccessToken, data, done) 564 requestsUtils.makePutBodyRequest(server.url, path + userId, userAccessToken, data, done)
617 }) 565 })
618 566
619 it('Should fail with a too long password', function (done) { 567 it('Should fail with a too long password', function (done) {
@@ -623,7 +571,7 @@ describe('Test parameters validator', function () {
623 'very very very very very very very very very very very very very very very very very very very very long' 571 'very very very very very very very very very very very very very very very very very very very very long'
624 } 572 }
625 573
626 makePutBodyRequest(path + userId, userAccessToken, data, done) 574 requestsUtils.makePutBodyRequest(server.url, path + userId, userAccessToken, data, done)
627 }) 575 })
628 576
629 it('Should fail with an non authenticated user', function (done) { 577 it('Should fail with an non authenticated user', function (done) {
@@ -631,7 +579,7 @@ describe('Test parameters validator', function () {
631 password: 'my super password' 579 password: 'my super password'
632 } 580 }
633 581
634 makePutBodyRequest(path + userId, 'super token', data, done, 401) 582 requestsUtils.makePutBodyRequest(server.url, path + userId, 'super token', data, done, 401)
635 }) 583 })
636 584
637 it('Should succeed with the correct params', function (done) { 585 it('Should succeed with the correct params', function (done) {
@@ -639,7 +587,7 @@ describe('Test parameters validator', function () {
639 password: 'my super password' 587 password: 'my super password'
640 } 588 }
641 589
642 makePutBodyRequest(path + userId, userAccessToken, data, done, 204) 590 requestsUtils.makePutBodyRequest(server.url, path + userId, userAccessToken, data, done, 204)
643 }) 591 })
644 }) 592 })
645 593
diff --git a/server/tests/utils/requests.js b/server/tests/utils/requests.js
new file mode 100644
index 000000000..410e42c77
--- /dev/null
+++ b/server/tests/utils/requests.js
@@ -0,0 +1,68 @@
1'use strict'
2
3const request = require('supertest')
4
5const requestsUtils = {
6 makePostUploadRequest: makePostUploadRequest,
7 makePostBodyRequest: makePostBodyRequest,
8 makePutBodyRequest: makePutBodyRequest
9}
10
11// ---------------------- Export functions --------------------
12
13function makePostUploadRequest (url, path, token, fields, attaches, done, statusCodeExpected) {
14 if (!statusCodeExpected) statusCodeExpected = 400
15
16 const req = request(url)
17 .post(path)
18 .set('Accept', 'application/json')
19
20 if (token) req.set('Authorization', 'Bearer ' + token)
21
22 Object.keys(fields).forEach(function (field) {
23 const value = fields[field]
24
25 if (Array.isArray(value)) {
26 for (let i = 0; i < value.length; i++) {
27 req.field(field + '[' + i + ']', value[i])
28 }
29 } else {
30 req.field(field, value)
31 }
32 })
33
34 Object.keys(attaches).forEach(function (attach) {
35 const value = attaches[attach]
36 req.attach(attach, value)
37 })
38
39 req.expect(statusCodeExpected, done)
40}
41
42function makePostBodyRequest (url, path, token, fields, done, statusCodeExpected) {
43 if (!statusCodeExpected) statusCodeExpected = 400
44
45 const req = request(url)
46 .post(path)
47 .set('Accept', 'application/json')
48
49 if (token) req.set('Authorization', 'Bearer ' + token)
50
51 req.send(fields).expect(statusCodeExpected, done)
52}
53
54function makePutBodyRequest (url, path, token, fields, done, statusCodeExpected) {
55 if (!statusCodeExpected) statusCodeExpected = 400
56
57 const req = request(url)
58 .put(path)
59 .set('Accept', 'application/json')
60
61 if (token) req.set('Authorization', 'Bearer ' + token)
62
63 req.send(fields).expect(statusCodeExpected, done)
64}
65
66// ---------------------------------------------------------------------------
67
68module.exports = requestsUtils