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

Allow another name convention for User model and AccessToken model #26

Open
wants to merge 1 commit 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
23 changes: 14 additions & 9 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ var helpers = exports = module.exports = {
it: _it,
beforeEach: _beforeEach
};
var _ = require('lodash');
var assert = require('assert');
var request = require('supertest');
var expect = require('chai').expect;

_beforeEach.withApp = function(app) {
if (app.models.User) {
_beforeEach.withApp = function(app, options) {
options = _.extend({ User: 'User', AccessToken: 'AccessToken' }, options);

if (app.models[options.User]) {
// Speed up the password hashing algorithm
app.models.User.settings.saltWorkFactor = 4;
app.models[options.User].settings.saltWorkFactor = 4;
}

beforeEach(function() {
Expand All @@ -23,6 +26,7 @@ _beforeEach.withApp = function(app) {
this.get = _request.get;
this.put = _request.put;
this.del = _request.del;
this.options = options;
});
}

Expand Down Expand Up @@ -77,7 +81,7 @@ _beforeEach.withArgs = function() {
}

_beforeEach.givenModel = function(modelName, attrs, optionalHandler) {
var modelKey = modelName;
var modelKey = _.camelCase(modelName);

if(typeof attrs === 'function') {
optionalHandler = attrs;
Expand All @@ -93,6 +97,7 @@ _beforeEach.givenModel = function(modelName, attrs, optionalHandler) {
beforeEach(function(done) {
var test = this;
var app = this.app;
modelName = _.capitalize(_.camelCase(this.options[modelName] || modelName));
var model = app.models[modelName];
assert(model, 'cannot get model of name ' + modelName + ' from app.models');
assert(model.dataSource, 'cannot test model '+ modelName
Expand All @@ -108,7 +113,7 @@ _beforeEach.givenModel = function(modelName, attrs, optionalHandler) {
if(err.details) console.error(err.details);
done(err);
} else {
test[modelKey] = result;
test[modelKey] = test[_.kebabCase(modelKey)] = result;
done();
}
});
Expand All @@ -124,8 +129,8 @@ _beforeEach.givenModel = function(modelName, attrs, optionalHandler) {
}

_beforeEach.givenUser = function(attrs, optionalHandler) {
_beforeEach.givenModel('user', attrs, optionalHandler);
}
_beforeEach.givenModel('User', attrs, optionalHandler);
}

_beforeEach.givenUserWithRole = function (attrs, role, optionalHandler) {
_beforeEach.givenUser(attrs, function (done) {
Expand Down Expand Up @@ -222,11 +227,11 @@ _beforeEach.givenLoggedInUserWithRole = function(credentials, role, optionalHand
}

_beforeEach.givenAnUnauthenticatedToken = function(attrs, optionalHandler) {
_beforeEach.givenModel('accessToken', attrs, optionalHandler);
_beforeEach.givenModel('AccessToken', attrs, optionalHandler);

Choose a reason for hiding this comment

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

Shouldn't it be options.AccessToken?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@justin-lau this doesn't point to the test object. 'AccessToken' is the key of options, it will be use to retrieve the configured model name in _beforeEach#givenModel().

}

_beforeEach.givenAnAnonymousToken = function(attrs, optionalHandler) {
_beforeEach.givenModel('accessToken', {id: '$anonymous'}, optionalHandler);
_beforeEach.givenModel('AccessToken', {id: '$anonymous'}, optionalHandler);
}

_describe.whenCalledRemotely = function(verb, url, data, cb) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"dependencies": {
"async": "~0.9.0",
"chai": "^1.9.2",
"lodash": "^3.5.0",
"mocha": "~1.21.4",
"supertest": "~0.13.0"
},
Expand Down
39 changes: 39 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ describe('helpers', function () {
it('should have an xxx-test-model property', function () {
assert(this['xxx-test-model']);
assert(this['xxx-test-model'].id);
assert(this['xxx-test-model'] instanceof testModel);
});
});

Expand Down Expand Up @@ -109,4 +110,42 @@ describe('helpers', function () {
});
});
});

describe('helpers.beforeEach.givenUser', function() {
describe('with default user model', function() {
testApp.model(loopback.User, {dataSource: 'db'});
helpers.beforeEach.givenUser({ email: '[email protected]', password: '000000' });
it('should create an user of User type', function () {
assert(this['user'] instanceof loopback.User);
});
});
describe('with custom User model', function() {
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens if we have multiple custom user models?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

multiple custom user models won't work properly, check discuss here: strongloop/loopback#527

var Account = loopback.User.extend('Account');
testApp.model(Account, {dataSource: 'db'});
helpers.beforeEach.withApp(testApp, { User: 'Account' });
helpers.beforeEach.givenUser({ email: '[email protected]', password: '000000' });
it('should create an user of Account type', function () {
assert(this.user instanceof Account);
});
});
});

describe('helpers.beforeEach.givenAnUnauthenticatedToken', function() {
describe('with default AccessToken model', function() {
testApp.model(loopback.AccessToken, {dataSource: 'db'});
helpers.beforeEach.givenAnUnauthenticatedToken({});
it('should create an accessToken of AccessToken type', function () {
assert(this.accessToken instanceof loopback.AccessToken);
});
});
describe('with custom AccessToken model', function() {
var Token = loopback.AccessToken.extend('Token');
testApp.model(Token, {dataSource: 'db'});
helpers.beforeEach.withApp(testApp, { AccessToken: 'Token' });
helpers.beforeEach.givenAnUnauthenticatedToken({});
it('should create an accessToken of Token type', function () {
assert(this.accessToken instanceof Token);
});
});
});
});