aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/controllers/api/pods.js2
-rw-r--r--server/middlewares/pods.js4
-rw-r--r--server/middlewares/validators/pods.js26
-rw-r--r--server/tests/api/check-params/pods.js8
-rw-r--r--server/tests/utils/requests.js11
5 files changed, 40 insertions, 11 deletions
diff --git a/server/controllers/api/pods.js b/server/controllers/api/pods.js
index e1fe6fb5d..da991f8d9 100644
--- a/server/controllers/api/pods.js
+++ b/server/controllers/api/pods.js
@@ -20,8 +20,8 @@ const router = express.Router()
20 20
21router.get('/', listPods) 21router.get('/', listPods)
22router.post('/', 22router.post('/',
23 podsMiddleware.setBodyHostPort, // We need to modify the host before running the validator!
23 validators.podsAdd, 24 validators.podsAdd,
24 podsMiddleware.setBodyHostPort,
25 addPods 25 addPods
26) 26)
27router.post('/makefriends', 27router.post('/makefriends',
diff --git a/server/middlewares/pods.js b/server/middlewares/pods.js
index e38fb341d..2647f9ff0 100644
--- a/server/middlewares/pods.js
+++ b/server/middlewares/pods.js
@@ -8,6 +8,8 @@ const podsMiddleware = {
8} 8}
9 9
10function setBodyHostsPort (req, res, next) { 10function setBodyHostsPort (req, res, next) {
11 if (!req.body.hosts) return next()
12
11 for (let i = 0; i < req.body.hosts.length; i++) { 13 for (let i = 0; i < req.body.hosts.length; i++) {
12 const hostWithPort = getHostWithPort(req.body.hosts[i]) 14 const hostWithPort = getHostWithPort(req.body.hosts[i])
13 15
@@ -23,6 +25,8 @@ function setBodyHostsPort (req, res, next) {
23} 25}
24 26
25function setBodyHostPort (req, res, next) { 27function setBodyHostPort (req, res, next) {
28 if (!req.body.host) return next()
29
26 const hostWithPort = getHostWithPort(req.body.host) 30 const hostWithPort = getHostWithPort(req.body.host)
27 31
28 // Problem with the url parsing? 32 // Problem with the url parsing?
diff --git a/server/middlewares/validators/pods.js b/server/middlewares/validators/pods.js
index 0723871b2..b9b30e7a1 100644
--- a/server/middlewares/validators/pods.js
+++ b/server/middlewares/validators/pods.js
@@ -2,6 +2,7 @@
2 2
3const checkErrors = require('./utils').checkErrors 3const checkErrors = require('./utils').checkErrors
4const constants = require('../../initializers/constants') 4const constants = require('../../initializers/constants')
5const db = require('../../initializers/database')
5const friends = require('../../lib/friends') 6const friends = require('../../lib/friends')
6const logger = require('../../helpers/logger') 7const logger = require('../../helpers/logger')
7const utils = require('../../helpers/utils') 8const utils = require('../../helpers/utils')
@@ -30,23 +31,34 @@ function makeFriends (req, res, next) {
30 31
31 if (hasFriends === true) { 32 if (hasFriends === true) {
32 // We need to quit our friends before make new ones 33 // We need to quit our friends before make new ones
33 res.sendStatus(409) 34 return res.sendStatus(409)
34 } else {
35 return next()
36 } 35 }
36
37 return next()
37 }) 38 })
38 }) 39 })
39} 40}
40 41
41function podsAdd (req, res, next) { 42function podsAdd (req, res, next) {
42 req.checkBody('host', 'Should have an host').notEmpty().isURL() 43 req.checkBody('host', 'Should have an host').isHostValid()
43 req.checkBody('publicKey', 'Should have a public key').notEmpty() 44 req.checkBody('publicKey', 'Should have a public key').notEmpty()
45 logger.debug('Checking podsAdd parameters', { parameters: req.body })
44 46
45 // TODO: check we don't have it already 47 checkErrors(req, res, function () {
48 db.Pod.loadByHost(req.body.host, function (err, pod) {
49 if (err) {
50 logger.error('Cannot load pod by host.', { error: err })
51 res.sendStatus(500)
52 }
46 53
47 logger.debug('Checking podsAdd parameters', { parameters: req.body }) 54 // Pod with this host already exists
55 if (pod) {
56 return res.sendStatus(409)
57 }
48 58
49 checkErrors(req, res, next) 59 return next()
60 })
61 })
50} 62}
51 63
52// --------------------------------------------------------------------------- 64// ---------------------------------------------------------------------------
diff --git a/server/tests/api/check-params/pods.js b/server/tests/api/check-params/pods.js
index 2f85af644..8d52b69b1 100644
--- a/server/tests/api/check-params/pods.js
+++ b/server/tests/api/check-params/pods.js
@@ -189,6 +189,14 @@ describe('Test pods API validators', function () {
189 } 189 }
190 requestsUtils.makePostBodyRequest(server.url, path, null, data, done, 200) 190 requestsUtils.makePostBodyRequest(server.url, path, null, data, done, 200)
191 }) 191 })
192
193 it('Should fail with a host that already exists', function (done) {
194 const data = {
195 host: 'coucou.com',
196 publicKey: 'mysuperpublickey'
197 }
198 requestsUtils.makePostBodyRequest(server.url, path, null, data, done, 409)
199 })
192 }) 200 })
193 201
194 after(function (done) { 202 after(function (done) {
diff --git a/server/tests/utils/requests.js b/server/tests/utils/requests.js
index b1470814d..84cf3483f 100644
--- a/server/tests/utils/requests.js
+++ b/server/tests/utils/requests.js
@@ -36,7 +36,8 @@ function makePostUploadRequest (url, path, token, fields, attaches, done, status
36 req.attach(attach, value) 36 req.attach(attach, value)
37 }) 37 })
38 38
39 req.expect(statusCodeExpected, done) 39 req.expect(statusCodeExpected)
40 .end(done)
40} 41}
41 42
42function makePostBodyRequest (url, path, token, fields, done, statusCodeExpected) { 43function makePostBodyRequest (url, path, token, fields, done, statusCodeExpected) {
@@ -48,7 +49,9 @@ function makePostBodyRequest (url, path, token, fields, done, statusCodeExpected
48 49
49 if (token) req.set('Authorization', 'Bearer ' + token) 50 if (token) req.set('Authorization', 'Bearer ' + token)
50 51
51 req.send(fields).expect(statusCodeExpected, done) 52 req.send(fields)
53 .expect(statusCodeExpected)
54 .end(done)
52} 55}
53 56
54function makePutBodyRequest (url, path, token, fields, done, statusCodeExpected) { 57function makePutBodyRequest (url, path, token, fields, done, statusCodeExpected) {
@@ -60,7 +63,9 @@ function makePutBodyRequest (url, path, token, fields, done, statusCodeExpected)
60 63
61 if (token) req.set('Authorization', 'Bearer ' + token) 64 if (token) req.set('Authorization', 'Bearer ' + token)
62 65
63 req.send(fields).expect(statusCodeExpected, done) 66 req.send(fields)
67 .expect(statusCodeExpected)
68 .end(done)
64} 69}
65 70
66// --------------------------------------------------------------------------- 71// ---------------------------------------------------------------------------