Skip to content

Commit ad49e3f

Browse files
committed
Add fail path for POST /api/auth/login
1 parent b12b463 commit ad49e3f

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

server/controllers/auth.controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function login(req, res, next) {
2929
});
3030
}
3131

32-
const err = new APIError('Authentication error', httpStatus.UNAUTHORIZED);
32+
const err = new APIError('Authentication error', httpStatus.UNAUTHORIZED, true);
3333
return next(err);
3434
}
3535

server/tests/auth.test.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,42 @@ import config from '../../config/env';
77

88
chai.config.includeStack = true;
99

10-
describe('## AUTH APIs', () => {
11-
const user = {
10+
describe('## Auth APIs', () => {
11+
const validUserCredentials = {
1212
username: 'react',
1313
password: 'express'
1414
};
15+
16+
const invalidUserCredentials = {
17+
username: 'react',
18+
password: 'IDontKnow'
19+
};
20+
1521
let jwtToken;
1622

1723
describe('# POST /api/auth/login', () => {
18-
it('should get (valid) JWT token', (done) => {
24+
it('should return Authentication error', (done) => {
25+
request(app)
26+
.post('/api/auth/login')
27+
.send(invalidUserCredentials)
28+
.expect(httpStatus.UNAUTHORIZED)
29+
.then((res) => {
30+
expect(res.body.message).to.equal('Authentication error');
31+
done();
32+
})
33+
.catch(done);
34+
});
35+
36+
it('should get valid JWT token', (done) => {
1937
request(app)
2038
.post('/api/auth/login')
21-
.send(user)
39+
.send(validUserCredentials)
2240
.expect(httpStatus.OK)
2341
.then((res) => {
2442
expect(res.body).to.have.property('token');
2543
jwt.verify(res.body.token, config.jwtSecret, (err, decoded) => {
2644
expect(err).to.not.be.ok; // eslint-disable-line no-unused-expressions
27-
expect(decoded.username).to.equal(user.username);
45+
expect(decoded.username).to.equal(validUserCredentials.username);
2846
jwtToken = `Bearer ${res.body.token}`;
2947
done();
3048
});

0 commit comments

Comments
 (0)