singlequote, semicolon & /*jshint -W030 */
This commit is contained in:
parent
800ce7f363
commit
918497c365
|
@ -1,3 +1,5 @@
|
||||||
|
/*jshint -W030 */
|
||||||
|
|
||||||
var loopback = require('../');
|
var loopback = require('../');
|
||||||
var lt = require('loopback-testing');
|
var lt = require('loopback-testing');
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
|
@ -20,7 +22,7 @@ describe('access control - integration', function () {
|
||||||
|
|
||||||
it('should have a validate method', function () {
|
it('should have a validate method', function () {
|
||||||
var token = new app.models.accessToken;
|
var token = new app.models.accessToken;
|
||||||
assert.equal(typeof token.validate, 'function');
|
assert.equal(typeof token.validate, 'function');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -65,7 +67,7 @@ describe('access control - integration', function () {
|
||||||
lt.it.shouldBeDeniedWhenCalledAnonymously('GET', '/api/users');
|
lt.it.shouldBeDeniedWhenCalledAnonymously('GET', '/api/users');
|
||||||
lt.it.shouldBeDeniedWhenCalledUnauthenticated('GET', '/api/users');
|
lt.it.shouldBeDeniedWhenCalledUnauthenticated('GET', '/api/users');
|
||||||
lt.it.shouldBeDeniedWhenCalledByUser(CURRENT_USER, 'GET', '/api/users');
|
lt.it.shouldBeDeniedWhenCalledByUser(CURRENT_USER, 'GET', '/api/users');
|
||||||
|
|
||||||
lt.it.shouldBeDeniedWhenCalledAnonymously('GET', urlForUser);
|
lt.it.shouldBeDeniedWhenCalledAnonymously('GET', urlForUser);
|
||||||
lt.it.shouldBeDeniedWhenCalledUnauthenticated('GET', urlForUser);
|
lt.it.shouldBeDeniedWhenCalledUnauthenticated('GET', urlForUser);
|
||||||
lt.it.shouldBeDeniedWhenCalledByUser(CURRENT_USER,'GET', urlForUser);
|
lt.it.shouldBeDeniedWhenCalledByUser(CURRENT_USER,'GET', urlForUser);
|
||||||
|
@ -224,5 +226,5 @@ describe('access control - integration', function () {
|
||||||
return '/api/accounts/' + this.account.id;
|
return '/api/accounts/' + this.account.id;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -257,8 +257,8 @@ function createTestApp(testToken, settings, done) {
|
||||||
var modelOptions = {
|
var modelOptions = {
|
||||||
acls: [
|
acls: [
|
||||||
{
|
{
|
||||||
principalType: "ROLE",
|
principalType: 'ROLE',
|
||||||
principalId: "$everyone",
|
principalId: '$everyone',
|
||||||
accessType: ACL.ALL,
|
accessType: ACL.ALL,
|
||||||
permission: ACL.DENY,
|
permission: ACL.DENY,
|
||||||
property: 'deleteById'
|
property: 'deleteById'
|
||||||
|
|
|
@ -29,7 +29,7 @@ describe('security scopes', function () {
|
||||||
testModel.attachTo(ds);
|
testModel.attachTo(ds);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should allow access to models for the given scope by wildcard", function () {
|
it('should allow access to models for the given scope by wildcard', function () {
|
||||||
Scope.create({name: 'userScope', description: 'access user information'}, function (err, scope) {
|
Scope.create({name: 'userScope', description: 'access user information'}, function (err, scope) {
|
||||||
ACL.create({principalType: ACL.SCOPE, principalId: scope.id, model: 'User', property: ACL.ALL,
|
ACL.create({principalType: ACL.SCOPE, principalId: scope.id, model: 'User', property: ACL.ALL,
|
||||||
accessType: ACL.ALL, permission: ACL.ALLOW},
|
accessType: ACL.ALL, permission: ACL.ALLOW},
|
||||||
|
@ -42,7 +42,7 @@ describe('security scopes', function () {
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should allow access to models for the given scope", function () {
|
it('should allow access to models for the given scope', function () {
|
||||||
Scope.create({name: 'testModelScope', description: 'access testModel information'}, function (err, scope) {
|
Scope.create({name: 'testModelScope', description: 'access testModel information'}, function (err, scope) {
|
||||||
ACL.create({principalType: ACL.SCOPE, principalId: scope.id,
|
ACL.create({principalType: ACL.SCOPE, principalId: scope.id,
|
||||||
model: 'testModel', property: 'name', accessType: ACL.READ, permission: ACL.ALLOW},
|
model: 'testModel', property: 'name', accessType: ACL.READ, permission: ACL.ALLOW},
|
||||||
|
@ -75,25 +75,25 @@ describe('security ACLs', function () {
|
||||||
it('should order ACL entries based on the matching score', function() {
|
it('should order ACL entries based on the matching score', function() {
|
||||||
var acls = [
|
var acls = [
|
||||||
{
|
{
|
||||||
"model": "account",
|
'model': 'account',
|
||||||
"accessType": "*",
|
'accessType': '*',
|
||||||
"permission": "DENY",
|
'permission': 'DENY',
|
||||||
"principalType": "ROLE",
|
'principalType': 'ROLE',
|
||||||
"principalId": "$everyone"
|
'principalId': '$everyone'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"model": "account",
|
'model': 'account',
|
||||||
"accessType": "*",
|
'accessType': '*',
|
||||||
"permission": "ALLOW",
|
'permission': 'ALLOW',
|
||||||
"principalType": "ROLE",
|
'principalType': 'ROLE',
|
||||||
"principalId": "$owner"
|
'principalId': '$owner'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"model": "account",
|
'model': 'account',
|
||||||
"accessType": "READ",
|
'accessType': 'READ',
|
||||||
"permission": "ALLOW",
|
'permission': 'ALLOW',
|
||||||
"principalType": "ROLE",
|
'principalType': 'ROLE',
|
||||||
"principalId": "$everyone"
|
'principalId': '$everyone'
|
||||||
}];
|
}];
|
||||||
var req = {
|
var req = {
|
||||||
model: 'account',
|
model: 'account',
|
||||||
|
@ -101,7 +101,7 @@ describe('security ACLs', function () {
|
||||||
accessType: 'WRITE'
|
accessType: 'WRITE'
|
||||||
};
|
};
|
||||||
|
|
||||||
acls = acls.map(function(a) { return new ACL(a)});
|
acls = acls.map(function(a) { return new ACL(a); });
|
||||||
|
|
||||||
var perm = ACL.resolvePermission(acls, req);
|
var perm = ACL.resolvePermission(acls, req);
|
||||||
assert.deepEqual(perm, { model: 'account',
|
assert.deepEqual(perm, { model: 'account',
|
||||||
|
@ -111,7 +111,7 @@ describe('security ACLs', function () {
|
||||||
methodNames: []});
|
methodNames: []});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should allow access to models for the given principal by wildcard", function () {
|
it('should allow access to models for the given principal by wildcard', function () {
|
||||||
ACL.create({principalType: ACL.USER, principalId: 'u001', model: 'User', property: ACL.ALL,
|
ACL.create({principalType: ACL.USER, principalId: 'u001', model: 'User', property: ACL.ALL,
|
||||||
accessType: ACL.ALL, permission: ACL.ALLOW}, function (err, acl) {
|
accessType: ACL.ALL, permission: ACL.ALLOW}, function (err, acl) {
|
||||||
|
|
||||||
|
@ -132,7 +132,7 @@ describe('security ACLs', function () {
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should allow access to models by exception", function () {
|
it('should allow access to models by exception', function () {
|
||||||
ACL.create({principalType: ACL.USER, principalId: 'u001', model: 'testModel', property: ACL.ALL,
|
ACL.create({principalType: ACL.USER, principalId: 'u001', model: 'testModel', property: ACL.ALL,
|
||||||
accessType: ACL.ALL, permission: ACL.DENY}, function (err, acl) {
|
accessType: ACL.ALL, permission: ACL.DENY}, function (err, acl) {
|
||||||
|
|
||||||
|
@ -161,7 +161,7 @@ describe('security ACLs', function () {
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should honor defaultPermission from the model", function () {
|
it('should honor defaultPermission from the model', function () {
|
||||||
var Customer = ds.createModel('Customer', {
|
var Customer = ds.createModel('Customer', {
|
||||||
name: {
|
name: {
|
||||||
type: String,
|
type: String,
|
||||||
|
@ -192,7 +192,7 @@ describe('security ACLs', function () {
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should honor static ACLs from the model", function () {
|
it('should honor static ACLs from the model', function () {
|
||||||
var Customer = ds.createModel('Customer', {
|
var Customer = ds.createModel('Customer', {
|
||||||
name: {
|
name: {
|
||||||
type: String,
|
type: String,
|
||||||
|
@ -227,7 +227,7 @@ describe('security ACLs', function () {
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should filter static ACLs by model/property", function() {
|
it('should filter static ACLs by model/property', function() {
|
||||||
var Model1 = ds.createModel('Model1', {
|
var Model1 = ds.createModel('Model1', {
|
||||||
name: {
|
name: {
|
||||||
type: String,
|
type: String,
|
||||||
|
@ -254,7 +254,7 @@ describe('security ACLs', function () {
|
||||||
assert(staticACLs.length === 1);
|
assert(staticACLs.length === 1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should check access against LDL, ACL, and Role", function () {
|
it('should check access against LDL, ACL, and Role', function () {
|
||||||
// var log = console.log;
|
// var log = console.log;
|
||||||
var log = function() {};
|
var log = function() {};
|
||||||
|
|
||||||
|
@ -327,6 +327,3 @@ describe('security ACLs', function () {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
/*jshint -W030 */
|
||||||
|
|
||||||
var async = require('async');
|
var async = require('async');
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
|
|
||||||
|
@ -39,7 +41,7 @@ describe('app', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('supports "before:" and "after:" prefixes', function(done) {
|
it('supports `before:` and `after:` prefixes', function(done) {
|
||||||
app.middleware('routes:before', namedHandler('routes:before'));
|
app.middleware('routes:before', namedHandler('routes:before'));
|
||||||
app.middleware('routes:after', namedHandler('routes:after'));
|
app.middleware('routes:after', namedHandler('routes:after'));
|
||||||
app.use(namedHandler('main'));
|
app.use(namedHandler('main'));
|
||||||
|
@ -247,7 +249,7 @@ describe('app', function() {
|
||||||
app = loopback();
|
app = loopback();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('adds the phase just before "routes" by default', function(done) {
|
it('adds the phase just before `routes` by default', function(done) {
|
||||||
app.defineMiddlewarePhases('custom');
|
app.defineMiddlewarePhases('custom');
|
||||||
verifyMiddlewarePhases(['custom', 'routes'], done);
|
verifyMiddlewarePhases(['custom', 'routes'], done);
|
||||||
});
|
});
|
||||||
|
@ -311,7 +313,7 @@ describe('app', function() {
|
||||||
db = loopback.createDataSource({connector: loopback.Memory});
|
db = loopback.createDataSource({connector: loopback.Memory});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Expose a `Model` to remote clients", function() {
|
it('Expose a `Model` to remote clients', function() {
|
||||||
var Color = PersistedModel.extend('color', {name: String});
|
var Color = PersistedModel.extend('color', {name: String});
|
||||||
app.model(Color);
|
app.model(Color);
|
||||||
Color.attachTo(db);
|
Color.attachTo(db);
|
||||||
|
@ -330,7 +332,7 @@ describe('app', function() {
|
||||||
var Color = PersistedModel.extend('color', {name: String});
|
var Color = PersistedModel.extend('color', {name: String});
|
||||||
app.model(Color);
|
app.model(Color);
|
||||||
Color.attachTo(db);
|
Color.attachTo(db);
|
||||||
var classes = app.remotes().classes().map(function(c) {return c.name});
|
var classes = app.remotes().classes().map(function(c) {return c.name;});
|
||||||
expect(classes).to.contain('color');
|
expect(classes).to.contain('color');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -343,7 +345,7 @@ describe('app', function() {
|
||||||
expect(app.models.Color).to.equal(Color);
|
expect(app.models.Color).to.equal(Color);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("emits a `modelRemoted` event", function() {
|
it('emits a `modelRemoted` event', function() {
|
||||||
var Color = PersistedModel.extend('color', {name: String});
|
var Color = PersistedModel.extend('color', {name: String});
|
||||||
Color.shared = true;
|
Color.shared = true;
|
||||||
var remotedClass;
|
var remotedClass;
|
||||||
|
@ -493,7 +495,7 @@ describe('app', function() {
|
||||||
.expect(200, done);
|
.expect(200, done);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('updates port on "listening" event', function(done) {
|
it('updates port on `listening` event', function(done) {
|
||||||
var app = loopback();
|
var app = loopback();
|
||||||
app.set('port', 0);
|
app.set('port', 0);
|
||||||
|
|
||||||
|
@ -503,7 +505,7 @@ describe('app', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('updates "url" on "listening" event', function(done) {
|
it('updates `url` on `listening` event', function(done) {
|
||||||
var app = loopback();
|
var app = loopback();
|
||||||
app.set('port', 0);
|
app.set('port', 0);
|
||||||
app.set('host', undefined);
|
app.set('host', undefined);
|
||||||
|
@ -559,7 +561,7 @@ describe('app', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe.onServer('app.get("/", loopback.status())', function () {
|
describe.onServer('app.get('/', loopback.status())', function () {
|
||||||
it('should return the status of the application', function (done) {
|
it('should return the status of the application', function (done) {
|
||||||
var app = loopback();
|
var app = loopback();
|
||||||
app.get('/', loopback.status());
|
app.get('/', loopback.status());
|
||||||
|
|
|
@ -1,16 +1,16 @@
|
||||||
describe('DataSource', function() {
|
describe('DataSource', function() {
|
||||||
var memory;
|
var memory;
|
||||||
|
|
||||||
beforeEach(function(){
|
beforeEach(function(){
|
||||||
memory = loopback.createDataSource({
|
memory = loopback.createDataSource({
|
||||||
connector: loopback.Memory
|
connector: loopback.Memory
|
||||||
});
|
});
|
||||||
|
|
||||||
assertValidDataSource(memory);
|
assertValidDataSource(memory);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('dataSource.createModel(name, properties, settings)', function() {
|
describe('dataSource.createModel(name, properties, settings)', function() {
|
||||||
it("Define a model and attach it to a `DataSource`", function() {
|
it('Define a model and attach it to a `DataSource`', function() {
|
||||||
var Color = memory.createModel('color', {name: String});
|
var Color = memory.createModel('color', {name: String});
|
||||||
assert.isFunc(Color, 'find');
|
assert.isFunc(Color, 'find');
|
||||||
assert.isFunc(Color, 'findById');
|
assert.isFunc(Color, 'findById');
|
||||||
|
@ -31,23 +31,23 @@ describe('DataSource', function() {
|
||||||
assert.isFunc(Color.prototype, 'destroy');
|
assert.isFunc(Color.prototype, 'destroy');
|
||||||
assert.isFunc(Color.prototype, 'updateAttribute');
|
assert.isFunc(Color.prototype, 'updateAttribute');
|
||||||
assert.isFunc(Color.prototype, 'updateAttributes');
|
assert.isFunc(Color.prototype, 'updateAttributes');
|
||||||
assert.isFunc(Color.prototype, 'reload');
|
assert.isFunc(Color.prototype, 'reload');
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should honor settings.base", function() {
|
it('should honor settings.base', function() {
|
||||||
var Base = memory.createModel('base');
|
var Base = memory.createModel('base');
|
||||||
var Color = memory.createModel('color', {name: String}, {base: Base});
|
var Color = memory.createModel('color', {name: String}, {base: Base});
|
||||||
assert(Color.prototype instanceof Base);
|
assert(Color.prototype instanceof Base);
|
||||||
assert.equal(Color.base, Base);
|
assert.equal(Color.base, Base);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should use loopback.PersistedModel as the base for DBs", function() {
|
it('should use loopback.PersistedModel as the base for DBs', function() {
|
||||||
var Color = memory.createModel('color', {name: String});
|
var Color = memory.createModel('color', {name: String});
|
||||||
assert(Color.prototype instanceof loopback.PersistedModel);
|
assert(Color.prototype instanceof loopback.PersistedModel);
|
||||||
assert.equal(Color.base, loopback.PersistedModel);
|
assert.equal(Color.base, loopback.PersistedModel);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should use loopback.Model as the base for non DBs", function() {
|
it('should use loopback.Model as the base for non DBs', function() {
|
||||||
// Mock up a non-DB connector
|
// Mock up a non-DB connector
|
||||||
var Connector = function() {
|
var Connector = function() {
|
||||||
};
|
};
|
||||||
|
@ -67,10 +67,10 @@ describe('DataSource', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe.skip('PersistedModel Methods', function() {
|
describe.skip('PersistedModel Methods', function() {
|
||||||
it("List the enabled and disabled methods", function() {
|
it('List the enabled and disabled methods', function() {
|
||||||
var TestModel = loopback.PersistedModel.extend('TestPersistedModel');
|
var TestModel = loopback.PersistedModel.extend('TestPersistedModel');
|
||||||
TestModel.attachTo(loopback.memory());
|
TestModel.attachTo(loopback.memory());
|
||||||
|
|
||||||
// assert the defaults
|
// assert the defaults
|
||||||
// - true: the method should be remote enabled
|
// - true: the method should be remote enabled
|
||||||
// - false: the method should not be remote enabled
|
// - false: the method should not be remote enabled
|
||||||
|
@ -97,7 +97,7 @@ describe('DataSource', function() {
|
||||||
existsAndShared('updateAttributes', true);
|
existsAndShared('updateAttributes', true);
|
||||||
existsAndShared('updateAll', true);
|
existsAndShared('updateAll', true);
|
||||||
existsAndShared('reload', false);
|
existsAndShared('reload', false);
|
||||||
|
|
||||||
function existsAndShared(Model, name, isRemoteEnabled, isProto) {
|
function existsAndShared(Model, name, isRemoteEnabled, isProto) {
|
||||||
var scope = isProto ? Model.prototype : Model;
|
var scope = isProto ? Model.prototype : Model;
|
||||||
var fn = scope[name];
|
var fn = scope[name];
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
describe('GeoPoint', function() {
|
describe('GeoPoint', function() {
|
||||||
describe('geoPoint.distanceTo(geoPoint, options)', function() {
|
describe('geoPoint.distanceTo(geoPoint, options)', function() {
|
||||||
it("Get the distance to another `GeoPoint`", function() {
|
it('Get the distance to another `GeoPoint`', function() {
|
||||||
var here = new GeoPoint({lat: 10, lng: 10});
|
var here = new GeoPoint({lat: 10, lng: 10});
|
||||||
var there = new GeoPoint({lat: 5, lng: 5});
|
var there = new GeoPoint({lat: 5, lng: 5});
|
||||||
var distance = here.distanceTo(there, {type: 'meters'});
|
var distance = here.distanceTo(there, {type: 'meters'});
|
||||||
|
@ -10,15 +10,15 @@ describe('GeoPoint', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('GeoPoint.distanceBetween(a, b, options)', function() {
|
describe('GeoPoint.distanceBetween(a, b, options)', function() {
|
||||||
it("Get the distance between two points", function() {
|
it('Get the distance between two points', function() {
|
||||||
var here = new GeoPoint({lat: 10, lng: 10});
|
var here = new GeoPoint({lat: 10, lng: 10});
|
||||||
var there = new GeoPoint({lat: 5, lng: 5});
|
var there = new GeoPoint({lat: 5, lng: 5});
|
||||||
var distance = GeoPoint.distanceBetween(here, there, {type: 'feet'});
|
var distance = GeoPoint.distanceBetween(here, there, {type: 'feet'});
|
||||||
|
|
||||||
assert.equal(Math.floor(distance), 2568169);
|
assert.equal(Math.floor(distance), 2568169);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('GeoPoint()', function(){
|
describe('GeoPoint()', function(){
|
||||||
it('Create from string', function() {
|
it('Create from string', function() {
|
||||||
var point = new GeoPoint('1.234,5.678');
|
var point = new GeoPoint('1.234,5.678');
|
||||||
|
@ -45,11 +45,11 @@ describe('GeoPoint', function() {
|
||||||
var Model = loopback.createModel('geo-model', {
|
var Model = loopback.createModel('geo-model', {
|
||||||
geo: {type: 'GeoPoint'}
|
geo: {type: 'GeoPoint'}
|
||||||
});
|
});
|
||||||
|
|
||||||
var m = new Model({
|
var m = new Model({
|
||||||
geo: '1.222,3.444'
|
geo: '1.222,3.444'
|
||||||
});
|
});
|
||||||
|
|
||||||
assert(m.geo instanceof GeoPoint);
|
assert(m.geo instanceof GeoPoint);
|
||||||
assert.equal(m.geo.lng, 1.222);
|
assert.equal(m.geo.lng, 1.222);
|
||||||
assert.equal(m.geo.lat, 3.444);
|
assert.equal(m.geo.lat, 3.444);
|
||||||
|
|
|
@ -8,15 +8,15 @@ describe('hidden properties', function () {
|
||||||
{hidden: ['secret']}
|
{hidden: ['secret']}
|
||||||
);
|
);
|
||||||
Product.attachTo(loopback.memory());
|
Product.attachTo(loopback.memory());
|
||||||
|
|
||||||
var Category = this.Category = loopback.PersistedModel.extend('category');
|
var Category = this.Category = loopback.PersistedModel.extend('category');
|
||||||
Category.attachTo(loopback.memory());
|
Category.attachTo(loopback.memory());
|
||||||
Category.hasMany(Product);
|
Category.hasMany(Product);
|
||||||
|
|
||||||
app.model(Product);
|
app.model(Product);
|
||||||
app.model(Category);
|
app.model(Category);
|
||||||
app.use(loopback.rest());
|
app.use(loopback.rest());
|
||||||
|
|
||||||
Category.create({
|
Category.create({
|
||||||
name: 'my category'
|
name: 'my category'
|
||||||
}, function(err, category) {
|
}, function(err, category) {
|
||||||
|
@ -32,7 +32,7 @@ describe('hidden properties', function () {
|
||||||
this.Category.destroyAll(function() {
|
this.Category.destroyAll(function() {
|
||||||
Product.destroyAll(done);
|
Product.destroyAll(done);
|
||||||
});
|
});
|
||||||
})
|
});
|
||||||
|
|
||||||
it('should hide a property remotely', function (done) {
|
it('should hide a property remotely', function (done) {
|
||||||
request(this.app)
|
request(this.app)
|
||||||
|
|
|
@ -79,12 +79,12 @@ describe('loopback', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('loopback.remoteMethod(Model, fn, [options]);', function() {
|
describe('loopback.remoteMethod(Model, fn, [options]);', function() {
|
||||||
it("Setup a remote method.", function() {
|
it('Setup a remote method.', function() {
|
||||||
var Product = loopback.createModel('product', {price: Number});
|
var Product = loopback.createModel('product', {price: Number});
|
||||||
|
|
||||||
Product.stats = function(fn) {
|
Product.stats = function(fn) {
|
||||||
// ...
|
// ...
|
||||||
}
|
};
|
||||||
|
|
||||||
loopback.remoteMethod(
|
loopback.remoteMethod(
|
||||||
Product.stats,
|
Product.stats,
|
||||||
|
|
|
@ -15,7 +15,7 @@ describe('Model / PersistedModel', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Model.validatesUniquenessOf(property, options)', function() {
|
describe('Model.validatesUniquenessOf(property, options)', function() {
|
||||||
it("Ensure the value for `property` is unique", function(done) {
|
it('Ensure the value for `property` is unique', function(done) {
|
||||||
var User = PersistedModel.extend('user', {
|
var User = PersistedModel.extend('user', {
|
||||||
'first': String,
|
'first': String,
|
||||||
'last': String,
|
'last': String,
|
||||||
|
@ -31,9 +31,9 @@ describe('Model / PersistedModel', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
User.attachTo(dataSource);
|
User.attachTo(dataSource);
|
||||||
|
|
||||||
User.validatesUniquenessOf('email', {message: 'email is not unique'});
|
User.validatesUniquenessOf('email', {message: 'email is not unique'});
|
||||||
|
|
||||||
var joe = new User({email: 'joe@joe.com'});
|
var joe = new User({email: 'joe@joe.com'});
|
||||||
var joe2 = new User({email: 'joe@joe.com'});
|
var joe2 = new User({email: 'joe@joe.com'});
|
||||||
|
|
||||||
|
@ -41,7 +41,7 @@ describe('Model / PersistedModel', function() {
|
||||||
joe2.save(function (err) {
|
joe2.save(function (err) {
|
||||||
assert(err, 'should get a validation error');
|
assert(err, 'should get a validation error');
|
||||||
assert(joe2.errors.email, 'model should have email error');
|
assert(joe2.errors.email, 'model should have email error');
|
||||||
|
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -49,14 +49,14 @@ describe('Model / PersistedModel', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Model.attachTo(dataSource)', function() {
|
describe('Model.attachTo(dataSource)', function() {
|
||||||
it("Attach a model to a [DataSource](#data-source)", function() {
|
it('Attach a model to a [DataSource](#data-source)', function() {
|
||||||
var MyModel = loopback.createModel('my-model', {name: String});
|
var MyModel = loopback.createModel('my-model', {name: String});
|
||||||
var dataSource = loopback.createDataSource({
|
var dataSource = loopback.createDataSource({
|
||||||
connector: loopback.Memory
|
connector: loopback.Memory
|
||||||
});
|
});
|
||||||
|
|
||||||
MyModel.attachTo(dataSource);
|
MyModel.attachTo(dataSource);
|
||||||
|
|
||||||
MyModel.find(function(err, results) {
|
MyModel.find(function(err, results) {
|
||||||
assert(results.length === 0, 'should have data access methods after attaching to a data source');
|
assert(results.length === 0, 'should have data access methods after attaching to a data source');
|
||||||
});
|
});
|
||||||
|
@ -95,7 +95,7 @@ describe.onServer('Remote Methods', function(){
|
||||||
} else {
|
} else {
|
||||||
throw new Error('bad username and password!');
|
throw new Error('bad username and password!');
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
loopback.remoteMethod(
|
loopback.remoteMethod(
|
||||||
User.login,
|
User.login,
|
||||||
|
@ -113,9 +113,9 @@ describe.onServer('Remote Methods', function(){
|
||||||
app.use(loopback.rest());
|
app.use(loopback.rest());
|
||||||
app.model(User);
|
app.model(User);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Model.destroyAll(callback)', function() {
|
describe('Model.destroyAll(callback)', function() {
|
||||||
it("Delete all Model instances from data source", function(done) {
|
it('Delete all Model instances from data source', function(done) {
|
||||||
(new TaskEmitter())
|
(new TaskEmitter())
|
||||||
.task(User, 'create', {first: 'jill'})
|
.task(User, 'create', {first: 'jill'})
|
||||||
.task(User, 'create', {first: 'bob'})
|
.task(User, 'create', {first: 'bob'})
|
||||||
|
@ -159,12 +159,12 @@ describe.onServer('Remote Methods', function(){
|
||||||
describe('Model.beforeRemote(name, fn)', function(){
|
describe('Model.beforeRemote(name, fn)', function(){
|
||||||
it('Run a function before a remote method is called by a client', function(done) {
|
it('Run a function before a remote method is called by a client', function(done) {
|
||||||
var hookCalled = false;
|
var hookCalled = false;
|
||||||
|
|
||||||
User.beforeRemote('create', function(ctx, user, next) {
|
User.beforeRemote('create', function(ctx, user, next) {
|
||||||
hookCalled = true;
|
hookCalled = true;
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
// invoke save
|
// invoke save
|
||||||
request(app)
|
request(app)
|
||||||
.post('/users')
|
.post('/users')
|
||||||
|
@ -183,7 +183,7 @@ describe.onServer('Remote Methods', function(){
|
||||||
it('Run a function after a remote method is called by a client', function(done) {
|
it('Run a function after a remote method is called by a client', function(done) {
|
||||||
var beforeCalled = false;
|
var beforeCalled = false;
|
||||||
var afterCalled = false;
|
var afterCalled = false;
|
||||||
|
|
||||||
User.beforeRemote('create', function(ctx, user, next) {
|
User.beforeRemote('create', function(ctx, user, next) {
|
||||||
assert(!afterCalled);
|
assert(!afterCalled);
|
||||||
beforeCalled = true;
|
beforeCalled = true;
|
||||||
|
@ -194,7 +194,7 @@ describe.onServer('Remote Methods', function(){
|
||||||
afterCalled = true;
|
afterCalled = true;
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
// invoke save
|
// invoke save
|
||||||
request(app)
|
request(app)
|
||||||
.post('/users')
|
.post('/users')
|
||||||
|
@ -212,9 +212,9 @@ describe.onServer('Remote Methods', function(){
|
||||||
|
|
||||||
describe('Remote Method invoking context', function () {
|
describe('Remote Method invoking context', function () {
|
||||||
describe('ctx.req', function() {
|
describe('ctx.req', function() {
|
||||||
it("The express ServerRequest object", function(done) {
|
it('The express ServerRequest object', function(done) {
|
||||||
var hookCalled = false;
|
var hookCalled = false;
|
||||||
|
|
||||||
User.beforeRemote('create', function(ctx, user, next) {
|
User.beforeRemote('create', function(ctx, user, next) {
|
||||||
hookCalled = true;
|
hookCalled = true;
|
||||||
assert(ctx.req);
|
assert(ctx.req);
|
||||||
|
@ -225,7 +225,7 @@ describe.onServer('Remote Methods', function(){
|
||||||
assert(ctx.res.end);
|
assert(ctx.res.end);
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
// invoke save
|
// invoke save
|
||||||
request(app)
|
request(app)
|
||||||
.post('/users')
|
.post('/users')
|
||||||
|
@ -241,9 +241,9 @@ describe.onServer('Remote Methods', function(){
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('ctx.res', function() {
|
describe('ctx.res', function() {
|
||||||
it("The express ServerResponse object", function(done) {
|
it('The express ServerResponse object', function(done) {
|
||||||
var hookCalled = false;
|
var hookCalled = false;
|
||||||
|
|
||||||
User.beforeRemote('create', function(ctx, user, next) {
|
User.beforeRemote('create', function(ctx, user, next) {
|
||||||
hookCalled = true;
|
hookCalled = true;
|
||||||
assert(ctx.req);
|
assert(ctx.req);
|
||||||
|
@ -254,7 +254,7 @@ describe.onServer('Remote Methods', function(){
|
||||||
assert(ctx.res.end);
|
assert(ctx.res.end);
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
// invoke save
|
// invoke save
|
||||||
request(app)
|
request(app)
|
||||||
.post('/users')
|
.post('/users')
|
||||||
|
@ -268,16 +268,16 @@ describe.onServer('Remote Methods', function(){
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
})
|
});
|
||||||
|
|
||||||
describe('Model.hasMany(Model)', function() {
|
describe('Model.hasMany(Model)', function() {
|
||||||
it("Define a one to many relationship", function(done) {
|
it('Define a one to many relationship', function(done) {
|
||||||
var Book = dataSource.createModel('book', {title: String, author: String});
|
var Book = dataSource.createModel('book', {title: String, author: String});
|
||||||
var Chapter = dataSource.createModel('chapter', {title: String});
|
var Chapter = dataSource.createModel('chapter', {title: String});
|
||||||
|
|
||||||
// by referencing model
|
// by referencing model
|
||||||
Book.hasMany(Chapter);
|
Book.hasMany(Chapter);
|
||||||
|
|
||||||
Book.create({title: 'Into the Wild', author: 'Jon Krakauer'}, function(err, book) {
|
Book.create({title: 'Into the Wild', author: 'Jon Krakauer'}, function(err, book) {
|
||||||
// using 'chapters' scope for build:
|
// using 'chapters' scope for build:
|
||||||
var c = book.chapters.build({title: 'Chapter 1'});
|
var c = book.chapters.build({title: 'Chapter 1'});
|
||||||
|
@ -296,7 +296,7 @@ describe.onServer('Remote Methods', function(){
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Model.properties', function(){
|
describe('Model.properties', function(){
|
||||||
it('Normalized properties passed in originally by loopback.createModel()', function() {
|
it('Normalized properties passed in originally by loopback.createModel()', function() {
|
||||||
var props = {
|
var props = {
|
||||||
|
@ -306,16 +306,16 @@ describe.onServer('Remote Methods', function(){
|
||||||
d: Date,
|
d: Date,
|
||||||
g: loopback.GeoPoint
|
g: loopback.GeoPoint
|
||||||
};
|
};
|
||||||
|
|
||||||
var MyModel = loopback.createModel('foo', props);
|
var MyModel = loopback.createModel('foo', props);
|
||||||
|
|
||||||
Object.keys(MyModel.definition.properties).forEach(function (key) {
|
Object.keys(MyModel.definition.properties).forEach(function (key) {
|
||||||
var p = MyModel.definition.properties[key];
|
var p = MyModel.definition.properties[key];
|
||||||
var o = MyModel.definition.properties[key];
|
var o = MyModel.definition.properties[key];
|
||||||
assert(p);
|
assert(p);
|
||||||
assert(o);
|
assert(o);
|
||||||
assert(typeof p.type === 'function');
|
assert(typeof p.type === 'function');
|
||||||
|
|
||||||
if(typeof o === 'function') {
|
if(typeof o === 'function') {
|
||||||
// the normalized property
|
// the normalized property
|
||||||
// should match the given property
|
// should match the given property
|
||||||
|
@ -323,40 +323,40 @@ describe.onServer('Remote Methods', function(){
|
||||||
p.type.name === o.name
|
p.type.name === o.name
|
||||||
||
|
||
|
||||||
p.type.name === o
|
p.type.name === o
|
||||||
)
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Model.extend()', function(){
|
describe('Model.extend()', function(){
|
||||||
it('Create a new model by extending an existing model', function() {
|
it('Create a new model by extending an existing model', function() {
|
||||||
var User = loopback.PersistedModel.extend('test-user', {
|
var User = loopback.PersistedModel.extend('test-user', {
|
||||||
email: String
|
email: String
|
||||||
});
|
});
|
||||||
|
|
||||||
User.foo = function () {
|
User.foo = function () {
|
||||||
return 'bar';
|
return 'bar';
|
||||||
}
|
};
|
||||||
|
|
||||||
User.prototype.bar = function () {
|
User.prototype.bar = function () {
|
||||||
return 'foo';
|
return 'foo';
|
||||||
}
|
};
|
||||||
|
|
||||||
var MyUser = User.extend('my-user', {
|
var MyUser = User.extend('my-user', {
|
||||||
a: String,
|
a: String,
|
||||||
b: String
|
b: String
|
||||||
});
|
});
|
||||||
|
|
||||||
assert.equal(MyUser.prototype.bar, User.prototype.bar);
|
assert.equal(MyUser.prototype.bar, User.prototype.bar);
|
||||||
assert.equal(MyUser.foo, User.foo);
|
assert.equal(MyUser.foo, User.foo);
|
||||||
|
|
||||||
var user = new MyUser({
|
var user = new MyUser({
|
||||||
email: 'foo@bar.com',
|
email: 'foo@bar.com',
|
||||||
a: 'foo',
|
a: 'foo',
|
||||||
b: 'bar'
|
b: 'bar'
|
||||||
});
|
});
|
||||||
|
|
||||||
assert.equal(user.email, 'foo@bar.com');
|
assert.equal(user.email, 'foo@bar.com');
|
||||||
assert.equal(user.a, 'foo');
|
assert.equal(user.a, 'foo');
|
||||||
assert.equal(user.b, 'bar');
|
assert.equal(user.b, 'bar');
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
/*jshint -W030 */
|
||||||
|
|
||||||
var loopback = require('../');
|
var loopback = require('../');
|
||||||
var lt = require('loopback-testing');
|
var lt = require('loopback-testing');
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
|
@ -53,7 +55,7 @@ describe('relations - integration', function () {
|
||||||
this.res.text);
|
this.res.text);
|
||||||
this.widgets = this.res.body;
|
this.widgets = this.res.body;
|
||||||
this.widget = this.res.body && this.res.body[0];
|
this.widget = this.res.body && this.res.body[0];
|
||||||
});
|
});
|
||||||
it('should be an array', function() {
|
it('should be an array', function() {
|
||||||
assert(Array.isArray(this.widgets));
|
assert(Array.isArray(this.widgets));
|
||||||
});
|
});
|
||||||
|
@ -91,7 +93,7 @@ describe('relations - integration', function () {
|
||||||
describe('widget (response.body)', function() {
|
describe('widget (response.body)', function() {
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
this.widget = this.res.body;
|
this.widget = this.res.body;
|
||||||
});
|
});
|
||||||
it('should be an object', function() {
|
it('should be an object', function() {
|
||||||
assert(typeof this.widget === 'object');
|
assert(typeof this.widget === 'object');
|
||||||
assert(!Array.isArray(this.widget));
|
assert(!Array.isArray(this.widget));
|
||||||
|
@ -113,7 +115,7 @@ describe('relations - integration', function () {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('/stores/:id/widgets/:fk - 200', function () {
|
describe('/stores/:id/widgets/:fk - 200', function () {
|
||||||
beforeEach(function (done) {
|
beforeEach(function (done) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
@ -144,7 +146,7 @@ describe('relations - integration', function () {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('/store/:id/widgets/count', function () {
|
describe('/store/:id/widgets/count', function () {
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
this.url = '/api/stores/' + this.store.id + '/widgets/count';
|
this.url = '/api/stores/' + this.store.id + '/widgets/count';
|
||||||
|
@ -158,7 +160,7 @@ describe('relations - integration', function () {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('/store/:id/widgets/count - filtered (matches)', function () {
|
describe('/store/:id/widgets/count - filtered (matches)', function () {
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
this.url = '/api/stores/' + this.store.id + '/widgets/count?where[name]=foo';
|
this.url = '/api/stores/' + this.store.id + '/widgets/count?where[name]=foo';
|
||||||
|
@ -172,7 +174,7 @@ describe('relations - integration', function () {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('/store/:id/widgets/count - filtered (no matches)', function () {
|
describe('/store/:id/widgets/count - filtered (no matches)', function () {
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
this.url = '/api/stores/' + this.store.id + '/widgets/count?where[name]=bar';
|
this.url = '/api/stores/' + this.store.id + '/widgets/count?where[name]=bar';
|
||||||
|
@ -612,13 +614,13 @@ describe('relations - integration', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('embedsOne', function() {
|
describe('embedsOne', function() {
|
||||||
|
|
||||||
before(function defineGroupAndPosterModels() {
|
before(function defineGroupAndPosterModels() {
|
||||||
var group = app.model(
|
var group = app.model(
|
||||||
'group',
|
'group',
|
||||||
{ properties: { name: 'string' },
|
{ properties: { name: 'string' },
|
||||||
dataSource: 'db',
|
dataSource: 'db',
|
||||||
plural: 'groups'
|
plural: 'groups'
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
var poster = app.model(
|
var poster = app.model(
|
||||||
|
@ -627,7 +629,7 @@ describe('relations - integration', function () {
|
||||||
);
|
);
|
||||||
group.embedsOne(poster, { as: 'cover' });
|
group.embedsOne(poster, { as: 'cover' });
|
||||||
});
|
});
|
||||||
|
|
||||||
before(function createImage(done) {
|
before(function createImage(done) {
|
||||||
var test = this;
|
var test = this;
|
||||||
app.models.group.create({ name: 'Group 1' },
|
app.models.group.create({ name: 'Group 1' },
|
||||||
|
@ -638,46 +640,46 @@ describe('relations - integration', function () {
|
||||||
group.save(done);
|
group.save(done);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
after(function(done) {
|
after(function(done) {
|
||||||
this.app.models.group.destroyAll(done);
|
this.app.models.group.destroyAll(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('includes the embedded models', function(done) {
|
it('includes the embedded models', function(done) {
|
||||||
var url = '/api/groups/' + this.group.id;
|
var url = '/api/groups/' + this.group.id;
|
||||||
|
|
||||||
this.get(url)
|
this.get(url)
|
||||||
.expect(200, function(err, res) {
|
.expect(200, function(err, res) {
|
||||||
expect(res.body.name).to.be.equal('Group 1');
|
expect(res.body.name).to.be.equal('Group 1');
|
||||||
expect(res.body.poster).to.be.eql(
|
expect(res.body.poster).to.be.eql(
|
||||||
{ url: 'http://image.url' }
|
{ url: 'http://image.url' }
|
||||||
);
|
);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns the embedded model', function(done) {
|
it('returns the embedded model', function(done) {
|
||||||
var url = '/api/groups/' + this.group.id + '/cover';
|
var url = '/api/groups/' + this.group.id + '/cover';
|
||||||
|
|
||||||
this.get(url)
|
this.get(url)
|
||||||
.expect(200, function(err, res) {
|
.expect(200, function(err, res) {
|
||||||
expect(res.body).to.be.eql(
|
expect(res.body).to.be.eql(
|
||||||
{ url: 'http://image.url' }
|
{ url: 'http://image.url' }
|
||||||
);
|
);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('embedsMany', function() {
|
describe('embedsMany', function() {
|
||||||
|
|
||||||
before(function defineProductAndCategoryModels() {
|
before(function defineProductAndCategoryModels() {
|
||||||
var todoList = app.model(
|
var todoList = app.model(
|
||||||
'todoList',
|
'todoList',
|
||||||
{ properties: { name: 'string' },
|
{ properties: { name: 'string' },
|
||||||
dataSource: 'db',
|
dataSource: 'db',
|
||||||
plural: 'todo-lists'
|
plural: 'todo-lists'
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
var todoItem = app.model(
|
var todoItem = app.model(
|
||||||
|
@ -702,52 +704,52 @@ describe('relations - integration', function () {
|
||||||
after(function(done) {
|
after(function(done) {
|
||||||
this.app.models.todoList.destroyAll(done);
|
this.app.models.todoList.destroyAll(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('includes the embedded models', function(done) {
|
it('includes the embedded models', function(done) {
|
||||||
var url = '/api/todo-lists/' + this.todoList.id;
|
var url = '/api/todo-lists/' + this.todoList.id;
|
||||||
|
|
||||||
this.get(url)
|
this.get(url)
|
||||||
.expect(200, function(err, res) {
|
.expect(200, function(err, res) {
|
||||||
expect(res.body.name).to.be.equal('List A');
|
expect(res.body.name).to.be.equal('List A');
|
||||||
expect(res.body.todoItems).to.be.eql([
|
expect(res.body.todoItems).to.be.eql([
|
||||||
{ content: 'Todo 1', id: 1 },
|
{ content: 'Todo 1', id: 1 },
|
||||||
{ content: 'Todo 2', id: 2 }
|
{ content: 'Todo 2', id: 2 }
|
||||||
]);
|
]);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns the embedded models', function(done) {
|
it('returns the embedded models', function(done) {
|
||||||
var url = '/api/todo-lists/' + this.todoList.id + '/items';
|
var url = '/api/todo-lists/' + this.todoList.id + '/items';
|
||||||
|
|
||||||
this.get(url)
|
this.get(url)
|
||||||
.expect(200, function(err, res) {
|
.expect(200, function(err, res) {
|
||||||
expect(res.body).to.be.eql([
|
expect(res.body).to.be.eql([
|
||||||
{ content: 'Todo 1', id: 1 },
|
{ content: 'Todo 1', id: 1 },
|
||||||
{ content: 'Todo 2', id: 2 }
|
{ content: 'Todo 2', id: 2 }
|
||||||
]);
|
]);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('filters the embedded models', function(done) {
|
it('filters the embedded models', function(done) {
|
||||||
var url = '/api/todo-lists/' + this.todoList.id + '/items';
|
var url = '/api/todo-lists/' + this.todoList.id + '/items';
|
||||||
url += '?filter[where][id]=2';
|
url += '?filter[where][id]=2';
|
||||||
|
|
||||||
this.get(url)
|
this.get(url)
|
||||||
.expect(200, function(err, res) {
|
.expect(200, function(err, res) {
|
||||||
expect(res.body).to.be.eql([
|
expect(res.body).to.be.eql([
|
||||||
{ content: 'Todo 2', id: 2 }
|
{ content: 'Todo 2', id: 2 }
|
||||||
]);
|
]);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('creates embedded models', function(done) {
|
it('creates embedded models', function(done) {
|
||||||
var url = '/api/todo-lists/' + this.todoList.id + '/items';
|
var url = '/api/todo-lists/' + this.todoList.id + '/items';
|
||||||
|
|
||||||
var expected = { content: 'Todo 3', id: 3 };
|
var expected = { content: 'Todo 3', id: 3 };
|
||||||
|
|
||||||
this.post(url)
|
this.post(url)
|
||||||
.send({ content: 'Todo 3' })
|
.send({ content: 'Todo 3' })
|
||||||
.expect(200, function(err, res) {
|
.expect(200, function(err, res) {
|
||||||
|
@ -755,13 +757,13 @@ describe('relations - integration', function () {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns the embedded models', function(done) {
|
it('returns the embedded models', function(done) {
|
||||||
var url = '/api/todo-lists/' + this.todoList.id + '/items';
|
var url = '/api/todo-lists/' + this.todoList.id + '/items';
|
||||||
|
|
||||||
this.get(url)
|
this.get(url)
|
||||||
.expect(200, function(err, res) {
|
.expect(200, function(err, res) {
|
||||||
expect(res.body).to.be.eql([
|
expect(res.body).to.be.eql([
|
||||||
{ content: 'Todo 1', id: 1 },
|
{ content: 'Todo 1', id: 1 },
|
||||||
{ content: 'Todo 2', id: 2 },
|
{ content: 'Todo 2', id: 2 },
|
||||||
{ content: 'Todo 3', id: 3 }
|
{ content: 'Todo 3', id: 3 }
|
||||||
|
@ -769,10 +771,10 @@ describe('relations - integration', function () {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns an embedded model by (internal) id', function(done) {
|
it('returns an embedded model by (internal) id', function(done) {
|
||||||
var url = '/api/todo-lists/' + this.todoList.id + '/items/3';
|
var url = '/api/todo-lists/' + this.todoList.id + '/items/3';
|
||||||
|
|
||||||
this.get(url)
|
this.get(url)
|
||||||
.expect(200, function(err, res) {
|
.expect(200, function(err, res) {
|
||||||
expect(res.body).to.be.eql(
|
expect(res.body).to.be.eql(
|
||||||
|
@ -781,30 +783,30 @@ describe('relations - integration', function () {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('removes an embedded model', function(done) {
|
it('removes an embedded model', function(done) {
|
||||||
var expectedProduct = this.product;
|
var expectedProduct = this.product;
|
||||||
var url = '/api/todo-lists/' + this.todoList.id + '/items/2';
|
var url = '/api/todo-lists/' + this.todoList.id + '/items/2';
|
||||||
|
|
||||||
this.del(url)
|
this.del(url)
|
||||||
.expect(200, function(err, res) {
|
.expect(200, function(err, res) {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns the embedded models - verify', function(done) {
|
it('returns the embedded models - verify', function(done) {
|
||||||
var url = '/api/todo-lists/' + this.todoList.id + '/items';
|
var url = '/api/todo-lists/' + this.todoList.id + '/items';
|
||||||
|
|
||||||
this.get(url)
|
this.get(url)
|
||||||
.expect(200, function(err, res) {
|
.expect(200, function(err, res) {
|
||||||
expect(res.body).to.be.eql([
|
expect(res.body).to.be.eql([
|
||||||
{ content: 'Todo 1', id: 1 },
|
{ content: 'Todo 1', id: 1 },
|
||||||
{ content: 'Todo 3', id: 3 }
|
{ content: 'Todo 3', id: 3 }
|
||||||
]);
|
]);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns a 404 response when embedded model is not found', function(done) {
|
it('returns a 404 response when embedded model is not found', function(done) {
|
||||||
var url = '/api/todo-lists/' + this.todoList.id + '/items/2';
|
var url = '/api/todo-lists/' + this.todoList.id + '/items/2';
|
||||||
this.get(url).expect(404, function(err, res) {
|
this.get(url).expect(404, function(err, res) {
|
||||||
|
@ -813,29 +815,29 @@ describe('relations - integration', function () {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it.skip('checks if an embedded model exists - ok', function(done) {
|
it.skip('checks if an embedded model exists - ok', function(done) {
|
||||||
var url = '/api/todo-lists/' + this.todoList.id + '/items/3';
|
var url = '/api/todo-lists/' + this.todoList.id + '/items/3';
|
||||||
|
|
||||||
this.head(url)
|
this.head(url)
|
||||||
.expect(200, function(err, res) {
|
.expect(200, function(err, res) {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it.skip('checks if an embedded model exists - fail', function(done) {
|
it.skip('checks if an embedded model exists - fail', function(done) {
|
||||||
var url = '/api/todo-lists/' + this.todoList.id + '/items/2';
|
var url = '/api/todo-lists/' + this.todoList.id + '/items/2';
|
||||||
|
|
||||||
this.head(url)
|
this.head(url)
|
||||||
.expect(404, function(err, res) {
|
.expect(404, function(err, res) {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('referencesMany', function() {
|
describe('referencesMany', function() {
|
||||||
|
|
||||||
before(function defineProductAndCategoryModels() {
|
before(function defineProductAndCategoryModels() {
|
||||||
var recipe = app.model(
|
var recipe = app.model(
|
||||||
'recipe',
|
'recipe',
|
||||||
|
@ -851,8 +853,8 @@ describe('relations - integration', function () {
|
||||||
);
|
);
|
||||||
recipe.referencesMany(ingredient);
|
recipe.referencesMany(ingredient);
|
||||||
// contrived example for test:
|
// contrived example for test:
|
||||||
recipe.hasOne(photo, { as: 'picture', options: {
|
recipe.hasOne(photo, { as: 'picture', options: {
|
||||||
http: { path: 'image' }
|
http: { path: 'image' }
|
||||||
} });
|
} });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -862,15 +864,15 @@ describe('relations - integration', function () {
|
||||||
function(err, recipe) {
|
function(err, recipe) {
|
||||||
if (err) return done(err);
|
if (err) return done(err);
|
||||||
test.recipe = recipe;
|
test.recipe = recipe;
|
||||||
recipe.ingredients.create({
|
recipe.ingredients.create({
|
||||||
name: 'Chocolate' },
|
name: 'Chocolate' },
|
||||||
function(err, ing) {
|
function(err, ing) {
|
||||||
test.ingredient1 = ing.id;
|
test.ingredient1 = ing.id;
|
||||||
recipe.picture.create({ name: 'Photo 1' }, done);
|
recipe.picture.create({ name: 'Photo 1' }, done);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
before(function createIngredient(done) {
|
before(function createIngredient(done) {
|
||||||
var test = this;
|
var test = this;
|
||||||
app.models.ingredient.create({ name: 'Sugar' }, function(err, ing) {
|
app.models.ingredient.create({ name: 'Sugar' }, function(err, ing) {
|
||||||
|
@ -887,11 +889,11 @@ describe('relations - integration', function () {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('keeps an array of ids', function(done) {
|
it('keeps an array of ids', function(done) {
|
||||||
var url = '/api/recipes/' + this.recipe.id;
|
var url = '/api/recipes/' + this.recipe.id;
|
||||||
var test = this;
|
var test = this;
|
||||||
|
|
||||||
this.get(url)
|
this.get(url)
|
||||||
.expect(200, function(err, res) {
|
.expect(200, function(err, res) {
|
||||||
expect(res.body.ingredientIds).to.eql([test.ingredient1]);
|
expect(res.body.ingredientIds).to.eql([test.ingredient1]);
|
||||||
|
@ -899,11 +901,11 @@ describe('relations - integration', function () {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('creates referenced models', function(done) {
|
it('creates referenced models', function(done) {
|
||||||
var url = '/api/recipes/' + this.recipe.id + '/ingredients';
|
var url = '/api/recipes/' + this.recipe.id + '/ingredients';
|
||||||
var test = this;
|
var test = this;
|
||||||
|
|
||||||
this.post(url)
|
this.post(url)
|
||||||
.send({ name: 'Butter' })
|
.send({ name: 'Butter' })
|
||||||
.expect(200, function(err, res) {
|
.expect(200, function(err, res) {
|
||||||
|
@ -912,14 +914,14 @@ describe('relations - integration', function () {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('has created models', function(done) {
|
it('has created models', function(done) {
|
||||||
var url = '/api/ingredients';
|
var url = '/api/ingredients';
|
||||||
var test = this;
|
var test = this;
|
||||||
|
|
||||||
this.get(url)
|
this.get(url)
|
||||||
.expect(200, function(err, res) {
|
.expect(200, function(err, res) {
|
||||||
expect(res.body).to.be.eql([
|
expect(res.body).to.be.eql([
|
||||||
{ name: 'Chocolate', id: test.ingredient1 },
|
{ name: 'Chocolate', id: test.ingredient1 },
|
||||||
{ name: 'Sugar', id: test.ingredient2 },
|
{ name: 'Sugar', id: test.ingredient2 },
|
||||||
{ name: 'Butter', id: test.ingredient3 }
|
{ name: 'Butter', id: test.ingredient3 }
|
||||||
|
@ -927,40 +929,40 @@ describe('relations - integration', function () {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns the referenced models', function(done) {
|
it('returns the referenced models', function(done) {
|
||||||
var url = '/api/recipes/' + this.recipe.id + '/ingredients';
|
var url = '/api/recipes/' + this.recipe.id + '/ingredients';
|
||||||
var test = this;
|
var test = this;
|
||||||
|
|
||||||
this.get(url)
|
this.get(url)
|
||||||
.expect(200, function(err, res) {
|
.expect(200, function(err, res) {
|
||||||
expect(res.body).to.be.eql([
|
expect(res.body).to.be.eql([
|
||||||
{ name: 'Chocolate', id: test.ingredient1 },
|
{ name: 'Chocolate', id: test.ingredient1 },
|
||||||
{ name: 'Butter', id: test.ingredient3 }
|
{ name: 'Butter', id: test.ingredient3 }
|
||||||
]);
|
]);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('filters the referenced models', function(done) {
|
it('filters the referenced models', function(done) {
|
||||||
var url = '/api/recipes/' + this.recipe.id + '/ingredients';
|
var url = '/api/recipes/' + this.recipe.id + '/ingredients';
|
||||||
url += '?filter[where][name]=Butter';
|
url += '?filter[where][name]=Butter';
|
||||||
var test = this;
|
var test = this;
|
||||||
|
|
||||||
this.get(url)
|
this.get(url)
|
||||||
.expect(200, function(err, res) {
|
.expect(200, function(err, res) {
|
||||||
expect(res.body).to.be.eql([
|
expect(res.body).to.be.eql([
|
||||||
{ name: 'Butter', id: test.ingredient3 }
|
{ name: 'Butter', id: test.ingredient3 }
|
||||||
]);
|
]);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('includes the referenced models', function(done) {
|
it('includes the referenced models', function(done) {
|
||||||
var url = '/api/recipes/findOne?filter[where][id]=' + this.recipe.id;
|
var url = '/api/recipes/findOne?filter[where][id]=' + this.recipe.id;
|
||||||
url += '&filter[include]=ingredients';
|
url += '&filter[include]=ingredients';
|
||||||
var test = this;
|
var test = this;
|
||||||
|
|
||||||
this.get(url)
|
this.get(url)
|
||||||
.expect(200, function(err, res) {
|
.expect(200, function(err, res) {
|
||||||
expect(res.body.ingredientIds).to.eql([
|
expect(res.body.ingredientIds).to.eql([
|
||||||
|
@ -978,7 +980,7 @@ describe('relations - integration', function () {
|
||||||
var url = '/api/recipes/' + this.recipe.id + '/ingredients/';
|
var url = '/api/recipes/' + this.recipe.id + '/ingredients/';
|
||||||
url += this.ingredient3;
|
url += this.ingredient3;
|
||||||
var test = this;
|
var test = this;
|
||||||
|
|
||||||
this.get(url)
|
this.get(url)
|
||||||
.expect(200, function(err, res) {
|
.expect(200, function(err, res) {
|
||||||
expect(res.body).to.be.eql(
|
expect(res.body).to.be.eql(
|
||||||
|
@ -987,13 +989,13 @@ describe('relations - integration', function () {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('keeps an array of ids - verify', function(done) {
|
it('keeps an array of ids - verify', function(done) {
|
||||||
var url = '/api/recipes/' + this.recipe.id;
|
var url = '/api/recipes/' + this.recipe.id;
|
||||||
var test = this;
|
var test = this;
|
||||||
|
|
||||||
var expected = [test.ingredient1, test.ingredient3];
|
var expected = [test.ingredient1, test.ingredient3];
|
||||||
|
|
||||||
this.get(url)
|
this.get(url)
|
||||||
.expect(200, function(err, res) {
|
.expect(200, function(err, res) {
|
||||||
expect(res.body.ingredientIds).to.eql(expected);
|
expect(res.body.ingredientIds).to.eql(expected);
|
||||||
|
@ -1001,50 +1003,50 @@ describe('relations - integration', function () {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('destroys a referenced model', function(done) {
|
it('destroys a referenced model', function(done) {
|
||||||
var expectedProduct = this.product;
|
var expectedProduct = this.product;
|
||||||
var url = '/api/recipes/' + this.recipe.id + '/ingredients/';
|
var url = '/api/recipes/' + this.recipe.id + '/ingredients/';
|
||||||
url += this.ingredient3;
|
url += this.ingredient3;
|
||||||
|
|
||||||
this.del(url)
|
this.del(url)
|
||||||
.expect(200, function(err, res) {
|
.expect(200, function(err, res) {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('has destroyed a referenced model', function(done) {
|
it('has destroyed a referenced model', function(done) {
|
||||||
var url = '/api/ingredients';
|
var url = '/api/ingredients';
|
||||||
var test = this;
|
var test = this;
|
||||||
|
|
||||||
this.get(url)
|
this.get(url)
|
||||||
.expect(200, function(err, res) {
|
.expect(200, function(err, res) {
|
||||||
expect(res.body).to.be.eql([
|
expect(res.body).to.be.eql([
|
||||||
{ name: 'Chocolate', id: test.ingredient1 },
|
{ name: 'Chocolate', id: test.ingredient1 },
|
||||||
{ name: 'Sugar', id: test.ingredient2 }
|
{ name: 'Sugar', id: test.ingredient2 }
|
||||||
]);
|
]);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns the referenced models - verify', function(done) {
|
it('returns the referenced models - verify', function(done) {
|
||||||
var url = '/api/recipes/' + this.recipe.id + '/ingredients';
|
var url = '/api/recipes/' + this.recipe.id + '/ingredients';
|
||||||
var test = this;
|
var test = this;
|
||||||
|
|
||||||
this.get(url)
|
this.get(url)
|
||||||
.expect(200, function(err, res) {
|
.expect(200, function(err, res) {
|
||||||
expect(res.body).to.be.eql([
|
expect(res.body).to.be.eql([
|
||||||
{ name: 'Chocolate', id: test.ingredient1 }
|
{ name: 'Chocolate', id: test.ingredient1 }
|
||||||
]);
|
]);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('creates/links a reference by id', function(done) {
|
it('creates/links a reference by id', function(done) {
|
||||||
var url = '/api/recipes/' + this.recipe.id + '/ingredients';
|
var url = '/api/recipes/' + this.recipe.id + '/ingredients';
|
||||||
url += '/rel/' + this.ingredient2;
|
url += '/rel/' + this.ingredient2;
|
||||||
var test = this;
|
var test = this;
|
||||||
|
|
||||||
this.put(url)
|
this.put(url)
|
||||||
.expect(200, function(err, res) {
|
.expect(200, function(err, res) {
|
||||||
expect(res.body).to.be.eql(
|
expect(res.body).to.be.eql(
|
||||||
|
@ -1053,62 +1055,62 @@ describe('relations - integration', function () {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns the referenced models - verify', function(done) {
|
it('returns the referenced models - verify', function(done) {
|
||||||
var url = '/api/recipes/' + this.recipe.id + '/ingredients';
|
var url = '/api/recipes/' + this.recipe.id + '/ingredients';
|
||||||
var test = this;
|
var test = this;
|
||||||
|
|
||||||
this.get(url)
|
this.get(url)
|
||||||
.expect(200, function(err, res) {
|
.expect(200, function(err, res) {
|
||||||
expect(res.body).to.be.eql([
|
expect(res.body).to.be.eql([
|
||||||
{ name: 'Chocolate', id: test.ingredient1 },
|
{ name: 'Chocolate', id: test.ingredient1 },
|
||||||
{ name: 'Sugar', id: test.ingredient2 }
|
{ name: 'Sugar', id: test.ingredient2 }
|
||||||
]);
|
]);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('removes/unlinks a reference by id', function(done) {
|
it('removes/unlinks a reference by id', function(done) {
|
||||||
var url = '/api/recipes/' + this.recipe.id + '/ingredients';
|
var url = '/api/recipes/' + this.recipe.id + '/ingredients';
|
||||||
url += '/rel/' + this.ingredient1;
|
url += '/rel/' + this.ingredient1;
|
||||||
var test = this;
|
var test = this;
|
||||||
|
|
||||||
this.del(url)
|
this.del(url)
|
||||||
.expect(200, function(err, res) {
|
.expect(200, function(err, res) {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns the referenced models - verify', function(done) {
|
it('returns the referenced models - verify', function(done) {
|
||||||
var url = '/api/recipes/' + this.recipe.id + '/ingredients';
|
var url = '/api/recipes/' + this.recipe.id + '/ingredients';
|
||||||
var test = this;
|
var test = this;
|
||||||
|
|
||||||
this.get(url)
|
this.get(url)
|
||||||
.expect(200, function(err, res) {
|
.expect(200, function(err, res) {
|
||||||
expect(res.body).to.be.eql([
|
expect(res.body).to.be.eql([
|
||||||
{ name: 'Sugar', id: test.ingredient2 }
|
{ name: 'Sugar', id: test.ingredient2 }
|
||||||
]);
|
]);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('has not destroyed an unlinked model', function(done) {
|
it('has not destroyed an unlinked model', function(done) {
|
||||||
var url = '/api/ingredients';
|
var url = '/api/ingredients';
|
||||||
var test = this;
|
var test = this;
|
||||||
|
|
||||||
this.get(url)
|
this.get(url)
|
||||||
.expect(200, function(err, res) {
|
.expect(200, function(err, res) {
|
||||||
expect(res.body).to.be.eql([
|
expect(res.body).to.be.eql([
|
||||||
{ name: 'Chocolate', id: test.ingredient1 },
|
{ name: 'Chocolate', id: test.ingredient1 },
|
||||||
{ name: 'Sugar', id: test.ingredient2 }
|
{ name: 'Sugar', id: test.ingredient2 }
|
||||||
]);
|
]);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('uses a custom relation path', function(done) {
|
it('uses a custom relation path', function(done) {
|
||||||
var url = '/api/recipes/' + this.recipe.id + '/image';
|
var url = '/api/recipes/' + this.recipe.id + '/image';
|
||||||
|
|
||||||
this.get(url)
|
this.get(url)
|
||||||
.expect(200, function(err, res) {
|
.expect(200, function(err, res) {
|
||||||
expect(err).to.not.exist;
|
expect(err).to.not.exist;
|
||||||
|
@ -1116,31 +1118,31 @@ describe('relations - integration', function () {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it.skip('checks if a referenced model exists - ok', function(done) {
|
it.skip('checks if a referenced model exists - ok', function(done) {
|
||||||
var url = '/api/recipes/' + this.recipe.id + '/ingredients/';
|
var url = '/api/recipes/' + this.recipe.id + '/ingredients/';
|
||||||
url += this.ingredient1;
|
url += this.ingredient1;
|
||||||
|
|
||||||
this.head(url)
|
this.head(url)
|
||||||
.expect(200, function(err, res) {
|
.expect(200, function(err, res) {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it.skip('checks if an referenced model exists - fail', function(done) {
|
it.skip('checks if an referenced model exists - fail', function(done) {
|
||||||
var url = '/api/recipes/' + this.recipe.id + '/ingredients/';
|
var url = '/api/recipes/' + this.recipe.id + '/ingredients/';
|
||||||
url += this.ingredient3;
|
url += this.ingredient3;
|
||||||
|
|
||||||
this.head(url)
|
this.head(url)
|
||||||
.expect(404, function(err, res) {
|
.expect(404, function(err, res) {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('nested relations', function() {
|
describe('nested relations', function() {
|
||||||
|
|
||||||
before(function defineModels() {
|
before(function defineModels() {
|
||||||
var Book = app.model(
|
var Book = app.model(
|
||||||
'Book',
|
'Book',
|
||||||
|
@ -1165,36 +1167,36 @@ describe('relations - integration', function () {
|
||||||
Book.hasMany(Page);
|
Book.hasMany(Page);
|
||||||
Page.hasMany(Note);
|
Page.hasMany(Note);
|
||||||
Image.belongsTo(Book);
|
Image.belongsTo(Book);
|
||||||
|
|
||||||
Book.nestRemoting('pages');
|
Book.nestRemoting('pages');
|
||||||
Image.nestRemoting('book');
|
Image.nestRemoting('book');
|
||||||
|
|
||||||
expect(Book.prototype['__findById__pages__notes']).to.be.a.function;
|
expect(Book.prototype['__findById__pages__notes']).to.be.a.function;
|
||||||
expect(Image.prototype['__findById__book__pages']).to.be.a.function;
|
expect(Image.prototype['__findById__book__pages']).to.be.a.function;
|
||||||
|
|
||||||
Page.beforeRemote('prototype.__findById__notes', function(ctx, result, next) {
|
Page.beforeRemote('prototype.__findById__notes', function(ctx, result, next) {
|
||||||
ctx.res.set('x-before', 'before');
|
ctx.res.set('x-before', 'before');
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
Page.afterRemote('prototype.__findById__notes', function(ctx, result, next) {
|
Page.afterRemote('prototype.__findById__notes', function(ctx, result, next) {
|
||||||
ctx.res.set('x-after', 'after');
|
ctx.res.set('x-after', 'after');
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
before(function createBook(done) {
|
before(function createBook(done) {
|
||||||
var test = this;
|
var test = this;
|
||||||
app.models.Book.create({ name: 'Book 1' },
|
app.models.Book.create({ name: 'Book 1' },
|
||||||
function(err, book) {
|
function(err, book) {
|
||||||
if (err) return done(err);
|
if (err) return done(err);
|
||||||
test.book = book;
|
test.book = book;
|
||||||
book.pages.create({ name: 'Page 1' },
|
book.pages.create({ name: 'Page 1' },
|
||||||
function(err, page) {
|
function(err, page) {
|
||||||
if (err) return done(err);
|
if (err) return done(err);
|
||||||
test.page = page;
|
test.page = page;
|
||||||
page.notes.create({ text: 'Page Note 1' },
|
page.notes.create({ text: 'Page Note 1' },
|
||||||
function(err, note) {
|
function(err, note) {
|
||||||
test.note = note;
|
test.note = note;
|
||||||
done();
|
done();
|
||||||
|
@ -1202,17 +1204,17 @@ describe('relations - integration', function () {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
before(function createCover(done) {
|
before(function createCover(done) {
|
||||||
var test = this;
|
var test = this;
|
||||||
app.models.Image.create({ name: 'Cover 1', book: test.book },
|
app.models.Image.create({ name: 'Cover 1', book: test.book },
|
||||||
function(err, image) {
|
function(err, image) {
|
||||||
if (err) return done(err);
|
if (err) return done(err);
|
||||||
test.image = image;
|
test.image = image;
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('has regular relationship routes - pages', function(done) {
|
it('has regular relationship routes - pages', function(done) {
|
||||||
var test = this;
|
var test = this;
|
||||||
this.get('/api/books/' + test.book.id + '/pages')
|
this.get('/api/books/' + test.book.id + '/pages')
|
||||||
|
@ -1223,7 +1225,7 @@ describe('relations - integration', function () {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('has regular relationship routes - notes', function(done) {
|
it('has regular relationship routes - notes', function(done) {
|
||||||
var test = this;
|
var test = this;
|
||||||
this.get('/api/pages/' + test.page.id + '/notes/' + test.note.id)
|
this.get('/api/pages/' + test.page.id + '/notes/' + test.note.id)
|
||||||
|
@ -1235,7 +1237,7 @@ describe('relations - integration', function () {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('has a basic error handler', function(done) {
|
it('has a basic error handler', function(done) {
|
||||||
var test = this;
|
var test = this;
|
||||||
this.get('/api/books/unknown/pages/' + test.page.id + '/notes')
|
this.get('/api/books/unknown/pages/' + test.page.id + '/notes')
|
||||||
|
@ -1246,7 +1248,7 @@ describe('relations - integration', function () {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('enables nested relationship routes - belongsTo find', function(done) {
|
it('enables nested relationship routes - belongsTo find', function(done) {
|
||||||
var test = this;
|
var test = this;
|
||||||
this.get('/api/images/' + test.image.id + '/book/pages')
|
this.get('/api/images/' + test.image.id + '/book/pages')
|
||||||
|
@ -1257,7 +1259,7 @@ describe('relations - integration', function () {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('enables nested relationship routes - belongsTo findById', function(done) {
|
it('enables nested relationship routes - belongsTo findById', function(done) {
|
||||||
var test = this;
|
var test = this;
|
||||||
this.get('/api/images/' + test.image.id + '/book/pages/' + test.page.id)
|
this.get('/api/images/' + test.image.id + '/book/pages/' + test.page.id)
|
||||||
|
@ -1267,7 +1269,7 @@ describe('relations - integration', function () {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('enables nested relationship routes - hasMany find', function(done) {
|
it('enables nested relationship routes - hasMany find', function(done) {
|
||||||
var test = this;
|
var test = this;
|
||||||
this.get('/api/books/' + test.book.id + '/pages/' + test.page.id + '/notes')
|
this.get('/api/books/' + test.book.id + '/pages/' + test.page.id + '/notes')
|
||||||
|
@ -1278,7 +1280,7 @@ describe('relations - integration', function () {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('enables nested relationship routes - hasMany findById', function(done) {
|
it('enables nested relationship routes - hasMany findById', function(done) {
|
||||||
var test = this;
|
var test = this;
|
||||||
this.get('/api/books/' + test.book.id + '/pages/' + test.page.id + '/notes/' + test.note.id)
|
this.get('/api/books/' + test.book.id + '/pages/' + test.page.id + '/notes/' + test.note.id)
|
||||||
|
@ -1290,7 +1292,7 @@ describe('relations - integration', function () {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -57,7 +57,7 @@ describe('RemoteConnector', function() {
|
||||||
calledServerCreate = true;
|
calledServerCreate = true;
|
||||||
data.id = 1;
|
data.id = 1;
|
||||||
cb(null, data);
|
cb(null, data);
|
||||||
}
|
};
|
||||||
|
|
||||||
ServerModel.setupRemoting();
|
ServerModel.setupRemoting();
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,7 @@ describe('remoting coercion', function() {
|
||||||
assert(inst instanceof TestModel);
|
assert(inst instanceof TestModel);
|
||||||
assert(inst.foo === 'bar');
|
assert(inst.foo === 'bar');
|
||||||
cb();
|
cb();
|
||||||
}
|
};
|
||||||
TestModel.remoteMethod('test', {
|
TestModel.remoteMethod('test', {
|
||||||
accepts: {arg: 'inst', type: 'TestModel', http: {source: 'body'}},
|
accepts: {arg: 'inst', type: 'TestModel', http: {source: 'body'}},
|
||||||
http: {path: '/test', verb: 'post'}
|
http: {path: '/test', verb: 'post'}
|
||||||
|
@ -31,4 +31,4 @@ describe('remoting coercion', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
})
|
});
|
||||||
|
|
|
@ -15,26 +15,26 @@ describe('remoting - integration', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('app.remotes.options', function () {
|
describe('app.remotes.options', function () {
|
||||||
it("should load remoting options", function () {
|
it('should load remoting options', function () {
|
||||||
var remotes = app.remotes();
|
var remotes = app.remotes();
|
||||||
assert.deepEqual(remotes.options, {"json": {"limit": "1kb", "strict": false},
|
assert.deepEqual(remotes.options, {'json': {'limit': '1kb', 'strict': false},
|
||||||
"urlencoded": {"limit": "8kb", "extended": true}});
|
'urlencoded': {'limit': '8kb', 'extended': true}});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("rest handler", function () {
|
it('rest handler', function () {
|
||||||
var handler = app.handler('rest');
|
var handler = app.handler('rest');
|
||||||
assert(handler);
|
assert(handler);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should accept request that has entity below 1kb', function (done) {
|
it('should accept request that has entity below 1kb', function (done) {
|
||||||
// Build an object that is smaller than 1kb
|
// Build an object that is smaller than 1kb
|
||||||
var name = "";
|
var name = '';
|
||||||
for (var i = 0; i < 256; i++) {
|
for (var i = 0; i < 256; i++) {
|
||||||
name += "11";
|
name += '11';
|
||||||
}
|
}
|
||||||
this.http = this.post('/api/stores');
|
this.http = this.post('/api/stores');
|
||||||
this.http.send({
|
this.http.send({
|
||||||
"name": name
|
'name': name
|
||||||
});
|
});
|
||||||
this.http.end(function (err) {
|
this.http.end(function (err) {
|
||||||
if (err) return done(err);
|
if (err) return done(err);
|
||||||
|
@ -47,13 +47,13 @@ describe('remoting - integration', function () {
|
||||||
|
|
||||||
it('should reject request that has entity beyond 1kb', function (done) {
|
it('should reject request that has entity beyond 1kb', function (done) {
|
||||||
// Build an object that is larger than 1kb
|
// Build an object that is larger than 1kb
|
||||||
var name = "";
|
var name = '';
|
||||||
for (var i = 0; i < 2048; i++) {
|
for (var i = 0; i < 2048; i++) {
|
||||||
name += "11111111111";
|
name += '11111111111';
|
||||||
}
|
}
|
||||||
this.http = this.post('/api/stores');
|
this.http = this.post('/api/stores');
|
||||||
this.http.send({
|
this.http.send({
|
||||||
"name": name
|
'name': name
|
||||||
});
|
});
|
||||||
this.http.end(function (err) {
|
this.http.end(function (err) {
|
||||||
if (err) return done(err);
|
if (err) return done(err);
|
||||||
|
@ -81,7 +81,7 @@ describe('remoting - integration', function () {
|
||||||
m.name,
|
m.name,
|
||||||
'(',
|
'(',
|
||||||
m.accepts.map(function(a) {
|
m.accepts.map(function(a) {
|
||||||
return a.arg + ':' + a.type
|
return a.arg + ':' + a.type;
|
||||||
}).join(','),
|
}).join(','),
|
||||||
')',
|
')',
|
||||||
formatReturns(m),
|
formatReturns(m),
|
||||||
|
|
|
@ -22,7 +22,7 @@ describe('role model', function () {
|
||||||
RoleMapping.attachTo(ds);
|
RoleMapping.attachTo(ds);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should define role/role relations", function () {
|
it('should define role/role relations', function () {
|
||||||
Role.create({name: 'user'}, function (err, userRole) {
|
Role.create({name: 'user'}, function (err, userRole) {
|
||||||
Role.create({name: 'admin'}, function (err, adminRole) {
|
Role.create({name: 'admin'}, function (err, adminRole) {
|
||||||
userRole.principals.create({principalType: RoleMapping.ROLE, principalId: adminRole.id}, function (err, mapping) {
|
userRole.principals.create({principalType: RoleMapping.ROLE, principalId: adminRole.id}, function (err, mapping) {
|
||||||
|
@ -46,7 +46,7 @@ describe('role model', function () {
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should define role/user relations", function () {
|
it('should define role/user relations', function () {
|
||||||
|
|
||||||
User.create({name: 'Raymond', email: 'x@y.com', password: 'foobar'}, function (err, user) {
|
User.create({name: 'Raymond', email: 'x@y.com', password: 'foobar'}, function (err, user) {
|
||||||
// console.log('User: ', user.id);
|
// console.log('User: ', user.id);
|
||||||
|
@ -77,7 +77,7 @@ describe('role model', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
it("should automatically generate role id", function () {
|
it('should automatically generate role id', function () {
|
||||||
|
|
||||||
User.create({name: 'Raymond', email: 'x@y.com', password: 'foobar'}, function (err, user) {
|
User.create({name: 'Raymond', email: 'x@y.com', password: 'foobar'}, function (err, user) {
|
||||||
// console.log('User: ', user.id);
|
// console.log('User: ', user.id);
|
||||||
|
@ -110,7 +110,7 @@ describe('role model', function () {
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should support getRoles() and isInRole()", function () {
|
it('should support getRoles() and isInRole()', function () {
|
||||||
User.create({name: 'Raymond', email: 'x@y.com', password: 'foobar'}, function (err, user) {
|
User.create({name: 'Raymond', email: 'x@y.com', password: 'foobar'}, function (err, user) {
|
||||||
// console.log('User: ', user.id);
|
// console.log('User: ', user.id);
|
||||||
Role.create({name: 'userRole'}, function (err, role) {
|
Role.create({name: 'userRole'}, function (err, role) {
|
||||||
|
@ -156,7 +156,7 @@ describe('role model', function () {
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should support owner role resolver", function () {
|
it('should support owner role resolver', function () {
|
||||||
|
|
||||||
var Album = ds.createModel('Album', {
|
var Album = ds.createModel('Album', {
|
||||||
name: String,
|
name: String,
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
/**
|
/**
|
||||||
* loopback test setup and support.
|
* loopback test setup and support.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
assert = require('assert');
|
assert = require('assert');
|
||||||
expect = require('chai').expect;
|
expect = require('chai').expect;
|
||||||
loopback = require('../');
|
loopback = require('../');
|
||||||
|
@ -44,9 +44,9 @@ assertValidDataSource = function (dataSource) {
|
||||||
assert.isFunc(dataSource, 'disableRemote');
|
assert.isFunc(dataSource, 'disableRemote');
|
||||||
assert.isFunc(dataSource, 'defineOperation');
|
assert.isFunc(dataSource, 'defineOperation');
|
||||||
assert.isFunc(dataSource, 'operations');
|
assert.isFunc(dataSource, 'operations');
|
||||||
}
|
};
|
||||||
|
|
||||||
assert.isFunc = function (obj, name) {
|
assert.isFunc = function (obj, name) {
|
||||||
assert(obj, 'cannot assert function ' + name + ' on object that doesnt exist');
|
assert(obj, 'cannot assert function ' + name + ' on object that doesnt exist');
|
||||||
assert(typeof obj[name] === 'function', name + ' is not a function');
|
assert(typeof obj[name] === 'function', name + ' is not a function');
|
||||||
}
|
};
|
||||||
|
|
|
@ -21,29 +21,29 @@ describe('User', function(){
|
||||||
|
|
||||||
// Update the AccessToken relation to use the subclass of User
|
// Update the AccessToken relation to use the subclass of User
|
||||||
AccessToken.belongsTo(User);
|
AccessToken.belongsTo(User);
|
||||||
|
|
||||||
// allow many User.afterRemote's to be called
|
// allow many User.afterRemote's to be called
|
||||||
User.setMaxListeners(0);
|
User.setMaxListeners(0);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
beforeEach(function (done) {
|
beforeEach(function (done) {
|
||||||
app.enableAuth();
|
app.enableAuth();
|
||||||
app.use(loopback.token());
|
app.use(loopback.token());
|
||||||
app.use(loopback.rest());
|
app.use(loopback.rest());
|
||||||
app.model(User);
|
app.model(User);
|
||||||
|
|
||||||
User.create(validCredentials, function(err, user) {
|
User.create(validCredentials, function(err, user) {
|
||||||
User.create(validCredentialsEmailVerified, done);
|
User.create(validCredentialsEmailVerified, done);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function (done) {
|
afterEach(function (done) {
|
||||||
User.destroyAll(function (err) {
|
User.destroyAll(function (err) {
|
||||||
User.accessToken.destroyAll(done);
|
User.accessToken.destroyAll(done);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('User.create', function(){
|
describe('User.create', function(){
|
||||||
it('Create a new user', function(done) {
|
it('Create a new user', function(done) {
|
||||||
User.create({email: 'f@b.com', password: 'bar'}, function (err, user) {
|
User.create({email: 'f@b.com', password: 'bar'}, function (err, user) {
|
||||||
|
@ -73,9 +73,9 @@ describe('User', function(){
|
||||||
it('Email is required', function (done) {
|
it('Email is required', function (done) {
|
||||||
User.create({password: '123'}, function (err) {
|
User.create({password: '123'}, function (err) {
|
||||||
assert(err);
|
assert(err);
|
||||||
assert.equal(err.name, "ValidationError");
|
assert.equal(err.name, 'ValidationError');
|
||||||
assert.equal(err.statusCode, 422);
|
assert.equal(err.statusCode, 422);
|
||||||
assert.equal(err.details.context, "user");
|
assert.equal(err.details.context, 'user');
|
||||||
assert.deepEqual(err.details.codes.email, [
|
assert.deepEqual(err.details.codes.email, [
|
||||||
'presence',
|
'presence',
|
||||||
'format.blank'
|
'format.blank'
|
||||||
|
@ -84,24 +84,24 @@ describe('User', function(){
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// will change in future versions where password will be optional by default
|
// will change in future versions where password will be optional by default
|
||||||
it('Password is required', function(done) {
|
it('Password is required', function(done) {
|
||||||
var u = new User({email: "123@456.com"});
|
var u = new User({email: '123@456.com'});
|
||||||
|
|
||||||
User.create({email: 'c@d.com'}, function (err) {
|
User.create({email: 'c@d.com'}, function (err) {
|
||||||
assert(err);
|
assert(err);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Requires a valid email', function(done) {
|
it('Requires a valid email', function(done) {
|
||||||
User.create({email: 'foo@', password: '123'}, function (err) {
|
User.create({email: 'foo@', password: '123'}, function (err) {
|
||||||
assert(err);
|
assert(err);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Requires a unique email', function(done) {
|
it('Requires a unique email', function(done) {
|
||||||
User.create({email: 'a@b.com', password: 'foobar'}, function () {
|
User.create({email: 'a@b.com', password: 'foobar'}, function () {
|
||||||
User.create({email: 'a@b.com', password: 'batbaz'}, function (err) {
|
User.create({email: 'a@b.com', password: 'batbaz'}, function (err) {
|
||||||
|
@ -118,8 +118,8 @@ describe('User', function(){
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Requires a password to login with basic auth', function(done) {
|
it('Requires a password to login with basic auth', function(done) {
|
||||||
User.create({email: 'b@c.com'}, function (err) {
|
User.create({email: 'b@c.com'}, function (err) {
|
||||||
User.login({email: 'b@c.com'}, function (err, accessToken) {
|
User.login({email: 'b@c.com'}, function (err, accessToken) {
|
||||||
|
@ -129,7 +129,7 @@ describe('User', function(){
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Hashes the given password', function() {
|
it('Hashes the given password', function() {
|
||||||
var u = new User({username: 'foo', password: 'bar'});
|
var u = new User({username: 'foo', password: 'bar'});
|
||||||
assert(u.password !== 'bar');
|
assert(u.password !== 'bar');
|
||||||
|
@ -147,14 +147,14 @@ describe('User', function(){
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('User.login', function() {
|
describe('User.login', function() {
|
||||||
it('Login a user by providing credentials', function(done) {
|
it('Login a user by providing credentials', function(done) {
|
||||||
User.login(validCredentials, function (err, accessToken) {
|
User.login(validCredentials, function (err, accessToken) {
|
||||||
assert(accessToken.userId);
|
assert(accessToken.userId);
|
||||||
assert(accessToken.id);
|
assert(accessToken.id);
|
||||||
assert.equal(accessToken.id.length, 64);
|
assert.equal(accessToken.id.length, 64);
|
||||||
|
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -206,12 +206,12 @@ describe('User', function(){
|
||||||
.end(function(err, res){
|
.end(function(err, res){
|
||||||
if(err) return done(err);
|
if(err) return done(err);
|
||||||
var accessToken = res.body;
|
var accessToken = res.body;
|
||||||
|
|
||||||
assert(accessToken.userId);
|
assert(accessToken.userId);
|
||||||
assert(accessToken.id);
|
assert(accessToken.id);
|
||||||
assert.equal(accessToken.id.length, 64);
|
assert.equal(accessToken.id.length, 64);
|
||||||
assert(accessToken.user === undefined);
|
assert(accessToken.user === undefined);
|
||||||
|
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -284,7 +284,7 @@ describe('User', function(){
|
||||||
|
|
||||||
it('Login should only allow correct credentials', function(done) {
|
it('Login should only allow correct credentials', function(done) {
|
||||||
User.create({email: 'foo22@bar.com', password: 'bar'}, function(user, err) {
|
User.create({email: 'foo22@bar.com', password: 'bar'}, function(user, err) {
|
||||||
User.login({email: 'foo44@bar.com', password: 'bar'}, function(err, accessToken) {
|
User.login({email: 'foo44@bar.com', password: 'bar'}, function(err, accessToken) {
|
||||||
assert(err);
|
assert(err);
|
||||||
assert(!accessToken);
|
assert(!accessToken);
|
||||||
done();
|
done();
|
||||||
|
@ -511,20 +511,20 @@ describe('User', function(){
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('User.logout', function() {
|
describe('User.logout', function() {
|
||||||
it('Logout a user by providing the current accessToken id (using node)', function(done) {
|
it('Logout a user by providing the current accessToken id (using node)', function(done) {
|
||||||
login(logout);
|
login(logout);
|
||||||
|
|
||||||
function login(fn) {
|
function login(fn) {
|
||||||
User.login({email: 'foo@bar.com', password: 'bar'}, fn);
|
User.login({email: 'foo@bar.com', password: 'bar'}, fn);
|
||||||
}
|
}
|
||||||
|
|
||||||
function logout(err, accessToken) {
|
function logout(err, accessToken) {
|
||||||
User.logout(accessToken.id, verify(accessToken.id, done));
|
User.logout(accessToken.id, verify(accessToken.id, done));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Logout a user by providing the current accessToken id (over rest)', function(done) {
|
it('Logout a user by providing the current accessToken id (over rest)', function(done) {
|
||||||
login(logout);
|
login(logout);
|
||||||
function login(fn) {
|
function login(fn) {
|
||||||
|
@ -536,14 +536,14 @@ describe('User', function(){
|
||||||
.end(function(err, res){
|
.end(function(err, res){
|
||||||
if(err) return done(err);
|
if(err) return done(err);
|
||||||
var accessToken = res.body;
|
var accessToken = res.body;
|
||||||
|
|
||||||
assert(accessToken.userId);
|
assert(accessToken.userId);
|
||||||
assert(accessToken.id);
|
assert(accessToken.id);
|
||||||
|
|
||||||
fn(null, accessToken.id);
|
fn(null, accessToken.id);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function logout(err, token) {
|
function logout(err, token) {
|
||||||
request(app)
|
request(app)
|
||||||
.post('/users/logout')
|
.post('/users/logout')
|
||||||
|
@ -552,33 +552,33 @@ describe('User', function(){
|
||||||
.end(verify(token, done));
|
.end(verify(token, done));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
function verify(token, done) {
|
function verify(token, done) {
|
||||||
assert(token);
|
assert(token);
|
||||||
|
|
||||||
return function (err) {
|
return function (err) {
|
||||||
if(err) return done(err);
|
if(err) return done(err);
|
||||||
|
|
||||||
AccessToken.findById(token, function (err, accessToken) {
|
AccessToken.findById(token, function (err, accessToken) {
|
||||||
assert(!accessToken, 'accessToken should not exist after logging out');
|
assert(!accessToken, 'accessToken should not exist after logging out');
|
||||||
done(err);
|
done(err);
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('user.hasPassword(plain, fn)', function(){
|
describe('user.hasPassword(plain, fn)', function(){
|
||||||
it('Determine if the password matches the stored password', function(done) {
|
it('Determine if the password matches the stored password', function(done) {
|
||||||
var u = new User({username: 'foo', password: 'bar'});
|
var u = new User({username: 'foo', password: 'bar'});
|
||||||
u.hasPassword('bar', function (err, isMatch) {
|
u.hasPassword('bar', function (err, isMatch) {
|
||||||
assert(isMatch, 'password doesnt match');
|
assert(isMatch, 'password doesnt match');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should match a password when saved', function(done) {
|
it('should match a password when saved', function(done) {
|
||||||
var u = new User({username: 'a', password: 'b', email: 'z@z.net'});
|
var u = new User({username: 'a', password: 'b', email: 'z@z.net'});
|
||||||
|
|
||||||
u.save(function (err, user) {
|
u.save(function (err, user) {
|
||||||
User.findById(user.id, function (err, uu) {
|
User.findById(user.id, function (err, uu) {
|
||||||
uu.hasPassword('b', function (err, isMatch) {
|
uu.hasPassword('b', function (err, isMatch) {
|
||||||
|
@ -588,7 +588,7 @@ describe('User', function(){
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should match a password after it is changed', function(done) {
|
it('should match a password after it is changed', function(done) {
|
||||||
User.create({email: 'foo@baz.net', username: 'bat', password: 'baz'}, function (err, user) {
|
User.create({email: 'foo@baz.net', username: 'bat', password: 'baz'}, function (err, user) {
|
||||||
User.findById(user.id, function (err, foundUser) {
|
User.findById(user.id, function (err, foundUser) {
|
||||||
|
@ -612,14 +612,14 @@ describe('User', function(){
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Verification', function(){
|
describe('Verification', function(){
|
||||||
|
|
||||||
describe('user.verify(options, fn)', function(){
|
describe('user.verify(options, fn)', function(){
|
||||||
it('Verify a user\'s email address', function(done) {
|
it('Verify a user\'s email address', function(done) {
|
||||||
User.afterRemote('create', function(ctx, user, next) {
|
User.afterRemote('create', function(ctx, user, next) {
|
||||||
assert(user, 'afterRemote should include result');
|
assert(user, 'afterRemote should include result');
|
||||||
|
|
||||||
var options = {
|
var options = {
|
||||||
type: 'email',
|
type: 'email',
|
||||||
to: user.email,
|
to: user.email,
|
||||||
|
@ -628,7 +628,7 @@ describe('User', function(){
|
||||||
protocol: ctx.req.protocol,
|
protocol: ctx.req.protocol,
|
||||||
host: ctx.req.get('host')
|
host: ctx.req.get('host')
|
||||||
};
|
};
|
||||||
|
|
||||||
user.verify(options, function (err, result) {
|
user.verify(options, function (err, result) {
|
||||||
assert(result.email);
|
assert(result.email);
|
||||||
assert(result.email.response);
|
assert(result.email.response);
|
||||||
|
@ -639,7 +639,7 @@ describe('User', function(){
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
request(app)
|
request(app)
|
||||||
.post('/users')
|
.post('/users')
|
||||||
.expect('Content-Type', /json/)
|
.expect('Content-Type', /json/)
|
||||||
|
@ -653,7 +653,7 @@ describe('User', function(){
|
||||||
it('Verify a user\'s email address with custom header', function(done) {
|
it('Verify a user\'s email address with custom header', function(done) {
|
||||||
User.afterRemote('create', function(ctx, user, next) {
|
User.afterRemote('create', function(ctx, user, next) {
|
||||||
assert(user, 'afterRemote should include result');
|
assert(user, 'afterRemote should include result');
|
||||||
|
|
||||||
var options = {
|
var options = {
|
||||||
type: 'email',
|
type: 'email',
|
||||||
to: user.email,
|
to: user.email,
|
||||||
|
@ -663,14 +663,14 @@ describe('User', function(){
|
||||||
host: ctx.req.get('host'),
|
host: ctx.req.get('host'),
|
||||||
headers: {'message-id':'custom-header-value'}
|
headers: {'message-id':'custom-header-value'}
|
||||||
};
|
};
|
||||||
|
|
||||||
user.verify(options, function (err, result) {
|
user.verify(options, function (err, result) {
|
||||||
assert(result.email);
|
assert(result.email);
|
||||||
assert.equal(result.email.messageId, 'custom-header-value');
|
assert.equal(result.email.messageId, 'custom-header-value');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
request(app)
|
request(app)
|
||||||
.post('/users')
|
.post('/users')
|
||||||
.expect('Content-Type', /json/)
|
.expect('Content-Type', /json/)
|
||||||
|
|
|
@ -33,7 +33,7 @@ describe('Model Tests', function() {
|
||||||
}
|
}
|
||||||
|
|
||||||
return extendedModel;
|
return extendedModel;
|
||||||
}
|
};
|
||||||
|
|
||||||
User = PersistedModel.extend('user', {
|
User = PersistedModel.extend('user', {
|
||||||
'first': String,
|
'first': String,
|
||||||
|
@ -53,7 +53,7 @@ describe('Model Tests', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Model.validatesPresenceOf(properties...)', function() {
|
describe('Model.validatesPresenceOf(properties...)', function() {
|
||||||
it("Require a model to include a property to be considered valid", function() {
|
it('Require a model to include a property to be considered valid', function() {
|
||||||
User.validatesPresenceOf('first', 'last', 'age');
|
User.validatesPresenceOf('first', 'last', 'age');
|
||||||
var joe = new User({first: 'joe'});
|
var joe = new User({first: 'joe'});
|
||||||
assert(joe.isValid() === false, 'model should not validate');
|
assert(joe.isValid() === false, 'model should not validate');
|
||||||
|
@ -63,7 +63,7 @@ describe('Model Tests', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Model.validatesLengthOf(property, options)', function() {
|
describe('Model.validatesLengthOf(property, options)', function() {
|
||||||
it("Require a property length to be within a specified range", function() {
|
it('Require a property length to be within a specified range', function() {
|
||||||
User.validatesLengthOf('password', {min: 5, message: {min: 'Password is too short'}});
|
User.validatesLengthOf('password', {min: 5, message: {min: 'Password is too short'}});
|
||||||
var joe = new User({password: '1234'});
|
var joe = new User({password: '1234'});
|
||||||
assert(joe.isValid() === false, 'model should not be valid');
|
assert(joe.isValid() === false, 'model should not be valid');
|
||||||
|
@ -72,7 +72,7 @@ describe('Model Tests', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Model.validatesInclusionOf(property, options)', function() {
|
describe('Model.validatesInclusionOf(property, options)', function() {
|
||||||
it("Require a value for `property` to be in the specified array", function() {
|
it('Require a value for `property` to be in the specified array', function() {
|
||||||
User.validatesInclusionOf('gender', {in: ['male', 'female']});
|
User.validatesInclusionOf('gender', {in: ['male', 'female']});
|
||||||
var foo = new User({gender: 'bar'});
|
var foo = new User({gender: 'bar'});
|
||||||
assert(foo.isValid() === false, 'model should not be valid');
|
assert(foo.isValid() === false, 'model should not be valid');
|
||||||
|
@ -81,7 +81,7 @@ describe('Model Tests', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Model.validatesExclusionOf(property, options)', function() {
|
describe('Model.validatesExclusionOf(property, options)', function() {
|
||||||
it("Require a value for `property` to not exist in the specified array", function() {
|
it('Require a value for `property` to not exist in the specified array', function() {
|
||||||
User.validatesExclusionOf('domain', {in: ['www', 'billing', 'admin']});
|
User.validatesExclusionOf('domain', {in: ['www', 'billing', 'admin']});
|
||||||
var foo = new User({domain: 'www'});
|
var foo = new User({domain: 'www'});
|
||||||
var bar = new User({domain: 'billing'});
|
var bar = new User({domain: 'billing'});
|
||||||
|
@ -96,7 +96,7 @@ describe('Model Tests', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Model.validatesNumericalityOf(property, options)', function() {
|
describe('Model.validatesNumericalityOf(property, options)', function() {
|
||||||
it("Require a value for `property` to be a specific type of `Number`", function() {
|
it('Require a value for `property` to be a specific type of `Number`', function() {
|
||||||
User.validatesNumericalityOf('age', {int: true});
|
User.validatesNumericalityOf('age', {int: true});
|
||||||
var joe = new User({age: 10.2});
|
var joe = new User({age: 10.2});
|
||||||
assert(joe.isValid() === false);
|
assert(joe.isValid() === false);
|
||||||
|
@ -107,14 +107,14 @@ describe('Model Tests', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('myModel.isValid()', function() {
|
describe('myModel.isValid()', function() {
|
||||||
it("Validate the model instance", function() {
|
it('Validate the model instance', function() {
|
||||||
User.validatesNumericalityOf('age', {int: true});
|
User.validatesNumericalityOf('age', {int: true});
|
||||||
var user = new User({first: 'joe', age: 'flarg'})
|
var user = new User({first: 'joe', age: 'flarg'});
|
||||||
var valid = user.isValid();
|
var valid = user.isValid();
|
||||||
assert(valid === false);
|
assert(valid === false);
|
||||||
assert(user.errors.age, 'model should have age error');
|
assert(user.errors.age, 'model should have age error');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Asynchronously validate the model', function(done) {
|
it('Asynchronously validate the model', function(done) {
|
||||||
User.validatesNumericalityOf('age', {int: true});
|
User.validatesNumericalityOf('age', {int: true});
|
||||||
var user = new User({first: 'joe', age: 'flarg'});
|
var user = new User({first: 'joe', age: 'flarg'});
|
||||||
|
@ -127,7 +127,7 @@ describe('Model Tests', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Model.create([data], [callback])', function() {
|
describe('Model.create([data], [callback])', function() {
|
||||||
it("Create an instance of Model with given data and save to the attached data source", function(done) {
|
it('Create an instance of Model with given data and save to the attached data source', function(done) {
|
||||||
User.create({first: 'Joe', last: 'Bob'}, function(err, user) {
|
User.create({first: 'Joe', last: 'Bob'}, function(err, user) {
|
||||||
assert(user instanceof User);
|
assert(user instanceof User);
|
||||||
done();
|
done();
|
||||||
|
@ -136,7 +136,7 @@ describe('Model Tests', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('model.save([options], [callback])', function() {
|
describe('model.save([options], [callback])', function() {
|
||||||
it("Save an instance of a Model to the attached data source", function(done) {
|
it('Save an instance of a Model to the attached data source', function(done) {
|
||||||
var joe = new User({first: 'Joe', last: 'Bob'});
|
var joe = new User({first: 'Joe', last: 'Bob'});
|
||||||
joe.save(function(err, user) {
|
joe.save(function(err, user) {
|
||||||
assert(user.id);
|
assert(user.id);
|
||||||
|
@ -148,11 +148,11 @@ describe('Model Tests', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('model.updateAttributes(data, [callback])', function() {
|
describe('model.updateAttributes(data, [callback])', function() {
|
||||||
it("Save specified attributes to the attached data source", function(done) {
|
it('Save specified attributes to the attached data source', function(done) {
|
||||||
User.create({first: 'joe', age: 100}, function (err, user) {
|
User.create({first: 'joe', age: 100}, function (err, user) {
|
||||||
assert(!err);
|
assert(!err);
|
||||||
assert.equal(user.first, 'joe');
|
assert.equal(user.first, 'joe');
|
||||||
|
|
||||||
user.updateAttributes({
|
user.updateAttributes({
|
||||||
first: 'updatedFirst',
|
first: 'updatedFirst',
|
||||||
last: 'updatedLast'
|
last: 'updatedLast'
|
||||||
|
@ -168,11 +168,11 @@ describe('Model Tests', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Model.upsert(data, callback)', function() {
|
describe('Model.upsert(data, callback)', function() {
|
||||||
it("Update when record with id=data.id found, insert otherwise", function(done) {
|
it('Update when record with id=data.id found, insert otherwise', function(done) {
|
||||||
User.upsert({first: 'joe', id: 7}, function (err, user) {
|
User.upsert({first: 'joe', id: 7}, function (err, user) {
|
||||||
assert(!err);
|
assert(!err);
|
||||||
assert.equal(user.first, 'joe');
|
assert.equal(user.first, 'joe');
|
||||||
|
|
||||||
User.upsert({first: 'bob', id: 7}, function (err, updatedUser) {
|
User.upsert({first: 'bob', id: 7}, function (err, updatedUser) {
|
||||||
assert(!err);
|
assert(!err);
|
||||||
assert.equal(updatedUser.first, 'bob');
|
assert.equal(updatedUser.first, 'bob');
|
||||||
|
@ -183,7 +183,7 @@ describe('Model Tests', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('model.destroy([callback])', function() {
|
describe('model.destroy([callback])', function() {
|
||||||
it("Remove a model from the attached data source", function(done) {
|
it('Remove a model from the attached data source', function(done) {
|
||||||
User.create({first: 'joe', last: 'bob'}, function (err, user) {
|
User.create({first: 'joe', last: 'bob'}, function (err, user) {
|
||||||
User.findById(user.id, function (err, foundUser) {
|
User.findById(user.id, function (err, foundUser) {
|
||||||
assert.equal(user.id, foundUser.id);
|
assert.equal(user.id, foundUser.id);
|
||||||
|
@ -199,7 +199,7 @@ describe('Model Tests', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Model.deleteById(id, [callback])', function () {
|
describe('Model.deleteById(id, [callback])', function () {
|
||||||
it("Delete a model instance from the attached data source", function (done) {
|
it('Delete a model instance from the attached data source', function (done) {
|
||||||
User.create({first: 'joe', last: 'bob'}, function (err, user) {
|
User.create({first: 'joe', last: 'bob'}, function (err, user) {
|
||||||
User.deleteById(user.id, function (err) {
|
User.deleteById(user.id, function (err) {
|
||||||
User.findById(user.id, function (err, notFound) {
|
User.findById(user.id, function (err, notFound) {
|
||||||
|
@ -212,7 +212,7 @@ describe('Model Tests', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Model.findById(id, callback)', function() {
|
describe('Model.findById(id, callback)', function() {
|
||||||
it("Find an instance by id", function(done) {
|
it('Find an instance by id', function(done) {
|
||||||
User.create({first: 'michael', last: 'jordan', id: 23}, function () {
|
User.create({first: 'michael', last: 'jordan', id: 23}, function () {
|
||||||
User.findById(23, function (err, user) {
|
User.findById(23, function (err, user) {
|
||||||
assert.equal(user.id, 23);
|
assert.equal(user.id, 23);
|
||||||
|
@ -225,7 +225,7 @@ describe('Model Tests', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Model.count([query], callback)', function() {
|
describe('Model.count([query], callback)', function() {
|
||||||
it("Query count of Model instances in data source", function(done) {
|
it('Query count of Model instances in data source', function(done) {
|
||||||
(new TaskEmitter())
|
(new TaskEmitter())
|
||||||
.task(User, 'create', {first: 'jill', age: 100})
|
.task(User, 'create', {first: 'jill', age: 100})
|
||||||
.task(User, 'create', {first: 'bob', age: 200})
|
.task(User, 'create', {first: 'bob', age: 200})
|
||||||
|
@ -243,5 +243,4 @@ describe('Model Tests', function() {
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
};
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue