From 895629632f970814fee38261701ca21de456558b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Tue, 26 Jul 2016 14:10:13 +0200 Subject: [PATCH] test: use local registry in test fixtures 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. --- common/models/acl.js | 4 +- common/models/change.js | 2 +- test/access-token.test.js | 3 ++ .../access-control/server/model-config.json | 3 +- test/fixtures/access-control/server/server.js | 2 +- test/fixtures/e2e/server/server.js | 2 +- .../simple-app/server/model-config.json | 3 +- .../server/model-config.json | 3 +- .../simple-integration-app/server/server.js | 2 +- .../common/models/post.json | 4 +- .../server/datasources.json | 2 +- .../server/model-config.json | 7 ++-- .../user-integration-app/server/server.js | 2 +- test/model.test.js | 39 ++++++++----------- test/user.integration.js | 8 +--- 15 files changed, 41 insertions(+), 45 deletions(-) diff --git a/common/models/acl.js b/common/models/acl.js index d4823cec..6bde460c 100644 --- a/common/models/acl.js +++ b/common/models/acl.js @@ -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) { @@ -360,7 +360,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); diff --git a/common/models/change.js b/common/models/change.js index 605caa23..ad19a754 100644 --- a/common/models/change.js +++ b/common/models/change.js @@ -146,7 +146,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; diff --git a/test/access-token.test.js b/test/access-token.test.js index 4b6ebc5d..c0b9a436 100644 --- a/test/access-token.test.js +++ b/test/access-token.test.js @@ -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) { diff --git a/test/fixtures/access-control/server/model-config.json b/test/fixtures/access-control/server/model-config.json index 2a48ed23..ddb886fc 100644 --- a/test/fixtures/access-control/server/model-config.json +++ b/test/fixtures/access-control/server/model-config.json @@ -2,7 +2,8 @@ "_meta": { "sources": [ "../common/models", - "./models" + "./models", + "../../../../common/models" ] }, "ACL": { diff --git a/test/fixtures/access-control/server/server.js b/test/fixtures/access-control/server/server.js index 13c2b910..ba596a36 100644 --- a/test/fixtures/access-control/server/server.js +++ b/test/fixtures/access-control/server/server.js @@ -5,7 +5,7 @@ var loopback = require('../../../..'); var boot = require('loopback-boot'); -var app = module.exports = loopback(); +var app = module.exports = loopback({ localRegistry: true }); boot(app, __dirname); diff --git a/test/fixtures/e2e/server/server.js b/test/fixtures/e2e/server/server.js index 7fd6b745..c353834f 100644 --- a/test/fixtures/e2e/server/server.js +++ b/test/fixtures/e2e/server/server.js @@ -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; diff --git a/test/fixtures/simple-app/server/model-config.json b/test/fixtures/simple-app/server/model-config.json index 08448d73..86602c00 100644 --- a/test/fixtures/simple-app/server/model-config.json +++ b/test/fixtures/simple-app/server/model-config.json @@ -2,7 +2,8 @@ "_meta": { "sources": [ "../common/models", - "./models" + "./models", + "../../../../common/models" ] }, "User": { diff --git a/test/fixtures/simple-integration-app/server/model-config.json b/test/fixtures/simple-integration-app/server/model-config.json index f5d34f28..ed73e7ca 100644 --- a/test/fixtures/simple-integration-app/server/model-config.json +++ b/test/fixtures/simple-integration-app/server/model-config.json @@ -2,7 +2,8 @@ "_meta": { "sources": [ "../common/models", - "./models" + "./models", + "../../../../common/models" ] }, "ACL": { diff --git a/test/fixtures/simple-integration-app/server/server.js b/test/fixtures/simple-integration-app/server/server.js index 7c92b9c2..95bc8961 100644 --- a/test/fixtures/simple-integration-app/server/server.js +++ b/test/fixtures/simple-integration-app/server/server.js @@ -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 }); boot(app, __dirname); var apiPath = '/api'; diff --git a/test/fixtures/user-integration-app/common/models/post.json b/test/fixtures/user-integration-app/common/models/post.json index e6990891..dd6d41f4 100644 --- a/test/fixtures/user-integration-app/common/models/post.json +++ b/test/fixtures/user-integration-app/common/models/post.json @@ -1,5 +1,5 @@ { - "name": "post", + "name": "Post", "base": "PersistedModel", "properties": { "title": { @@ -18,4 +18,4 @@ "model": "User" } } -} \ No newline at end of file +} diff --git a/test/fixtures/user-integration-app/server/datasources.json b/test/fixtures/user-integration-app/server/datasources.json index b024f6f3..9ebcf84f 100644 --- a/test/fixtures/user-integration-app/server/datasources.json +++ b/test/fixtures/user-integration-app/server/datasources.json @@ -8,4 +8,4 @@ {"type": "STUB"} ] } -} \ No newline at end of file +} diff --git a/test/fixtures/user-integration-app/server/model-config.json b/test/fixtures/user-integration-app/server/model-config.json index b8181828..172ba6f8 100644 --- a/test/fixtures/user-integration-app/server/model-config.json +++ b/test/fixtures/user-integration-app/server/model-config.json @@ -2,7 +2,8 @@ "_meta": { "sources": [ "../common/models", - "./models" + "./models", + "../../../../common/models" ] }, "Email": { @@ -14,7 +15,7 @@ "public": true, "relations": { "posts": { - "model": "post", + "model": "Post", "type": "hasMany", "foreignKey": "userId" } @@ -51,7 +52,7 @@ "dataSource": "db", "public": true }, - "post": { + "Post": { "dataSource": "db", "public": true } diff --git a/test/fixtures/user-integration-app/server/server.js b/test/fixtures/user-integration-app/server/server.js index bbcb7fcc..bfd7e1af 100644 --- a/test/fixtures/user-integration-app/server/server.js +++ b/test/fixtures/user-integration-app/server/server.js @@ -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 }); app.enableAuth(); boot(app, __dirname); app.use(loopback.token({model: app.models.AccessToken})); diff --git a/test/model.test.js b/test/model.test.js index a0269001..27e89ca5 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -8,7 +8,6 @@ var chai = require('chai'); var expect = chai.expect; var loopback = require('../'); var ACL = loopback.ACL; -var Change = loopback.Change; var defineModelTestsWithDataSource = require('./util/model-tests'); var PersistedModel = loopback.PersistedModel; var sinonChai = require('sinon-chai'); @@ -80,7 +79,10 @@ describe.onServer('Remote Methods', function() { var 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, @@ -93,7 +95,7 @@ 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 @@ -101,12 +103,10 @@ describe.onServer('Remote Methods', function() { 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); @@ -118,21 +118,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.use(loopback.rest()); - app.model(User); }); describe('Model.destroyAll(callback)', function() { @@ -556,7 +551,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')); }); }); diff --git a/test/user.integration.js b/test/user.integration.js index 753df249..46f597c5 100644 --- a/test/user.integration.js +++ b/test/user.integration.js @@ -22,16 +22,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) {