Merge pull request #2555 from strongloop/fix/global-registry-in-fixtures-2x

test: use local registry in test fixtures
This commit is contained in:
Miroslav Bajtoš 2016-07-27 15:29:23 +02:00 committed by GitHub
commit b3c66f34c8
15 changed files with 41 additions and 45 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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) {

View File

@ -2,7 +2,8 @@
"_meta": {
"sources": [
"../common/models",
"./models"
"./models",
"../../../../common/models"
]
},
"ACL": {

View File

@ -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);

View File

@ -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;

View File

@ -2,7 +2,8 @@
"_meta": {
"sources": [
"../common/models",
"./models"
"./models",
"../../../../common/models"
]
},
"User": {

View File

@ -2,7 +2,8 @@
"_meta": {
"sources": [
"../common/models",
"./models"
"./models",
"../../../../common/models"
]
},
"ACL": {

View File

@ -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';

View File

@ -1,5 +1,5 @@
{
"name": "post",
"name": "Post",
"base": "PersistedModel",
"properties": {
"title": {
@ -18,4 +18,4 @@
"model": "User"
}
}
}
}

View File

@ -8,4 +8,4 @@
{"type": "STUB"}
]
}
}
}

View File

@ -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
}

View File

@ -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}));

View File

@ -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'));
});
});

View File

@ -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) {