2014-05-03 01:12:50 +00:00
|
|
|
var async = require('async');
|
|
|
|
var loopback = require('../');
|
|
|
|
var ACL = loopback.ACL;
|
|
|
|
var Change = loopback.Change;
|
|
|
|
var defineModelTestsWithDataSource = require('./util/model-tests');
|
2014-06-05 07:45:09 +00:00
|
|
|
var PersistedModel = loopback.PersistedModel;
|
2014-05-03 01:12:50 +00:00
|
|
|
|
2014-06-03 08:39:54 +00:00
|
|
|
var describe = require('./util/describe');
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
describe('Model / PersistedModel', function() {
|
2014-05-03 01:12:50 +00:00
|
|
|
defineModelTestsWithDataSource({
|
|
|
|
dataSource: {
|
|
|
|
connector: loopback.Memory
|
|
|
|
}
|
|
|
|
});
|
2014-05-03 03:07:59 +00:00
|
|
|
|
|
|
|
describe('Model.validatesUniquenessOf(property, options)', function() {
|
|
|
|
it("Ensure the value for `property` is unique", function(done) {
|
2014-06-05 07:45:09 +00:00
|
|
|
var User = PersistedModel.extend('user', {
|
2014-05-03 04:19:14 +00:00
|
|
|
'first': String,
|
|
|
|
'last': String,
|
|
|
|
'age': Number,
|
|
|
|
'password': String,
|
|
|
|
'gender': String,
|
|
|
|
'domain': String,
|
|
|
|
'email': String
|
|
|
|
});
|
|
|
|
|
|
|
|
var dataSource = loopback.createDataSource({
|
|
|
|
connector: loopback.Memory
|
|
|
|
});
|
|
|
|
|
|
|
|
User.attachTo(dataSource);
|
|
|
|
|
2014-05-03 03:07:59 +00:00
|
|
|
User.validatesUniquenessOf('email', {message: 'email is not unique'});
|
|
|
|
|
|
|
|
var joe = new User({email: 'joe@joe.com'});
|
|
|
|
var joe2 = new User({email: 'joe@joe.com'});
|
|
|
|
|
|
|
|
joe.save(function () {
|
|
|
|
joe2.save(function (err) {
|
|
|
|
assert(err, 'should get a validation error');
|
|
|
|
assert(joe2.errors.email, 'model should have email error');
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Model.attachTo(dataSource)', function() {
|
|
|
|
it("Attach a model to a [DataSource](#data-source)", function() {
|
|
|
|
var MyModel = loopback.createModel('my-model', {name: String});
|
2014-05-03 04:19:14 +00:00
|
|
|
var dataSource = loopback.createDataSource({
|
|
|
|
connector: loopback.Memory
|
|
|
|
});
|
2014-05-03 03:07:59 +00:00
|
|
|
|
|
|
|
MyModel.attachTo(dataSource);
|
|
|
|
|
2014-05-19 22:56:26 +00:00
|
|
|
MyModel.find(function(err, results) {
|
|
|
|
assert(results.length === 0, 'should have data access methods after attaching to a data source');
|
|
|
|
});
|
2014-05-03 03:07:59 +00:00
|
|
|
});
|
|
|
|
});
|
2014-05-03 01:12:50 +00:00
|
|
|
});
|
2014-05-03 03:04:06 +00:00
|
|
|
|
|
|
|
describe.onServer('Remote Methods', function(){
|
|
|
|
|
|
|
|
var User;
|
|
|
|
var dataSource;
|
|
|
|
var app;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
2014-06-05 07:45:09 +00:00
|
|
|
User = PersistedModel.extend('user', {
|
2014-05-03 03:04:06 +00:00
|
|
|
'first': String,
|
|
|
|
'last': String,
|
|
|
|
'age': Number,
|
|
|
|
'password': String,
|
|
|
|
'gender': String,
|
|
|
|
'domain': String,
|
|
|
|
'email': String
|
|
|
|
}, {
|
|
|
|
trackChanges: true
|
|
|
|
});
|
|
|
|
|
|
|
|
dataSource = loopback.createDataSource({
|
|
|
|
connector: loopback.Memory
|
|
|
|
});
|
|
|
|
|
|
|
|
User.attachTo(dataSource);
|
|
|
|
|
|
|
|
User.login = function (username, password, fn) {
|
|
|
|
if(username === 'foo' && password === 'bar') {
|
|
|
|
fn(null, 123);
|
|
|
|
} else {
|
|
|
|
throw new Error('bad username and password!');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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'}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
app = loopback();
|
|
|
|
app.use(loopback.rest());
|
|
|
|
app.model(User);
|
|
|
|
});
|
2014-05-03 04:19:14 +00:00
|
|
|
|
|
|
|
describe('Model.destroyAll(callback)', function() {
|
|
|
|
it("Delete all Model instances from data source", function(done) {
|
|
|
|
(new TaskEmitter())
|
|
|
|
.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'})
|
|
|
|
.on('done', function () {
|
|
|
|
User.count(function (err, count) {
|
|
|
|
User.destroyAll(function () {
|
|
|
|
User.count(function (err, count) {
|
|
|
|
assert.equal(count, 0);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-05-03 03:07:59 +00:00
|
|
|
|
2014-05-03 03:04:06 +00:00
|
|
|
describe('Example Remote Method', function () {
|
|
|
|
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)
|
|
|
|
.end(function(err, res){
|
|
|
|
if(err) return done(err);
|
|
|
|
assert.equal(res.body, 123);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Converts null result of findById to 404 Not Found', function(done) {
|
|
|
|
request(app)
|
|
|
|
.get('/users/not-found')
|
|
|
|
.expect(404)
|
|
|
|
.end(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Model.beforeRemote(name, fn)', function(){
|
|
|
|
it('Run a function before a remote method is called by a client', function(done) {
|
|
|
|
var hookCalled = false;
|
|
|
|
|
|
|
|
User.beforeRemote('create', function(ctx, user, next) {
|
|
|
|
hookCalled = true;
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
// invoke save
|
|
|
|
request(app)
|
|
|
|
.post('/users')
|
|
|
|
.send({data: {first: 'foo', last: 'bar'}})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
if(err) return done(err);
|
|
|
|
assert(hookCalled, 'hook wasnt called');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Model.afterRemote(name, fn)', function(){
|
|
|
|
it('Run a function after a remote method is called by a client', function(done) {
|
|
|
|
var beforeCalled = false;
|
|
|
|
var afterCalled = false;
|
|
|
|
|
|
|
|
User.beforeRemote('create', function(ctx, user, next) {
|
|
|
|
assert(!afterCalled);
|
|
|
|
beforeCalled = true;
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
User.afterRemote('create', function(ctx, user, next) {
|
|
|
|
assert(beforeCalled);
|
|
|
|
afterCalled = true;
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
// invoke save
|
|
|
|
request(app)
|
|
|
|
.post('/users')
|
|
|
|
.send({data: {first: 'foo', last: 'bar'}})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
if(err) return done(err);
|
|
|
|
assert(beforeCalled, 'before hook was not called');
|
|
|
|
assert(afterCalled, 'after hook was not called');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Remote Method invoking context', function () {
|
|
|
|
describe('ctx.req', function() {
|
|
|
|
it("The express ServerRequest object", function(done) {
|
|
|
|
var hookCalled = false;
|
|
|
|
|
|
|
|
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);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
// invoke save
|
|
|
|
request(app)
|
|
|
|
.post('/users')
|
|
|
|
.send({data: {first: 'foo', last: 'bar'}})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
if(err) return done(err);
|
|
|
|
assert(hookCalled);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('ctx.res', function() {
|
|
|
|
it("The express ServerResponse object", function(done) {
|
|
|
|
var hookCalled = false;
|
|
|
|
|
|
|
|
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);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
// invoke save
|
|
|
|
request(app)
|
|
|
|
.post('/users')
|
|
|
|
.send({data: {first: 'foo', last: 'bar'}})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
if(err) return done(err);
|
|
|
|
assert(hookCalled);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('Model.hasMany(Model)', function() {
|
|
|
|
it("Define a one to many relationship", function(done) {
|
|
|
|
var Book = dataSource.createModel('book', {title: String, author: String});
|
|
|
|
var Chapter = dataSource.createModel('chapter', {title: String});
|
|
|
|
|
|
|
|
// by referencing model
|
|
|
|
Book.hasMany(Chapter);
|
|
|
|
|
|
|
|
Book.create({title: 'Into the Wild', author: 'Jon Krakauer'}, function(err, book) {
|
|
|
|
// using 'chapters' scope for build:
|
|
|
|
var c = book.chapters.build({title: 'Chapter 1'});
|
|
|
|
book.chapters.create({title: 'Chapter 2'}, function () {
|
|
|
|
c.save(function () {
|
|
|
|
Chapter.count({bookId: book.id}, function (err, count) {
|
|
|
|
assert.equal(count, 2);
|
|
|
|
book.chapters({where: {title: 'Chapter 1'}}, function(err, chapters) {
|
|
|
|
assert.equal(chapters.length, 1);
|
|
|
|
assert.equal(chapters[0].title, 'Chapter 1');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Model.properties', function(){
|
|
|
|
it('Normalized properties passed in originally by loopback.createModel()', function() {
|
|
|
|
var props = {
|
|
|
|
s: String,
|
|
|
|
n: {type: 'Number'},
|
|
|
|
o: {type: 'String', min: 10, max: 100},
|
|
|
|
d: Date,
|
|
|
|
g: loopback.GeoPoint
|
|
|
|
};
|
|
|
|
|
|
|
|
var MyModel = loopback.createModel('foo', props);
|
|
|
|
|
|
|
|
Object.keys(MyModel.definition.properties).forEach(function (key) {
|
|
|
|
var p = MyModel.definition.properties[key];
|
|
|
|
var o = MyModel.definition.properties[key];
|
|
|
|
assert(p);
|
|
|
|
assert(o);
|
|
|
|
assert(typeof p.type === 'function');
|
|
|
|
|
|
|
|
if(typeof o === 'function') {
|
|
|
|
// the normalized property
|
|
|
|
// should match the given property
|
|
|
|
assert(
|
|
|
|
p.type.name === o.name
|
|
|
|
||
|
|
|
|
p.type.name === o
|
|
|
|
)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Model.extend()', function(){
|
|
|
|
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', {
|
2014-05-03 03:04:06 +00:00
|
|
|
email: String
|
|
|
|
});
|
|
|
|
|
|
|
|
User.foo = function () {
|
|
|
|
return 'bar';
|
|
|
|
}
|
|
|
|
|
|
|
|
User.prototype.bar = function () {
|
|
|
|
return 'foo';
|
|
|
|
}
|
|
|
|
|
|
|
|
var MyUser = User.extend('my-user', {
|
|
|
|
a: String,
|
|
|
|
b: String
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(MyUser.prototype.bar, User.prototype.bar);
|
|
|
|
assert.equal(MyUser.foo, User.foo);
|
|
|
|
|
|
|
|
var user = new MyUser({
|
|
|
|
email: 'foo@bar.com',
|
|
|
|
a: 'foo',
|
|
|
|
b: 'bar'
|
|
|
|
});
|
|
|
|
|
|
|
|
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,
|
|
|
|
'last': String
|
|
|
|
});
|
|
|
|
|
|
|
|
var User2 = loopback.createModel('User2', {
|
|
|
|
'name': String
|
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Model.checkAccessTypeForMethod(remoteMethod)', function () {
|
|
|
|
shouldReturn('create', ACL.WRITE);
|
|
|
|
shouldReturn('updateOrCreate', ACL.WRITE);
|
|
|
|
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) {
|
|
|
|
describe(methodName, function () {
|
|
|
|
it('should return ' + expectedAccessType, function() {
|
|
|
|
var remoteMethod = {name: methodName};
|
|
|
|
assert.equal(
|
|
|
|
User._getAccessTypeForMethod(remoteMethod),
|
|
|
|
expectedAccessType
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Model.getChangeModel()', function() {
|
|
|
|
it('Get the Change Model', function () {
|
|
|
|
var UserChange = User.getChangeModel();
|
|
|
|
var change = new UserChange();
|
|
|
|
assert(change instanceof Change);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Model.getSourceId(callback)', function() {
|
|
|
|
it('Get the Source Id', function (done) {
|
|
|
|
User.getSourceId(function(err, id) {
|
|
|
|
assert.equal('memory-user', id);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Model.checkpoint(callback)', function() {
|
|
|
|
it('Create a checkpoint', function (done) {
|
|
|
|
var Checkpoint = User.getChangeModel().getCheckpointModel();
|
|
|
|
var tasks = [
|
|
|
|
getCurrentCheckpoint,
|
|
|
|
checkpoint
|
|
|
|
];
|
|
|
|
var result;
|
|
|
|
var current;
|
|
|
|
|
2014-05-16 00:27:02 +00:00
|
|
|
async.series(tasks, function(err) {
|
2014-05-03 03:04:06 +00:00
|
|
|
if(err) return done(err);
|
|
|
|
|
|
|
|
assert.equal(result, current + 1);
|
|
|
|
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;
|
|
|
|
var acl = ACL.extend('acl');
|
|
|
|
Model._ACL(null); // Reset the ACL class for the base model
|
|
|
|
var model = Model._ACL();
|
|
|
|
assert.equal(model, acl);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|