Skip to content

Commit d3f73e4

Browse files
committed
verify JWT token
1 parent 0f09abd commit d3f73e4

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

server/tests/auth.test.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import request from 'supertest-as-promised';
22
import httpStatus from 'http-status';
3+
import jwt from 'jsonwebtoken';
34
import chai, { expect } from 'chai';
45
import app from '../../index';
56

7+
const config = require('../../config/env');
8+
const should = require('chai').should();
9+
610
chai.config.includeStack = true;
711

812
describe('## AUTH APIs', () => {
@@ -12,16 +16,20 @@ describe('## AUTH APIs', () => {
1216
};
1317
let jwtToken;
1418

15-
describe('# GET /api/auth/login (login to get JWT token)', () => {
16-
it('should get JWT token', (done) => {
19+
describe('# GET /api/auth/login', () => {
20+
it('should get (valid) JWT token', (done) => {
1721
request(app)
1822
.post('/api/auth/login')
1923
.send(user)
2024
.expect(httpStatus.OK)
2125
.then((res) => {
22-
expect(res.body.username).to.equal(user.username);
23-
jwtToken = `Bearer ${res.body.token}`;
24-
done();
26+
should.exist(res.body.token);
27+
jwt.verify(res.body.token, config.jwtSecret, (err, decoded) => {
28+
should.not.exist(err);
29+
expect(decoded.username).to.equal(user.username);
30+
jwtToken = `Bearer ${res.body.token}`;
31+
done();
32+
});
2533
})
2634
.catch(done);
2735
});

0 commit comments

Comments
 (0)