2014-07-10 19:08:56 +00:00
|
|
|
'use strict';
|
2014-07-04 22:09:03 +00:00
|
|
|
|
|
|
|
var url = require('url');
|
2014-07-10 18:49:09 +00:00
|
|
|
var urlJoin = require('../lib/url-join');
|
2014-07-04 22:09:03 +00:00
|
|
|
var loopback = require('loopback');
|
2014-07-10 18:49:09 +00:00
|
|
|
var swagger = require('../lib/swagger');
|
2014-07-04 22:09:03 +00:00
|
|
|
|
|
|
|
var request = require('supertest');
|
|
|
|
var expect = require('chai').expect;
|
|
|
|
|
|
|
|
describe('swagger definition', function() {
|
2015-08-13 15:20:11 +00:00
|
|
|
describe('defaults', function() {
|
|
|
|
var swaggerResource;
|
|
|
|
before(function() {
|
|
|
|
var app = createLoopbackAppWithModel();
|
|
|
|
swaggerResource = swagger.createSwaggerObject(app);
|
2014-07-05 19:32:00 +00:00
|
|
|
});
|
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
it('advertises Swagger Spec version 2.0', function() {
|
|
|
|
expect(swaggerResource).to.have.property('swagger', '2.0');
|
|
|
|
});
|
2014-07-05 19:32:00 +00:00
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
it('has "basePath" set to "/api"', function() {
|
|
|
|
expect(swaggerResource).to.have.property('basePath', '/api');
|
2014-07-04 22:09:03 +00:00
|
|
|
});
|
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
it('uses the "host" serving the documentation', function() {
|
|
|
|
// see swagger-spec/2.0.md#fixed-fields
|
|
|
|
// If the host is not included, the host serving the documentation is to
|
|
|
|
// be used (including the port).
|
|
|
|
expect(swaggerResource).to.have.property('host', undefined);
|
|
|
|
});
|
2014-07-04 22:09:03 +00:00
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
it('uses the "schemes" serving the documentation', function() {
|
|
|
|
// see swagger-spec/2.0.md#fixed-fields
|
|
|
|
// If the schemes is not included, the default scheme to be used is the
|
|
|
|
// one used to access the Swagger definition itself.
|
|
|
|
expect(swaggerResource).to.have.property('schemes', undefined);
|
2014-07-04 22:09:03 +00:00
|
|
|
});
|
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
it('provides info.title', function() {
|
|
|
|
expect(swaggerResource.info)
|
|
|
|
.to.have.property('title', 'loopback-explorer');
|
|
|
|
});
|
|
|
|
});
|
2014-07-09 22:38:05 +00:00
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
describe('basePath', function() {
|
|
|
|
it('is "{basePath}" when basePath is a path', function() {
|
|
|
|
var app = createLoopbackAppWithModel();
|
|
|
|
var swaggerResource = swagger.createSwaggerObject(app, {
|
|
|
|
basePath: '/api-root'
|
2014-07-09 22:38:05 +00:00
|
|
|
});
|
2015-08-13 15:20:11 +00:00
|
|
|
|
|
|
|
expect(swaggerResource.basePath).to.equal('/api-root');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('is inferred from app.get("apiRoot")', function() {
|
|
|
|
var app = createLoopbackAppWithModel();
|
|
|
|
app.set('restApiRoot', '/custom-api-root');
|
|
|
|
var swaggerResource = swagger.createSwaggerObject(app);
|
|
|
|
expect(swaggerResource.basePath).to.equal('/custom-api-root');
|
2014-07-09 22:38:05 +00:00
|
|
|
});
|
2014-07-04 22:09:03 +00:00
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
it('is reachable when explorer mounting location is changed',
|
|
|
|
function(done) {
|
2014-07-09 22:38:05 +00:00
|
|
|
var explorerRoot = '/erforscher';
|
2014-10-14 11:06:01 +00:00
|
|
|
var app = givenAppWithSwagger({}, {explorerRoot: explorerRoot});
|
2014-07-04 22:09:03 +00:00
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
getSwaggerResource(app, explorerRoot).end(function(err, res) {
|
2014-07-04 22:09:03 +00:00
|
|
|
if (err) return done(err);
|
2014-07-09 22:38:05 +00:00
|
|
|
expect(res.body.basePath).to.be.a('string');
|
2014-07-04 22:09:03 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-07-26 15:53:08 +00:00
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
it('respects a hardcoded protocol (behind SSL terminator)', function() {
|
|
|
|
var app = createLoopbackAppWithModel();
|
|
|
|
var swaggerResource = swagger.createSwaggerObject(app, {
|
|
|
|
protocol: 'https'
|
2014-07-26 15:53:08 +00:00
|
|
|
});
|
2015-08-13 15:20:11 +00:00
|
|
|
expect(swaggerResource.schemes).to.eql(['https']);
|
2014-07-26 15:53:08 +00:00
|
|
|
});
|
2014-10-14 08:07:10 +00:00
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
it('supports opts.host', function() {
|
|
|
|
var app = createLoopbackAppWithModel();
|
|
|
|
var swaggerResource = swagger.createSwaggerObject(app, {
|
|
|
|
host: 'example.com:8080'
|
2015-06-23 14:36:19 +00:00
|
|
|
});
|
2015-08-13 15:20:11 +00:00
|
|
|
expect(swaggerResource.host).to.equal('example.com:8080');
|
2015-06-23 14:36:19 +00:00
|
|
|
});
|
2015-08-13 15:20:11 +00:00
|
|
|
});
|
2015-05-26 22:04:40 +00:00
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
it('has global "consumes"', function() {
|
|
|
|
var app = createLoopbackAppWithModel();
|
|
|
|
var swaggerResource = swagger.createSwaggerObject(app);
|
|
|
|
expect(swaggerResource.consumes).to.have.members([
|
|
|
|
'application/json',
|
|
|
|
'application/x-www-form-urlencoded',
|
|
|
|
'application/xml', 'text/xml'
|
|
|
|
]);
|
2014-07-04 22:09:03 +00:00
|
|
|
});
|
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
it('has global "produces"', function() {
|
|
|
|
var app = createLoopbackAppWithModel();
|
|
|
|
var swaggerResource = swagger.createSwaggerObject(app);
|
|
|
|
expect(swaggerResource.produces).to.have.members([
|
|
|
|
'application/json',
|
|
|
|
'application/xml', 'text/xml',
|
|
|
|
// JSONP content types
|
|
|
|
'application/javascript', 'text/javascript'
|
|
|
|
]);
|
|
|
|
});
|
2014-07-09 22:38:05 +00:00
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
describe('tags', function() {
|
|
|
|
it('has one tag for each model', function() {
|
|
|
|
var app = createLoopbackAppWithModel();
|
|
|
|
var swaggerResource = swagger.createSwaggerObject(app);
|
|
|
|
expect(swaggerResource.tags).to.eql([
|
|
|
|
{ name: 'Product', description: 'a-description\nline2' }
|
|
|
|
]);
|
2014-07-04 22:09:03 +00:00
|
|
|
});
|
2015-08-13 15:20:11 +00:00
|
|
|
});
|
2014-10-13 15:24:58 +00:00
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
describe('paths node', function() {
|
|
|
|
it('contains model routes for static methods', function() {
|
|
|
|
var app = createLoopbackAppWithModel();
|
|
|
|
var swaggerResource = swagger.createSwaggerObject(app);
|
|
|
|
expect(swaggerResource.paths).to.have.property('/Products');
|
|
|
|
var products = swaggerResource.paths['/Products'];
|
|
|
|
var verbs = Object.keys(products);
|
|
|
|
verbs.sort();
|
|
|
|
expect(verbs).to.eql(['get', 'post', 'put']);
|
2014-10-13 15:24:58 +00:00
|
|
|
});
|
2015-08-13 15:20:11 +00:00
|
|
|
});
|
2014-10-13 15:24:58 +00:00
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
describe('definitions node', function() {
|
|
|
|
it('properly defines basic attributes', function() {
|
|
|
|
var app = createLoopbackAppWithModel();
|
|
|
|
var swaggerResource = swagger.createSwaggerObject(app);
|
|
|
|
var data = swaggerResource.definitions.Product;
|
|
|
|
expect(data.required.sort()).to.eql(['aNum', 'foo'].sort());
|
|
|
|
expect(data.properties.foo.type).to.equal('string');
|
|
|
|
expect(data.properties.bar.type).to.equal('string');
|
|
|
|
expect(data.properties.aNum.type).to.equal('number');
|
|
|
|
// These will be Numbers for Swagger 2.0
|
|
|
|
expect(data.properties.aNum.minimum).to.equal(1);
|
|
|
|
expect(data.properties.aNum.maximum).to.equal(10);
|
|
|
|
// Should be Number even in 1.2
|
|
|
|
expect(data.properties.aNum.default).to.equal(5);
|
2014-10-13 15:24:58 +00:00
|
|
|
});
|
2014-10-14 11:06:01 +00:00
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
it('includes models from "accepts" args', function() {
|
2014-10-14 11:06:01 +00:00
|
|
|
var app = createLoopbackAppWithModel();
|
|
|
|
givenPrivateAppModel(app, 'Image');
|
|
|
|
givenSharedMethod(app.models.Product, 'setImage', {
|
|
|
|
accepts: { name: 'image', type: 'Image' }
|
|
|
|
});
|
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
var swaggerResource = swagger.createSwaggerObject(app);
|
|
|
|
expect(Object.keys(swaggerResource.definitions)).to.include('Image');
|
2014-10-14 11:06:01 +00:00
|
|
|
});
|
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
it('includes models from "returns" args', function() {
|
2014-10-14 11:06:01 +00:00
|
|
|
var app = createLoopbackAppWithModel();
|
|
|
|
givenPrivateAppModel(app, 'Image');
|
|
|
|
givenSharedMethod(app.models.Product, 'getImage', {
|
2015-08-13 15:20:11 +00:00
|
|
|
returns: { name: 'image', type: 'Image', root: true }
|
2014-10-14 11:06:01 +00:00
|
|
|
});
|
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
var swaggerResource = swagger.createSwaggerObject(app);
|
|
|
|
expect(Object.keys(swaggerResource.definitions)).to.include('Image');
|
2014-10-14 11:06:01 +00:00
|
|
|
});
|
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
it('includes "accepts" models not attached to the app', function() {
|
2014-10-14 11:06:01 +00:00
|
|
|
var app = createLoopbackAppWithModel();
|
|
|
|
loopback.createModel('Image');
|
|
|
|
givenSharedMethod(app.models.Product, 'setImage', {
|
|
|
|
accepts: { name: 'image', type: 'Image' }
|
|
|
|
});
|
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
var swaggerResource = swagger.createSwaggerObject(app);
|
|
|
|
expect(Object.keys(swaggerResource.definitions)).to.include('Image');
|
2014-10-14 11:06:01 +00:00
|
|
|
});
|
2014-10-14 11:42:09 +00:00
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
it('includes "responseMessages" models', function() {
|
2014-10-14 11:42:09 +00:00
|
|
|
var app = createLoopbackAppWithModel();
|
|
|
|
loopback.createModel('ValidationError');
|
|
|
|
givenSharedMethod(app.models.Product, 'setImage', {
|
|
|
|
errors: [{
|
|
|
|
code: '422',
|
|
|
|
message: 'Validation failed',
|
|
|
|
responseModel: 'ValidationError'
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
var swaggerResource = swagger.createSwaggerObject(app);
|
|
|
|
expect(Object.keys(swaggerResource.definitions))
|
|
|
|
.to.include('ValidationError');
|
2014-10-16 12:35:55 +00:00
|
|
|
});
|
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
it('includes nested model references in properties', function() {
|
2014-10-16 12:35:55 +00:00
|
|
|
var app = createLoopbackAppWithModel();
|
|
|
|
givenWarehouseWithAddressModels(app);
|
|
|
|
|
|
|
|
app.models.Product.defineProperty('location', { type: 'Warehouse' });
|
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
var swaggerResource = swagger.createSwaggerObject(app);
|
|
|
|
expect(Object.keys(swaggerResource.definitions))
|
|
|
|
.to.include.members(['Address', 'Warehouse']);
|
2014-10-16 12:35:55 +00:00
|
|
|
});
|
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
it('includes nested array model references in properties', function() {
|
2014-10-16 12:35:55 +00:00
|
|
|
var app = createLoopbackAppWithModel();
|
|
|
|
givenWarehouseWithAddressModels(app);
|
|
|
|
|
|
|
|
app.models.Product.defineProperty('location', { type: ['Warehouse'] });
|
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
var swaggerResource = swagger.createSwaggerObject(app);
|
|
|
|
expect(Object.keys(swaggerResource.definitions))
|
|
|
|
.to.include.members(['Address', 'Warehouse']);
|
2014-10-16 12:35:55 +00:00
|
|
|
});
|
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
it('includes nested model references in modelTo relation', function() {
|
2014-10-16 12:35:55 +00:00
|
|
|
var app = createLoopbackAppWithModel();
|
|
|
|
givenWarehouseWithAddressModels(app);
|
|
|
|
|
|
|
|
app.models.Product.belongsTo(app.models.Warehouse);
|
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
var swaggerResource = swagger.createSwaggerObject(app);
|
|
|
|
expect(Object.keys(swaggerResource.definitions))
|
|
|
|
.to.include.members(['Address', 'Warehouse']);
|
2014-10-16 12:35:55 +00:00
|
|
|
});
|
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
it('includes nested model references in modelThrough relation', function() {
|
2014-10-16 12:35:55 +00:00
|
|
|
var app = createLoopbackAppWithModel();
|
|
|
|
givenWarehouseWithAddressModels(app);
|
|
|
|
givenPrivateAppModel(app, 'ProductLocations');
|
|
|
|
|
|
|
|
app.models.Product.hasMany(app.models.Warehouse,
|
|
|
|
{ through: app.models.ProductLocations });
|
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
var swaggerResource = swagger.createSwaggerObject(app);
|
|
|
|
expect(Object.keys(swaggerResource.definitions))
|
|
|
|
.to.include.members(['Address', 'Warehouse', 'ProductLocations']);
|
2014-10-16 12:35:55 +00:00
|
|
|
});
|
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
it('includes nested model references in accept args', function() {
|
2014-10-16 12:35:55 +00:00
|
|
|
var app = createLoopbackAppWithModel();
|
|
|
|
givenWarehouseWithAddressModels(app);
|
|
|
|
|
|
|
|
givenSharedMethod(app.models.Product, 'aMethod', {
|
|
|
|
accepts: { arg: 'w', type: 'Warehouse' }
|
2014-10-14 11:42:09 +00:00
|
|
|
});
|
2014-10-16 12:35:55 +00:00
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
var swaggerResource = swagger.createSwaggerObject(app);
|
|
|
|
expect(Object.keys(swaggerResource.definitions))
|
|
|
|
.to.include.members(['Address', 'Warehouse']);
|
2014-10-16 12:35:55 +00:00
|
|
|
});
|
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
it('includes nested array model references in accept args', function() {
|
2014-10-16 12:35:55 +00:00
|
|
|
var app = createLoopbackAppWithModel();
|
|
|
|
givenWarehouseWithAddressModels(app);
|
|
|
|
|
|
|
|
givenSharedMethod(app.models.Product, 'aMethod', {
|
2015-08-13 15:20:11 +00:00
|
|
|
accepts: { arg: 'w', type: ['Warehouse'] }
|
2014-10-16 12:35:55 +00:00
|
|
|
});
|
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
var swaggerResource = swagger.createSwaggerObject(app);
|
|
|
|
expect(Object.keys(swaggerResource.definitions))
|
|
|
|
.to.include.members(['Address', 'Warehouse']);
|
2014-10-16 12:35:55 +00:00
|
|
|
});
|
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
it('includes nested model references in return args', function() {
|
2014-10-16 12:35:55 +00:00
|
|
|
var app = createLoopbackAppWithModel();
|
|
|
|
givenWarehouseWithAddressModels(app);
|
|
|
|
|
|
|
|
givenSharedMethod(app.models.Product, 'aMethod', {
|
|
|
|
returns: { arg: 'w', type: 'Warehouse', root: true }
|
|
|
|
});
|
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
var swaggerResource = swagger.createSwaggerObject(app);
|
|
|
|
expect(Object.keys(swaggerResource.definitions))
|
|
|
|
.to.include.members(['Address', 'Warehouse']);
|
2014-10-16 12:35:55 +00:00
|
|
|
});
|
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
it('includes nested array model references in return args', function() {
|
2014-10-16 12:35:55 +00:00
|
|
|
var app = createLoopbackAppWithModel();
|
|
|
|
givenWarehouseWithAddressModels(app);
|
|
|
|
|
|
|
|
givenSharedMethod(app.models.Product, 'aMethod', {
|
|
|
|
returns: { arg: 'w', type: ['Warehouse'], root: true }
|
|
|
|
});
|
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
var swaggerResource = swagger.createSwaggerObject(app);
|
|
|
|
expect(Object.keys(swaggerResource.definitions))
|
|
|
|
.to.include.members(['Address', 'Warehouse']);
|
2014-10-16 12:35:55 +00:00
|
|
|
});
|
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
it('includes nested model references in error responses', function() {
|
2014-10-16 12:35:55 +00:00
|
|
|
var app = createLoopbackAppWithModel();
|
|
|
|
givenWarehouseWithAddressModels(app);
|
|
|
|
|
|
|
|
givenSharedMethod(app.models.Product, 'aMethod', {
|
|
|
|
errors: {
|
|
|
|
code: '222',
|
|
|
|
message: 'Warehouse',
|
|
|
|
responseModel: 'Warehouse'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
var swaggerResource = swagger.createSwaggerObject(app);
|
|
|
|
expect(Object.keys(swaggerResource.definitions))
|
|
|
|
.to.include.members(['Address', 'Warehouse']);
|
2014-10-14 11:42:09 +00:00
|
|
|
});
|
2014-07-04 22:09:03 +00:00
|
|
|
});
|
|
|
|
|
2014-10-07 07:19:44 +00:00
|
|
|
describe('Cross-origin resource sharing', function() {
|
|
|
|
it('allows cross-origin requests by default', function(done) {
|
2014-10-14 11:06:01 +00:00
|
|
|
var app = givenAppWithSwagger();
|
2014-10-07 07:19:44 +00:00
|
|
|
request(app)
|
2015-08-13 15:20:11 +00:00
|
|
|
.options('/explorer/swagger.json')
|
2014-10-07 07:19:44 +00:00
|
|
|
.set('Origin', 'http://example.com/')
|
|
|
|
.expect('Access-Control-Allow-Origin', /^http:\/\/example.com\/|\*/)
|
|
|
|
.expect('Access-Control-Allow-Methods', /\bGET\b/)
|
|
|
|
.end(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can be disabled by configuration', function(done) {
|
2015-08-13 15:20:11 +00:00
|
|
|
var app = givenAppWithSwagger({}, {
|
|
|
|
remoting: { cors: { origin: false } }
|
|
|
|
});
|
2014-10-07 07:19:44 +00:00
|
|
|
request(app)
|
2015-08-13 15:20:11 +00:00
|
|
|
.options('/explorer/swagger.json')
|
2014-10-07 07:19:44 +00:00
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
|
|
|
var allowOrigin = res.get('Access-Control-Allow-Origin');
|
|
|
|
expect(allowOrigin, 'Access-Control-Allow-Origin')
|
|
|
|
.to.equal(undefined);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
function getSwaggerResource(app, restPath, classPath) {
|
|
|
|
if (classPath) throw new Error('classPath is no longer supported');
|
2014-07-04 22:09:03 +00:00
|
|
|
return request(app)
|
2015-08-13 15:20:11 +00:00
|
|
|
.get(urlJoin(restPath || '/explorer', '/swagger.json'))
|
2014-07-04 22:09:03 +00:00
|
|
|
.set('Accept', 'application/json')
|
2014-10-16 12:35:55 +00:00
|
|
|
.expect(200)
|
|
|
|
.expect('Content-Type', /json/);
|
2014-07-04 22:09:03 +00:00
|
|
|
}
|
|
|
|
|
2014-07-09 22:38:05 +00:00
|
|
|
function getAPIDeclaration(app, className) {
|
2015-08-13 15:20:11 +00:00
|
|
|
return getSwaggerResource(app, '', urlJoin('/', className));
|
2014-07-09 22:38:05 +00:00
|
|
|
}
|
|
|
|
|
2014-10-14 11:06:01 +00:00
|
|
|
function givenAppWithSwagger(swaggerOptions, appConfig) {
|
|
|
|
appConfig = appConfig || {};
|
|
|
|
var app = createLoopbackAppWithModel(appConfig.apiRoot);
|
|
|
|
|
|
|
|
if (appConfig.remoting) app.set('remoting', appConfig.remoting);
|
|
|
|
if (appConfig.explorerRoot) app.set('explorerRoot', appConfig.explorerRoot);
|
|
|
|
|
|
|
|
mountExplorer(app, swaggerOptions);
|
|
|
|
return app;
|
|
|
|
}
|
|
|
|
|
|
|
|
function mountExplorer(app, options) {
|
2015-07-01 16:09:38 +00:00
|
|
|
var swaggerApp = loopback();
|
2015-08-13 15:20:11 +00:00
|
|
|
swagger.mountSwagger(app, swaggerApp, options);
|
2014-10-14 11:06:01 +00:00
|
|
|
app.use(app.get('explorerRoot') || '/explorer', swaggerApp);
|
2014-07-09 22:38:05 +00:00
|
|
|
return app;
|
2014-07-04 22:09:03 +00:00
|
|
|
}
|
|
|
|
|
2014-07-09 22:38:05 +00:00
|
|
|
function createLoopbackAppWithModel(apiRoot) {
|
2014-07-04 22:09:03 +00:00
|
|
|
var app = loopback();
|
|
|
|
|
2014-10-14 11:06:01 +00:00
|
|
|
app.dataSource('db', { connector: 'memory' });
|
|
|
|
|
2015-08-13 15:20:11 +00:00
|
|
|
var Product = loopback.createModel('Product', {
|
2014-07-04 22:09:03 +00:00
|
|
|
foo: {type: 'string', required: true},
|
|
|
|
bar: 'string',
|
|
|
|
aNum: {type: 'number', min: 1, max: 10, required: true, default: 5}
|
2015-08-13 15:20:11 +00:00
|
|
|
}, { description: ['a-description', 'line2'] });
|
|
|
|
app.model(Product, { dataSource: 'db' });
|
2014-07-05 19:32:00 +00:00
|
|
|
|
2014-07-09 22:38:05 +00:00
|
|
|
// Simulate a restApiRoot set in config
|
|
|
|
app.set('restApiRoot', apiRoot || '/api');
|
|
|
|
app.use(app.get('restApiRoot'), loopback.rest());
|
2014-07-05 19:32:00 +00:00
|
|
|
|
2014-07-04 22:09:03 +00:00
|
|
|
return app;
|
|
|
|
}
|
2014-10-14 11:06:01 +00:00
|
|
|
|
|
|
|
function givenSharedMethod(model, name, metadata) {
|
2015-08-13 15:20:11 +00:00
|
|
|
model[name] = function() {};
|
2014-10-14 11:06:01 +00:00
|
|
|
loopback.remoteMethod(model[name], metadata);
|
|
|
|
}
|
|
|
|
|
2014-10-16 12:35:55 +00:00
|
|
|
function givenPrivateAppModel(app, name, properties) {
|
|
|
|
var model = loopback.createModel(name, properties);
|
2015-08-13 15:20:11 +00:00
|
|
|
app.model(model, { dataSource: 'db', public: false });
|
2014-10-14 11:06:01 +00:00
|
|
|
}
|
2014-10-16 12:35:55 +00:00
|
|
|
|
|
|
|
function givenWarehouseWithAddressModels(app) {
|
|
|
|
givenPrivateAppModel(app, 'Address');
|
|
|
|
givenPrivateAppModel(app, 'Warehouse', {
|
|
|
|
shippingAddress: { type: 'Address' }
|
|
|
|
});
|
|
|
|
}
|
2014-07-04 22:09:03 +00:00
|
|
|
});
|