2016-05-03 22:50:21 +00:00
|
|
|
// Copyright IBM Corp. 2014,2016. All Rights Reserved.
|
|
|
|
// Node module: loopback
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2014-02-19 23:14:31 +00:00
|
|
|
var loopback = require('../');
|
2015-12-31 23:41:18 +00:00
|
|
|
var lt = require('./helpers/loopback-testing-helper');
|
2014-02-19 23:14:31 +00:00
|
|
|
var path = require('path');
|
|
|
|
var SIMPLE_APP = path.join(__dirname, 'fixtures', 'simple-integration-app');
|
2015-04-20 16:23:44 +00:00
|
|
|
var app = require(path.join(SIMPLE_APP, 'server/server.js'));
|
2014-02-19 23:14:31 +00:00
|
|
|
var assert = require('assert');
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('remoting - integration', function() {
|
2014-02-19 23:14:31 +00:00
|
|
|
lt.beforeEach.withApp(app);
|
|
|
|
lt.beforeEach.givenModel('store');
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
afterEach(function(done) {
|
2014-02-19 23:14:31 +00:00
|
|
|
this.app.models.store.destroyAll(done);
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('app.remotes.options', function() {
|
|
|
|
it('should load remoting options', function() {
|
2014-02-19 23:14:31 +00:00
|
|
|
var remotes = app.remotes();
|
2016-04-01 09:14:26 +00:00
|
|
|
assert.deepEqual(remotes.options, { 'json': { 'limit': '1kb', 'strict': false },
|
2016-05-24 20:46:32 +00:00
|
|
|
'urlencoded': { 'limit': '8kb', 'extended': true },
|
|
|
|
'errorHandler': { 'debug': true, log: false }});
|
2014-02-19 23:14:31 +00:00
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('rest handler', function() {
|
2014-02-19 23:14:31 +00:00
|
|
|
var handler = app.handler('rest');
|
|
|
|
assert(handler);
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('should accept request that has entity below 1kb', function(done) {
|
2014-02-19 23:14:31 +00:00
|
|
|
// Build an object that is smaller than 1kb
|
2014-11-21 01:46:21 +00:00
|
|
|
var name = '';
|
2014-02-19 23:14:31 +00:00
|
|
|
for (var i = 0; i < 256; i++) {
|
2014-11-21 01:46:21 +00:00
|
|
|
name += '11';
|
2014-02-19 23:14:31 +00:00
|
|
|
}
|
|
|
|
this.http = this.post('/api/stores');
|
|
|
|
this.http.send({
|
2016-04-01 09:14:26 +00:00
|
|
|
'name': name,
|
2014-02-19 23:14:31 +00:00
|
|
|
});
|
2014-11-21 02:35:36 +00:00
|
|
|
this.http.end(function(err) {
|
2014-02-19 23:14:31 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
this.req = this.http.req;
|
|
|
|
this.res = this.http.res;
|
|
|
|
assert.equal(this.res.statusCode, 200);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-02-19 23:14:31 +00:00
|
|
|
done();
|
|
|
|
}.bind(this));
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('should reject request that has entity beyond 1kb', function(done) {
|
2014-02-19 23:14:31 +00:00
|
|
|
// Build an object that is larger than 1kb
|
2014-11-21 01:46:21 +00:00
|
|
|
var name = '';
|
2014-02-19 23:14:31 +00:00
|
|
|
for (var i = 0; i < 2048; i++) {
|
2014-11-21 01:46:21 +00:00
|
|
|
name += '11111111111';
|
2014-02-19 23:14:31 +00:00
|
|
|
}
|
|
|
|
this.http = this.post('/api/stores');
|
|
|
|
this.http.send({
|
2016-04-01 09:14:26 +00:00
|
|
|
'name': name,
|
2014-02-19 23:14:31 +00:00
|
|
|
});
|
2014-11-21 02:35:36 +00:00
|
|
|
this.http.end(function(err) {
|
2014-02-19 23:14:31 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
this.req = this.http.req;
|
|
|
|
this.res = this.http.res;
|
|
|
|
// Request is rejected with 413
|
|
|
|
assert.equal(this.res.statusCode, 413);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-02-19 23:14:31 +00:00
|
|
|
done();
|
|
|
|
}.bind(this));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-17 17:44:20 +00:00
|
|
|
describe('Model shared classes', function() {
|
2016-05-09 19:24:03 +00:00
|
|
|
it('has expected remote methods with default model.settings.replaceOnPUT' +
|
|
|
|
'set to true (3.x)',
|
|
|
|
function() {
|
2014-11-17 17:44:20 +00:00
|
|
|
var storeClass = findClass('store');
|
2016-05-09 19:24:03 +00:00
|
|
|
var methods = getFormattedMethodsExcludingRelations(storeClass.methods);
|
2014-07-03 06:19:41 +00:00
|
|
|
|
2014-11-17 17:44:20 +00:00
|
|
|
var expectedMethods = [
|
2016-09-02 12:40:11 +00:00
|
|
|
'create(data:object:store):store POST /stores',
|
|
|
|
'patchOrCreate(data:object:store):store PATCH /stores',
|
|
|
|
'replaceOrCreate(data:object:store):store PUT /stores',
|
|
|
|
'replaceOrCreate(data:object:store):store POST /stores/replaceOrCreate',
|
2014-11-17 17:44:20 +00:00
|
|
|
'exists(id:any):boolean GET /stores/:id/exists',
|
2015-04-16 22:02:41 +00:00
|
|
|
'findById(id:any,filter:object):store GET /stores/:id',
|
2016-09-02 12:40:11 +00:00
|
|
|
'replaceById(id:any,data:object:store):store PUT /stores/:id',
|
|
|
|
'replaceById(id:any,data:object:store):store POST /stores/:id/replace',
|
2014-11-17 17:44:20 +00:00
|
|
|
'find(filter:object):store GET /stores',
|
|
|
|
'findOne(filter:object):store GET /stores/findOne',
|
2016-09-02 12:40:11 +00:00
|
|
|
'updateAll(where:object,data:object:store):object POST /stores/update',
|
2015-09-19 02:13:30 +00:00
|
|
|
'deleteById(id:any):object DELETE /stores/:id',
|
2014-11-17 17:44:20 +00:00
|
|
|
'count(where:object):number GET /stores/count',
|
2016-09-02 12:40:11 +00:00
|
|
|
'prototype.patchAttributes(data:object:store):store PATCH /stores/:id',
|
2016-04-01 09:14:26 +00:00
|
|
|
'createChangeStream(options:object):ReadableStream POST /stores/change-stream',
|
2014-11-17 17:44:20 +00:00
|
|
|
];
|
|
|
|
|
2014-07-03 06:19:41 +00:00
|
|
|
// The list of methods is from docs:
|
2015-09-19 02:13:30 +00:00
|
|
|
// https://docs.strongloop.com/display/public/LB/Exposing+models+over+REST
|
2014-11-17 17:44:20 +00:00
|
|
|
expect(methods).to.include.members(expectedMethods);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('has expected remote methods for scopes', function() {
|
|
|
|
var storeClass = findClass('store');
|
2016-05-09 19:24:03 +00:00
|
|
|
var methods = getFormattedScopeMethods(storeClass.methods);
|
2014-11-17 17:44:20 +00:00
|
|
|
|
|
|
|
var expectedMethods = [
|
|
|
|
'__get__superStores(filter:object):store GET /stores/superStores',
|
2016-09-02 12:40:11 +00:00
|
|
|
'__create__superStores(data:object:store):store POST /stores/superStores',
|
2014-11-17 17:44:20 +00:00
|
|
|
'__delete__superStores() DELETE /stores/superStores',
|
2016-04-01 09:14:26 +00:00
|
|
|
'__count__superStores(where:object):number GET /stores/superStores/count',
|
2014-11-17 17:44:20 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
expect(methods).to.include.members(expectedMethods);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should have correct signatures for belongsTo methods',
|
|
|
|
function() {
|
|
|
|
var widgetClass = findClass('widget');
|
2016-05-09 19:24:03 +00:00
|
|
|
var methods = getFormattedPrototypeMethods(widgetClass.methods);
|
2014-11-17 17:44:20 +00:00
|
|
|
|
|
|
|
var expectedMethods = [
|
2016-04-01 09:14:26 +00:00
|
|
|
'prototype.__get__store(refresh:boolean):store ' +
|
|
|
|
'GET /widgets/:id/store',
|
2014-11-17 17:44:20 +00:00
|
|
|
];
|
|
|
|
expect(methods).to.include.members(expectedMethods);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should have correct signatures for hasMany methods',
|
|
|
|
function() {
|
|
|
|
var physicianClass = findClass('store');
|
2016-05-09 19:24:03 +00:00
|
|
|
var methods = getFormattedPrototypeMethods(physicianClass.methods);
|
2014-11-17 17:44:20 +00:00
|
|
|
|
|
|
|
var expectedMethods = [
|
2016-04-01 09:14:26 +00:00
|
|
|
'prototype.__findById__widgets(fk:any):widget ' +
|
2014-11-17 17:44:20 +00:00
|
|
|
'GET /stores/:id/widgets/:fk',
|
2016-04-01 09:14:26 +00:00
|
|
|
'prototype.__destroyById__widgets(fk:any) ' +
|
2014-11-17 17:44:20 +00:00
|
|
|
'DELETE /stores/:id/widgets/:fk',
|
2016-09-02 12:40:11 +00:00
|
|
|
'prototype.__updateById__widgets(fk:any,data:object:widget):widget ' +
|
2014-11-17 17:44:20 +00:00
|
|
|
'PUT /stores/:id/widgets/:fk',
|
2016-04-01 09:14:26 +00:00
|
|
|
'prototype.__get__widgets(filter:object):widget ' +
|
2014-11-17 17:44:20 +00:00
|
|
|
'GET /stores/:id/widgets',
|
2016-09-02 12:40:11 +00:00
|
|
|
'prototype.__create__widgets(data:object:widget):widget ' +
|
2014-11-17 17:44:20 +00:00
|
|
|
'POST /stores/:id/widgets',
|
2016-04-01 09:14:26 +00:00
|
|
|
'prototype.__delete__widgets() ' +
|
2014-11-17 17:44:20 +00:00
|
|
|
'DELETE /stores/:id/widgets',
|
2016-04-01 09:14:26 +00:00
|
|
|
'prototype.__count__widgets(where:object):number ' +
|
|
|
|
'GET /stores/:id/widgets/count',
|
2014-11-17 17:44:20 +00:00
|
|
|
];
|
|
|
|
expect(methods).to.include.members(expectedMethods);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should have correct signatures for hasMany-through methods',
|
2015-02-20 14:31:15 +00:00
|
|
|
function() { // jscs:disable validateIndentation
|
2016-04-01 09:14:26 +00:00
|
|
|
var physicianClass = findClass('physician');
|
2016-05-09 19:24:03 +00:00
|
|
|
var methods = getFormattedPrototypeMethods(physicianClass.methods);
|
2014-11-17 17:44:20 +00:00
|
|
|
|
2016-04-01 09:14:26 +00:00
|
|
|
var expectedMethods = [
|
2014-11-17 17:44:20 +00:00
|
|
|
'prototype.__findById__patients(fk:any):patient ' +
|
|
|
|
'GET /physicians/:id/patients/:fk',
|
|
|
|
'prototype.__destroyById__patients(fk:any) ' +
|
|
|
|
'DELETE /physicians/:id/patients/:fk',
|
2016-09-02 12:40:11 +00:00
|
|
|
'prototype.__updateById__patients(fk:any,data:object:patient):patient ' +
|
2014-11-17 17:44:20 +00:00
|
|
|
'PUT /physicians/:id/patients/:fk',
|
2016-09-02 12:40:11 +00:00
|
|
|
'prototype.__link__patients(fk:any,data:object:appointment):appointment ' +
|
2014-11-17 17:44:20 +00:00
|
|
|
'PUT /physicians/:id/patients/rel/:fk',
|
|
|
|
'prototype.__unlink__patients(fk:any) ' +
|
|
|
|
'DELETE /physicians/:id/patients/rel/:fk',
|
|
|
|
'prototype.__exists__patients(fk:any):boolean ' +
|
|
|
|
'HEAD /physicians/:id/patients/rel/:fk',
|
|
|
|
'prototype.__get__patients(filter:object):patient ' +
|
|
|
|
'GET /physicians/:id/patients',
|
2016-09-02 12:40:11 +00:00
|
|
|
'prototype.__create__patients(data:object:patient):patient ' +
|
2014-11-17 17:44:20 +00:00
|
|
|
'POST /physicians/:id/patients',
|
|
|
|
'prototype.__delete__patients() ' +
|
|
|
|
'DELETE /physicians/:id/patients',
|
|
|
|
'prototype.__count__patients(where:object):number ' +
|
2016-04-01 09:14:26 +00:00
|
|
|
'GET /physicians/:id/patients/count',
|
|
|
|
];
|
|
|
|
expect(methods).to.include.members(expectedMethods);
|
|
|
|
});
|
2014-07-03 06:19:41 +00:00
|
|
|
});
|
2016-09-02 12:40:47 +00:00
|
|
|
|
|
|
|
it('has upsertWithWhere remote method', function() {
|
|
|
|
var storeClass = findClass('store');
|
|
|
|
var methods = getFormattedMethodsExcludingRelations(storeClass.methods);
|
|
|
|
var expectedMethods = [
|
2016-09-02 12:40:11 +00:00
|
|
|
'upsertWithWhere(where:object,data:object:store):store POST /stores/upsertWithWhere',
|
2016-09-02 12:40:47 +00:00
|
|
|
];
|
|
|
|
expect(methods).to.include.members(expectedMethods);
|
|
|
|
});
|
2014-03-21 19:18:00 +00:00
|
|
|
});
|
2016-05-09 19:24:03 +00:00
|
|
|
|
|
|
|
describe('With model.settings.replaceOnPUT false', function() {
|
|
|
|
lt.beforeEach.withApp(app);
|
|
|
|
lt.beforeEach.givenModel('storeWithReplaceOnPUTfalse');
|
|
|
|
afterEach(function(done) {
|
|
|
|
this.app.models.storeWithReplaceOnPUTfalse.destroyAll(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should have expected remote methods',
|
|
|
|
function() {
|
|
|
|
var storeClass = findClass('storeWithReplaceOnPUTfalse');
|
|
|
|
var methods = getFormattedMethodsExcludingRelations(storeClass.methods);
|
|
|
|
|
|
|
|
var expectedMethods = [
|
2016-09-02 12:40:11 +00:00
|
|
|
'create(data:object:storeWithReplaceOnPUTfalse):storeWithReplaceOnPUTfalse POST /stores-updating',
|
|
|
|
'patchOrCreate(data:object:storeWithReplaceOnPUTfalse):storeWithReplaceOnPUTfalse PUT /stores-updating',
|
|
|
|
'patchOrCreate(data:object:storeWithReplaceOnPUTfalse):storeWithReplaceOnPUTfalse PATCH /stores-updating',
|
|
|
|
'replaceOrCreate(data:object:storeWithReplaceOnPUTfalse):storeWithReplaceOnPUTfalse POST /stores-updating/replaceOrCreate',
|
|
|
|
'upsertWithWhere(where:object,data:object:storeWithReplaceOnPUTfalse):storeWithReplaceOnPUTfalse POST /stores-updating/upsertWithWhere',
|
2016-08-24 20:19:06 +00:00
|
|
|
'exists(id:any):boolean GET /stores-updating/:id/exists',
|
|
|
|
'exists(id:any):boolean HEAD /stores-updating/:id',
|
|
|
|
'findById(id:any,filter:object):storeWithReplaceOnPUTfalse GET /stores-updating/:id',
|
2016-09-02 12:40:11 +00:00
|
|
|
'replaceById(id:any,data:object:storeWithReplaceOnPUTfalse):storeWithReplaceOnPUTfalse POST /stores-updating/:id/replace',
|
2016-08-24 20:19:06 +00:00
|
|
|
'find(filter:object):storeWithReplaceOnPUTfalse GET /stores-updating',
|
|
|
|
'findOne(filter:object):storeWithReplaceOnPUTfalse GET /stores-updating/findOne',
|
2016-09-02 12:40:11 +00:00
|
|
|
'updateAll(where:object,data:object:storeWithReplaceOnPUTfalse):object POST /stores-updating/update',
|
2016-08-24 20:19:06 +00:00
|
|
|
'deleteById(id:any):object DELETE /stores-updating/:id',
|
|
|
|
'count(where:object):number GET /stores-updating/count',
|
2016-09-02 12:40:11 +00:00
|
|
|
'prototype.patchAttributes(data:object:storeWithReplaceOnPUTfalse):storeWithReplaceOnPUTfalse PUT /stores-updating/:id',
|
|
|
|
'prototype.patchAttributes(data:object:storeWithReplaceOnPUTfalse):storeWithReplaceOnPUTfalse PATCH /stores-updating/:id',
|
2016-08-24 20:19:06 +00:00
|
|
|
'createChangeStream(options:object):ReadableStream POST /stores-updating/change-stream',
|
|
|
|
'createChangeStream(options:object):ReadableStream GET /stores-updating/change-stream',
|
2016-05-09 19:24:03 +00:00
|
|
|
];
|
|
|
|
|
2016-08-24 20:19:06 +00:00
|
|
|
expect(methods).to.eql(expectedMethods);
|
2016-05-09 19:24:03 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('With model.settings.replaceOnPUT true', function() {
|
|
|
|
lt.beforeEach.withApp(app);
|
|
|
|
lt.beforeEach.givenModel('storeWithReplaceOnPUTtrue');
|
|
|
|
afterEach(function(done) {
|
|
|
|
this.app.models.storeWithReplaceOnPUTtrue.destroyAll(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should have expected remote methods',
|
|
|
|
function() {
|
|
|
|
var storeClass = findClass('storeWithReplaceOnPUTtrue');
|
|
|
|
var methods = getFormattedMethodsExcludingRelations(storeClass.methods);
|
|
|
|
|
|
|
|
var expectedMethods = [
|
2016-09-02 12:40:11 +00:00
|
|
|
'patchOrCreate(data:object:storeWithReplaceOnPUTtrue):storeWithReplaceOnPUTtrue PATCH /stores-replacing',
|
|
|
|
'replaceOrCreate(data:object:storeWithReplaceOnPUTtrue):storeWithReplaceOnPUTtrue POST /stores-replacing/replaceOrCreate',
|
|
|
|
'replaceOrCreate(data:object:storeWithReplaceOnPUTtrue):storeWithReplaceOnPUTtrue PUT /stores-replacing',
|
|
|
|
'replaceById(id:any,data:object:storeWithReplaceOnPUTtrue):storeWithReplaceOnPUTtrue POST /stores-replacing/:id/replace',
|
|
|
|
'replaceById(id:any,data:object:storeWithReplaceOnPUTtrue):storeWithReplaceOnPUTtrue PUT /stores-replacing/:id',
|
|
|
|
'prototype.patchAttributes(data:object:storeWithReplaceOnPUTtrue):storeWithReplaceOnPUTtrue PATCH /stores-replacing/:id',
|
2016-05-09 19:24:03 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
expect(methods).to.include.members(expectedMethods);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function formatReturns(m) {
|
|
|
|
var returns = m.returns;
|
|
|
|
if (!returns || returns.length === 0) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
var type = returns[0].type;
|
|
|
|
return type ? ':' + type : '';
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatMethod(m) {
|
|
|
|
var arr = [];
|
|
|
|
var endpoints = m.getEndpoints();
|
|
|
|
for (var i = 0; i < endpoints.length; i++) {
|
|
|
|
arr.push([
|
|
|
|
m.name,
|
|
|
|
'(',
|
|
|
|
m.accepts.map(function(a) {
|
2016-09-02 12:40:11 +00:00
|
|
|
return a.arg + ':' + a.type + (a.model ? ':' + a.model : '');
|
2016-05-09 19:24:03 +00:00
|
|
|
}).join(','),
|
|
|
|
')',
|
|
|
|
formatReturns(m),
|
|
|
|
' ',
|
|
|
|
endpoints[i].verb,
|
|
|
|
' ',
|
|
|
|
endpoints[i].fullPath,
|
|
|
|
].join(''));
|
|
|
|
}
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
function findClass(name) {
|
|
|
|
return app.handler('rest').adapter
|
|
|
|
.getClasses()
|
|
|
|
.filter(function(c) {
|
|
|
|
return c.name === name;
|
|
|
|
})[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
function getFormattedMethodsExcludingRelations(methods) {
|
|
|
|
return result = methods.filter(function(m) {
|
|
|
|
return m.name.indexOf('__') === -1;
|
|
|
|
})
|
|
|
|
.map(function(m) {
|
|
|
|
return formatMethod(m);
|
|
|
|
})
|
|
|
|
.reduce(function(p, c) {
|
|
|
|
return p.concat(c);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getFormattedScopeMethods(methods) {
|
|
|
|
return result = methods.filter(function(m) {
|
|
|
|
return m.name.indexOf('__') === 0;
|
|
|
|
})
|
|
|
|
.map(function(m) {
|
|
|
|
return formatMethod(m);
|
|
|
|
})
|
|
|
|
.reduce(function(p, c) {
|
|
|
|
return p.concat(c);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getFormattedPrototypeMethods(methods) {
|
|
|
|
return result = methods.filter(function(m) {
|
|
|
|
return m.name.indexOf('prototype.__') === 0;
|
|
|
|
})
|
|
|
|
.map(function(m) {
|
|
|
|
return formatMethod(m);
|
|
|
|
})
|
|
|
|
.reduce(function(p, c) {
|
|
|
|
return p.concat(c);
|
|
|
|
});
|
|
|
|
}
|