2016-05-03 22:50:21 +00:00
|
|
|
// Copyright IBM Corp. 2013,2016. All Rights Reserved.
|
|
|
|
// Node module: loopback
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
'use strict';
|
2014-11-03 09:00:24 +00:00
|
|
|
var it = require('./util/it');
|
2014-12-01 10:36:34 +00:00
|
|
|
var describe = require('./util/describe');
|
|
|
|
var Domain = require('domain');
|
|
|
|
var EventEmitter = require('events').EventEmitter;
|
2016-09-02 21:22:42 +00:00
|
|
|
var loopback = require('../');
|
2016-11-22 14:30:04 +00:00
|
|
|
var expect = require('./helpers/expect');
|
2016-09-02 21:22:42 +00:00
|
|
|
var assert = require('assert');
|
2014-11-03 09:00:24 +00:00
|
|
|
|
2013-07-16 17:42:47 +00:00
|
|
|
describe('loopback', function() {
|
Add createModelFromConfig and configureModel()
Add new API allowing developers to split the model definition and
configuration into two steps:
1. Build models from JSON config, export them for re-use:
```js
var Customer = loopback.createModelFromConfig({
name: 'Customer',
base: 'User',
properties: {
address: 'string'
}
});
```
2. Attach existing models to a dataSource and a loopback app,
modify certain model aspects like relations:
```js
loopback.configureModel(Customer, {
dataSource: db,
relations: { /* ... */ }
});
```
Rework `app.model` to use `loopback.configureModel` under the hood.
Here is the new usage:
```js
var Customer = require('./models').Customer;
app.model(Customer, {
dataSource: 'db',
relations: { /* ... */ }
});
```
In order to preserve backwards compatibility with loopback 1.x,
`app.model(name, config)` calls both `createModelFromConfig`
and `configureModel`.
2014-06-05 15:41:12 +00:00
|
|
|
var nameCounter = 0;
|
|
|
|
var uniqueModelName;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
uniqueModelName = 'TestModel-' + (++nameCounter);
|
|
|
|
});
|
|
|
|
|
2014-01-02 09:31:47 +00:00
|
|
|
describe('exports', function() {
|
|
|
|
it('ValidationError', function() {
|
|
|
|
expect(loopback.ValidationError).to.be.a('function')
|
|
|
|
.and.have.property('name', 'ValidationError');
|
|
|
|
});
|
2014-11-03 09:00:24 +00:00
|
|
|
|
|
|
|
it.onServer('includes `faviconFile`', function() {
|
|
|
|
var file = loopback.faviconFile;
|
|
|
|
expect(file, 'faviconFile').to.not.equal(undefined);
|
|
|
|
expect(require('fs').existsSync(loopback.faviconFile), 'file exists')
|
|
|
|
.to.equal(true);
|
|
|
|
});
|
2014-11-10 18:12:00 +00:00
|
|
|
|
|
|
|
it.onServer('has `getCurrentContext` method', function() {
|
|
|
|
expect(loopback.getCurrentContext).to.be.a('function');
|
|
|
|
});
|
2015-04-17 16:02:04 +00:00
|
|
|
|
|
|
|
it.onServer('exports all expected properties', function() {
|
|
|
|
var EXPECTED = [
|
|
|
|
'ACL',
|
|
|
|
'AccessToken',
|
|
|
|
'Application',
|
|
|
|
'Change',
|
|
|
|
'Checkpoint',
|
|
|
|
'Connector',
|
|
|
|
'DataSource',
|
|
|
|
'Email',
|
|
|
|
'GeoPoint',
|
2016-08-08 15:24:17 +00:00
|
|
|
'KeyValueModel',
|
2015-04-17 16:02:04 +00:00
|
|
|
'Mail',
|
|
|
|
'Memory',
|
|
|
|
'Model',
|
|
|
|
'PersistedModel',
|
|
|
|
'Remote',
|
|
|
|
'Role',
|
|
|
|
'RoleMapping',
|
|
|
|
'Route',
|
|
|
|
'Router',
|
|
|
|
'Scope',
|
|
|
|
'User',
|
|
|
|
'ValidationError',
|
|
|
|
'application',
|
|
|
|
'configureModel',
|
|
|
|
'context',
|
|
|
|
'createContext',
|
|
|
|
'createDataSource',
|
|
|
|
'createModel',
|
|
|
|
'defaultDataSources',
|
|
|
|
'errorHandler',
|
|
|
|
'favicon',
|
|
|
|
'faviconFile',
|
|
|
|
'findModel',
|
|
|
|
'getCurrentContext',
|
|
|
|
'getModel',
|
|
|
|
'getModelByType',
|
|
|
|
'isBrowser',
|
|
|
|
'isServer',
|
|
|
|
'length',
|
|
|
|
'memory',
|
|
|
|
'modelBuilder',
|
|
|
|
'name',
|
|
|
|
'prototype',
|
|
|
|
'query',
|
|
|
|
'registry',
|
|
|
|
'remoteMethod',
|
|
|
|
'request',
|
|
|
|
'response',
|
|
|
|
'rest',
|
|
|
|
'runInContext',
|
|
|
|
'static',
|
|
|
|
'status',
|
|
|
|
'template',
|
|
|
|
'token',
|
|
|
|
'urlNotFound',
|
|
|
|
'version',
|
|
|
|
];
|
|
|
|
|
|
|
|
var actual = Object.getOwnPropertyNames(loopback);
|
|
|
|
actual.sort();
|
|
|
|
expect(actual).to.eql(EXPECTED);
|
|
|
|
});
|
2014-01-02 09:31:47 +00:00
|
|
|
});
|
|
|
|
|
2015-06-01 10:07:15 +00:00
|
|
|
describe('loopback(options)', function() {
|
|
|
|
it('supports localRegistry:true', function() {
|
2016-11-15 21:46:23 +00:00
|
|
|
var app = loopback({localRegistry: true});
|
2015-06-01 10:07:15 +00:00
|
|
|
expect(app.registry).to.not.equal(loopback.registry);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not load builtin models into the local registry', function() {
|
2016-11-15 21:46:23 +00:00
|
|
|
var app = loopback({localRegistry: true});
|
2015-06-01 10:07:15 +00:00
|
|
|
expect(app.registry.findModel('User')).to.equal(undefined);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('supports loadBuiltinModels:true', function() {
|
2016-11-15 21:46:23 +00:00
|
|
|
var app = loopback({localRegistry: true, loadBuiltinModels: true});
|
2015-06-01 10:07:15 +00:00
|
|
|
expect(app.registry.findModel('User'))
|
|
|
|
.to.have.property('modelName', 'User');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-10 20:46:06 +00:00
|
|
|
describe('loopback.createDataSource(options)', function() {
|
2013-06-11 16:01:44 +00:00
|
|
|
it('Create a data source with a connector.', function() {
|
2013-07-16 17:42:47 +00:00
|
|
|
var dataSource = loopback.createDataSource({
|
2016-04-01 09:14:26 +00:00
|
|
|
connector: loopback.Memory,
|
2013-06-11 16:01:44 +00:00
|
|
|
});
|
2013-07-23 19:59:40 +00:00
|
|
|
assert(dataSource.connector);
|
2013-06-08 00:37:30 +00:00
|
|
|
});
|
|
|
|
});
|
2014-01-23 23:27:31 +00:00
|
|
|
|
2014-07-10 20:46:06 +00:00
|
|
|
describe('data source created by loopback', function() {
|
2014-11-21 02:35:36 +00:00
|
|
|
it('should create model extending Model by default', function() {
|
2014-07-10 20:46:06 +00:00
|
|
|
var dataSource = loopback.createDataSource({
|
2016-04-01 09:14:26 +00:00
|
|
|
connector: loopback.Memory,
|
2014-07-10 20:46:06 +00:00
|
|
|
});
|
|
|
|
var m1 = dataSource.createModel('m1', {});
|
|
|
|
assert(m1.prototype instanceof loopback.Model);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('model created by loopback', function() {
|
|
|
|
it('should extend from Model by default', function() {
|
|
|
|
var m1 = loopback.createModel('m1', {});
|
|
|
|
assert(m1.prototype instanceof loopback.Model);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-07-16 17:42:47 +00:00
|
|
|
describe('loopback.remoteMethod(Model, fn, [options]);', function() {
|
2014-11-21 01:46:21 +00:00
|
|
|
it('Setup a remote method.', function() {
|
2016-11-15 21:46:23 +00:00
|
|
|
var Product = loopback.createModel('product', {price: Number});
|
2014-11-03 09:00:24 +00:00
|
|
|
|
2013-06-07 19:57:51 +00:00
|
|
|
Product.stats = function(fn) {
|
2013-06-11 16:01:44 +00:00
|
|
|
// ...
|
2014-11-21 01:46:21 +00:00
|
|
|
};
|
2014-11-03 09:00:24 +00:00
|
|
|
|
2013-07-16 17:42:47 +00:00
|
|
|
loopback.remoteMethod(
|
2013-06-07 19:57:51 +00:00
|
|
|
Product.stats,
|
|
|
|
{
|
2016-11-15 21:46:23 +00:00
|
|
|
returns: {arg: 'stats', type: 'array'},
|
|
|
|
http: {path: '/info', verb: 'get'},
|
2013-06-07 19:57:51 +00:00
|
|
|
}
|
|
|
|
);
|
2014-11-03 09:00:24 +00:00
|
|
|
|
2013-06-11 16:01:44 +00:00
|
|
|
assert.equal(Product.stats.returns.arg, 'stats');
|
|
|
|
assert.equal(Product.stats.returns.type, 'array');
|
|
|
|
assert.equal(Product.stats.http.path, '/info');
|
|
|
|
assert.equal(Product.stats.http.verb, 'get');
|
|
|
|
assert.equal(Product.stats.shared, true);
|
2013-06-07 19:57:51 +00:00
|
|
|
});
|
|
|
|
});
|
2013-12-10 01:11:01 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('loopback.createModel(name, properties, options)', function() {
|
|
|
|
describe('options.base', function() {
|
|
|
|
it('should extend from options.base', function() {
|
2013-12-10 01:11:01 +00:00
|
|
|
var MyModel = loopback.createModel('MyModel', {}, {
|
|
|
|
foo: {
|
2016-04-01 09:14:26 +00:00
|
|
|
bar: 'bat',
|
|
|
|
},
|
2013-12-10 01:11:01 +00:00
|
|
|
});
|
|
|
|
var MyCustomModel = loopback.createModel('MyCustomModel', {}, {
|
|
|
|
base: 'MyModel',
|
|
|
|
foo: {
|
2016-04-01 09:14:26 +00:00
|
|
|
bat: 'baz',
|
|
|
|
},
|
2013-12-10 01:11:01 +00:00
|
|
|
});
|
|
|
|
assert(MyCustomModel.super_ === MyModel);
|
2016-11-15 21:46:23 +00:00
|
|
|
assert.deepEqual(MyCustomModel.settings.foo, {bar: 'bat', bat: 'baz'});
|
2013-12-10 01:11:01 +00:00
|
|
|
assert(MyCustomModel.super_.modelName === MyModel.modelName);
|
|
|
|
});
|
|
|
|
});
|
2014-01-23 22:26:45 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('loopback.getModel and getModelByType', function() {
|
|
|
|
it('should be able to get model by name', function() {
|
2014-01-23 22:26:45 +00:00
|
|
|
var MyModel = loopback.createModel('MyModel', {}, {
|
|
|
|
foo: {
|
2016-04-01 09:14:26 +00:00
|
|
|
bar: 'bat',
|
|
|
|
},
|
2014-01-23 22:26:45 +00:00
|
|
|
});
|
|
|
|
var MyCustomModel = loopback.createModel('MyCustomModel', {}, {
|
|
|
|
base: 'MyModel',
|
|
|
|
foo: {
|
2016-04-01 09:14:26 +00:00
|
|
|
bat: 'baz',
|
|
|
|
},
|
2014-01-23 22:26:45 +00:00
|
|
|
});
|
|
|
|
assert(loopback.getModel('MyModel') === MyModel);
|
|
|
|
assert(loopback.getModel('MyCustomModel') === MyCustomModel);
|
2014-06-16 14:13:24 +00:00
|
|
|
assert(loopback.findModel('Invalid') === undefined);
|
2015-11-09 15:01:43 +00:00
|
|
|
assert(loopback.getModel(MyModel) === MyModel);
|
2014-01-23 22:26:45 +00:00
|
|
|
});
|
2014-11-21 02:35:36 +00:00
|
|
|
it('should be able to get model by type', function() {
|
2014-01-23 22:26:45 +00:00
|
|
|
var MyModel = loopback.createModel('MyModel', {}, {
|
|
|
|
foo: {
|
2016-04-01 09:14:26 +00:00
|
|
|
bar: 'bat',
|
|
|
|
},
|
2014-01-23 22:26:45 +00:00
|
|
|
});
|
|
|
|
var MyCustomModel = loopback.createModel('MyCustomModel', {}, {
|
|
|
|
base: 'MyModel',
|
|
|
|
foo: {
|
2016-04-01 09:14:26 +00:00
|
|
|
bat: 'baz',
|
|
|
|
},
|
2014-01-23 22:26:45 +00:00
|
|
|
});
|
|
|
|
assert(loopback.getModelByType(MyModel) === MyCustomModel);
|
|
|
|
assert(loopback.getModelByType(MyCustomModel) === MyCustomModel);
|
|
|
|
});
|
2014-06-16 14:13:24 +00:00
|
|
|
|
|
|
|
it('should throw when the model does not exist', function() {
|
|
|
|
expect(function() { loopback.getModel(uniqueModelName); })
|
|
|
|
.to.throw(Error, new RegExp('Model not found: ' + uniqueModelName));
|
|
|
|
});
|
2014-01-23 22:26:45 +00:00
|
|
|
});
|
2015-05-06 08:04:06 +00:00
|
|
|
|
|
|
|
it('configures remote methods', function() {
|
|
|
|
var TestModel = loopback.createModel(uniqueModelName, {}, {
|
|
|
|
methods: {
|
|
|
|
staticMethod: {
|
|
|
|
isStatic: true,
|
2016-11-15 21:46:23 +00:00
|
|
|
http: {path: '/static'},
|
2015-05-06 08:04:06 +00:00
|
|
|
},
|
|
|
|
instanceMethod: {
|
|
|
|
isStatic: false,
|
2016-11-15 21:46:23 +00:00
|
|
|
http: {path: '/instance'},
|
2016-04-01 09:14:26 +00:00
|
|
|
},
|
|
|
|
},
|
2015-05-06 08:04:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var methodNames = TestModel.sharedClass.methods().map(function(m) {
|
|
|
|
return m.stringName.replace(/^[^.]+\./, ''); // drop the class name
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(methodNames).to.include.members([
|
|
|
|
'staticMethod',
|
2016-04-01 09:14:26 +00:00
|
|
|
'prototype.instanceMethod',
|
2015-05-06 08:04:06 +00:00
|
|
|
]);
|
|
|
|
});
|
2013-12-10 01:11:01 +00:00
|
|
|
});
|
Add createModelFromConfig and configureModel()
Add new API allowing developers to split the model definition and
configuration into two steps:
1. Build models from JSON config, export them for re-use:
```js
var Customer = loopback.createModelFromConfig({
name: 'Customer',
base: 'User',
properties: {
address: 'string'
}
});
```
2. Attach existing models to a dataSource and a loopback app,
modify certain model aspects like relations:
```js
loopback.configureModel(Customer, {
dataSource: db,
relations: { /* ... */ }
});
```
Rework `app.model` to use `loopback.configureModel` under the hood.
Here is the new usage:
```js
var Customer = require('./models').Customer;
app.model(Customer, {
dataSource: 'db',
relations: { /* ... */ }
});
```
In order to preserve backwards compatibility with loopback 1.x,
`app.model(name, config)` calls both `createModelFromConfig`
and `configureModel`.
2014-06-05 15:41:12 +00:00
|
|
|
|
2014-06-09 09:18:52 +00:00
|
|
|
describe('loopback.createModel(config)', function() {
|
Add createModelFromConfig and configureModel()
Add new API allowing developers to split the model definition and
configuration into two steps:
1. Build models from JSON config, export them for re-use:
```js
var Customer = loopback.createModelFromConfig({
name: 'Customer',
base: 'User',
properties: {
address: 'string'
}
});
```
2. Attach existing models to a dataSource and a loopback app,
modify certain model aspects like relations:
```js
loopback.configureModel(Customer, {
dataSource: db,
relations: { /* ... */ }
});
```
Rework `app.model` to use `loopback.configureModel` under the hood.
Here is the new usage:
```js
var Customer = require('./models').Customer;
app.model(Customer, {
dataSource: 'db',
relations: { /* ... */ }
});
```
In order to preserve backwards compatibility with loopback 1.x,
`app.model(name, config)` calls both `createModelFromConfig`
and `configureModel`.
2014-06-05 15:41:12 +00:00
|
|
|
it('creates the model', function() {
|
2014-06-09 09:18:52 +00:00
|
|
|
var model = loopback.createModel({
|
2016-04-01 09:14:26 +00:00
|
|
|
name: uniqueModelName,
|
Add createModelFromConfig and configureModel()
Add new API allowing developers to split the model definition and
configuration into two steps:
1. Build models from JSON config, export them for re-use:
```js
var Customer = loopback.createModelFromConfig({
name: 'Customer',
base: 'User',
properties: {
address: 'string'
}
});
```
2. Attach existing models to a dataSource and a loopback app,
modify certain model aspects like relations:
```js
loopback.configureModel(Customer, {
dataSource: db,
relations: { /* ... */ }
});
```
Rework `app.model` to use `loopback.configureModel` under the hood.
Here is the new usage:
```js
var Customer = require('./models').Customer;
app.model(Customer, {
dataSource: 'db',
relations: { /* ... */ }
});
```
In order to preserve backwards compatibility with loopback 1.x,
`app.model(name, config)` calls both `createModelFromConfig`
and `configureModel`.
2014-06-05 15:41:12 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(model.prototype).to.be.instanceof(loopback.Model);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('interprets extra first-level keys as options', function() {
|
2014-06-09 09:18:52 +00:00
|
|
|
var model = loopback.createModel({
|
Add createModelFromConfig and configureModel()
Add new API allowing developers to split the model definition and
configuration into two steps:
1. Build models from JSON config, export them for re-use:
```js
var Customer = loopback.createModelFromConfig({
name: 'Customer',
base: 'User',
properties: {
address: 'string'
}
});
```
2. Attach existing models to a dataSource and a loopback app,
modify certain model aspects like relations:
```js
loopback.configureModel(Customer, {
dataSource: db,
relations: { /* ... */ }
});
```
Rework `app.model` to use `loopback.configureModel` under the hood.
Here is the new usage:
```js
var Customer = require('./models').Customer;
app.model(Customer, {
dataSource: 'db',
relations: { /* ... */ }
});
```
In order to preserve backwards compatibility with loopback 1.x,
`app.model(name, config)` calls both `createModelFromConfig`
and `configureModel`.
2014-06-05 15:41:12 +00:00
|
|
|
name: uniqueModelName,
|
2016-04-01 09:14:26 +00:00
|
|
|
base: 'User',
|
Add createModelFromConfig and configureModel()
Add new API allowing developers to split the model definition and
configuration into two steps:
1. Build models from JSON config, export them for re-use:
```js
var Customer = loopback.createModelFromConfig({
name: 'Customer',
base: 'User',
properties: {
address: 'string'
}
});
```
2. Attach existing models to a dataSource and a loopback app,
modify certain model aspects like relations:
```js
loopback.configureModel(Customer, {
dataSource: db,
relations: { /* ... */ }
});
```
Rework `app.model` to use `loopback.configureModel` under the hood.
Here is the new usage:
```js
var Customer = require('./models').Customer;
app.model(Customer, {
dataSource: 'db',
relations: { /* ... */ }
});
```
In order to preserve backwards compatibility with loopback 1.x,
`app.model(name, config)` calls both `createModelFromConfig`
and `configureModel`.
2014-06-05 15:41:12 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(model.prototype).to.be.instanceof(loopback.User);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('prefers config.options.key over config.key', function() {
|
2014-06-09 09:18:52 +00:00
|
|
|
var model = loopback.createModel({
|
Add createModelFromConfig and configureModel()
Add new API allowing developers to split the model definition and
configuration into two steps:
1. Build models from JSON config, export them for re-use:
```js
var Customer = loopback.createModelFromConfig({
name: 'Customer',
base: 'User',
properties: {
address: 'string'
}
});
```
2. Attach existing models to a dataSource and a loopback app,
modify certain model aspects like relations:
```js
loopback.configureModel(Customer, {
dataSource: db,
relations: { /* ... */ }
});
```
Rework `app.model` to use `loopback.configureModel` under the hood.
Here is the new usage:
```js
var Customer = require('./models').Customer;
app.model(Customer, {
dataSource: 'db',
relations: { /* ... */ }
});
```
In order to preserve backwards compatibility with loopback 1.x,
`app.model(name, config)` calls both `createModelFromConfig`
and `configureModel`.
2014-06-05 15:41:12 +00:00
|
|
|
name: uniqueModelName,
|
|
|
|
base: 'User',
|
|
|
|
options: {
|
2016-04-01 09:14:26 +00:00
|
|
|
base: 'Application',
|
|
|
|
},
|
Add createModelFromConfig and configureModel()
Add new API allowing developers to split the model definition and
configuration into two steps:
1. Build models from JSON config, export them for re-use:
```js
var Customer = loopback.createModelFromConfig({
name: 'Customer',
base: 'User',
properties: {
address: 'string'
}
});
```
2. Attach existing models to a dataSource and a loopback app,
modify certain model aspects like relations:
```js
loopback.configureModel(Customer, {
dataSource: db,
relations: { /* ... */ }
});
```
Rework `app.model` to use `loopback.configureModel` under the hood.
Here is the new usage:
```js
var Customer = require('./models').Customer;
app.model(Customer, {
dataSource: 'db',
relations: { /* ... */ }
});
```
In order to preserve backwards compatibility with loopback 1.x,
`app.model(name, config)` calls both `createModelFromConfig`
and `configureModel`.
2014-06-05 15:41:12 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(model.prototype).to.be.instanceof(loopback.Application);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('loopback.configureModel(ModelCtor, config)', function() {
|
|
|
|
it('adds new relations', function() {
|
|
|
|
var model = loopback.Model.extend(uniqueModelName);
|
|
|
|
|
|
|
|
loopback.configureModel(model, {
|
2014-08-26 15:58:59 +00:00
|
|
|
dataSource: null,
|
Add createModelFromConfig and configureModel()
Add new API allowing developers to split the model definition and
configuration into two steps:
1. Build models from JSON config, export them for re-use:
```js
var Customer = loopback.createModelFromConfig({
name: 'Customer',
base: 'User',
properties: {
address: 'string'
}
});
```
2. Attach existing models to a dataSource and a loopback app,
modify certain model aspects like relations:
```js
loopback.configureModel(Customer, {
dataSource: db,
relations: { /* ... */ }
});
```
Rework `app.model` to use `loopback.configureModel` under the hood.
Here is the new usage:
```js
var Customer = require('./models').Customer;
app.model(Customer, {
dataSource: 'db',
relations: { /* ... */ }
});
```
In order to preserve backwards compatibility with loopback 1.x,
`app.model(name, config)` calls both `createModelFromConfig`
and `configureModel`.
2014-06-05 15:41:12 +00:00
|
|
|
relations: {
|
|
|
|
owner: {
|
|
|
|
type: 'belongsTo',
|
2016-04-01 09:14:26 +00:00
|
|
|
model: 'User',
|
|
|
|
},
|
|
|
|
},
|
Add createModelFromConfig and configureModel()
Add new API allowing developers to split the model definition and
configuration into two steps:
1. Build models from JSON config, export them for re-use:
```js
var Customer = loopback.createModelFromConfig({
name: 'Customer',
base: 'User',
properties: {
address: 'string'
}
});
```
2. Attach existing models to a dataSource and a loopback app,
modify certain model aspects like relations:
```js
loopback.configureModel(Customer, {
dataSource: db,
relations: { /* ... */ }
});
```
Rework `app.model` to use `loopback.configureModel` under the hood.
Here is the new usage:
```js
var Customer = require('./models').Customer;
app.model(Customer, {
dataSource: 'db',
relations: { /* ... */ }
});
```
In order to preserve backwards compatibility with loopback 1.x,
`app.model(name, config)` calls both `createModelFromConfig`
and `configureModel`.
2014-06-05 15:41:12 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(model.settings.relations).to.have.property('owner');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('updates existing relations', function() {
|
|
|
|
var model = loopback.Model.extend(uniqueModelName, {}, {
|
|
|
|
relations: {
|
|
|
|
owner: {
|
|
|
|
type: 'belongsTo',
|
2016-04-01 09:14:26 +00:00
|
|
|
model: 'User',
|
|
|
|
},
|
|
|
|
},
|
Add createModelFromConfig and configureModel()
Add new API allowing developers to split the model definition and
configuration into two steps:
1. Build models from JSON config, export them for re-use:
```js
var Customer = loopback.createModelFromConfig({
name: 'Customer',
base: 'User',
properties: {
address: 'string'
}
});
```
2. Attach existing models to a dataSource and a loopback app,
modify certain model aspects like relations:
```js
loopback.configureModel(Customer, {
dataSource: db,
relations: { /* ... */ }
});
```
Rework `app.model` to use `loopback.configureModel` under the hood.
Here is the new usage:
```js
var Customer = require('./models').Customer;
app.model(Customer, {
dataSource: 'db',
relations: { /* ... */ }
});
```
In order to preserve backwards compatibility with loopback 1.x,
`app.model(name, config)` calls both `createModelFromConfig`
and `configureModel`.
2014-06-05 15:41:12 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
loopback.configureModel(model, {
|
2015-04-24 15:32:17 +00:00
|
|
|
dataSource: false,
|
Add createModelFromConfig and configureModel()
Add new API allowing developers to split the model definition and
configuration into two steps:
1. Build models from JSON config, export them for re-use:
```js
var Customer = loopback.createModelFromConfig({
name: 'Customer',
base: 'User',
properties: {
address: 'string'
}
});
```
2. Attach existing models to a dataSource and a loopback app,
modify certain model aspects like relations:
```js
loopback.configureModel(Customer, {
dataSource: db,
relations: { /* ... */ }
});
```
Rework `app.model` to use `loopback.configureModel` under the hood.
Here is the new usage:
```js
var Customer = require('./models').Customer;
app.model(Customer, {
dataSource: 'db',
relations: { /* ... */ }
});
```
In order to preserve backwards compatibility with loopback 1.x,
`app.model(name, config)` calls both `createModelFromConfig`
and `configureModel`.
2014-06-05 15:41:12 +00:00
|
|
|
relations: {
|
|
|
|
owner: {
|
2016-04-01 09:14:26 +00:00
|
|
|
model: 'Application',
|
|
|
|
},
|
|
|
|
},
|
Add createModelFromConfig and configureModel()
Add new API allowing developers to split the model definition and
configuration into two steps:
1. Build models from JSON config, export them for re-use:
```js
var Customer = loopback.createModelFromConfig({
name: 'Customer',
base: 'User',
properties: {
address: 'string'
}
});
```
2. Attach existing models to a dataSource and a loopback app,
modify certain model aspects like relations:
```js
loopback.configureModel(Customer, {
dataSource: db,
relations: { /* ... */ }
});
```
Rework `app.model` to use `loopback.configureModel` under the hood.
Here is the new usage:
```js
var Customer = require('./models').Customer;
app.model(Customer, {
dataSource: 'db',
relations: { /* ... */ }
});
```
In order to preserve backwards compatibility with loopback 1.x,
`app.model(name, config)` calls both `createModelFromConfig`
and `configureModel`.
2014-06-05 15:41:12 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(model.settings.relations.owner).to.eql({
|
|
|
|
type: 'belongsTo',
|
2016-04-01 09:14:26 +00:00
|
|
|
model: 'Application',
|
Add createModelFromConfig and configureModel()
Add new API allowing developers to split the model definition and
configuration into two steps:
1. Build models from JSON config, export them for re-use:
```js
var Customer = loopback.createModelFromConfig({
name: 'Customer',
base: 'User',
properties: {
address: 'string'
}
});
```
2. Attach existing models to a dataSource and a loopback app,
modify certain model aspects like relations:
```js
loopback.configureModel(Customer, {
dataSource: db,
relations: { /* ... */ }
});
```
Rework `app.model` to use `loopback.configureModel` under the hood.
Here is the new usage:
```js
var Customer = require('./models').Customer;
app.model(Customer, {
dataSource: 'db',
relations: { /* ... */ }
});
```
In order to preserve backwards compatibility with loopback 1.x,
`app.model(name, config)` calls both `createModelFromConfig`
and `configureModel`.
2014-06-05 15:41:12 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('updates relations before attaching to a dataSource', function() {
|
2016-11-15 21:46:23 +00:00
|
|
|
var db = loopback.createDataSource({connector: loopback.Memory});
|
Add createModelFromConfig and configureModel()
Add new API allowing developers to split the model definition and
configuration into two steps:
1. Build models from JSON config, export them for re-use:
```js
var Customer = loopback.createModelFromConfig({
name: 'Customer',
base: 'User',
properties: {
address: 'string'
}
});
```
2. Attach existing models to a dataSource and a loopback app,
modify certain model aspects like relations:
```js
loopback.configureModel(Customer, {
dataSource: db,
relations: { /* ... */ }
});
```
Rework `app.model` to use `loopback.configureModel` under the hood.
Here is the new usage:
```js
var Customer = require('./models').Customer;
app.model(Customer, {
dataSource: 'db',
relations: { /* ... */ }
});
```
In order to preserve backwards compatibility with loopback 1.x,
`app.model(name, config)` calls both `createModelFromConfig`
and `configureModel`.
2014-06-05 15:41:12 +00:00
|
|
|
var model = loopback.Model.extend(uniqueModelName);
|
|
|
|
|
2016-01-25 09:36:51 +00:00
|
|
|
// This test used to work because User model was already attached
|
|
|
|
// by other tests via `loopback.autoAttach()`
|
|
|
|
// Now that autoAttach is gone, it turns out the tested functionality
|
|
|
|
// does not work exactly as intended. To keep this change narrowly
|
|
|
|
// focused on removing autoAttach, we are attaching the User model
|
|
|
|
// to simulate the old test setup.
|
|
|
|
loopback.User.attachTo(db);
|
|
|
|
|
Add createModelFromConfig and configureModel()
Add new API allowing developers to split the model definition and
configuration into two steps:
1. Build models from JSON config, export them for re-use:
```js
var Customer = loopback.createModelFromConfig({
name: 'Customer',
base: 'User',
properties: {
address: 'string'
}
});
```
2. Attach existing models to a dataSource and a loopback app,
modify certain model aspects like relations:
```js
loopback.configureModel(Customer, {
dataSource: db,
relations: { /* ... */ }
});
```
Rework `app.model` to use `loopback.configureModel` under the hood.
Here is the new usage:
```js
var Customer = require('./models').Customer;
app.model(Customer, {
dataSource: 'db',
relations: { /* ... */ }
});
```
In order to preserve backwards compatibility with loopback 1.x,
`app.model(name, config)` calls both `createModelFromConfig`
and `configureModel`.
2014-06-05 15:41:12 +00:00
|
|
|
loopback.configureModel(model, {
|
|
|
|
dataSource: db,
|
|
|
|
relations: {
|
|
|
|
owner: {
|
|
|
|
type: 'belongsTo',
|
2016-04-01 09:14:26 +00:00
|
|
|
model: 'User',
|
|
|
|
},
|
|
|
|
},
|
Add createModelFromConfig and configureModel()
Add new API allowing developers to split the model definition and
configuration into two steps:
1. Build models from JSON config, export them for re-use:
```js
var Customer = loopback.createModelFromConfig({
name: 'Customer',
base: 'User',
properties: {
address: 'string'
}
});
```
2. Attach existing models to a dataSource and a loopback app,
modify certain model aspects like relations:
```js
loopback.configureModel(Customer, {
dataSource: db,
relations: { /* ... */ }
});
```
Rework `app.model` to use `loopback.configureModel` under the hood.
Here is the new usage:
```js
var Customer = require('./models').Customer;
app.model(Customer, {
dataSource: 'db',
relations: { /* ... */ }
});
```
In order to preserve backwards compatibility with loopback 1.x,
`app.model(name, config)` calls both `createModelFromConfig`
and `configureModel`.
2014-06-05 15:41:12 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var owner = model.prototype.owner;
|
|
|
|
expect(owner, 'model.prototype.owner').to.be.a('function');
|
|
|
|
expect(owner._targetClass).to.equal('User');
|
|
|
|
});
|
2014-11-07 19:14:40 +00:00
|
|
|
|
|
|
|
it('adds new acls', function() {
|
|
|
|
var model = loopback.Model.extend(uniqueModelName, {}, {
|
|
|
|
acls: [
|
|
|
|
{
|
|
|
|
property: 'find',
|
|
|
|
accessType: 'EXECUTE',
|
|
|
|
principalType: 'ROLE',
|
|
|
|
principalId: '$everyone',
|
2016-04-01 09:14:26 +00:00
|
|
|
permission: 'DENY',
|
|
|
|
},
|
|
|
|
],
|
2014-11-07 19:14:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
loopback.configureModel(model, {
|
|
|
|
dataSource: null,
|
|
|
|
acls: [
|
|
|
|
{
|
|
|
|
property: 'find',
|
|
|
|
accessType: 'EXECUTE',
|
|
|
|
principalType: 'ROLE',
|
|
|
|
principalId: 'admin',
|
2016-04-01 09:14:26 +00:00
|
|
|
permission: 'ALLOW',
|
|
|
|
},
|
|
|
|
],
|
2014-11-07 19:14:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(model.settings.acls).eql([
|
|
|
|
{
|
|
|
|
property: 'find',
|
|
|
|
accessType: 'EXECUTE',
|
|
|
|
principalType: 'ROLE',
|
|
|
|
principalId: '$everyone',
|
2016-04-01 09:14:26 +00:00
|
|
|
permission: 'DENY',
|
2014-11-07 19:14:40 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
property: 'find',
|
|
|
|
accessType: 'EXECUTE',
|
|
|
|
principalType: 'ROLE',
|
|
|
|
principalId: 'admin',
|
2016-04-01 09:14:26 +00:00
|
|
|
permission: 'ALLOW',
|
|
|
|
},
|
2014-11-07 19:14:40 +00:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('updates existing acls', function() {
|
|
|
|
var model = loopback.Model.extend(uniqueModelName, {}, {
|
|
|
|
acls: [
|
|
|
|
{
|
|
|
|
property: 'find',
|
|
|
|
accessType: 'EXECUTE',
|
|
|
|
principalType: 'ROLE',
|
|
|
|
principalId: '$everyone',
|
2016-04-01 09:14:26 +00:00
|
|
|
permission: 'DENY',
|
|
|
|
},
|
|
|
|
],
|
2014-11-07 19:14:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
loopback.configureModel(model, {
|
|
|
|
dataSource: null,
|
|
|
|
acls: [
|
|
|
|
{
|
|
|
|
property: 'find',
|
|
|
|
accessType: 'EXECUTE',
|
|
|
|
principalType: 'ROLE',
|
|
|
|
principalId: '$everyone',
|
2016-04-01 09:14:26 +00:00
|
|
|
permission: 'ALLOW',
|
|
|
|
},
|
|
|
|
],
|
2014-11-07 19:14:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(model.settings.acls).eql([
|
|
|
|
{
|
|
|
|
property: 'find',
|
|
|
|
accessType: 'EXECUTE',
|
|
|
|
principalType: 'ROLE',
|
|
|
|
principalId: '$everyone',
|
2016-04-01 09:14:26 +00:00
|
|
|
permission: 'ALLOW',
|
|
|
|
},
|
2014-11-07 19:14:40 +00:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('updates existing settings', function() {
|
|
|
|
var model = loopback.Model.extend(uniqueModelName, {}, {
|
|
|
|
ttl: 10,
|
2016-04-01 09:14:26 +00:00
|
|
|
emailVerificationRequired: false,
|
2014-11-07 19:14:40 +00:00
|
|
|
});
|
|
|
|
|
2015-02-20 12:02:08 +00:00
|
|
|
var baseName = model.settings.base.name;
|
|
|
|
|
2014-11-07 19:14:40 +00:00
|
|
|
loopback.configureModel(model, {
|
|
|
|
dataSource: null,
|
|
|
|
options: {
|
|
|
|
ttl: 20,
|
|
|
|
realmRequired: true,
|
2016-04-01 09:14:26 +00:00
|
|
|
base: 'X',
|
|
|
|
},
|
2014-11-07 19:14:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(model.settings).to.have.property('ttl', 20);
|
|
|
|
expect(model.settings).to.have.property('emailVerificationRequired',
|
|
|
|
false);
|
|
|
|
expect(model.settings).to.have.property('realmRequired', true);
|
2015-02-20 12:02:08 +00:00
|
|
|
|
|
|
|
// configureModel MUST NOT change Model's base class
|
|
|
|
expect(model.settings.base.name).to.equal(baseName);
|
2014-11-07 19:14:40 +00:00
|
|
|
});
|
2015-05-06 08:04:06 +00:00
|
|
|
|
|
|
|
it('configures remote methods', function() {
|
|
|
|
var TestModel = loopback.createModel(uniqueModelName);
|
|
|
|
loopback.configureModel(TestModel, {
|
|
|
|
dataSource: null,
|
|
|
|
methods: {
|
|
|
|
staticMethod: {
|
|
|
|
isStatic: true,
|
2016-11-15 21:46:23 +00:00
|
|
|
http: {path: '/static'},
|
2015-05-06 08:04:06 +00:00
|
|
|
},
|
|
|
|
instanceMethod: {
|
|
|
|
isStatic: false,
|
2016-11-15 21:46:23 +00:00
|
|
|
http: {path: '/instance'},
|
2016-04-01 09:14:26 +00:00
|
|
|
},
|
|
|
|
},
|
2015-05-06 08:04:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var methodNames = TestModel.sharedClass.methods().map(function(m) {
|
|
|
|
return m.stringName.replace(/^[^.]+\./, ''); // drop the class name
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(methodNames).to.include.members([
|
|
|
|
'staticMethod',
|
2016-04-01 09:14:26 +00:00
|
|
|
'prototype.instanceMethod',
|
2015-05-06 08:04:06 +00:00
|
|
|
]);
|
|
|
|
});
|
Add createModelFromConfig and configureModel()
Add new API allowing developers to split the model definition and
configuration into two steps:
1. Build models from JSON config, export them for re-use:
```js
var Customer = loopback.createModelFromConfig({
name: 'Customer',
base: 'User',
properties: {
address: 'string'
}
});
```
2. Attach existing models to a dataSource and a loopback app,
modify certain model aspects like relations:
```js
loopback.configureModel(Customer, {
dataSource: db,
relations: { /* ... */ }
});
```
Rework `app.model` to use `loopback.configureModel` under the hood.
Here is the new usage:
```js
var Customer = require('./models').Customer;
app.model(Customer, {
dataSource: 'db',
relations: { /* ... */ }
});
```
In order to preserve backwards compatibility with loopback 1.x,
`app.model(name, config)` calls both `createModelFromConfig`
and `configureModel`.
2014-06-05 15:41:12 +00:00
|
|
|
});
|
2014-10-10 09:21:15 +00:00
|
|
|
|
|
|
|
describe('loopback object', function() {
|
2014-11-11 19:27:39 +00:00
|
|
|
it('inherits properties from express', function() {
|
|
|
|
var express = require('express');
|
|
|
|
for (var i in express) {
|
|
|
|
expect(loopback).to.have.property(i, express[i]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-10-10 09:21:15 +00:00
|
|
|
it('exports all built-in models', function() {
|
|
|
|
var expectedModelNames = [
|
|
|
|
'Email',
|
|
|
|
'User',
|
|
|
|
'Application',
|
|
|
|
'AccessToken',
|
|
|
|
'Role',
|
|
|
|
'RoleMapping',
|
|
|
|
'ACL',
|
|
|
|
'Scope',
|
|
|
|
'Change',
|
2016-04-01 09:14:26 +00:00
|
|
|
'Checkpoint',
|
2014-10-10 09:21:15 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
expect(Object.keys(loopback)).to.include.members(expectedModelNames);
|
|
|
|
|
|
|
|
expectedModelNames.forEach(function(name) {
|
|
|
|
expect(loopback[name], name).to.be.a('function');
|
|
|
|
expect(loopback[name].modelName, name + '.modelName').to.eql(name);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-12-01 10:36:34 +00:00
|
|
|
|
2016-03-28 15:11:22 +00:00
|
|
|
describe('new remote method configuration', function() {
|
|
|
|
function getAllMethodNamesWithoutClassName(TestModel) {
|
|
|
|
return TestModel.sharedClass.methods().map(function(m) {
|
|
|
|
return m.stringName.replace(/^[^.]+\./, ''); // drop the class name
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
it('treats method names that don\'t start with "prototype." as "isStatic:true"', function() {
|
|
|
|
var TestModel = loopback.createModel(uniqueModelName);
|
|
|
|
loopback.configureModel(TestModel, {
|
|
|
|
dataSource: null,
|
|
|
|
methods: {
|
|
|
|
staticMethod: {
|
2016-11-15 21:46:23 +00:00
|
|
|
http: {path: '/static'},
|
2016-04-01 09:14:26 +00:00
|
|
|
},
|
|
|
|
},
|
2016-03-28 15:11:22 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var methodNames = getAllMethodNamesWithoutClassName(TestModel);
|
|
|
|
|
|
|
|
expect(methodNames).to.include('staticMethod');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('treats method names starting with "prototype." as "isStatic:false"', function() {
|
|
|
|
var TestModel = loopback.createModel(uniqueModelName);
|
|
|
|
loopback.configureModel(TestModel, {
|
|
|
|
dataSource: null,
|
|
|
|
methods: {
|
|
|
|
'prototype.instanceMethod': {
|
2016-11-15 21:46:23 +00:00
|
|
|
http: {path: '/instance'},
|
2016-04-01 09:14:26 +00:00
|
|
|
},
|
|
|
|
},
|
2016-03-28 15:11:22 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var methodNames = getAllMethodNamesWithoutClassName(TestModel);
|
|
|
|
|
|
|
|
expect(methodNames).to.include('prototype.instanceMethod');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throws an error when "isStatic:true" and method name starts with "prototype."', function() {
|
|
|
|
var TestModel = loopback.createModel(uniqueModelName);
|
2016-04-01 09:14:26 +00:00
|
|
|
expect(function() {
|
|
|
|
loopback.configureModel(TestModel, {
|
|
|
|
dataSource: null,
|
|
|
|
methods: {
|
|
|
|
'prototype.instanceMethod': {
|
|
|
|
isStatic: true,
|
2016-11-15 21:46:23 +00:00
|
|
|
http: {path: '/instance'},
|
2016-04-01 09:14:26 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}).to.throw(Error, new Error('Remoting metadata for' + TestModel.modelName +
|
2016-03-28 15:11:22 +00:00
|
|
|
' "isStatic" does not match new method name-based style.'));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('use "isStatic:true" if method name does not start with "prototype."', function() {
|
|
|
|
var TestModel = loopback.createModel(uniqueModelName);
|
|
|
|
loopback.configureModel(TestModel, {
|
|
|
|
dataSource: null,
|
|
|
|
methods: {
|
|
|
|
staticMethod: {
|
|
|
|
isStatic: true,
|
2016-11-15 21:46:23 +00:00
|
|
|
http: {path: '/static'},
|
2016-04-01 09:14:26 +00:00
|
|
|
},
|
|
|
|
},
|
2016-03-28 15:11:22 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var methodNames = getAllMethodNamesWithoutClassName(TestModel);
|
|
|
|
|
|
|
|
expect(methodNames).to.include('staticMethod');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('use "isStatic:false" if method name starts with "prototype."', function() {
|
|
|
|
var TestModel = loopback.createModel(uniqueModelName);
|
|
|
|
loopback.configureModel(TestModel, {
|
|
|
|
dataSource: null,
|
|
|
|
methods: {
|
|
|
|
'prototype.instanceMethod': {
|
|
|
|
isStatic: false,
|
2016-11-15 21:46:23 +00:00
|
|
|
http: {path: '/instance'},
|
2016-04-01 09:14:26 +00:00
|
|
|
},
|
|
|
|
},
|
2016-03-28 15:11:22 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var methodNames = getAllMethodNamesWithoutClassName(TestModel);
|
|
|
|
|
|
|
|
expect(methodNames).to.include('prototype.instanceMethod');
|
|
|
|
});
|
|
|
|
});
|
2016-09-02 21:22:42 +00:00
|
|
|
|
|
|
|
describe('Remote method inheritance', function() {
|
|
|
|
var app;
|
|
|
|
|
|
|
|
beforeEach(setupLoopback);
|
|
|
|
|
|
|
|
it('inherits remote methods defined via createModel', function() {
|
|
|
|
var Base = app.registry.createModel('Base', {}, {
|
|
|
|
methods: {
|
|
|
|
greet: {
|
2016-11-15 21:46:23 +00:00
|
|
|
http: {path: '/greet'},
|
2016-09-02 21:22:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
var MyCustomModel = app.registry.createModel('MyCustomModel', {}, {
|
|
|
|
base: 'Base',
|
|
|
|
methods: {
|
|
|
|
hello: {
|
2016-11-15 21:46:23 +00:00
|
|
|
http: {path: '/hello'},
|
2016-09-02 21:22:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
var methodNames = getAllMethodNamesWithoutClassName(MyCustomModel);
|
|
|
|
|
|
|
|
expect(methodNames).to.include('greet');
|
|
|
|
expect(methodNames).to.include('hello');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('same remote method with different metadata should override parent', function() {
|
|
|
|
var Base = app.registry.createModel('Base', {}, {
|
|
|
|
methods: {
|
|
|
|
greet: {
|
2016-11-15 21:46:23 +00:00
|
|
|
http: {path: '/greet'},
|
2016-09-02 21:22:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
var MyCustomModel = app.registry.createModel('MyCustomModel', {}, {
|
|
|
|
base: 'Base',
|
|
|
|
methods: {
|
|
|
|
greet: {
|
2016-11-15 21:46:23 +00:00
|
|
|
http: {path: '/hello'},
|
2016-09-02 21:22:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
var methodNames = getAllMethodNamesWithoutClassName(MyCustomModel);
|
|
|
|
var baseMethod = Base.sharedClass.findMethodByName('greet');
|
|
|
|
var customMethod = MyCustomModel.sharedClass.findMethodByName('greet');
|
|
|
|
|
|
|
|
// Base Method
|
2016-11-15 21:46:23 +00:00
|
|
|
expect(baseMethod.http).to.eql({path: '/greet'});
|
2016-09-02 21:22:42 +00:00
|
|
|
expect(baseMethod.http.path).to.equal('/greet');
|
|
|
|
expect(baseMethod.http.path).to.not.equal('/hello');
|
|
|
|
|
|
|
|
// Custom Method
|
|
|
|
expect(methodNames).to.include('greet');
|
2016-11-15 21:46:23 +00:00
|
|
|
expect(customMethod.http).to.eql({path: '/hello'});
|
2016-09-02 21:22:42 +00:00
|
|
|
expect(customMethod.http.path).to.equal('/hello');
|
|
|
|
expect(customMethod.http.path).to.not.equal('/greet');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not inherit remote methods defined via configureModel', function() {
|
|
|
|
var Base = app.registry.createModel('Base');
|
|
|
|
app.registry.configureModel(Base, {
|
|
|
|
dataSource: null,
|
|
|
|
methods: {
|
|
|
|
greet: {
|
2016-11-15 21:46:23 +00:00
|
|
|
http: {path: '/greet'},
|
2016-09-02 21:22:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
var MyCustomModel = app.registry.createModel('MyCustomModel', {}, {
|
|
|
|
base: 'Base',
|
|
|
|
methods: {
|
|
|
|
hello: {
|
2016-11-15 21:46:23 +00:00
|
|
|
http: {path: '/hello'},
|
2016-09-02 21:22:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
var methodNames = getAllMethodNamesWithoutClassName(MyCustomModel);
|
|
|
|
|
|
|
|
expect(methodNames).to.not.include('greet');
|
|
|
|
expect(methodNames).to.include('hello');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not inherit remote methods defined via configureModel after child model ' +
|
|
|
|
'was created', function() {
|
|
|
|
var Base = app.registry.createModel('Base');
|
|
|
|
var MyCustomModel = app.registry.createModel('MyCustomModel', {}, {
|
|
|
|
base: 'Base',
|
|
|
|
});
|
|
|
|
|
|
|
|
app.registry.configureModel(Base, {
|
|
|
|
dataSource: null,
|
|
|
|
methods: {
|
|
|
|
greet: {
|
2016-11-15 21:46:23 +00:00
|
|
|
http: {path: '/greet'},
|
2016-09-02 21:22:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
app.registry.configureModel(MyCustomModel, {
|
|
|
|
dataSource: null,
|
|
|
|
methods: {
|
|
|
|
hello: {
|
2016-11-15 21:46:23 +00:00
|
|
|
http: {path: '/hello'},
|
2016-09-02 21:22:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
var baseMethodNames = getAllMethodNamesWithoutClassName(Base);
|
|
|
|
var methodNames = getAllMethodNamesWithoutClassName(MyCustomModel);
|
|
|
|
|
|
|
|
expect(baseMethodNames).to.include('greet');
|
|
|
|
expect(methodNames).to.not.include('greet');
|
|
|
|
expect(methodNames).to.include('hello');
|
|
|
|
});
|
|
|
|
|
|
|
|
function setupLoopback() {
|
2016-11-15 21:46:23 +00:00
|
|
|
app = loopback({localRegistry: true});
|
2016-09-02 21:22:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getAllMethodNamesWithoutClassName(Model) {
|
|
|
|
return Model.sharedClass.methods().map(function(m) {
|
|
|
|
return m.stringName.replace(/^[^.]+\./, ''); // drop the class name
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2017-07-18 07:17:17 +00:00
|
|
|
|
|
|
|
describe('Hiding shared methods', function() {
|
|
|
|
var app;
|
|
|
|
|
|
|
|
beforeEach(setupLoopback);
|
|
|
|
|
|
|
|
it('hides remote methods using fixed method names', function() {
|
|
|
|
var TestModel = app.registry.createModel(uniqueModelName);
|
|
|
|
app.model(TestModel, {
|
|
|
|
dataSource: null,
|
|
|
|
methods: {
|
|
|
|
staticMethod: {
|
|
|
|
isStatic: true,
|
|
|
|
http: {path: '/static'},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
options: {
|
|
|
|
remoting: {
|
|
|
|
sharedMethods: {
|
|
|
|
staticMethod: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
var publicMethods = getSharedMethods(TestModel);
|
|
|
|
|
|
|
|
expect(publicMethods).not.to.include.members([
|
|
|
|
'staticMethod',
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('hides remote methods using a glob pattern', function() {
|
|
|
|
var TestModel = app.registry.createModel(uniqueModelName);
|
|
|
|
app.model(TestModel, {
|
|
|
|
dataSource: null,
|
|
|
|
methods: {
|
|
|
|
staticMethod: {
|
|
|
|
isStatic: true,
|
|
|
|
http: {path: '/static'},
|
|
|
|
},
|
|
|
|
instanceMethod: {
|
|
|
|
isStatic: false,
|
|
|
|
http: {path: '/instance'},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
options: {
|
|
|
|
remoting: {
|
|
|
|
sharedMethods: {
|
|
|
|
'prototype.*': false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
var publicMethods = getSharedMethods(TestModel);
|
|
|
|
|
|
|
|
expect(publicMethods).to.include.members([
|
|
|
|
'staticMethod',
|
|
|
|
]);
|
|
|
|
expect(publicMethods).not.to.include.members([
|
|
|
|
'instanceMethod',
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('hides all remote methods using *', function() {
|
|
|
|
var TestModel = app.registry.createModel(uniqueModelName);
|
|
|
|
app.model(TestModel, {
|
|
|
|
dataSource: null,
|
|
|
|
methods: {
|
|
|
|
staticMethod: {
|
|
|
|
isStatic: true,
|
|
|
|
http: {path: '/static'},
|
|
|
|
},
|
|
|
|
instanceMethod: {
|
|
|
|
isStatic: false,
|
|
|
|
http: {path: '/instance'},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
options: {
|
|
|
|
remoting: {
|
|
|
|
sharedMethods: {
|
|
|
|
'*': false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
var publicMethods = getSharedMethods(TestModel);
|
|
|
|
|
|
|
|
expect(publicMethods).to.be.empty();
|
|
|
|
});
|
|
|
|
|
2017-08-15 01:34:46 +00:00
|
|
|
it('hides methods for related models using globs (model configured first)', function() {
|
|
|
|
const TestModel = app.registry.createModel('TestModel');
|
|
|
|
const RelatedModel = app.registry.createModel('RelatedModel');
|
|
|
|
app.dataSource('test', {connector: 'memory'});
|
|
|
|
app.model(TestModel, {
|
|
|
|
dataSource: 'test',
|
|
|
|
relations: {
|
|
|
|
related: {
|
|
|
|
type: 'hasOne',
|
|
|
|
model: RelatedModel,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
options: {
|
|
|
|
remoting: {
|
|
|
|
sharedMethods: {
|
|
|
|
'*__related': false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
app.model(RelatedModel, {dataSource: 'test'});
|
|
|
|
|
|
|
|
const publicMethods = getSharedMethods(TestModel);
|
|
|
|
|
|
|
|
expect(publicMethods).to.not.include.members([
|
|
|
|
'prototype.__create__related',
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('hides methods for related models using globs (related model configured first)', function() {
|
|
|
|
const TestModel = app.registry.createModel('TestModel');
|
|
|
|
const RelatedModel = app.registry.createModel('RelatedModel');
|
2017-07-18 07:17:17 +00:00
|
|
|
app.dataSource('test', {connector: 'memory'});
|
|
|
|
app.model(RelatedModel, {dataSource: 'test'});
|
|
|
|
app.model(TestModel, {
|
|
|
|
dataSource: 'test',
|
|
|
|
relations: {
|
|
|
|
related: {
|
|
|
|
type: 'hasOne',
|
|
|
|
model: RelatedModel,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
options: {
|
|
|
|
remoting: {
|
|
|
|
sharedMethods: {
|
|
|
|
'*__related': false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2017-08-15 01:34:46 +00:00
|
|
|
const publicMethods = getSharedMethods(TestModel);
|
2017-07-18 07:17:17 +00:00
|
|
|
|
|
|
|
expect(publicMethods).to.not.include.members([
|
|
|
|
'prototype.__create__related',
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
function setupLoopback() {
|
|
|
|
app = loopback({localRegistry: true});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSharedMethods(Model) {
|
|
|
|
return Model.sharedClass
|
|
|
|
.methods()
|
|
|
|
.filter(function(m) {
|
|
|
|
return m.shared === true;
|
|
|
|
})
|
|
|
|
.map(function(m) {
|
|
|
|
return m.stringName.replace(/^[^.]+\./, ''); // drop the class name
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2013-07-23 19:59:40 +00:00
|
|
|
});
|