Skip to content

Commit 1cf75a7

Browse files
authored
Merge pull request #289 from KunalKapadia/develop
Develop to Master
2 parents 5eb8901 + 3089d7e commit 1cf75a7

File tree

3 files changed

+81
-3
lines changed

3 files changed

+81
-3
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
},
1313
"scripts": {
1414
"start": "gulp serve",
15+
"start:debug": "cross-env DEBUG=express-mongoose-es6-rest-api:* yarn start",
1516
"build": "gulp",
1617
"lint": "esw *.js server config --color",
1718
"lint:watch": "yarn lint -- --watch",

server/controllers/auth.controller.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import jwt from 'jsonwebtoken';
22
import httpStatus from 'http-status';
33
import APIError from '../helpers/APIError';
4-
5-
const config = require('../../config/env');
4+
import config from '../../config/env';
65

76
// sample user, used for authentication
87
const user = {
@@ -30,7 +29,7 @@ function login(req, res, next) {
3029
});
3130
}
3231

33-
const err = new APIError('Authentication error', httpStatus.UNAUTHORIZED);
32+
const err = new APIError('Authentication error', httpStatus.UNAUTHORIZED, true);
3433
return next(err);
3534
}
3635

server/tests/auth.test.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import request from 'supertest-as-promised';
2+
import httpStatus from 'http-status';
3+
import jwt from 'jsonwebtoken';
4+
import chai, { expect } from 'chai';
5+
import app from '../../index';
6+
import config from '../../config/env';
7+
8+
chai.config.includeStack = true;
9+
10+
describe('## Auth APIs', () => {
11+
const validUserCredentials = {
12+
username: 'react',
13+
password: 'express'
14+
};
15+
16+
const invalidUserCredentials = {
17+
username: 'react',
18+
password: 'IDontKnow'
19+
};
20+
21+
let jwtToken;
22+
23+
describe('# POST /api/auth/login', () => {
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) => {
37+
request(app)
38+
.post('/api/auth/login')
39+
.send(validUserCredentials)
40+
.expect(httpStatus.OK)
41+
.then((res) => {
42+
expect(res.body).to.have.property('token');
43+
jwt.verify(res.body.token, config.jwtSecret, (err, decoded) => {
44+
expect(err).to.not.be.ok; // eslint-disable-line no-unused-expressions
45+
expect(decoded.username).to.equal(validUserCredentials.username);
46+
jwtToken = `Bearer ${res.body.token}`;
47+
done();
48+
});
49+
})
50+
.catch(done);
51+
});
52+
});
53+
54+
describe('# GET /api/auth/random-number', () => {
55+
it('should fail to get random number because of missing Authorization', (done) => {
56+
request(app)
57+
.get('/api/auth/random-number')
58+
.expect(httpStatus.UNAUTHORIZED)
59+
.then((res) => {
60+
expect(res.body.message).to.equal('Unauthorized');
61+
done();
62+
})
63+
.catch(done);
64+
});
65+
66+
it('should get a random number', (done) => {
67+
request(app)
68+
.get('/api/auth/random-number')
69+
.set('Authorization', jwtToken)
70+
.expect(httpStatus.OK)
71+
.then((res) => {
72+
expect(res.body.num).to.be.a('number');
73+
done();
74+
})
75+
.catch(done);
76+
});
77+
});
78+
});

0 commit comments

Comments
 (0)