diff --git a/package.json b/package.json index 480959e118..ba6d1d5edf 100644 --- a/package.json +++ b/package.json @@ -121,13 +121,14 @@ "karma-ng-html2js-preprocessor": "~0.1.0", "karma-phantomjs-launcher": "~0.1.0", "karma-requirejs": "~0.2.0", - "requirejs": "~2.1", "karma-script-launcher": "~0.1.0", "lcov-result-merger": "^1.0.2", + "lodash.defaultsdeep": "^3.10.0", "mocha": "^2.3.3", "mongodb": "^2.0.46", "mongoskin": "~0.6.1", "protractor": "~2.0.0", + "requirejs": "~2.1", "rewire": "^2.3.3", "shelljs": "^0.4.0", "sinon": "^1.17.2", diff --git a/tasks/gulp-tests.js b/tasks/gulp-tests.js index 3f2f469f29..3979cbd52b 100644 --- a/tasks/gulp-tests.js +++ b/tasks/gulp-tests.js @@ -297,6 +297,7 @@ gulp.task('test:e2e:safe', ['test:prepare', 'test:prepare:server'], (cb) => { }); gulp.task('test:api-v2', ['test:prepare:server'], (done) => { + process.env.API_VERSION = 'v2'; awaitPort(TEST_SERVER_PORT).then(() => { runMochaTests('./test/api/v2/**/*.js', server, done) }); @@ -329,15 +330,16 @@ gulp.task('test:api-v3', ['test:api-v3:unit', 'test:api-v3:integration']); gulp.task('test:api-v3:watch', ['test:api-v3:unit:watch', 'test:api-v3:integration:watch']); -gulp.task('test:api-v3:unit', ['test:prepare:server'], (done) => { +gulp.task('test:api-v3:unit', (done) => { runMochaTests('./test/api/v3/unit/**/*.js', null, done) }); -gulp.task('test:api-v3:unit:watch', ['test:prepare:server'], () => { +gulp.task('test:api-v3:unit:watch', () => { gulp.watch(['website/src/**', 'test/api/v3/unit/**'], ['test:api-v3:unit']); }); gulp.task('test:api-v3:integration', ['test:prepare:server'], (done) => { + process.env.API_VERSION = 'v3'; awaitPort(TEST_SERVER_PORT).then(() => { runMochaTests('./test/api/v3/unit/**/*.js', server, done) }); diff --git a/test/api/v3/unit/middlewares/errorHandler.test.js b/test/api/v3/unit/middlewares/errorHandler.test.js index 39e72b2e45..7ce86127e5 100644 --- a/test/api/v3/unit/middlewares/errorHandler.test.js +++ b/test/api/v3/unit/middlewares/errorHandler.test.js @@ -1,33 +1,29 @@ +import { + generateRes, + generateReq, + generateNext, +} from '../../../../helpers/api-unit.helper'; + 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; + let res, req, next; beforeEach(() => { - res = { - status: sinon.stub().returnsThis(), - json: sinon.stub(), - }; - req = { - originalUrl: 'foo', - headers: {}, - body: {}, - }; + res = generateRes(); + req = generateReq(); + next = generateNext(); - sinon.stub(logger, 'error'); - }); - - afterEach(() => { - logger.error.restore(); + sandbox.stub(logger, 'error'); }); it('sends internal server error if error is not a CustomError', () => { let error = new Error(); - errorHandler(error, req, res); + errorHandler(error, req, res, next); expect(res.status).to.be.calledOnce; expect(res.json).to.be.calledOnce; @@ -42,7 +38,7 @@ describe('errorHandler', () => { it('sends CustomError', () => { let error = new BadRequest(); - errorHandler(error, req, res); + errorHandler(error, req, res, next); expect(res.status).to.be.calledOnce; expect(res.json).to.be.calledOnce; @@ -57,7 +53,7 @@ describe('errorHandler', () => { it('logs error', () => { let error = new BadRequest(); - errorHandler(error, req, res); + errorHandler(error, req, res, next); expect(logger.error).to.be.calledOnce; expect(logger.error).to.be.calledWith(error.stack, { @@ -68,7 +64,6 @@ describe('errorHandler', () => { }); it('does not send error if error is not defined', () => { - let next = sinon.stub(); errorHandler(null, req, res, next); expect(next).to.be.calledOnce; diff --git a/test/helpers/api-integration.helper.js b/test/helpers/api-integration.helper.js index b2b0ac99c6..1ddaa75b0d 100644 --- a/test/helpers/api-integration.helper.js +++ b/test/helpers/api-integration.helper.js @@ -208,9 +208,10 @@ export function resetHabiticaDB() { } function _requestMaker(user, method, additionalSets) { + const API_V = process.env.API_VERSION || 'v2' return (route, send, query) => { return new Promise((resolve, reject) => { - let request = superagent[method](`http://localhost:${API_TEST_SERVER_PORT}/api/v2${route}`) + let request = superagent[method](`http://localhost:${API_TEST_SERVER_PORT}/api/${API_V}${route}`) .accept('application/json'); if (user && user._id && user.apiToken) { diff --git a/test/helpers/api-unit.helper.js b/test/helpers/api-unit.helper.js new file mode 100644 index 0000000000..6e561df076 --- /dev/null +++ b/test/helpers/api-unit.helper.js @@ -0,0 +1,46 @@ +// @TODO: remove when lodash can be upgraded +import defaults from 'lodash.defaultsdeep'; +import { model as User } from '../../website/src/models/user' +import { model as Group } from '../../website/src/models/group' +import i18n from '../../common/script/src/i18n'; +require('coffee-script'); +i18n.translations = require('../../website/src/libs/i18n.js').translations; + +afterEach(() => { + sandbox.restore(); +}); + +export function generateUser(options={}) { + return new User(options).toObject(); +} + +export function generateGroup(options={}) { + return new Group(options).toObject(); +} + +export function generateRes(options={}) { + let defaultRes = { + send: sandbox.stub(), + status: sandbox.stub().returnsThis(), + json: sandbox.stub(), + locals: { + user: generateUser(options.localsUser), + group: generateGroup(options.localsGroup), + }, + }; + + return defaults(options, defaultRes); +} + +export function generateReq(options={}) { + let defaultReq = { + body: {}, + query: {}, + }; + + return defaults(options, defaultReq); +} + +export function generateNext(func) { + return func || sandbox.stub(); +} diff --git a/test/helpers/globals.helper.js b/test/helpers/globals.helper.js index 74a9ae6045..32ec18f79e 100644 --- a/test/helpers/globals.helper.js +++ b/test/helpers/globals.helper.js @@ -8,3 +8,5 @@ global.sinon = require("sinon"); chai.use(require("sinon-chai")) chai.use(require("chai-as-promised")); global.expect = chai.expect + +global.sandbox = sinon.sandbox.create();