Skip to content

Commit

Permalink
Add auth middleware tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hagopj13 committed Nov 3, 2019
1 parent 9111b31 commit a813ea8
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"start": "pm2 start ecosystem.config.json --no-daemon",
"dev": "cross-env NODE_ENV=development nodemon src/index.js",
"test": "jest -i",
"test:watch": "jest -i --verbose --watchAll",
"test:watch": "jest -i --watchAll",
"coverage": "jest -i --coverage",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
Expand Down
130 changes: 128 additions & 2 deletions tests/integration/auth.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
const request = require('supertest');
const faker = require('faker');
const httpStatus = require('http-status');
const httpMocks = require('node-mocks-http');
const moment = require('moment');
const app = require('../../src/app');
const config = require('../../src/config/config');
const auth = require('../../src/middlewares/auth');
const tokenService = require('../../src/services/token.service');
const AppError = require('../../src/utils/AppError');
const setupDatabase = require('../utils/setupDatabase');
const { User } = require('../../src/models');
const { userOne, insertUsers } = require('../fixtures/user.fixture');
const { roleRights } = require('../../src/config/roles');
const { userOne, admin, insertUsers } = require('../fixtures/user.fixture');
const { userOneAccessToken, adminAccessToken } = require('../fixtures/token.fixture');

setupDatabase();

describe('Auth route', () => {
describe('Auth routes', () => {
describe('POST /v1/auth/register', () => {
let newUser;
beforeEach(() => {
Expand Down Expand Up @@ -140,3 +148,121 @@ describe('Auth route', () => {
});
});
});

describe('Auth middleware', () => {
test('should call next with no errors if access token is valid', async () => {
await insertUsers([userOne]);
const req = httpMocks.createRequest({ headers: { Authorization: `Bearer ${userOneAccessToken}` } });
const next = jest.fn();

await auth()(req, httpMocks.createResponse(), next);

expect(next).toHaveBeenCalledWith();
expect(req.user._id).toEqual(userOne._id);
});

test('should call next with unauthorized error if access token is not found in header', async () => {
await insertUsers([userOne]);
const req = httpMocks.createRequest();
const next = jest.fn();

await auth()(req, httpMocks.createResponse(), next);

expect(next).toHaveBeenCalledWith(expect.any(AppError));
expect(next).toHaveBeenCalledWith(
expect.objectContaining({ statusCode: httpStatus.UNAUTHORIZED, message: 'Please authenticate' })
);
});

test('should call next with unauthorized error if access token is not a valid jwt token', async () => {
await insertUsers([userOne]);
const req = httpMocks.createRequest({ headers: { Authorization: 'Bearer randomToken' } });
const next = jest.fn();

await auth()(req, httpMocks.createResponse(), next);

expect(next).toHaveBeenCalledWith(expect.any(AppError));
expect(next).toHaveBeenCalledWith(
expect.objectContaining({ statusCode: httpStatus.UNAUTHORIZED, message: 'Please authenticate' })
);
});

test('should call next with unauthorized error if access token is generated with an invalid secret', async () => {
await insertUsers([userOne]);
const tokenExpires = moment().add(config.jwt.accessExpirationMinutes, 'minutes');
const accessToken = tokenService.generateToken(userOne._id, tokenExpires, 'invalidSecret');
const req = httpMocks.createRequest({ headers: { Authorization: `Bearer ${accessToken}` } });
const next = jest.fn();

await auth()(req, httpMocks.createResponse(), next);

expect(next).toHaveBeenCalledWith(expect.any(AppError));
expect(next).toHaveBeenCalledWith(
expect.objectContaining({ statusCode: httpStatus.UNAUTHORIZED, message: 'Please authenticate' })
);
});

test('should call next with unauthorized error if access token is expired', async () => {
await insertUsers([userOne]);
const tokenExpires = moment().subtract(1, 'minutes');
const accessToken = tokenService.generateToken(userOne._id, tokenExpires);
const req = httpMocks.createRequest({ headers: { Authorization: `Bearer ${accessToken}` } });
const next = jest.fn();

await auth()(req, httpMocks.createResponse(), next);

expect(next).toHaveBeenCalledWith(expect.any(AppError));
expect(next).toHaveBeenCalledWith(
expect.objectContaining({ statusCode: httpStatus.UNAUTHORIZED, message: 'Please authenticate' })
);
});

test('should call next with unauthorized error if user is not found', async () => {
const req = httpMocks.createRequest({ headers: { Authorization: `Bearer ${userOneAccessToken}` } });
const next = jest.fn();

await auth()(req, httpMocks.createResponse(), next);

expect(next).toHaveBeenCalledWith(expect.any(AppError));
expect(next).toHaveBeenCalledWith(
expect.objectContaining({ statusCode: httpStatus.UNAUTHORIZED, message: 'Please authenticate' })
);
});

test('should call next with forbidden error if user does not have required rights and userId is not in params', async () => {
await insertUsers([userOne]);
const req = httpMocks.createRequest({ headers: { Authorization: `Bearer ${userOneAccessToken}` } });
const next = jest.fn();

await auth('anyRight')(req, httpMocks.createResponse(), next);

expect(next).toHaveBeenCalledWith(expect.any(AppError));
expect(next).toHaveBeenCalledWith(expect.objectContaining({ statusCode: httpStatus.FORBIDDEN, message: 'Forbidden' }));
});

test('should call next with no errors if user does not have required rights but userId is in params', async () => {
await insertUsers([userOne]);
const req = httpMocks.createRequest({
headers: { Authorization: `Bearer ${userOneAccessToken}` },
params: { userId: userOne._id.toHexString() },
});
const next = jest.fn();

await auth('anyRight')(req, httpMocks.createResponse(), next);

expect(next).toHaveBeenCalledWith();
});

test('should call next with no errors if user has required rights', async () => {
await insertUsers([admin]);
const req = httpMocks.createRequest({
headers: { Authorization: `Bearer ${adminAccessToken}` },
params: { userId: userOne._id.toHexString() },
});
const next = jest.fn();

await auth(...roleRights.get('admin'))(req, httpMocks.createResponse(), next);

expect(next).toHaveBeenCalledWith();
});
});
2 changes: 1 addition & 1 deletion tests/integration/user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const { userOneAccessToken, adminAccessToken } = require('../fixtures/token.fixt

setupDatabase();

describe('User route', () => {
describe('User routes', () => {
describe('POST /v1/users', () => {
let newUser;

Expand Down

0 comments on commit a813ea8

Please sign in to comment.