|
| 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 | +}); |
0 commit comments