Skip to content

Commit 0f09abd

Browse files
committed
Add test for JWT Authorization
1 parent 03eba31 commit 0f09abd

File tree

2 files changed

+194
-85
lines changed

2 files changed

+194
-85
lines changed

server/tests/auth.test.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import request from 'supertest-as-promised';
2+
import httpStatus from 'http-status';
3+
import chai, { expect } from 'chai';
4+
import app from '../../index';
5+
6+
chai.config.includeStack = true;
7+
8+
describe('## AUTH APIs', () => {
9+
const user = {
10+
username: 'react',
11+
password: 'express'
12+
};
13+
let jwtToken;
14+
15+
describe('# GET /api/auth/login (login to get JWT token)', () => {
16+
it('should get JWT token', (done) => {
17+
request(app)
18+
.post('/api/auth/login')
19+
.send(user)
20+
.expect(httpStatus.OK)
21+
.then((res) => {
22+
expect(res.body.username).to.equal(user.username);
23+
jwtToken = `Bearer ${res.body.token}`;
24+
done();
25+
})
26+
.catch(done);
27+
});
28+
});
29+
30+
describe('# GET /api/auth/random-number', () => {
31+
it('should fail to get random number because of missing Authorization', (done) => {
32+
request(app)
33+
.get('/api/auth/random-number')
34+
.expect(httpStatus.UNAUTHORIZED)
35+
.then((res) => {
36+
expect(res.body.message).to.equal('Unauthorized');
37+
done();
38+
})
39+
.catch(done);
40+
});
41+
42+
it('should get a random number', (done) => {
43+
request(app)
44+
.get('/api/auth/random-number')
45+
.set('Authorization', jwtToken)
46+
.expect(httpStatus.OK)
47+
.then((res) => {
48+
expect(res.body.num).to.be.a('number');
49+
done();
50+
})
51+
.catch(done);
52+
});
53+
});
54+
});

0 commit comments

Comments
 (0)