mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 15:17:25 +01:00
Add tests for error handling
This commit is contained in:
94
test/api/v3/unit/libs/errors.test.js
Normal file
94
test/api/v3/unit/libs/errors.test.js
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
import {
|
||||||
|
CustomError,
|
||||||
|
NotAuthorized,
|
||||||
|
BadRequest,
|
||||||
|
InternalServerError,
|
||||||
|
} from '../../../../../website/src/libs/api-v3/errors';
|
||||||
|
|
||||||
|
describe('Custom Errors', () => {
|
||||||
|
describe('CustomError', () => {
|
||||||
|
it('is an instance of Error', () => {
|
||||||
|
let customError = new CustomError();
|
||||||
|
|
||||||
|
expect(customError).to.be.an.instanceOf(Error);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('NotAuthorized', () => {
|
||||||
|
it('is an instance of CustomError', () => {
|
||||||
|
let notAuthorizedError = new NotAuthorized();
|
||||||
|
|
||||||
|
expect(notAuthorizedError).to.be.an.instanceOf(CustomError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('it returns an http code of 400', () => {
|
||||||
|
let notAuthorizedError = new NotAuthorized();
|
||||||
|
|
||||||
|
expect(notAuthorizedError.httpCode).to.eql(401);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns a default message', () => {
|
||||||
|
let notAuthorizedError = new NotAuthorized();
|
||||||
|
|
||||||
|
expect(notAuthorizedError.message).to.eql('Not authorized.');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('allows a custom message', () => {
|
||||||
|
let notAuthorizedError = new NotAuthorized('Custom Error Message');
|
||||||
|
|
||||||
|
expect(notAuthorizedError.message).to.eql('Custom Error Message');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('BadRequest', () => {
|
||||||
|
it('is an instance of CustomError', () => {
|
||||||
|
let badRequestError = new BadRequest();
|
||||||
|
|
||||||
|
expect(badRequestError).to.be.an.instanceOf(CustomError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('it returns an http code of 401', () => {
|
||||||
|
let badRequestError = new BadRequest();
|
||||||
|
|
||||||
|
expect(badRequestError.httpCode).to.eql(400);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns a default message', () => {
|
||||||
|
let badRequestError = new BadRequest();
|
||||||
|
|
||||||
|
expect(badRequestError.message).to.eql('Bad request.');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('allows a custom message', () => {
|
||||||
|
let badRequestError = new BadRequest('Custom Error Message');
|
||||||
|
|
||||||
|
expect(badRequestError.message).to.eql('Custom Error Message');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('InternalServerError', () => {
|
||||||
|
it('is an instance of CustomError', () => {
|
||||||
|
let internalServerError = new InternalServerError();
|
||||||
|
|
||||||
|
expect(internalServerError).to.be.an.instanceOf(CustomError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('it returns an http code of 500', () => {
|
||||||
|
let internalServerError = new InternalServerError();
|
||||||
|
|
||||||
|
expect(internalServerError.httpCode).to.eql(500);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns a default message', () => {
|
||||||
|
let internalServerError = new InternalServerError();
|
||||||
|
|
||||||
|
expect(internalServerError.message).to.eql('Internal server error.');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('allows a custom message', () => {
|
||||||
|
let internalServerError = new InternalServerError('Custom Error Message');
|
||||||
|
|
||||||
|
expect(internalServerError.message).to.eql('Custom Error Message');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
77
test/api/v3/unit/middlewares/errorHandler.test.js
Normal file
77
test/api/v3/unit/middlewares/errorHandler.test.js
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
import errorHandler from '../../../../../website/src/middlewares/api-v3/errorHandler';
|
||||||
|
|
||||||
|
import { BadRequest } from '../../../../../website/src/libs/api-v3/errors';
|
||||||
|
import logger from '../../../../../website/src/libs/api-v3/logger';
|
||||||
|
|
||||||
|
describe('errorHandler', () => {
|
||||||
|
let res, req;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
res = {
|
||||||
|
status: sinon.stub().returnsThis(),
|
||||||
|
json: sinon.stub(),
|
||||||
|
};
|
||||||
|
req = {
|
||||||
|
originalUrl: 'foo',
|
||||||
|
headers: {},
|
||||||
|
body: {},
|
||||||
|
};
|
||||||
|
|
||||||
|
sinon.stub(logger, 'error');
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
logger.error.restore();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('sends internal server error if error is not a CustomError', () => {
|
||||||
|
let error = new Error();
|
||||||
|
|
||||||
|
errorHandler(error, req, res);
|
||||||
|
|
||||||
|
expect(res.status).to.be.calledOnce;
|
||||||
|
expect(res.json).to.be.calledOnce;
|
||||||
|
|
||||||
|
expect(res.status).to.be.calledWith(500);
|
||||||
|
expect(res.json).to.be.calledWith({
|
||||||
|
error: 'InternalServerError',
|
||||||
|
message: 'Internal server error.',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('sends CustomError', () => {
|
||||||
|
let error = new BadRequest();
|
||||||
|
|
||||||
|
errorHandler(error, req, res);
|
||||||
|
|
||||||
|
expect(res.status).to.be.calledOnce;
|
||||||
|
expect(res.json).to.be.calledOnce;
|
||||||
|
|
||||||
|
expect(res.status).to.be.calledWith(400);
|
||||||
|
expect(res.json).to.be.calledWith({
|
||||||
|
error: 'BadRequest',
|
||||||
|
message: 'Bad request.',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('logs error', () => {
|
||||||
|
let error = new BadRequest();
|
||||||
|
|
||||||
|
errorHandler(error, req, res);
|
||||||
|
|
||||||
|
expect(logger.error).to.be.calledOnce;
|
||||||
|
expect(logger.error).to.be.calledWith(error.stack, {
|
||||||
|
originalUrl: req.originalUrl,
|
||||||
|
headers: req.headers,
|
||||||
|
body: req.body,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not send error if error is not defined', () => {
|
||||||
|
let next = sinon.stub();
|
||||||
|
errorHandler(null, req, res, next);
|
||||||
|
|
||||||
|
expect(next).to.be.calledOnce;
|
||||||
|
expect(res.status).to.not.be.called;
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user