diff --git a/test/api/v3/unit/libs/errors.test.js b/test/api/v3/unit/libs/errors.test.js new file mode 100644 index 0000000000..e859d2bb02 --- /dev/null +++ b/test/api/v3/unit/libs/errors.test.js @@ -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'); + }); + }); +}); diff --git a/test/api/v3/unit/middlewares/errorHandler.test.js b/test/api/v3/unit/middlewares/errorHandler.test.js new file mode 100644 index 0000000000..39e72b2e45 --- /dev/null +++ b/test/api/v3/unit/middlewares/errorHandler.test.js @@ -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; + }); +});