Skip to content

Commit abeb992

Browse files
committed
fix linting error in auth.test and misc.test js
1 parent ade76dd commit abeb992

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed

server/tests/auth.test.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/* eslint-env mocha */
2+
3+
import request from 'supertest';
4+
import httpStatus from 'http-status';
5+
import jwt from 'jsonwebtoken';
6+
import chai, { expect } from 'chai';
7+
import app from '../../index';
8+
import config from '../../config/config';
9+
10+
chai.config.includeStack = true;
11+
12+
describe('## Auth APIs', () => {
13+
const validUserCredentials = {
14+
username: 'react',
15+
password: 'express',
16+
};
17+
18+
const invalidUserCredentials = {
19+
username: 'react',
20+
password: 'IDontKnow',
21+
};
22+
23+
let jwtToken;
24+
25+
describe('# POST /api/auth/login', () => {
26+
it('should return Authentication error', (done) => {
27+
request(app)
28+
.post('/api/auth/login')
29+
.send(invalidUserCredentials)
30+
.expect(httpStatus.UNAUTHORIZED)
31+
.then((res) => {
32+
expect(res.body.message).to.equal('Authentication error');
33+
done();
34+
})
35+
.catch(done);
36+
});
37+
38+
it('should get valid JWT token', (done) => {
39+
request(app)
40+
.post('/api/auth/login')
41+
.send(validUserCredentials)
42+
.expect(httpStatus.OK)
43+
.then((res) => {
44+
expect(res.body).to.have.property('token');
45+
jwt.verify(res.body.token, config.jwtSecret, (err, decoded) => {
46+
expect(err).to.not.be.ok; // eslint-disable-line no-unused-expressions
47+
expect(decoded.username).to.equal(validUserCredentials.username);
48+
jwtToken = `Bearer ${res.body.token}`;
49+
done();
50+
});
51+
})
52+
.catch(done);
53+
});
54+
});
55+
56+
describe('# GET /api/auth/random-number', () => {
57+
it('should fail to get random number because of missing Authorization', (done) => {
58+
request(app)
59+
.get('/api/auth/random-number')
60+
.expect(httpStatus.UNAUTHORIZED)
61+
.then((res) => {
62+
expect(res.body.message).to.equal('Unauthorized');
63+
done();
64+
})
65+
.catch(done);
66+
});
67+
68+
it('should fail to get random number because of wrong token', (done) => {
69+
request(app)
70+
.get('/api/auth/random-number')
71+
.set('Authorization', 'Bearer inValidToken')
72+
.expect(httpStatus.UNAUTHORIZED)
73+
.then((res) => {
74+
expect(res.body.message).to.equal('Unauthorized');
75+
done();
76+
})
77+
.catch(done);
78+
});
79+
80+
it('should get a random number', (done) => {
81+
request(app)
82+
.get('/api/auth/random-number')
83+
.set('Authorization', jwtToken)
84+
.expect(httpStatus.OK)
85+
.then((res) => {
86+
expect(res.body.num).to.be.a('number');
87+
done();
88+
})
89+
.catch(done);
90+
});
91+
});
92+
});

server/tests/misc.test.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* eslint-env mocha */
2+
3+
import request from 'supertest';
4+
import httpStatus from 'http-status';
5+
import chai, { expect } from 'chai';
6+
import app from '../../index';
7+
8+
chai.config.includeStack = true;
9+
10+
describe('## Misc', () => {
11+
describe('# GET /api/health-check', () => {
12+
it('should return OK', (done) => {
13+
request(app)
14+
.get('/api/health-check')
15+
.expect(httpStatus.OK)
16+
.then((res) => {
17+
expect(res.text).to.equal('OK');
18+
done();
19+
})
20+
.catch(done);
21+
});
22+
});
23+
24+
describe('# GET /api/404', () => {
25+
it('should return 404 status', (done) => {
26+
request(app)
27+
.get('/api/404')
28+
.expect(httpStatus.NOT_FOUND)
29+
.then((res) => {
30+
expect(res.body.message).to.equal('Not Found');
31+
done();
32+
})
33+
.catch(done);
34+
});
35+
});
36+
37+
describe('# Error Handling', () => {
38+
it('should handle express validation error - username is required', (done) => {
39+
request(app)
40+
.post('/api/users')
41+
.send({
42+
mobileNumber: '1234567890',
43+
})
44+
.expect(httpStatus.BAD_REQUEST)
45+
.then((res) => {
46+
expect(res.body.message).to.equal('"username" is required');
47+
done();
48+
})
49+
.catch(done);
50+
});
51+
});
52+
});

0 commit comments

Comments
 (0)