From 7b41458eb1d52a5d87136709997bda9eb054cdce Mon Sep 17 00:00:00 2001 From: Lemon Cola Date: Sat, 18 Jul 2015 08:57:12 +0800 Subject: [PATCH 01/11] Typo: whenCalledUnauthenticated should use givenAnUnauthenticatedToken() --- lib/helpers.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/helpers.js b/lib/helpers.js index 000cfdc..de096a3 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -36,7 +36,7 @@ _beforeEach.cleanDatasource = function(dsName) { this.app.datasources[dsName].automigrate(); this.app.datasources[dsName].connector.ids = {}; } - + done(); }); } @@ -164,7 +164,7 @@ _beforeEach.givenUserWithRole = function (attrs, role, optionalHandler) { if(err.details) console.error(err.details); return done(err); } - + test.userRoleMapping = result; done(); } @@ -328,7 +328,7 @@ _describe.whenCalledByUserWithRole = function (credentials, role, verb, url, dat describe('when called by logged in user with role ' + role, function () { _beforeEach.givenLoggedInUserWithRole(credentials, role); _describe.whenCalledRemotely(verb, url, data, cb); - }); + }); } _describe.whenCalledAnonymously = function(verb, url, data, cb) { @@ -340,7 +340,7 @@ _describe.whenCalledAnonymously = function(verb, url, data, cb) { _describe.whenCalledUnauthenticated = function(verb, url, data, cb) { describe('when called with unauthenticated token', function () { - _beforeEach.givenAnAnonymousToken(); + _beforeEach.givenAnUnauthenticatedToken(); _describe.whenCalledRemotely(verb, url, data, cb); }); } From 25e924d481e991784daebc298dc47acb7b04444d Mon Sep 17 00:00:00 2001 From: Lemon Cola Date: Mon, 20 Jul 2015 12:40:31 +0800 Subject: [PATCH 02/11] Replace call of login method on constructor --- lib/helpers.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/helpers.js b/lib/helpers.js index de096a3..644466f 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -194,7 +194,7 @@ _beforeEach.givenUserWithRole = function (attrs, role, optionalHandler) { _beforeEach.givenLoggedInUser = function(credentials, optionalHandler) { _beforeEach.givenUser(credentials, function(done) { var test = this; - this.app.models[this.userModel].constructor.login(credentials, function(err, token) { + this.app.models[this.userModel].login(credentials, function(err, token) { if(err) { done(err); } else { @@ -217,7 +217,7 @@ _beforeEach.givenLoggedInUser = function(credentials, optionalHandler) { _beforeEach.givenLoggedInUserWithRole = function(credentials, role, optionalHandler){ _beforeEach.givenUserWithRole(credentials, role, function(done) { var test = this; - this.app.models[this.userModel].constructor.login(credentials, function(err, token) { + this.app.models[this.userModel].login(credentials, function(err, token) { if(err) { done(err); } else { From 8e769638c202df4db5e18bdfb01c32c1e20e60fb Mon Sep 17 00:00:00 2001 From: Lemon Cola Date: Mon, 20 Jul 2015 12:44:49 +0800 Subject: [PATCH 03/11] Modify github link and bump version --- package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 83666e6..5a70a58 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "loopback-testing", - "version": "1.2.0", + "version": "1.2.1", "description": "Utilities for testing LoopBack applications", "main": "index.js", "scripts": { @@ -8,7 +8,7 @@ }, "repository": { "type": "git", - "url": "https://github.com/strongloop/loopback-testing" + "url": "https://github.com/tscoder/loopback-testing" }, "author": "Ritchie Martori", "dependencies": { @@ -22,6 +22,6 @@ }, "license": { "name": "Dual MIT/StrongLoop", - "url": "https://github.com/strongloop/loopback-testing/blob/master/LICENSE" + "url": "https://github.com/tscoder/loopback-testing/blob/master/LICENSE" } } From e7e0b6dc6eb294bbde7be8675a0f4217b73b99b8 Mon Sep 17 00:00:00 2001 From: Lemon Cola Date: Mon, 20 Jul 2015 13:52:10 +0800 Subject: [PATCH 04/11] Wait until automigrate finished before returning control https://github.com/strongloop/loopback-testing/pull/58 --- lib/helpers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/helpers.js b/lib/helpers.js index 644466f..d2cf007 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -33,7 +33,7 @@ _beforeEach.cleanDatasource = function(dsName) { if (typeof this.app === 'function' && typeof this.app.datasources === 'object' && typeof this.app.datasources[dsName] === 'object') { - this.app.datasources[dsName].automigrate(); + this.app.datasources[dsName].automigrate(done); this.app.datasources[dsName].connector.ids = {}; } From 29b51f212552b82fb8a4326efd8b1bebf4495eea Mon Sep 17 00:00:00 2001 From: Lemon Cola Date: Mon, 20 Jul 2015 15:12:33 +0800 Subject: [PATCH 05/11] Avoid multiple calls to done() --- lib/helpers.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/helpers.js b/lib/helpers.js index d2cf007..c0f3e9c 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -35,9 +35,9 @@ _beforeEach.cleanDatasource = function(dsName) { && typeof this.app.datasources[dsName] === 'object') { this.app.datasources[dsName].automigrate(done); this.app.datasources[dsName].connector.ids = {}; + } else { + done(); } - - done(); }); } From 19e75ffdae92438d46536cbdbdc5fa94f0b7a5a2 Mon Sep 17 00:00:00 2001 From: Lemon Cola Date: Fri, 24 Jul 2015 11:00:43 +0800 Subject: [PATCH 06/11] Add it.shouldBeDeniedByAcl() which expect aclErrorStatus (if defined) or 401 - it.shouldBeDenied() will expect 401 only. - it.shouldBeDeniedWhenCalledByUser() uses it.shouldBeDeniedByAcl() - it.shouldBeDeniedWhenCalledByUserWithRole() uses it.shouldBeDeniedByAcl() http://docs.strongloop.com/display/public/LB/config.json https://github.com/strongloop/loopback/issues/1513 --- lib/helpers.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/helpers.js b/lib/helpers.js index c0f3e9c..91837ee 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -356,6 +356,14 @@ _it.shouldBeAllowed = function() { _it.shouldBeDenied = function() { it('should not be allowed', function() { + assert(this.res); + var expectedStatus = 401; + expect(this.res.statusCode).to.equal(expectedStatus); + }); +} + +_it.shouldBeDeniedByAcl = function() { + it('should not be allowed by ACL', function() { assert(this.res); var expectedStatus = this.aclErrorStatus || this.app && this.app.get('aclErrorStatus') || @@ -409,7 +417,7 @@ function(credentials, verb, url, data) { _it.shouldBeDeniedWhenCalledByUser = function(credentials, verb, url) { _describe.whenCalledByUser(credentials, verb, url, function() { - _it.shouldBeDenied(); + _it.shouldBeDeniedByAcl(); }); } @@ -423,6 +431,6 @@ function(credentials, role, verb, url, data) { _it.shouldBeDeniedWhenCalledByUserWithRole = function(credentials, role, verb, url) { _describe.whenCalledByUserWithRole(credentials, role, verb, url, function() { - _it.shouldBeDenied(); + _it.shouldBeDeniedByAcl(); }); } From d9f3c02e7021dbf11e43996b5c57ca9c185088e7 Mon Sep 17 00:00:00 2001 From: Lemon Cola Date: Fri, 24 Jul 2015 11:37:00 +0800 Subject: [PATCH 07/11] Add util.[dumpHttpContext, dumpHttpRequest, dumpHttpResponse] --- index.js | 1 + lib/helpers.js | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 4adb883..853efc3 100644 --- a/index.js +++ b/index.js @@ -2,4 +2,5 @@ var helpers = require('./lib/helpers'); exports.describe = helpers.describe; exports.it = helpers.it; exports.beforeEach = helpers.beforeEach; +exports.util = helpers.util; exports.TestDataBuilder = require('./lib/test-data-builder'); diff --git a/lib/helpers.js b/lib/helpers.js index 91837ee..78f9f0f 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -1,10 +1,12 @@ var _describe = {}; var _it = {}; var _beforeEach = {}; +var _util = {}; var helpers = exports = module.exports = { describe: _describe, it: _it, - beforeEach: _beforeEach + beforeEach: _beforeEach, + util: _util }; var assert = require('assert'); var request = require('supertest'); @@ -434,3 +436,23 @@ function(credentials, role, verb, url) { _it.shouldBeDeniedByAcl(); }); } + +_util.dumpHttpRequest = +function(logger, tag, req) { + logger('[' + tag + '] Request: \nheaders: %j\n', req._headers); +} + +_util.dumpHttpResponse = +function(logger, tag, res) { + logger('[' + tag + '] Response: \nstatus: %s\nmessage: %s\nheaders: %j\nbody: %j', + res.statusCode, + res.statusMessage, + res.headers, + res.body); +} + +_util.dumpHttpContext = +function(logger, tag, ctx) { + this.dumpHttpRequest(logger, tag, ctx.req); + this.dumpHttpResponse(logger, tag, ctx.res); +} \ No newline at end of file From ada726774cff65df0ca148f2b45b413f712c5752 Mon Sep 17 00:00:00 2001 From: Lemon Cola Date: Fri, 24 Jul 2015 12:10:16 +0800 Subject: [PATCH 08/11] Bump version to 1.2.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 5a70a58..491a5a6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "loopback-testing", - "version": "1.2.1", + "version": "1.2.2", "description": "Utilities for testing LoopBack applications", "main": "index.js", "scripts": { From a6fe739f15cf87a73fa8ceb1d83dd101f500538f Mon Sep 17 00:00:00 2001 From: Lemon Cola Date: Fri, 24 Jul 2015 12:39:42 +0800 Subject: [PATCH 09/11] Rename shouldBeDeniedByAcl() to shouldBeForbidden() --- lib/helpers.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/helpers.js b/lib/helpers.js index 78f9f0f..8973ebe 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -364,8 +364,8 @@ _it.shouldBeDenied = function() { }); } -_it.shouldBeDeniedByAcl = function() { - it('should not be allowed by ACL', function() { +_it.shouldBeForbidden = function() { + it('should be forbidden', function() { assert(this.res); var expectedStatus = this.aclErrorStatus || this.app && this.app.get('aclErrorStatus') || @@ -419,7 +419,7 @@ function(credentials, verb, url, data) { _it.shouldBeDeniedWhenCalledByUser = function(credentials, verb, url) { _describe.whenCalledByUser(credentials, verb, url, function() { - _it.shouldBeDeniedByAcl(); + _it.shouldBeForbidden(); }); } @@ -433,7 +433,7 @@ function(credentials, role, verb, url, data) { _it.shouldBeDeniedWhenCalledByUserWithRole = function(credentials, role, verb, url) { _describe.whenCalledByUserWithRole(credentials, role, verb, url, function() { - _it.shouldBeDeniedByAcl(); + _it.shouldBeForbidden(); }); } From 24be73aeb754c19d9c1a39834bf660f1301f3349 Mon Sep 17 00:00:00 2001 From: Lemon Cola Date: Fri, 24 Jul 2015 18:01:23 +0800 Subject: [PATCH 10/11] givenModel(): use Model.upsert instead of create so that it won't error out if the model already existed in DB --- lib/helpers.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/helpers.js b/lib/helpers.js index 8973ebe..fe9a5b4 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -100,15 +100,16 @@ _beforeEach.givenModel = function(modelName, attrs, optionalHandler) { var test = this; var app = this.app; var model = app.models[modelName]; + + var createFunc = (typeof model.upsert === 'function')? 'upsert':'create'; + assert(model, 'cannot get model of name ' + modelName + ' from app.models'); assert(model.dataSource, 'cannot test model '+ modelName + ' without attached dataSource'); - assert( - typeof model.create === 'function', - modelName + ' does not have a create method' - ); + assert(typeof model[createFunc] === 'function', modelName + + ' does not have a create/upsert method'); - model.create(attrs, function(err, result) { + model[createFunc](attrs, function(err, result) { if(err) { console.error(err.message); if(err.details) console.error(err.details); @@ -125,7 +126,11 @@ _beforeEach.givenModel = function(modelName, attrs, optionalHandler) { } afterEach(function(done) { - this[modelKey].destroy(done); + if (this[modelKey]) { + this[modelKey].destroy(done); + } else { + done(); + } }); } @@ -455,4 +460,4 @@ _util.dumpHttpContext = function(logger, tag, ctx) { this.dumpHttpRequest(logger, tag, ctx.req); this.dumpHttpResponse(logger, tag, ctx.res); -} \ No newline at end of file +} From 3d2c2ee39879a9bd92e84639c84530ce891fb2d1 Mon Sep 17 00:00:00 2001 From: Lemon Cola Date: Fri, 24 Jul 2015 18:04:47 +0800 Subject: [PATCH 11/11] Release 1.2.3 givenUserModel() can now won't conflict with the functions below: - givenLoggedInUser() - whenLoggedInAsUser() - whenCalledByUser() - shouldBeXXXWhenCalledByUser() --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 491a5a6..169baf2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "loopback-testing", - "version": "1.2.2", + "version": "1.2.3", "description": "Utilities for testing LoopBack applications", "main": "index.js", "scripts": {