Skip to content

Commit

Permalink
test: use local registry in test fixtures
Browse files Browse the repository at this point in the history
Use local registry in test fixtures to prevent collision in globally
shared models.

Fix issues discoverd in auth implementation where the global registry
was used instead of the correct local one.
  • Loading branch information
bajtos committed Jul 27, 2016
1 parent 1b55d35 commit 9881621
Show file tree
Hide file tree
Showing 16 changed files with 43 additions and 48 deletions.
4 changes: 2 additions & 2 deletions common/models/acl.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ module.exports = function(ACL) {
* @return {Object[]} An array of ACLs
*/
ACL.getStaticACLs = function getStaticACLs(model, property) {
var modelClass = loopback.findModel(model);
var modelClass = this.registry.findModel(model);
var staticACLs = [];
if (modelClass && modelClass.settings.acls) {
modelClass.settings.acls.forEach(function(acl) {
Expand Down Expand Up @@ -361,7 +361,7 @@ module.exports = function(ACL) {
acls = acls.concat(dynACLs);
resolved = self.resolvePermission(acls, req);
if (resolved && resolved.permission === ACL.DEFAULT) {
var modelClass = loopback.findModel(model);
var modelClass = self.registry.findModel(model);
resolved.permission = (modelClass && modelClass.settings.defaultPermission) || ACL.ALLOW;
}
if (callback) callback(null, resolved);
Expand Down
2 changes: 1 addition & 1 deletion common/models/change.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ module.exports = function(Change) {
*/

Change.findOrCreateChange = function(modelName, modelId, callback) {
assert(loopback.findModel(modelName), modelName + ' does not exist');
assert(this.registry.findModel(modelName), modelName + ' does not exist');
callback = callback || utils.createPromiseCallback();
var id = this.idForModel(modelName, modelId);
var Change = this;
Expand Down
4 changes: 2 additions & 2 deletions common/models/user.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@
"principalType": "ROLE",
"principalId": "$owner",
"permission": "ALLOW",
"property": "updateAttributes"
"property": "patchAttributes"
},
{
"principalType": "ROLE",
"principalId": "$owner",
"permission": "ALLOW",
"property": "replaceById"
},
},
{
"principalType": "ROLE",
"principalId": "$everyone",
Expand Down
3 changes: 3 additions & 0 deletions test/access-token.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,9 @@ describe('AccessToken', function() {
});

describe('app.enableAuth()', function() {
beforeEach(function setupAuthWithModels() {
app.enableAuth({ dataSource: ds });
});
beforeEach(createTestingToken);

it('prevents remote call with 401 status on denied ACL', function(done) {
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/access-control/server/model-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"_meta": {
"sources": [
"../common/models",
"./models"
"./models",
"../../../../common/models"
]
},
"ACL": {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/access-control/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

var loopback = require('../../../..');
var boot = require('loopback-boot');
var app = module.exports = loopback();
var app = module.exports = loopback({ localRegistry: true });
var errorHandler = require('strong-error-handler');
boot(app, __dirname);

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/e2e/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// License text available at https://opensource.org/licenses/MIT

var loopback = require('../../../../index');
var app = module.exports = loopback();
var app = module.exports = loopback({ localRegistry: true });
var models = require('./models');
var TestModel = models.TestModel;

Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/simple-app/server/model-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"_meta": {
"sources": [
"../common/models",
"./models"
"./models",
"../../../../common/models"
]
},
"User": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"_meta": {
"sources": [
"../common/models",
"./models"
"./models",
"../../../../common/models"
]
},
"ACL": {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/simple-integration-app/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

var loopback = require('../../../../index');
var boot = require('loopback-boot');
var app = module.exports = loopback();
var app = module.exports = loopback({ localRegistry: true });
var errorHandler = require('strong-error-handler');

boot(app, __dirname);
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/user-integration-app/common/models/post.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "post",
"name": "Post",
"base": "PersistedModel",
"properties": {
"title": {
Expand All @@ -18,4 +18,4 @@
"model": "User"
}
}
}
}
2 changes: 1 addition & 1 deletion test/fixtures/user-integration-app/server/datasources.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
{"type": "STUB"}
]
}
}
}
7 changes: 4 additions & 3 deletions test/fixtures/user-integration-app/server/model-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"_meta": {
"sources": [
"../common/models",
"./models"
"./models",
"../../../../common/models"
]
},
"Email": {
Expand All @@ -14,7 +15,7 @@
"public": true,
"relations": {
"posts": {
"model": "post",
"model": "Post",
"type": "hasMany",
"foreignKey": "userId"
}
Expand Down Expand Up @@ -51,7 +52,7 @@
"dataSource": "db",
"public": true
},
"post": {
"Post": {
"dataSource": "db",
"public": true
}
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/user-integration-app/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

var loopback = require('../../../../index');
var boot = require('loopback-boot');
var app = module.exports = loopback();
var app = module.exports = loopback({ localRegistry: true });
var errorHandler = require('strong-error-handler');
app.enableAuth();
boot(app, __dirname);
Expand Down
40 changes: 17 additions & 23 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
var async = require('async');
var loopback = require('../');
var ACL = loopback.ACL;
var Change = loopback.Change;
var defineModelTestsWithDataSource = require('./util/model-tests');
var PersistedModel = loopback.PersistedModel;

Expand Down Expand Up @@ -74,7 +73,10 @@ describe.onServer('Remote Methods', function() {
var User, Post, dataSource, app;

beforeEach(function() {
User = PersistedModel.extend('user', {
app = loopback({ localRegistry: true, loadBuiltinModels: true });
app.set('remoting', { errorHandler: { debug: true, log: false }});

User = app.registry.createModel('user', {
id: { id: true, type: String, defaultFn: 'guid' },
'first': String,
'last': String,
Expand All @@ -87,20 +89,18 @@ describe.onServer('Remote Methods', function() {
trackChanges: true,
});

Post = PersistedModel.extend('post', {
Post = app.registry.createModel('post', {
id: { id: true, type: String, defaultFn: 'guid' },
title: String,
content: String,
}, {
trackChanges: true,
});

dataSource = loopback.createDataSource({
connector: loopback.Memory,
});
dataSource = app.dataSource('db', { connector: 'memory' });

User.attachTo(dataSource);
Post.attachTo(dataSource);
app.model(User, { dataSource: 'db' });
app.model(Post, { dataSource: 'db' });

User.hasMany(Post);

Expand All @@ -112,22 +112,16 @@ describe.onServer('Remote Methods', function() {
}
};

loopback.remoteMethod(
User.login,
{
accepts: [
{ arg: 'username', type: 'string', required: true },
{ arg: 'password', type: 'string', required: true },
],
returns: { arg: 'sessionId', type: 'any', root: true },
http: { path: '/sign-in', verb: 'get' },
}
);
User.remoteMethod('login', {
accepts: [
{ arg: 'username', type: 'string', required: true },
{ arg: 'password', type: 'string', required: true },
],
returns: { arg: 'sessionId', type: 'any', root: true },
http: { path: '/sign-in', verb: 'get' },
});

app = loopback();
app.set('remoting', { errorHandler: { debug: true, log: false }});
app.use(loopback.rest());
app.model(User);
});

describe('Model.destroyAll(callback)', function() {
Expand Down Expand Up @@ -549,7 +543,7 @@ describe.onServer('Remote Methods', function() {
it('Get the Change Model', function() {
var UserChange = User.getChangeModel();
var change = new UserChange();
assert(change instanceof Change);
assert(change instanceof app.registry.getModel('Change'));
});
});

Expand Down
8 changes: 1 addition & 7 deletions test/user.integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,10 @@ describe('users - integration', function() {
lt.beforeEach.withApp(app);

before(function(done) {
// HACK: [rfeng] We have to reset the relations as they are polluted by
// other tests
app.models.User.hasMany(app.models.post);
app.models.User.hasMany(app.models.AccessToken,
{ options: { disableInclude: true }});
app.models.AccessToken.belongsTo(app.models.User);
app.models.User.destroyAll(function(err) {
if (err) return done(err);

app.models.post.destroyAll(function(err) {
app.models.Post.destroyAll(function(err) {
if (err) return done(err);

app.models.blog.destroyAll(function(err) {
Expand Down

0 comments on commit 9881621

Please sign in to comment.