From 18328c0c4265ca21fc2d920a55cf743bf1ac5053 Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Fri, 6 Nov 2015 21:27:38 -0600 Subject: [PATCH 1/8] Add lodash.deepDefaults as a dependency --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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", From 303b88b6fae88efb095152dcd165e68466832fb4 Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Fri, 6 Nov 2015 21:28:19 -0600 Subject: [PATCH 2/8] Adjust api tests --- tasks/gulp-tests.js | 6 ++++-- test/helpers/api-integration.helper.js | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/tasks/gulp-tests.js b/tasks/gulp-tests.js index 8264f91321..1fb2242e5f 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/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) { From 6e344ce04b1eb8c7a386e608deb76c59462a7f72 Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Fri, 6 Nov 2015 21:28:31 -0600 Subject: [PATCH 3/8] Add sandbox as a global --- test/helpers/globals.helper.js | 2 ++ 1 file changed, 2 insertions(+) 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(); From 94dbb25fa61b94ad04196b9c0841e534b430ee31 Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Fri, 6 Nov 2015 21:28:47 -0600 Subject: [PATCH 4/8] Add unit helper --- test/helpers/api-unit.helper.js | 41 +++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 test/helpers/api-unit.helper.js diff --git a/test/helpers/api-unit.helper.js b/test/helpers/api-unit.helper.js new file mode 100644 index 0000000000..3bcf4be4b9 --- /dev/null +++ b/test/helpers/api-unit.helper.js @@ -0,0 +1,41 @@ +// @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)._doc; +} + +export function generateGroup(options={}) { + return new Group(options)._doc; +} + +export function generateRes(options={}) { + let defaultRes = { + send: sandbox.stub(), + 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); +} From e657a30320f8101c7e4a3cef3c4f71d95441abcb Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Sat, 7 Nov 2015 08:32:02 -0600 Subject: [PATCH 5/8] Add next generator for easier controller testing. --- test/helpers/api-unit.helper.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/helpers/api-unit.helper.js b/test/helpers/api-unit.helper.js index 3bcf4be4b9..5aa2816677 100644 --- a/test/helpers/api-unit.helper.js +++ b/test/helpers/api-unit.helper.js @@ -39,3 +39,7 @@ export function generateReq(options={}) { return defaults(options, defaultReq); } + +export function generateNext(func) { + return func || sandbox.stub(); +} From 1eadceea3d5ce88e01c05e1199b28a119732c09e Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Sat, 7 Nov 2015 09:25:43 -0600 Subject: [PATCH 6/8] Add status to default res --- test/helpers/api-unit.helper.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/helpers/api-unit.helper.js b/test/helpers/api-unit.helper.js index 5aa2816677..30bdab8fc3 100644 --- a/test/helpers/api-unit.helper.js +++ b/test/helpers/api-unit.helper.js @@ -21,6 +21,7 @@ export function generateGroup(options={}) { export function generateRes(options={}) { let defaultRes = { send: sandbox.stub(), + status: sandbox.stub().returnsThis(), json: sandbox.stub(), locals: { user: generateUser(options.localsUser), From b323c3b5e97d5388d81a50ca88a072cc68dcb660 Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Sat, 7 Nov 2015 09:26:13 -0600 Subject: [PATCH 7/8] Use res and req generators in error handler test --- .../v3/unit/middlewares/errorHandler.test.js | 33 ++++++++----------- 1 file changed, 14 insertions(+), 19 deletions(-) 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; From 3559be0f83d2be36012393998fbd396ed4672f35 Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Sat, 7 Nov 2015 09:42:57 -0600 Subject: [PATCH 8/8] Use toObject instead of _doc --- test/helpers/api-unit.helper.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/helpers/api-unit.helper.js b/test/helpers/api-unit.helper.js index 30bdab8fc3..6e561df076 100644 --- a/test/helpers/api-unit.helper.js +++ b/test/helpers/api-unit.helper.js @@ -11,11 +11,11 @@ afterEach(() => { }); export function generateUser(options={}) { - return new User(options)._doc; + return new User(options).toObject(); } export function generateGroup(options={}) { - return new Group(options)._doc; + return new Group(options).toObject(); } export function generateRes(options={}) {