From 349122c9a1a26f2ab4122d54d60c9d5835d66057 Mon Sep 17 00:00:00 2001 From: Matteo Pagliazzi Date: Mon, 23 Nov 2015 13:01:47 +0100 Subject: [PATCH] add res.respond, fix linting --- test/api/v3/integration/notFound.test.js | 1 + .../v3/integration/user/auth/POST-register_local.test.js | 7 +++++++ test/helpers/api-integration.helper.js | 1 + website/src/controllers/api-v3/user.js | 1 + website/src/middlewares/api-v3/auth.js | 2 +- website/src/middlewares/api-v3/errorHandler.js | 4 +--- website/src/middlewares/api-v3/index.js | 2 ++ 7 files changed, 14 insertions(+), 4 deletions(-) diff --git a/test/api/v3/integration/notFound.test.js b/test/api/v3/integration/notFound.test.js index e86b6b35e8..4c17b13dda 100644 --- a/test/api/v3/integration/notFound.test.js +++ b/test/api/v3/integration/notFound.test.js @@ -5,6 +5,7 @@ describe('notFound Middleware', () => { let request = requester().get('/api/v3/dummy-url'); return expect(request).to.eventually.be.rejected.and.eql({ + success: false, code: 404, error: 'NotFound', message: 'Not found.', diff --git a/test/api/v3/integration/user/auth/POST-register_local.test.js b/test/api/v3/integration/user/auth/POST-register_local.test.js index 4ad2127cd4..8b21447a57 100644 --- a/test/api/v3/integration/user/auth/POST-register_local.test.js +++ b/test/api/v3/integration/user/auth/POST-register_local.test.js @@ -39,6 +39,7 @@ describe('POST /user/auth/local/register', () => { password, confirmPassword: confirmPassword, })).to.eventually.be.rejected.and.eql({ + success: false, code: 400, error: 'BadRequest', message: t('invalidReqParams'), @@ -56,6 +57,7 @@ describe('POST /user/auth/local/register', () => { password, confirmPassword, })).to.eventually.be.rejected.and.eql({ + success: false, code: 400, error: 'BadRequest', message: t('invalidReqParams'), @@ -72,6 +74,7 @@ describe('POST /user/auth/local/register', () => { password, confirmPassword: password, })).to.eventually.be.rejected.and.eql({ + success: false, code: 400, error: 'BadRequest', message: t('invalidReqParams'), @@ -90,6 +93,7 @@ describe('POST /user/auth/local/register', () => { password, confirmPassword: password, })).to.eventually.be.rejected.and.eql({ + success: false, code: 400, error: 'BadRequest', message: t('invalidReqParams'), @@ -107,6 +111,7 @@ describe('POST /user/auth/local/register', () => { email: email, confirmPassword: confirmPassword, })).to.eventually.be.rejected.and.eql({ + success: false, code: 400, error: 'BadRequest', message: t('invalidReqParams'), @@ -138,6 +143,7 @@ describe('POST /user/auth/local/register', () => { password: password, confirmPassword: password, })).to.eventually.be.rejected.and.eql({ + success: false, code: 401, error: 'NotAuthorized', message: t('usernameTaken'), @@ -155,6 +161,7 @@ describe('POST /user/auth/local/register', () => { password, confirmPassword: password, })).to.eventually.be.rejected.and.eql({ + success: false, code: 401, error: 'NotAuthorized', message: t('emailTaken'), diff --git a/test/helpers/api-integration.helper.js b/test/helpers/api-integration.helper.js index 48ee56739d..be3e9e66b7 100644 --- a/test/helpers/api-integration.helper.js +++ b/test/helpers/api-integration.helper.js @@ -252,6 +252,7 @@ function _requestMaker (user, method, additionalSets) { if (API_V === 'v3') { return reject({ + success: err.response.body.success, code: err.status, error: err.response.body.error, message: err.response.body.message, diff --git a/website/src/controllers/api-v3/user.js b/website/src/controllers/api-v3/user.js index 9dd471868f..cd154faca7 100644 --- a/website/src/controllers/api-v3/user.js +++ b/website/src/controllers/api-v3/user.js @@ -48,6 +48,7 @@ api.registerLocal = { if (validationErrors) return next(validationErrors); let { email, username, password } = req.body; + // Get the lowercase version of username to check that we do not have duplicates // So we can search for it in the database and then reject the choosen username if 1 or more results are found email = email.toLowerCase(); diff --git a/website/src/middlewares/api-v3/auth.js b/website/src/middlewares/api-v3/auth.js index 27604bc445..f2158278bf 100644 --- a/website/src/middlewares/api-v3/auth.js +++ b/website/src/middlewares/api-v3/auth.js @@ -34,7 +34,7 @@ export function authWithHeaders (optional = false) { next(); }) .catch(next); - } + }; } // Authenticate a request through a valid session diff --git a/website/src/middlewares/api-v3/errorHandler.js b/website/src/middlewares/api-v3/errorHandler.js index 57280db12a..e017910297 100644 --- a/website/src/middlewares/api-v3/errorHandler.js +++ b/website/src/middlewares/api-v3/errorHandler.js @@ -68,7 +68,5 @@ export default function errorHandler (err, req, res, next) { if (responseErr.errors) jsonRes.errors = responseErr.errors; - return res - .status(responseErr.httpCode) - .json(jsonRes); + return res.respond(responseErr.httpCode, jsonRes); } diff --git a/website/src/middlewares/api-v3/index.js b/website/src/middlewares/api-v3/index.js index 46a546b549..56d78add8e 100644 --- a/website/src/middlewares/api-v3/index.js +++ b/website/src/middlewares/api-v3/index.js @@ -8,6 +8,7 @@ import routes from '../../libs/api-v3/setupRoutes'; import notFoundHandler from './notFound'; import nconf from 'nconf'; import morgan from 'morgan'; +import responseHandler from './response'; const IS_PROD = nconf.get('IS_PROD'); const DISABLE_LOGGING = nconf.get('DISABLE_REQUEST_LOGGING'); @@ -22,6 +23,7 @@ export default function attachMiddlewares (app) { app.use(bodyParser.json()); app.use(expressValidator()); // TODO config app.use(analytics); + app.use(responseHandler); app.use(getUserLanguage); app.use('/api/v3', routes);