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';
|
|
|
|
var assert = require('assert');
|
2014-05-03 01:12:50 +00:00
|
|
|
var async = require('async');
|
2016-10-21 15:17:02 +00:00
|
|
|
var describe = require('./util/describe');
|
2014-05-03 01:12:50 +00:00
|
|
|
var loopback = require('../');
|
|
|
|
var ACL = loopback.ACL;
|
|
|
|
var defineModelTestsWithDataSource = require('./util/model-tests');
|
2014-06-05 07:45:09 +00:00
|
|
|
var PersistedModel = loopback.PersistedModel;
|
2016-11-15 21:46:23 +00:00
|
|
|
var Promise = require('bluebird');
|
|
|
|
var TaskEmitter = require('strong-task-emitter');
|
|
|
|
var request = require('supertest');
|
2014-05-03 01:12:50 +00:00
|
|
|
|
2016-11-22 14:30:04 +00:00
|
|
|
var expect = require('./helpers/expect');
|
2014-06-03 08:39:54 +00:00
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
describe('Model / PersistedModel', function() {
|
2014-05-03 01:12:50 +00:00
|
|
|
defineModelTestsWithDataSource({
|
|
|
|
dataSource: {
|
2016-04-01 09:14:26 +00:00
|
|
|
connector: loopback.Memory,
|
|
|
|
},
|
2014-05-03 01:12:50 +00:00
|
|
|
});
|
2014-05-03 03:07:59 +00:00
|
|
|
|
|
|
|
describe('Model.validatesUniquenessOf(property, options)', function() {
|
2014-11-21 01:46:21 +00:00
|
|
|
it('Ensure the value for `property` is unique', function(done) {
|
2015-02-26 15:36:44 +00:00
|
|
|
var User = PersistedModel.extend('ValidatedUser', {
|
2014-05-03 04:19:14 +00:00
|
|
|
'first': String,
|
|
|
|
'last': String,
|
|
|
|
'age': Number,
|
|
|
|
'password': String,
|
|
|
|
'gender': String,
|
|
|
|
'domain': String,
|
2016-04-01 09:14:26 +00:00
|
|
|
'email': String,
|
2014-05-03 04:19:14 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var dataSource = loopback.createDataSource({
|
2016-04-01 09:14:26 +00:00
|
|
|
connector: loopback.Memory,
|
2014-05-03 04:19:14 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
User.attachTo(dataSource);
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
User.validatesUniquenessOf('email', {message: 'email is not unique'});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
var joe = new User({email: 'joe@joe.com'});
|
|
|
|
var joe2 = new User({email: 'joe@joe.com'});
|
2014-05-03 03:07:59 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
joe.save(function() {
|
|
|
|
joe2.save(function(err) {
|
2014-05-03 03:07:59 +00:00
|
|
|
assert(err, 'should get a validation error');
|
|
|
|
assert(joe2.errors.email, 'model should have email error');
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-05-03 03:07:59 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Model.attachTo(dataSource)', function() {
|
2014-11-21 01:46:21 +00:00
|
|
|
it('Attach a model to a [DataSource](#data-source)', function() {
|
2016-11-15 21:46:23 +00:00
|
|
|
var MyModel = loopback.createModel('my-model', {name: String});
|
2014-05-03 04:19:14 +00:00
|
|
|
var dataSource = loopback.createDataSource({
|
2016-04-01 09:14:26 +00:00
|
|
|
connector: loopback.Memory,
|
2014-05-03 04:19:14 +00:00
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-05-03 03:07:59 +00:00
|
|
|
MyModel.attachTo(dataSource);
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-05-19 22:56:26 +00:00
|
|
|
MyModel.find(function(err, results) {
|
2016-04-01 09:14:26 +00:00
|
|
|
assert(results.length === 0,
|
|
|
|
'should have data access methods after attaching to a data source');
|
2014-05-19 22:56:26 +00:00
|
|
|
});
|
2014-05-03 03:07:59 +00:00
|
|
|
});
|
|
|
|
});
|
2014-05-03 01:12:50 +00:00
|
|
|
});
|
2014-05-03 03:04:06 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe.onServer('Remote Methods', function() {
|
2016-04-01 09:14:26 +00:00
|
|
|
var User, Post, dataSource, app;
|
2014-05-03 03:04:06 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
beforeEach(function() {
|
2016-11-15 21:46:23 +00:00
|
|
|
app = loopback({localRegistry: true, loadBuiltinModels: true});
|
|
|
|
app.set('remoting', {errorHandler: {debug: true, log: false}});
|
2016-07-26 12:10:13 +00:00
|
|
|
|
|
|
|
User = app.registry.createModel('user', {
|
2016-11-15 21:46:23 +00:00
|
|
|
id: {id: true, type: String, defaultFn: 'guid'},
|
2014-05-03 03:04:06 +00:00
|
|
|
'first': String,
|
|
|
|
'last': String,
|
|
|
|
'age': Number,
|
|
|
|
'password': String,
|
|
|
|
'gender': String,
|
|
|
|
'domain': String,
|
2016-04-01 09:14:26 +00:00
|
|
|
'email': String,
|
2014-05-03 03:04:06 +00:00
|
|
|
}, {
|
2016-04-01 09:14:26 +00:00
|
|
|
trackChanges: true,
|
2014-05-03 03:04:06 +00:00
|
|
|
});
|
|
|
|
|
2016-07-26 12:10:13 +00:00
|
|
|
Post = app.registry.createModel('post', {
|
2016-11-15 21:46:23 +00:00
|
|
|
id: {id: true, type: String, defaultFn: 'guid'},
|
2015-04-16 22:02:41 +00:00
|
|
|
title: String,
|
2016-04-01 09:14:26 +00:00
|
|
|
content: String,
|
2015-04-16 22:02:41 +00:00
|
|
|
}, {
|
2016-04-01 09:14:26 +00:00
|
|
|
trackChanges: true,
|
2015-04-16 22:02:41 +00:00
|
|
|
});
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
dataSource = app.dataSource('db', {connector: 'memory'});
|
2014-05-03 03:04:06 +00:00
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
app.model(User, {dataSource: 'db'});
|
|
|
|
app.model(Post, {dataSource: 'db'});
|
2015-04-16 22:02:41 +00:00
|
|
|
|
|
|
|
User.hasMany(Post);
|
2014-05-03 03:04:06 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
User.login = function(username, password, fn) {
|
|
|
|
if (username === 'foo' && password === 'bar') {
|
2014-05-03 03:04:06 +00:00
|
|
|
fn(null, 123);
|
|
|
|
} else {
|
|
|
|
throw new Error('bad username and password!');
|
|
|
|
}
|
2014-11-21 01:46:21 +00:00
|
|
|
};
|
2014-05-03 03:04:06 +00:00
|
|
|
|
2016-07-26 12:10:13 +00:00
|
|
|
User.remoteMethod('login', {
|
|
|
|
accepts: [
|
2016-11-15 21:46:23 +00:00
|
|
|
{arg: 'username', type: 'string', required: true},
|
|
|
|
{arg: 'password', type: 'string', required: true},
|
2016-07-26 12:10:13 +00:00
|
|
|
],
|
2016-11-15 21:46:23 +00:00
|
|
|
returns: {arg: 'sessionId', type: 'any', root: true},
|
|
|
|
http: {path: '/sign-in', verb: 'get'},
|
2016-07-26 12:10:13 +00:00
|
|
|
});
|
2014-05-03 03:04:06 +00:00
|
|
|
|
|
|
|
app.use(loopback.rest());
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2016-10-19 21:59:39 +00:00
|
|
|
describe('Model.create(data, callback)', function() {
|
|
|
|
it('creates model', function(done) {
|
2016-11-15 21:46:23 +00:00
|
|
|
var anObject = {first: 'June'};
|
2016-10-19 21:59:39 +00:00
|
|
|
request(app)
|
|
|
|
.post('/users')
|
|
|
|
// sends an object
|
|
|
|
.send(anObject)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
|
|
|
expect(res.body).to.have.property('id');
|
|
|
|
expect(res.body).to.have.property('first', 'June');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
// batch create must be tested with a remote request because there are
|
|
|
|
// coercion being done on strong-remoting side
|
|
|
|
it('creates array of models', function(done) {
|
|
|
|
var arrayOfObjects = [
|
2016-11-15 21:46:23 +00:00
|
|
|
{first: 'John'}, {first: 'Jane'},
|
2016-10-19 21:59:39 +00:00
|
|
|
];
|
|
|
|
request(app)
|
|
|
|
.post('/users')
|
|
|
|
// sends an array of objects
|
|
|
|
.send(arrayOfObjects)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
|
|
|
expect(res.body.length).to.eql(2);
|
|
|
|
expect(res.body).to.have.deep.property('[0].first', 'John');
|
|
|
|
expect(res.body).to.have.deep.property('[1].first', 'Jane');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2016-11-15 22:51:22 +00:00
|
|
|
|
|
|
|
it('creates related models', function(done) {
|
|
|
|
User.create({first: 'Bob'}, function(err, res) {
|
|
|
|
expect(res).to.have.property('id');
|
|
|
|
var aPost = {title: 'A story', content: 'Once upon a time'};
|
|
|
|
request(app)
|
|
|
|
.post('/users/' + res.id + '/posts')
|
|
|
|
.send(aPost)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, result) {
|
|
|
|
if (err) return done(err);
|
|
|
|
expect(result.body).to.have.property('id');
|
|
|
|
expect(result.body).to.have.property('title', aPost.title);
|
|
|
|
expect(result.body).to.have.property('content', aPost.content);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('creates array of hasMany models', function(done) {
|
|
|
|
User.create({first: 'Bob'}, function(err, res) {
|
|
|
|
expect(res).to.have.property('id');
|
|
|
|
var twoPosts = [
|
|
|
|
{title: 'One story', content: 'Content #1'},
|
|
|
|
{title: 'Two story', content: 'Content #2'},
|
|
|
|
];
|
|
|
|
request(app)
|
|
|
|
.post('/users/' + res.id + '/posts')
|
|
|
|
.send(twoPosts)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, result) {
|
|
|
|
if (err) return done(err);
|
|
|
|
expect(result.body.length).to.eql(2);
|
|
|
|
expect(result.body).to.have.deep.property('[0].title', 'One story');
|
|
|
|
expect(result.body).to.have.deep.property('[1].title', 'Two story');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('rejects array of obj input for hasOne relation', function(done) {
|
|
|
|
var Friend = app.registry.createModel('friend', {name: String});
|
|
|
|
app.model(Friend, {dataSource: 'db'});
|
|
|
|
User.hasOne(Friend);
|
|
|
|
|
|
|
|
User.create({first: 'Bob'}, function(err, res) {
|
|
|
|
expect(res).to.have.property('id');
|
|
|
|
var twoFriends = [
|
|
|
|
{name: 'bob'},
|
|
|
|
{name: 'rob'},
|
|
|
|
];
|
|
|
|
request(app)
|
|
|
|
.post('/users/' + res.id + '/friend')
|
|
|
|
.send(twoFriends)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(400)
|
|
|
|
.end(function(err, result) {
|
|
|
|
if (err) return done(err);
|
|
|
|
var resError = result.body.error;
|
|
|
|
expect(resError.message).to.match(/value(.*?)not(.*?)object(\.?)/i);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-10-19 21:59:39 +00:00
|
|
|
});
|
|
|
|
// destoryAll is not exposed as a remoteMethod by default
|
2014-05-03 04:19:14 +00:00
|
|
|
describe('Model.destroyAll(callback)', function() {
|
2014-11-21 01:46:21 +00:00
|
|
|
it('Delete all Model instances from data source', function(done) {
|
2014-05-03 04:19:14 +00:00
|
|
|
(new TaskEmitter())
|
2016-11-15 21:46:23 +00:00
|
|
|
.task(User, 'create', {first: 'jill'})
|
|
|
|
.task(User, 'create', {first: 'bob'})
|
|
|
|
.task(User, 'create', {first: 'jan'})
|
|
|
|
.task(User, 'create', {first: 'sam'})
|
|
|
|
.task(User, 'create', {first: 'suzy'})
|
2014-11-21 02:35:36 +00:00
|
|
|
.on('done', function() {
|
|
|
|
User.count(function(err, count) {
|
|
|
|
User.destroyAll(function() {
|
|
|
|
User.count(function(err, count) {
|
2014-05-03 04:19:14 +00:00
|
|
|
assert.equal(count, 0);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-03 04:19:14 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-05-03 03:07:59 +00:00
|
|
|
|
2016-09-02 12:40:47 +00:00
|
|
|
describe('Model.upsertWithWhere(where, data, callback)', function() {
|
|
|
|
it('Updates when a Model instance is retreived from data source', function(done) {
|
|
|
|
var taskEmitter = new TaskEmitter();
|
|
|
|
taskEmitter
|
2016-11-15 21:46:23 +00:00
|
|
|
.task(User, 'create', {first: 'jill', second: 'pill'})
|
|
|
|
.task(User, 'create', {first: 'bob', second: 'sob'})
|
2016-09-02 12:40:47 +00:00
|
|
|
.on('done', function() {
|
2016-11-15 21:46:23 +00:00
|
|
|
User.upsertWithWhere({second: 'pill'}, {second: 'jones'}, function(err, user) {
|
2016-09-02 12:40:47 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
var id = user.id;
|
|
|
|
User.findById(id, function(err, user) {
|
|
|
|
if (err) return done(err);
|
|
|
|
assert.equal(user.second, 'jones');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Creates when no Model instance is retreived from data source', function(done) {
|
|
|
|
var taskEmitter = new TaskEmitter();
|
|
|
|
taskEmitter
|
2016-11-15 21:46:23 +00:00
|
|
|
.task(User, 'create', {first: 'simon', second: 'somers'})
|
2016-09-02 12:40:47 +00:00
|
|
|
.on('done', function() {
|
2016-11-15 21:46:23 +00:00
|
|
|
User.upsertWithWhere({first: 'somers'}, {first: 'Simon'}, function(err, user) {
|
2016-09-02 12:40:47 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
var id = user.id;
|
|
|
|
User.findById(id, function(err, user) {
|
|
|
|
if (err) return done(err);
|
|
|
|
assert.equal(user.first, 'Simon');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('Example Remote Method', function() {
|
2014-05-03 03:04:06 +00:00
|
|
|
it('Call the method using HTTP / REST', function(done) {
|
|
|
|
request(app)
|
|
|
|
.get('/users/sign-in?username=foo&password=bar')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
2014-11-21 02:35:36 +00:00
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-03 03:04:06 +00:00
|
|
|
assert.equal(res.body, 123);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-03 03:04:06 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Converts null result of findById to 404 Not Found', function(done) {
|
|
|
|
request(app)
|
|
|
|
.get('/users/not-found')
|
|
|
|
.expect(404)
|
2014-12-18 20:26:27 +00:00
|
|
|
.end(function(err, res) {
|
2016-05-05 04:09:06 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
|
2014-12-18 20:26:27 +00:00
|
|
|
var errorResponse = res.body.error;
|
|
|
|
assert(errorResponse);
|
|
|
|
assert.equal(errorResponse.code, 'MODEL_NOT_FOUND');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-12-18 20:26:27 +00:00
|
|
|
done();
|
|
|
|
});
|
2014-05-03 03:04:06 +00:00
|
|
|
});
|
2015-04-16 22:02:41 +00:00
|
|
|
|
|
|
|
it('Call the findById with filter.fields using HTTP / REST', function(done) {
|
|
|
|
request(app)
|
|
|
|
.post('/users')
|
2016-11-15 21:46:23 +00:00
|
|
|
.send({first: 'x', last: 'y'})
|
2015-04-16 22:02:41 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-16 22:02:41 +00:00
|
|
|
var userId = res.body.id;
|
|
|
|
assert(userId);
|
|
|
|
request(app)
|
|
|
|
.get('/users/' + userId + '?filter[fields]=first')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-16 22:02:41 +00:00
|
|
|
assert.equal(res.body.first, 'x', 'first should be x');
|
|
|
|
assert(res.body.last === undefined, 'last should not be present');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-16 22:02:41 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Call the findById with filter.include using HTTP / REST', function(done) {
|
|
|
|
request(app)
|
|
|
|
.post('/users')
|
2016-11-15 21:46:23 +00:00
|
|
|
.send({first: 'x', last: 'y'})
|
2015-04-16 22:02:41 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-16 22:02:41 +00:00
|
|
|
var userId = res.body.id;
|
|
|
|
assert(userId);
|
|
|
|
request(app)
|
|
|
|
.post('/users/' + userId + '/posts')
|
2016-11-15 21:46:23 +00:00
|
|
|
.send({title: 'T1', content: 'C1'})
|
2015-04-16 22:02:41 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-16 22:02:41 +00:00
|
|
|
var post = res.body;
|
|
|
|
request(app)
|
|
|
|
.get('/users/' + userId + '?filter[include]=posts')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-16 22:02:41 +00:00
|
|
|
assert.equal(res.body.first, 'x', 'first should be x');
|
|
|
|
assert.equal(res.body.last, 'y', 'last should be y');
|
|
|
|
assert.deepEqual(post, res.body.posts[0]);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-16 22:02:41 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-05-03 03:04:06 +00:00
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('Model.beforeRemote(name, fn)', function() {
|
2014-05-03 03:04:06 +00:00
|
|
|
it('Run a function before a remote method is called by a client', function(done) {
|
|
|
|
var hookCalled = false;
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-05-03 03:04:06 +00:00
|
|
|
User.beforeRemote('create', function(ctx, user, next) {
|
|
|
|
hookCalled = true;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-03 03:04:06 +00:00
|
|
|
next();
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-05-03 03:04:06 +00:00
|
|
|
// invoke save
|
|
|
|
request(app)
|
|
|
|
.post('/users')
|
2016-11-15 21:46:23 +00:00
|
|
|
.send({data: {first: 'foo', last: 'bar'}})
|
2014-05-03 03:04:06 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
2014-11-21 02:35:36 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-03 03:04:06 +00:00
|
|
|
assert(hookCalled, 'hook wasnt called');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-03 03:04:06 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2016-09-15 21:33:56 +00:00
|
|
|
|
|
|
|
it('Does not stop the hook chain after returning a promise', function(done) {
|
|
|
|
var hooksCalled = [];
|
|
|
|
|
|
|
|
User.beforeRemote('create', function() {
|
|
|
|
hooksCalled.push('first');
|
|
|
|
return Promise.resolve();
|
|
|
|
});
|
|
|
|
|
|
|
|
User.beforeRemote('create', function(ctx, user, next) {
|
|
|
|
hooksCalled.push('second');
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
// invoke save
|
|
|
|
request(app)
|
|
|
|
.post('/users')
|
2016-11-15 21:46:23 +00:00
|
|
|
.send({data: {first: 'foo', last: 'bar'}})
|
2016-09-15 21:33:56 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
|
|
|
expect(hooksCalled).to.eql(['first', 'second']);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-05-03 03:04:06 +00:00
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('Model.afterRemote(name, fn)', function() {
|
2014-05-03 03:04:06 +00:00
|
|
|
it('Run a function after a remote method is called by a client', function(done) {
|
|
|
|
var beforeCalled = false;
|
|
|
|
var afterCalled = false;
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-05-03 03:04:06 +00:00
|
|
|
User.beforeRemote('create', function(ctx, user, next) {
|
|
|
|
assert(!afterCalled);
|
|
|
|
beforeCalled = true;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-03 03:04:06 +00:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
User.afterRemote('create', function(ctx, user, next) {
|
|
|
|
assert(beforeCalled);
|
|
|
|
afterCalled = true;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-03 03:04:06 +00:00
|
|
|
next();
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-05-03 03:04:06 +00:00
|
|
|
// invoke save
|
|
|
|
request(app)
|
|
|
|
.post('/users')
|
2016-11-15 21:46:23 +00:00
|
|
|
.send({data: {first: 'foo', last: 'bar'}})
|
2014-05-03 03:04:06 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
2014-11-21 02:35:36 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-03 03:04:06 +00:00
|
|
|
assert(beforeCalled, 'before hook was not called');
|
|
|
|
assert(afterCalled, 'after hook was not called');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-03 03:04:06 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-04-03 08:31:03 +00:00
|
|
|
describe('Model.afterRemoteError(name, fn)', function() {
|
|
|
|
it('runs the function when method fails', function(done) {
|
|
|
|
var actualError = 'hook not called';
|
|
|
|
User.afterRemoteError('login', function(ctx, next) {
|
|
|
|
actualError = ctx.error;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-03 08:31:03 +00:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
request(app).get('/users/sign-in?username=bob&password=123')
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-03 08:31:03 +00:00
|
|
|
expect(actualError)
|
|
|
|
.to.have.property('message', 'bad username and password!');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-03 08:31:03 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('Remote Method invoking context', function() {
|
2014-05-03 03:04:06 +00:00
|
|
|
describe('ctx.req', function() {
|
2014-11-21 01:46:21 +00:00
|
|
|
it('The express ServerRequest object', function(done) {
|
2014-05-03 03:04:06 +00:00
|
|
|
var hookCalled = false;
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-05-03 03:04:06 +00:00
|
|
|
User.beforeRemote('create', function(ctx, user, next) {
|
|
|
|
hookCalled = true;
|
|
|
|
assert(ctx.req);
|
|
|
|
assert(ctx.req.url);
|
|
|
|
assert(ctx.req.method);
|
|
|
|
assert(ctx.res);
|
|
|
|
assert(ctx.res.write);
|
|
|
|
assert(ctx.res.end);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-03 03:04:06 +00:00
|
|
|
next();
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-05-03 03:04:06 +00:00
|
|
|
// invoke save
|
|
|
|
request(app)
|
|
|
|
.post('/users')
|
2016-11-15 21:46:23 +00:00
|
|
|
.send({data: {first: 'foo', last: 'bar'}})
|
2014-05-03 03:04:06 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
2014-11-21 02:35:36 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-03 03:04:06 +00:00
|
|
|
assert(hookCalled);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-03 03:04:06 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('ctx.res', function() {
|
2014-11-21 01:46:21 +00:00
|
|
|
it('The express ServerResponse object', function(done) {
|
2014-05-03 03:04:06 +00:00
|
|
|
var hookCalled = false;
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-05-03 03:04:06 +00:00
|
|
|
User.beforeRemote('create', function(ctx, user, next) {
|
|
|
|
hookCalled = true;
|
|
|
|
assert(ctx.req);
|
|
|
|
assert(ctx.req.url);
|
|
|
|
assert(ctx.req.method);
|
|
|
|
assert(ctx.res);
|
|
|
|
assert(ctx.res.write);
|
|
|
|
assert(ctx.res.end);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-03 03:04:06 +00:00
|
|
|
next();
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-05-03 03:04:06 +00:00
|
|
|
// invoke save
|
|
|
|
request(app)
|
|
|
|
.post('/users')
|
2016-11-15 21:46:23 +00:00
|
|
|
.send({data: {first: 'foo', last: 'bar'}})
|
2014-05-03 03:04:06 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
2014-11-21 02:35:36 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-03 03:04:06 +00:00
|
|
|
assert(hookCalled);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-03 03:04:06 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
});
|
2014-05-03 03:04:06 +00:00
|
|
|
|
|
|
|
describe('Model.hasMany(Model)', function() {
|
2014-11-21 01:46:21 +00:00
|
|
|
it('Define a one to many relationship', function(done) {
|
2016-11-15 21:46:23 +00:00
|
|
|
var Book = dataSource.createModel('book', {title: String, author: String});
|
|
|
|
var Chapter = dataSource.createModel('chapter', {title: String});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-05-03 03:04:06 +00:00
|
|
|
// by referencing model
|
|
|
|
Book.hasMany(Chapter);
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
Book.create({title: 'Into the Wild', author: 'Jon Krakauer'}, function(err, book) {
|
2014-05-03 03:04:06 +00:00
|
|
|
// using 'chapters' scope for build:
|
2016-11-15 21:46:23 +00:00
|
|
|
var c = book.chapters.build({title: 'Chapter 1'});
|
|
|
|
book.chapters.create({title: 'Chapter 2'}, function() {
|
2014-11-21 02:35:36 +00:00
|
|
|
c.save(function() {
|
2016-11-15 21:46:23 +00:00
|
|
|
Chapter.count({bookId: book.id}, function(err, count) {
|
2014-05-03 03:04:06 +00:00
|
|
|
assert.equal(count, 2);
|
2016-11-15 21:46:23 +00:00
|
|
|
book.chapters({where: {title: 'Chapter 1'}}, function(err, chapters) {
|
2014-05-03 03:04:06 +00:00
|
|
|
assert.equal(chapters.length, 1);
|
|
|
|
assert.equal(chapters[0].title, 'Chapter 1');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-03 03:04:06 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('Model.properties', function() {
|
2014-05-03 03:04:06 +00:00
|
|
|
it('Normalized properties passed in originally by loopback.createModel()', function() {
|
|
|
|
var props = {
|
|
|
|
s: String,
|
2016-11-15 21:46:23 +00:00
|
|
|
n: {type: 'Number'},
|
|
|
|
o: {type: 'String', min: 10, max: 100},
|
2014-05-03 03:04:06 +00:00
|
|
|
d: Date,
|
2016-04-01 09:14:26 +00:00
|
|
|
g: loopback.GeoPoint,
|
2014-05-03 03:04:06 +00:00
|
|
|
};
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-05-03 03:04:06 +00:00
|
|
|
var MyModel = loopback.createModel('foo', props);
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
Object.keys(MyModel.definition.properties).forEach(function(key) {
|
2014-05-03 03:04:06 +00:00
|
|
|
var p = MyModel.definition.properties[key];
|
|
|
|
var o = MyModel.definition.properties[key];
|
|
|
|
assert(p);
|
|
|
|
assert(o);
|
|
|
|
assert(typeof p.type === 'function');
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
if (typeof o === 'function') {
|
2014-05-03 03:04:06 +00:00
|
|
|
// the normalized property
|
|
|
|
// should match the given property
|
|
|
|
assert(
|
2014-11-21 02:35:36 +00:00
|
|
|
p.type.name === o.name ||
|
2014-05-03 03:04:06 +00:00
|
|
|
p.type.name === o
|
2014-11-21 01:46:21 +00:00
|
|
|
);
|
2014-05-03 03:04:06 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('Model.extend()', function() {
|
2014-05-03 03:04:06 +00:00
|
|
|
it('Create a new model by extending an existing model', function() {
|
2014-06-05 07:45:09 +00:00
|
|
|
var User = loopback.PersistedModel.extend('test-user', {
|
2016-04-01 09:14:26 +00:00
|
|
|
email: String,
|
2014-05-03 03:04:06 +00:00
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
User.foo = function() {
|
2014-05-03 03:04:06 +00:00
|
|
|
return 'bar';
|
2014-11-21 01:46:21 +00:00
|
|
|
};
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
User.prototype.bar = function() {
|
2014-05-03 03:04:06 +00:00
|
|
|
return 'foo';
|
2014-11-21 01:46:21 +00:00
|
|
|
};
|
|
|
|
|
2014-05-03 03:04:06 +00:00
|
|
|
var MyUser = User.extend('my-user', {
|
|
|
|
a: String,
|
2016-04-01 09:14:26 +00:00
|
|
|
b: String,
|
2014-05-03 03:04:06 +00:00
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-05-03 03:04:06 +00:00
|
|
|
assert.equal(MyUser.prototype.bar, User.prototype.bar);
|
|
|
|
assert.equal(MyUser.foo, User.foo);
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-05-03 03:04:06 +00:00
|
|
|
var user = new MyUser({
|
|
|
|
email: 'foo@bar.com',
|
|
|
|
a: 'foo',
|
2016-04-01 09:14:26 +00:00
|
|
|
b: 'bar',
|
2014-05-03 03:04:06 +00:00
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-05-03 03:04:06 +00:00
|
|
|
assert.equal(user.email, 'foo@bar.com');
|
|
|
|
assert.equal(user.a, 'foo');
|
|
|
|
assert.equal(user.b, 'bar');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Model.extend() events', function() {
|
|
|
|
it('create isolated emitters for subclasses', function() {
|
|
|
|
var User1 = loopback.createModel('User1', {
|
|
|
|
'first': String,
|
2016-04-01 09:14:26 +00:00
|
|
|
'last': String,
|
2014-05-03 03:04:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var User2 = loopback.createModel('User2', {
|
2016-04-01 09:14:26 +00:00
|
|
|
'name': String,
|
2014-05-03 03:04:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var user1Triggered = false;
|
|
|
|
User1.once('x', function(event) {
|
|
|
|
user1Triggered = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
var user2Triggered = false;
|
|
|
|
User2.once('x', function(event) {
|
|
|
|
user2Triggered = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
assert(User1.once !== User2.once);
|
|
|
|
assert(User1.once !== loopback.Model.once);
|
|
|
|
|
|
|
|
User1.emit('x', User1);
|
|
|
|
|
|
|
|
assert(user1Triggered);
|
|
|
|
assert(!user2Triggered);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('Model.checkAccessTypeForMethod(remoteMethod)', function() {
|
2014-05-03 03:04:06 +00:00
|
|
|
shouldReturn('create', ACL.WRITE);
|
|
|
|
shouldReturn('updateOrCreate', ACL.WRITE);
|
2016-09-02 12:40:47 +00:00
|
|
|
shouldReturn('upsertWithWhere', ACL.WRITE);
|
2014-05-03 03:04:06 +00:00
|
|
|
shouldReturn('upsert', ACL.WRITE);
|
|
|
|
shouldReturn('exists', ACL.READ);
|
|
|
|
shouldReturn('findById', ACL.READ);
|
|
|
|
shouldReturn('find', ACL.READ);
|
|
|
|
shouldReturn('findOne', ACL.READ);
|
|
|
|
shouldReturn('destroyById', ACL.WRITE);
|
|
|
|
shouldReturn('deleteById', ACL.WRITE);
|
|
|
|
shouldReturn('removeById', ACL.WRITE);
|
|
|
|
shouldReturn('count', ACL.READ);
|
|
|
|
shouldReturn('unkown-model-method', ACL.EXECUTE);
|
|
|
|
|
|
|
|
function shouldReturn(methodName, expectedAccessType) {
|
2014-11-21 02:35:36 +00:00
|
|
|
describe(methodName, function() {
|
2014-05-03 03:04:06 +00:00
|
|
|
it('should return ' + expectedAccessType, function() {
|
2016-11-15 21:46:23 +00:00
|
|
|
var remoteMethod = {name: methodName};
|
2014-05-03 03:04:06 +00:00
|
|
|
assert.equal(
|
|
|
|
User._getAccessTypeForMethod(remoteMethod),
|
|
|
|
expectedAccessType
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Model.getChangeModel()', function() {
|
2014-11-21 02:35:36 +00:00
|
|
|
it('Get the Change Model', function() {
|
2014-05-03 03:04:06 +00:00
|
|
|
var UserChange = User.getChangeModel();
|
|
|
|
var change = new UserChange();
|
2016-07-26 12:10:13 +00:00
|
|
|
assert(change instanceof app.registry.getModel('Change'));
|
2014-05-03 03:04:06 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Model.getSourceId(callback)', function() {
|
2014-11-21 02:35:36 +00:00
|
|
|
it('Get the Source Id', function(done) {
|
2014-05-03 03:04:06 +00:00
|
|
|
User.getSourceId(function(err, id) {
|
|
|
|
assert.equal('memory-user', id);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-03 03:04:06 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Model.checkpoint(callback)', function() {
|
2014-11-21 02:35:36 +00:00
|
|
|
it('Create a checkpoint', function(done) {
|
2014-05-03 03:04:06 +00:00
|
|
|
var Checkpoint = User.getChangeModel().getCheckpointModel();
|
|
|
|
var tasks = [
|
|
|
|
getCurrentCheckpoint,
|
2016-04-01 09:14:26 +00:00
|
|
|
checkpoint,
|
2014-05-03 03:04:06 +00:00
|
|
|
];
|
2016-04-01 09:14:26 +00:00
|
|
|
var result, current;
|
2014-05-03 03:04:06 +00:00
|
|
|
|
2014-05-16 00:27:02 +00:00
|
|
|
async.series(tasks, function(err) {
|
2014-11-21 02:35:36 +00:00
|
|
|
if (err) return done(err);
|
2014-05-03 03:04:06 +00:00
|
|
|
|
|
|
|
assert.equal(result, current + 1);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-03 03:04:06 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
function getCurrentCheckpoint(cb) {
|
|
|
|
Checkpoint.current(function(err, cp) {
|
|
|
|
current = cp;
|
|
|
|
cb(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkpoint(cb) {
|
|
|
|
User.checkpoint(function(err, cp) {
|
2014-05-16 00:27:02 +00:00
|
|
|
result = cp.seq;
|
2014-05-03 03:04:06 +00:00
|
|
|
cb(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Model._getACLModel()', function() {
|
|
|
|
it('should return the subclass of ACL', function() {
|
|
|
|
var Model = require('../').Model;
|
2015-04-07 11:43:26 +00:00
|
|
|
var originalValue = Model._ACL();
|
2014-05-03 03:04:06 +00:00
|
|
|
var acl = ACL.extend('acl');
|
|
|
|
Model._ACL(null); // Reset the ACL class for the base model
|
|
|
|
var model = Model._ACL();
|
2015-04-07 11:43:26 +00:00
|
|
|
Model._ACL(originalValue); // Reset the value back
|
2014-05-03 03:04:06 +00:00
|
|
|
assert.equal(model, acl);
|
|
|
|
});
|
|
|
|
});
|
2014-09-10 16:35:02 +00:00
|
|
|
|
2015-02-26 15:36:44 +00:00
|
|
|
describe('PersistedModel remote methods', function() {
|
2014-09-10 16:35:02 +00:00
|
|
|
it('includes all aliases', function() {
|
|
|
|
var app = loopback();
|
2015-02-26 15:36:44 +00:00
|
|
|
var model = PersistedModel.extend('PersistedModelForAliases');
|
2016-11-15 21:46:23 +00:00
|
|
|
app.dataSource('db', {connector: 'memory'});
|
|
|
|
app.model(model, {dataSource: 'db'});
|
2014-09-10 16:35:02 +00:00
|
|
|
|
|
|
|
// this code is used by loopback-sdk-angular codegen
|
|
|
|
var metadata = app.handler('rest')
|
|
|
|
.adapter
|
|
|
|
.getClasses()
|
2015-02-26 15:36:44 +00:00
|
|
|
.filter(function(c) { return c.name === model.modelName; })[0];
|
2014-09-10 16:35:02 +00:00
|
|
|
|
|
|
|
var methodNames = [];
|
|
|
|
metadata.methods.forEach(function(method) {
|
|
|
|
methodNames.push(method.name);
|
2016-05-09 19:24:03 +00:00
|
|
|
var aliases = method.sharedMethod.aliases;
|
|
|
|
if (method.name.indexOf('prototype.') === 0) {
|
|
|
|
aliases = aliases.map(function(alias) {
|
|
|
|
return 'prototype.' + alias;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
methodNames = methodNames.concat(aliases || []);
|
2014-09-10 16:35:02 +00:00
|
|
|
});
|
|
|
|
expect(methodNames).to.have.members([
|
2015-02-26 15:36:44 +00:00
|
|
|
// NOTE(bajtos) These three methods are disabled by default
|
|
|
|
// Because all tests share the same global registry model
|
|
|
|
// and one of the tests was enabling remoting of "destroyAll",
|
|
|
|
// this test was seeing this method (with all aliases) as public
|
|
|
|
// 'destroyAll', 'deleteAll', 'remove',
|
2014-09-10 16:35:02 +00:00
|
|
|
'create',
|
2016-05-09 19:24:03 +00:00
|
|
|
'upsert', 'updateOrCreate', 'patchOrCreate',
|
2016-09-02 12:40:47 +00:00
|
|
|
'upsertWithWhere', 'patchOrCreateWithWhere',
|
2014-09-10 16:35:02 +00:00
|
|
|
'exists',
|
|
|
|
'findById',
|
2016-05-09 19:24:03 +00:00
|
|
|
'replaceById',
|
|
|
|
'replaceOrCreate',
|
2014-09-10 16:35:02 +00:00
|
|
|
'find',
|
|
|
|
'findOne',
|
|
|
|
'updateAll', 'update',
|
|
|
|
'deleteById',
|
|
|
|
'destroyById',
|
|
|
|
'removeById',
|
|
|
|
'count',
|
2016-05-09 19:24:03 +00:00
|
|
|
'prototype.patchAttributes', 'prototype.updateAttributes',
|
2016-04-01 09:14:26 +00:00
|
|
|
'createChangeStream',
|
2014-09-10 16:35:02 +00:00
|
|
|
]);
|
|
|
|
});
|
2016-04-27 11:15:24 +00:00
|
|
|
|
|
|
|
it('emits a `remoteMethodDisabled` event', function() {
|
|
|
|
var app = loopback();
|
|
|
|
var model = PersistedModel.extend('TestModelForDisablingRemoteMethod');
|
2016-11-15 21:46:23 +00:00
|
|
|
app.dataSource('db', {connector: 'memory'});
|
|
|
|
app.model(model, {dataSource: 'db'});
|
2016-04-27 11:15:24 +00:00
|
|
|
|
|
|
|
var callbackSpy = require('sinon').spy();
|
|
|
|
var TestModel = app.models.TestModelForDisablingRemoteMethod;
|
|
|
|
TestModel.on('remoteMethodDisabled', callbackSpy);
|
2016-09-19 16:52:34 +00:00
|
|
|
TestModel.disableRemoteMethod('findOne', true);
|
|
|
|
|
|
|
|
expect(callbackSpy).to.have.been.calledWith(TestModel.sharedClass, 'findOne');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('emits a `remoteMethodDisabled` event from disableRemoteMethodByName', function() {
|
|
|
|
var app = loopback();
|
|
|
|
var model = PersistedModel.extend('TestModelForDisablingRemoteMethod');
|
2016-11-15 21:46:23 +00:00
|
|
|
app.dataSource('db', {connector: 'memory'});
|
|
|
|
app.model(model, {dataSource: 'db'});
|
2016-09-19 16:52:34 +00:00
|
|
|
|
|
|
|
var callbackSpy = require('sinon').spy();
|
|
|
|
var TestModel = app.models.TestModelForDisablingRemoteMethod;
|
|
|
|
TestModel.on('remoteMethodDisabled', callbackSpy);
|
|
|
|
TestModel.disableRemoteMethodByName('findOne');
|
2016-04-27 11:15:24 +00:00
|
|
|
|
|
|
|
expect(callbackSpy).to.have.been.calledWith(TestModel.sharedClass, 'findOne');
|
|
|
|
});
|
2014-09-10 16:35:02 +00:00
|
|
|
});
|
2015-04-28 10:46:02 +00:00
|
|
|
|
|
|
|
describe('Model.getApp(cb)', function() {
|
|
|
|
var app, TestModel;
|
|
|
|
beforeEach(function setup() {
|
|
|
|
app = loopback();
|
|
|
|
TestModel = loopback.createModel('TestModelForGetApp'); // unique name
|
2016-11-15 21:46:23 +00:00
|
|
|
app.dataSource('db', {connector: 'memory'});
|
2015-04-28 10:46:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('calls the callback when already attached', function(done) {
|
2016-11-15 21:46:23 +00:00
|
|
|
app.model(TestModel, {dataSource: 'db'});
|
2015-04-28 10:46:02 +00:00
|
|
|
TestModel.getApp(function(err, a) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-28 10:46:02 +00:00
|
|
|
expect(a).to.equal(app);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-28 10:46:02 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
// fails on time-out when not implemented correctly
|
|
|
|
});
|
|
|
|
|
|
|
|
it('calls the callback after attached', function(done) {
|
|
|
|
TestModel.getApp(function(err, a) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-28 10:46:02 +00:00
|
|
|
expect(a).to.equal(app);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-28 10:46:02 +00:00
|
|
|
done();
|
|
|
|
});
|
2016-11-15 21:46:23 +00:00
|
|
|
app.model(TestModel, {dataSource: 'db'});
|
2015-04-28 10:46:02 +00:00
|
|
|
// fails on time-out when not implemented correctly
|
|
|
|
});
|
|
|
|
});
|
2016-09-20 07:51:50 +00:00
|
|
|
|
|
|
|
describe('Model.createOptionsFromRemotingContext', function() {
|
|
|
|
var app, TestModel, accessToken, userId, actualOptions;
|
|
|
|
|
|
|
|
before(setupAppAndRequest);
|
|
|
|
before(createUserAndAccessToken);
|
|
|
|
|
|
|
|
it('sets empty options.accessToken for anonymous requests', function(done) {
|
|
|
|
request(app).get('/TestModels/saveOptions')
|
|
|
|
.expect(204, function(err) {
|
|
|
|
if (err) return done(err);
|
2016-12-06 14:24:49 +00:00
|
|
|
expect(actualOptions).to.include({accessToken: null});
|
2016-09-20 07:51:50 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sets options.accessToken for authorized requests', function(done) {
|
|
|
|
request(app).get('/TestModels/saveOptions')
|
|
|
|
.set('Authorization', accessToken.id)
|
|
|
|
.expect(204, function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
expect(actualOptions).to.have.property('accessToken');
|
|
|
|
expect(actualOptions.accessToken.toObject())
|
|
|
|
.to.eql(accessToken.toObject());
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('allows "beforeRemote" hooks to contribute options', function(done) {
|
|
|
|
TestModel.beforeRemote('saveOptions', function(ctx, unused, next) {
|
|
|
|
ctx.args.options.hooked = true;
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
request(app).get('/TestModels/saveOptions')
|
|
|
|
.expect(204, function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
expect(actualOptions).to.have.property('hooked', true);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('allows apps to add options before remoting hooks', function(done) {
|
|
|
|
TestModel.createOptionsFromRemotingContext = function(ctx) {
|
|
|
|
return {hooks: []};
|
|
|
|
};
|
|
|
|
|
|
|
|
TestModel.beforeRemote('saveOptions', function(ctx, unused, next) {
|
|
|
|
ctx.args.options.hooks.push('beforeRemote');
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
// In real apps, this code can live in a component or in a boot script
|
|
|
|
app.remotes().phases
|
|
|
|
.addBefore('invoke', 'options-from-request')
|
|
|
|
.use(function(ctx, next) {
|
|
|
|
ctx.args.options.hooks.push('custom');
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
request(app).get('/TestModels/saveOptions')
|
|
|
|
.expect(204, function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
expect(actualOptions.hooks).to.eql(['custom', 'beforeRemote']);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function setupAppAndRequest() {
|
|
|
|
app = loopback({localRegistry: true, loadBuiltinModels: true});
|
|
|
|
|
|
|
|
app.dataSource('db', {connector: 'memory'});
|
|
|
|
|
|
|
|
TestModel = app.registry.createModel('TestModel', {base: 'Model'});
|
|
|
|
TestModel.saveOptions = function(options, cb) {
|
|
|
|
actualOptions = options;
|
|
|
|
cb();
|
|
|
|
};
|
|
|
|
|
|
|
|
TestModel.remoteMethod('saveOptions', {
|
|
|
|
accepts: {arg: 'options', type: 'object', http: 'optionsFromRequest'},
|
|
|
|
http: {verb: 'GET', path: '/saveOptions'},
|
|
|
|
});
|
|
|
|
|
|
|
|
app.model(TestModel, {dataSource: null});
|
|
|
|
|
|
|
|
app.enableAuth({dataSource: 'db'});
|
|
|
|
|
|
|
|
app.use(loopback.token());
|
|
|
|
app.use(loopback.rest());
|
|
|
|
}
|
|
|
|
|
|
|
|
function createUserAndAccessToken() {
|
|
|
|
var CREDENTIALS = {email: 'context@example.com', password: 'pass'};
|
|
|
|
var User = app.registry.getModel('User');
|
|
|
|
return User.create(CREDENTIALS)
|
|
|
|
.then(function(u) {
|
|
|
|
return User.login(CREDENTIALS);
|
|
|
|
}).then(function(token) {
|
|
|
|
accessToken = token;
|
|
|
|
userId = token.userId;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2014-05-03 03:04:06 +00:00
|
|
|
});
|