Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

Fix bugs in givenUser/WithRole when combined withUserModel helper #69

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to use loopback.getModelByType(Loopback.User) to lookup the actual user model.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@clarkorz can you please clarify further. In this case we are interested in simply getting the name(ie string, to use as a key to lookup subsequently) of the User model, and don't really need the actual Model or the Type.

modelKey = modelName;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's better to use the camelCase of modelName for modelKey

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure why camelCase would be better.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brijs User for constructor(Class) name and user for instance name, as common.

}

var test = this;
Expand Down Expand Up @@ -146,17 +147,18 @@ _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);
return done(err);
}

test.userRole = result;
test.app.models.roleMapping.create(
{principalId: test.user.id,
principalType: test.app.models.roleMapping.USER,
var userModelName = test.userModel ? test.userModel : 'User';
test.app.models.RoleMapping.create(
{principalId: test[userModelName].id,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should use modelKey

principalType: test.app.models.RoleMapping.USER,
roleId: result.id},
function (err, result) {
if(err) {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
39 changes: 39 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ 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'});

testApp.use(loopback.rest());
helpers.beforeEach.withApp(testApp);
Expand Down Expand Up @@ -109,4 +112,40 @@ describe('helpers', function () {
});
});
});

describe('givenUserWithRole', function() {
helpers.beforeEach.givenUserWithRole(
{id: 1, email: "[email protected]", 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() {
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);
});
});
});
});