Skip to content

Commit

Permalink
Add fail path for POST /api/auth/login
Browse files Browse the repository at this point in the history
  • Loading branch information
kunalkapadia committed Feb 13, 2017
1 parent b12b463 commit ad49e3f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion server/controllers/auth.controller.js
Expand Up @@ -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);
}

Expand Down
28 changes: 23 additions & 5 deletions server/tests/auth.test.js
Expand Up @@ -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();
});
Expand Down

0 comments on commit ad49e3f

Please sign in to comment.