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

Commit 63a1727

Browse files
committed
Fix built-in models not found
If project doesn't extend built-in models **User** and **AccessToken** as **user** and **accessToken**, `givenModel()` can NOT find these models by keys `user` and `accessToken` via `app.models[key]`. By capitalizing the keys, `givenModel()` can always find the proper built-in model. Note: When `user` model extends `User` model, it will register both `user` and `User` to `app.models`. Signed-off-by: Clark Wang <[email protected]> Allow to specify user and accessToken models Add an options param to `lt.beforeEach.withApp()`, allow to specify the user model and/or accessToken model that will be used in the tests. Signed-off-by: Clark Wang <[email protected]> refactor according to @ritch's notes Signed-off-by: Clark Wang <[email protected]> use a simple approach Signed-off-by: Clark Wang <[email protected]> remove unused dep Signed-off-by: Clark Wang <[email protected]> pass tests Signed-off-by: Clark Wang <[email protected]> fix missing dep upgrade to use lodash string util methods Signed-off-by: Clark Wang <[email protected]>
1 parent 5e0528a commit 63a1727

File tree

3 files changed

+54
-9
lines changed

3 files changed

+54
-9
lines changed

lib/helpers.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ var helpers = exports = module.exports = {
66
it: _it,
77
beforeEach: _beforeEach
88
};
9+
var _ = require('lodash');
910
var assert = require('assert');
1011
var request = require('supertest');
1112
var expect = require('chai').expect;
1213

13-
_beforeEach.withApp = function(app) {
14-
if (app.models.User) {
14+
_beforeEach.withApp = function(app, options) {
15+
options = _.extend({ User: 'User', AccessToken: 'AccessToken' }, options);
16+
17+
if (app.models[options.User]) {
1518
// Speed up the password hashing algorithm
16-
app.models.User.settings.saltWorkFactor = 4;
19+
app.models[options.User].settings.saltWorkFactor = 4;
1720
}
1821

1922
beforeEach(function() {
@@ -23,6 +26,7 @@ _beforeEach.withApp = function(app) {
2326
this.get = _request.get;
2427
this.put = _request.put;
2528
this.del = _request.del;
29+
this.options = options;
2630
});
2731
}
2832

@@ -77,7 +81,7 @@ _beforeEach.withArgs = function() {
7781
}
7882

7983
_beforeEach.givenModel = function(modelName, attrs, optionalHandler) {
80-
var modelKey = modelName;
84+
var modelKey = _.camelCase(modelName);
8185

8286
if(typeof attrs === 'function') {
8387
optionalHandler = attrs;
@@ -93,6 +97,7 @@ _beforeEach.givenModel = function(modelName, attrs, optionalHandler) {
9397
beforeEach(function(done) {
9498
var test = this;
9599
var app = this.app;
100+
modelName = _.capitalize(_.camelCase(this.options[modelName] || modelName));
96101
var model = app.models[modelName];
97102
assert(model, 'cannot get model of name ' + modelName + ' from app.models');
98103
assert(model.dataSource, 'cannot test model '+ modelName
@@ -108,7 +113,7 @@ _beforeEach.givenModel = function(modelName, attrs, optionalHandler) {
108113
if(err.details) console.error(err.details);
109114
done(err);
110115
} else {
111-
test[modelKey] = result;
116+
test[modelKey] = test[_.kebabCase(modelKey)] = result;
112117
done();
113118
}
114119
});
@@ -124,8 +129,8 @@ _beforeEach.givenModel = function(modelName, attrs, optionalHandler) {
124129
}
125130

126131
_beforeEach.givenUser = function(attrs, optionalHandler) {
127-
_beforeEach.givenModel('user', attrs, optionalHandler);
128-
}
132+
_beforeEach.givenModel('User', attrs, optionalHandler);
133+
}
129134

130135
_beforeEach.givenUserWithRole = function (attrs, role, optionalHandler) {
131136
_beforeEach.givenUser(attrs, function (done) {
@@ -222,11 +227,11 @@ _beforeEach.givenLoggedInUserWithRole = function(credentials, role, optionalHand
222227
}
223228

224229
_beforeEach.givenAnUnauthenticatedToken = function(attrs, optionalHandler) {
225-
_beforeEach.givenModel('accessToken', attrs, optionalHandler);
230+
_beforeEach.givenModel('AccessToken', attrs, optionalHandler);
226231
}
227232

228233
_beforeEach.givenAnAnonymousToken = function(attrs, optionalHandler) {
229-
_beforeEach.givenModel('accessToken', {id: '$anonymous'}, optionalHandler);
234+
_beforeEach.givenModel('AccessToken', {id: '$anonymous'}, optionalHandler);
230235
}
231236

232237
_describe.whenCalledRemotely = function(verb, url, data, cb) {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"dependencies": {
1515
"async": "~0.9.0",
1616
"chai": "^1.9.2",
17+
"lodash": "^3.5.0",
1718
"mocha": "~1.21.4",
1819
"supertest": "~0.13.0"
1920
},

test/test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ describe('helpers', function () {
6060
it('should have an xxx-test-model property', function () {
6161
assert(this['xxx-test-model']);
6262
assert(this['xxx-test-model'].id);
63+
assert(this['xxx-test-model'] instanceof testModel);
6364
});
6465
});
6566

@@ -109,4 +110,42 @@ describe('helpers', function () {
109110
});
110111
});
111112
});
113+
114+
describe('helpers.beforeEach.givenUser', function() {
115+
describe('with default user model', function() {
116+
testApp.model(loopback.User, {dataSource: 'db'});
117+
helpers.beforeEach.givenUser({ email: '[email protected]', password: '000000' });
118+
it('should create an user of User type', function () {
119+
assert(this['user'] instanceof loopback.User);
120+
});
121+
});
122+
describe('with custom User model', function() {
123+
var Account = loopback.User.extend('Account');
124+
testApp.model(Account, {dataSource: 'db'});
125+
helpers.beforeEach.withApp(testApp, { User: 'Account' });
126+
helpers.beforeEach.givenUser({ email: '[email protected]', password: '000000' });
127+
it('should create an user of Account type', function () {
128+
assert(this.user instanceof Account);
129+
});
130+
});
131+
});
132+
133+
describe('helpers.beforeEach.givenAnUnauthenticatedToken', function() {
134+
describe('with default AccessToken model', function() {
135+
testApp.model(loopback.AccessToken, {dataSource: 'db'});
136+
helpers.beforeEach.givenAnUnauthenticatedToken({});
137+
it('should create an accessToken of AccessToken type', function () {
138+
assert(this.accessToken instanceof loopback.AccessToken);
139+
});
140+
});
141+
describe('with custom AccessToken model', function() {
142+
var Token = loopback.AccessToken.extend('Token');
143+
testApp.model(Token, {dataSource: 'db'});
144+
helpers.beforeEach.withApp(testApp, { AccessToken: 'Token' });
145+
helpers.beforeEach.givenAnUnauthenticatedToken({});
146+
it('should create an accessToken of Token type', function () {
147+
assert(this.accessToken instanceof Token);
148+
});
149+
});
150+
});
112151
});

0 commit comments

Comments
 (0)