From 7d2a34c21b543188eee37e95343228039e556611 Mon Sep 17 00:00:00 2001 From: Brijesh Shetty Date: Mon, 5 Oct 2015 00:55:14 -0700 Subject: [PATCH 1/2] Fix bugs in givenUser/WithRole when combined withUserModel helper Summary: - Added tests to verify combined of withUserModel and givenUser (& givenUserWithRole). These failed previously. - Changed default User model to "User" instead of "user". - Removed "constructor" in User.login invocations. - Role.findOrCreate was incorrectly double-nesting the role-name. {name: {name: "testRole"} } --- lib/helpers.js | 16 +++++++++------- test/test.js | 26 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/lib/helpers.js b/lib/helpers.js index d747c70..4534994 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -92,7 +92,8 @@ _beforeEach.givenModel = function(modelName, attrs, optionalHandler) { beforeEach(function(done) { if(modelName === '__USERMODEL__') { - modelName = this.userModel ? this.userModel : 'user'; + modelName = this.userModel ? this.userModel : 'User'; + modelKey = modelName; } var test = this; @@ -146,7 +147,7 @@ _beforeEach.givenUserWithRole = function (attrs, role, optionalHandler) { } _beforeEach.givenUser(attrs, function (done) { var test = this; - test.app.models.Role.findOrCreate({name: role}, function (err, result) { + test.app.models.Role.findOrCreate(role, function (err, result) { if(err) { console.error(err.message); if(err.details) console.error(err.details); @@ -154,9 +155,10 @@ _beforeEach.givenUserWithRole = function (attrs, role, optionalHandler) { } test.userRole = result; - test.app.models.roleMapping.create( - {principalId: test.user.id, - principalType: test.app.models.roleMapping.USER, + var userModelName = test.userModel ? test.userModel : '__USERMODEL__'; + test.app.models.RoleMapping.create( + {principalId: test[userModelName].id, + principalType: test.app.models.RoleMapping.USER, roleId: result.id}, function (err, result) { if(err) { @@ -194,7 +196,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 +219,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 { diff --git a/test/test.js b/test/test.js index 70b4448..217262a 100644 --- a/test/test.js +++ b/test/test.js @@ -6,6 +6,8 @@ describe('helpers', function () { var testApp = loopback(); var db = testApp.dataSource('db', {connector: loopback.Memory}); var testModel = testApp.model('xxx-test-model', {dataSource: 'db'}); + testApp.model(loopback.Role, { dataSource: 'db'}); + testApp.model(loopback.RoleMapping, { dataSource: 'db'}); testApp.use(loopback.rest()); helpers.beforeEach.withApp(testApp); @@ -109,4 +111,28 @@ describe('helpers', function () { }); }); }); + + describe('withUserModel', function() { + helpers.beforeEach.withUserModel('xxx-test-model'); + it("should set the user model name", function() { + assert.equal(this.userModel, 'xxx-test-model'); + }); + + describe('givenUser', function() { + helpers.beforeEach.givenUser(); + it("should create a new instance of specified User model", function() { + assert(this[this.userModel]); + }); + }); + + describe('givenUserWithRole', function() { + helpers.beforeEach.givenUserWithRole({id: 1}, {id: 2, name: "testRole"}); + it("should create a user instance (of specified User model) with the given role", function() { + assert.equal(this[this.userModel].id, 1); + assert.equal(this.userRole.id, 2); + assert.equal(this.userRole.name, "testRole"); + assert(this.userRoleMapping); + }); + }); + }); }); From 5d681512948105a3604596e8adff3363827db5a6 Mon Sep 17 00:00:00 2001 From: Brijesh Shetty Date: Wed, 21 Oct 2015 22:23:59 -0700 Subject: [PATCH 2/2] Fix bug in givenUserWithRole with default User model Added a new test to verify givenUserWithRole when using the default User model. --- lib/helpers.js | 2 +- test/test.js | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/helpers.js b/lib/helpers.js index 4534994..9839d2c 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -155,7 +155,7 @@ _beforeEach.givenUserWithRole = function (attrs, role, optionalHandler) { } test.userRole = result; - var userModelName = test.userModel ? test.userModel : '__USERMODEL__'; + var userModelName = test.userModel ? test.userModel : 'User'; test.app.models.RoleMapping.create( {principalId: test[userModelName].id, principalType: test.app.models.RoleMapping.USER, diff --git a/test/test.js b/test/test.js index 217262a..5f19c2a 100644 --- a/test/test.js +++ b/test/test.js @@ -6,6 +6,7 @@ describe('helpers', function () { var testApp = loopback(); var db = testApp.dataSource('db', {connector: loopback.Memory}); var testModel = testApp.model('xxx-test-model', {dataSource: 'db'}); + testApp.model(loopback.User, { dataSource: 'db'}); testApp.model(loopback.Role, { dataSource: 'db'}); testApp.model(loopback.RoleMapping, { dataSource: 'db'}); @@ -112,6 +113,18 @@ describe('helpers', function () { }); }); + describe('givenUserWithRole', function() { + helpers.beforeEach.givenUserWithRole( + {id: 1, email: "abc@abc.com", password: "abc"}, + {id: 2, name: "testRole"}); + it("should create a user instance with default userModel with the given role", function() { + assert.equal(this['User'].id, 1); + assert.equal(this.userRole.id, 2); + assert.equal(this.userRole.name, "testRole"); + assert(this.userRoleMapping); + }); + }); + describe('withUserModel', function() { helpers.beforeEach.withUserModel('xxx-test-model'); it("should set the user model name", function() {