From ad49e3f8e28336a50a56c7e6f9eb22a18d8f4701 Mon Sep 17 00:00:00 2001 From: Kunal Kapadia Date: Mon, 13 Feb 2017 20:25:21 +0530 Subject: [PATCH] Add fail path for POST /api/auth/login --- server/controllers/auth.controller.js | 2 +- server/tests/auth.test.js | 28 ++++++++++++++++++++++----- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/server/controllers/auth.controller.js b/server/controllers/auth.controller.js index a7c13d26..2ea47632 100644 --- a/server/controllers/auth.controller.js +++ b/server/controllers/auth.controller.js @@ -29,7 +29,7 @@ function login(req, res, next) { }); } - const err = new APIError('Authentication error', httpStatus.UNAUTHORIZED); + const err = new APIError('Authentication error', httpStatus.UNAUTHORIZED, true); return next(err); } diff --git a/server/tests/auth.test.js b/server/tests/auth.test.js index cb43e2e8..5e87859a 100644 --- a/server/tests/auth.test.js +++ b/server/tests/auth.test.js @@ -7,24 +7,42 @@ import config from '../../config/env'; chai.config.includeStack = true; -describe('## AUTH APIs', () => { - const user = { +describe('## Auth APIs', () => { + const validUserCredentials = { username: 'react', password: 'express' }; + + const invalidUserCredentials = { + username: 'react', + password: 'IDontKnow' + }; + let jwtToken; describe('# POST /api/auth/login', () => { - it('should get (valid) JWT token', (done) => { + it('should return Authentication error', (done) => { + request(app) + .post('/api/auth/login') + .send(invalidUserCredentials) + .expect(httpStatus.UNAUTHORIZED) + .then((res) => { + expect(res.body.message).to.equal('Authentication error'); + done(); + }) + .catch(done); + }); + + it('should get valid JWT token', (done) => { request(app) .post('/api/auth/login') - .send(user) + .send(validUserCredentials) .expect(httpStatus.OK) .then((res) => { expect(res.body).to.have.property('token'); jwt.verify(res.body.token, config.jwtSecret, (err, decoded) => { expect(err).to.not.be.ok; // eslint-disable-line no-unused-expressions - expect(decoded.username).to.equal(user.username); + expect(decoded.username).to.equal(validUserCredentials.username); jwtToken = `Bearer ${res.body.token}`; done(); });