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-09 22:38:05 +00:00
|
|
|
var express = require('express');
|
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() {
|
|
|
|
describe('basePath', function() {
|
2014-07-05 19:32:00 +00:00
|
|
|
// No basepath on resource doc in 1.2
|
|
|
|
it('no longer exists on resource doc', function(done) {
|
2014-10-14 11:06:01 +00:00
|
|
|
var app = givenAppWithSwagger();
|
2014-07-04 22:09:03 +00:00
|
|
|
|
2014-07-09 22:38:05 +00:00
|
|
|
var getReq = getSwaggerResources(app);
|
2014-07-04 22:09:03 +00:00
|
|
|
getReq.end(function(err, res) {
|
2014-07-05 19:32:00 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
expect(res.body.basePath).to.equal(undefined);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-09 22:38:05 +00:00
|
|
|
it('is "http://{host}/api" by default', function(done) {
|
2014-10-14 11:06:01 +00:00
|
|
|
var app = givenAppWithSwagger();
|
2014-07-05 19:32:00 +00:00
|
|
|
|
2014-07-09 22:38:05 +00:00
|
|
|
var getReq = getAPIDeclaration(app, 'products');
|
2014-07-05 19:32:00 +00:00
|
|
|
getReq.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
2014-07-09 22:38:05 +00:00
|
|
|
expect(res.body.basePath).to.equal(url.resolve(getReq.url, '/api'));
|
2014-07-05 19:32:00 +00:00
|
|
|
done();
|
|
|
|
});
|
2014-07-04 22:09:03 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('is "http://{host}/{basePath}" when basePath is a path', function(done){
|
2014-10-14 11:06:01 +00:00
|
|
|
var app = givenAppWithSwagger({ basePath: '/api-root'});
|
2014-07-04 22:09:03 +00:00
|
|
|
|
2014-07-09 22:38:05 +00:00
|
|
|
var getReq = getAPIDeclaration(app, 'products');
|
2014-07-04 22:09:03 +00:00
|
|
|
getReq.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
|
|
|
var apiRoot = url.resolve(getReq.url, '/api-root');
|
|
|
|
expect(res.body.basePath).to.equal(apiRoot);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-09 22:38:05 +00:00
|
|
|
it('infers API basePath from app', function(done){
|
2014-10-14 11:06:01 +00:00
|
|
|
var app = givenAppWithSwagger({}, {apiRoot: '/custom-api-root'});
|
2014-07-09 22:38:05 +00:00
|
|
|
|
|
|
|
var getReq = getAPIDeclaration(app, 'products');
|
|
|
|
getReq.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
|
|
|
var apiRoot = url.resolve(getReq.url, '/custom-api-root');
|
|
|
|
expect(res.body.basePath).to.equal(apiRoot);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-07-04 22:09:03 +00:00
|
|
|
|
2014-07-09 22:38:05 +00:00
|
|
|
it('is reachable when explorer mounting location is changed', function(done){
|
|
|
|
var explorerRoot = '/erforscher';
|
2014-10-14 11:06:01 +00:00
|
|
|
var app = givenAppWithSwagger({}, {explorerRoot: explorerRoot});
|
2014-07-04 22:09:03 +00:00
|
|
|
|
2014-07-09 22:38:05 +00:00
|
|
|
var getReq = getSwaggerResources(app, explorerRoot, 'products');
|
2014-07-04 22:09:03 +00:00
|
|
|
getReq.end(function(err, res) {
|
|
|
|
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
|
|
|
|
|
|
|
it('respects a hardcoded protocol (behind SSL terminator)', function(done){
|
2014-10-14 11:06:01 +00:00
|
|
|
var app = givenAppWithSwagger({protocol: 'https'});
|
2014-07-26 15:53:08 +00:00
|
|
|
|
|
|
|
var getReq = getAPIDeclaration(app, 'products');
|
|
|
|
getReq.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
|
|
|
var parsed = url.parse(res.body.basePath);
|
|
|
|
expect(parsed.protocol).to.equal('https:');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-10-14 08:07:10 +00:00
|
|
|
|
|
|
|
it('respects X-Forwarded-Host header (behind a proxy)', function(done) {
|
2014-10-14 11:06:01 +00:00
|
|
|
var app = givenAppWithSwagger();
|
2014-10-14 08:07:10 +00:00
|
|
|
getAPIDeclaration(app, 'products')
|
|
|
|
.set('X-Forwarded-Host', 'example.com')
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
|
|
|
var baseUrl = url.parse(res.body.basePath);
|
|
|
|
expect(baseUrl.hostname).to.equal('example.com');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-10-22 09:10:15 +00:00
|
|
|
|
|
|
|
it('respects X-Forwarded-Proto header (behind a proxy)', function(done) {
|
|
|
|
var app = givenAppWithSwagger();
|
|
|
|
getAPIDeclaration(app, 'products')
|
|
|
|
.set('X-Forwarded-Proto', 'https')
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
|
|
|
var baseUrl = url.parse(res.body.basePath);
|
|
|
|
expect(baseUrl.protocol).to.equal('https:');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-07-04 22:09:03 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('Model definition attributes', function() {
|
|
|
|
it('Properly defines basic attributes', function(done) {
|
2014-10-14 11:06:01 +00:00
|
|
|
var app = givenAppWithSwagger();
|
2014-07-09 22:38:05 +00:00
|
|
|
|
|
|
|
var getReq = getAPIDeclaration(app, 'products');
|
|
|
|
getReq.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
|
|
|
var data = res.body.models.product;
|
2014-07-04 22:09:03 +00:00
|
|
|
expect(data.id).to.equal('product');
|
2014-07-10 16:32:58 +00:00
|
|
|
expect(data.required.sort()).to.eql(['aNum', 'foo'].sort());
|
2014-07-04 22:09:03 +00:00
|
|
|
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.defaultValue).to.equal(5);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-10-13 15:24:58 +00:00
|
|
|
|
|
|
|
it('includes `consumes`', function(done) {
|
2014-10-14 11:06:01 +00:00
|
|
|
var app = givenAppWithSwagger();
|
2014-10-13 15:24:58 +00:00
|
|
|
getAPIDeclaration(app, 'products').end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
|
|
|
expect(res.body.consumes).to.have.members([
|
|
|
|
'application/json',
|
|
|
|
'application/x-www-form-urlencoded',
|
|
|
|
'application/xml', 'text/xml'
|
|
|
|
]);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('includes `produces`', function(done) {
|
2014-10-14 11:06:01 +00:00
|
|
|
var app = givenAppWithSwagger();
|
2014-10-13 15:24:58 +00:00
|
|
|
getAPIDeclaration(app, 'products').end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
|
|
|
expect(res.body.produces).to.have.members([
|
|
|
|
'application/json',
|
|
|
|
'application/xml', 'text/xml',
|
|
|
|
// JSONP content types
|
|
|
|
'application/javascript', 'text/javascript'
|
|
|
|
]);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-10-14 11:06:01 +00:00
|
|
|
|
|
|
|
it('includes models from `accepts` args', function(done) {
|
|
|
|
var app = createLoopbackAppWithModel();
|
|
|
|
givenPrivateAppModel(app, 'Image');
|
|
|
|
givenSharedMethod(app.models.Product, 'setImage', {
|
|
|
|
accepts: { name: 'image', type: 'Image' }
|
|
|
|
});
|
|
|
|
mountExplorer(app);
|
|
|
|
|
|
|
|
getAPIDeclaration(app, 'products').end(function(err, res) {
|
|
|
|
expect(Object.keys(res.body.models)).to.include('Image');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('includes models from `returns` args', function(done) {
|
|
|
|
var app = createLoopbackAppWithModel();
|
|
|
|
givenPrivateAppModel(app, 'Image');
|
|
|
|
givenSharedMethod(app.models.Product, 'getImage', {
|
|
|
|
returns: { name: 'image', type: 'Image' }
|
|
|
|
});
|
|
|
|
mountExplorer(app);
|
|
|
|
|
|
|
|
getAPIDeclaration(app, 'products').end(function(err, res) {
|
|
|
|
expect(Object.keys(res.body.models)).to.include('Image');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('includes `accepts` models not attached to the app', function(done) {
|
|
|
|
var app = createLoopbackAppWithModel();
|
|
|
|
loopback.createModel('Image');
|
|
|
|
givenSharedMethod(app.models.Product, 'setImage', {
|
|
|
|
accepts: { name: 'image', type: 'Image' }
|
|
|
|
});
|
|
|
|
mountExplorer(app);
|
|
|
|
|
|
|
|
getAPIDeclaration(app, 'products').end(function(err, res) {
|
|
|
|
expect(Object.keys(res.body.models)).to.include('Image');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-10-14 11:42:09 +00:00
|
|
|
|
|
|
|
it('includes `responseMessages` models', function(done) {
|
|
|
|
var app = createLoopbackAppWithModel();
|
|
|
|
loopback.createModel('ValidationError');
|
|
|
|
givenSharedMethod(app.models.Product, 'setImage', {
|
|
|
|
errors: [{
|
|
|
|
code: '422',
|
|
|
|
message: 'Validation failed',
|
|
|
|
responseModel: 'ValidationError'
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
|
2014-10-16 12:35:55 +00:00
|
|
|
expectProductDocIncludesModels(app, 'ValidationError', done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('includes nested model references in properties', function(done) {
|
|
|
|
var app = createLoopbackAppWithModel();
|
|
|
|
givenWarehouseWithAddressModels(app);
|
|
|
|
|
|
|
|
app.models.Product.defineProperty('location', { type: 'Warehouse' });
|
|
|
|
|
|
|
|
expectProductDocIncludesModels(app, ['Address', 'Warehouse'], done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('includes nested array model references in properties', function(done) {
|
|
|
|
var app = createLoopbackAppWithModel();
|
|
|
|
givenWarehouseWithAddressModels(app);
|
|
|
|
|
|
|
|
app.models.Product.defineProperty('location', { type: ['Warehouse'] });
|
|
|
|
|
|
|
|
expectProductDocIncludesModels(app, ['Address', 'Warehouse'], done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('includes nested model references in modelTo relation', function(done) {
|
|
|
|
var app = createLoopbackAppWithModel();
|
|
|
|
givenWarehouseWithAddressModels(app);
|
|
|
|
|
|
|
|
app.models.Product.belongsTo(app.models.Warehouse);
|
|
|
|
|
|
|
|
expectProductDocIncludesModels(app, ['Address', 'Warehouse'], done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('includes nested model references in modelTo relation', function(done) {
|
|
|
|
var app = createLoopbackAppWithModel();
|
|
|
|
givenWarehouseWithAddressModels(app);
|
|
|
|
givenPrivateAppModel(app, 'ProductLocations');
|
|
|
|
|
|
|
|
app.models.Product.hasMany(app.models.Warehouse,
|
|
|
|
{ through: app.models.ProductLocations });
|
|
|
|
|
|
|
|
expectProductDocIncludesModels(
|
|
|
|
app,
|
|
|
|
['Address', 'Warehouse', 'ProductLocations'],
|
|
|
|
done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('includes nested model references in accept args', function(done) {
|
|
|
|
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
|
|
|
|
|
|
|
expectProductDocIncludesModels(app, ['Address', 'Warehouse'], done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('includes nested array model references in accept args', function(done) {
|
|
|
|
var app = createLoopbackAppWithModel();
|
|
|
|
givenWarehouseWithAddressModels(app);
|
|
|
|
|
|
|
|
givenSharedMethod(app.models.Product, 'aMethod', {
|
|
|
|
accepts: { arg: 'w', type: [ 'Warehouse' ] }
|
|
|
|
});
|
|
|
|
|
|
|
|
expectProductDocIncludesModels(app, ['Address', 'Warehouse'], done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('includes nested model references in return args', function(done) {
|
|
|
|
var app = createLoopbackAppWithModel();
|
|
|
|
givenWarehouseWithAddressModels(app);
|
|
|
|
|
|
|
|
givenSharedMethod(app.models.Product, 'aMethod', {
|
|
|
|
returns: { arg: 'w', type: 'Warehouse', root: true }
|
|
|
|
});
|
|
|
|
|
|
|
|
expectProductDocIncludesModels(app, ['Address', 'Warehouse'], done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('includes nested array model references in return args', function(done) {
|
|
|
|
var app = createLoopbackAppWithModel();
|
|
|
|
givenWarehouseWithAddressModels(app);
|
|
|
|
|
|
|
|
givenSharedMethod(app.models.Product, 'aMethod', {
|
|
|
|
returns: { arg: 'w', type: ['Warehouse'], root: true }
|
|
|
|
});
|
|
|
|
|
|
|
|
expectProductDocIncludesModels(app, ['Address', 'Warehouse'], done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('includes nested model references in error responses', function(done) {
|
|
|
|
var app = createLoopbackAppWithModel();
|
|
|
|
givenWarehouseWithAddressModels(app);
|
|
|
|
|
|
|
|
givenSharedMethod(app.models.Product, 'aMethod', {
|
|
|
|
errors: {
|
|
|
|
code: '222',
|
|
|
|
message: 'Warehouse',
|
|
|
|
responseModel: 'Warehouse'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
expectProductDocIncludesModels(app, ['Address', 'Warehouse'], done);
|
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)
|
|
|
|
.options('/explorer/resources')
|
|
|
|
.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) {
|
2014-10-14 11:06:01 +00:00
|
|
|
var app = givenAppWithSwagger({}, { remoting: { cors: { origin: false } } });
|
2014-10-07 07:19:44 +00:00
|
|
|
request(app)
|
|
|
|
.options('/explorer/resources')
|
|
|
|
.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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-09 22:38:05 +00:00
|
|
|
function getSwaggerResources(app, restPath, classPath) {
|
2014-07-04 22:09:03 +00:00
|
|
|
return request(app)
|
2014-07-10 18:49:09 +00:00
|
|
|
.get(urlJoin(restPath || '/explorer', '/resources', classPath || ''))
|
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) {
|
2014-07-10 18:49:09 +00:00
|
|
|
return getSwaggerResources(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) {
|
2014-07-09 22:38:05 +00:00
|
|
|
var swaggerApp = express();
|
|
|
|
swagger(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' });
|
|
|
|
|
2014-07-04 22:09:03 +00:00
|
|
|
var Product = loopback.Model.extend('product', {
|
|
|
|
foo: {type: 'string', required: true},
|
|
|
|
bar: 'string',
|
|
|
|
aNum: {type: 'number', min: 1, max: 10, required: true, default: 5}
|
|
|
|
});
|
2014-10-14 11:06:01 +00:00
|
|
|
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) {
|
|
|
|
model[name] = function(){};
|
|
|
|
loopback.remoteMethod(model[name], metadata);
|
|
|
|
}
|
|
|
|
|
2014-10-16 12:35:55 +00:00
|
|
|
function givenPrivateAppModel(app, name, properties) {
|
|
|
|
var model = loopback.createModel(name, properties);
|
2014-10-14 11:06:01 +00:00
|
|
|
app.model(model, { dataSource: 'db', public: false} );
|
|
|
|
}
|
2014-10-16 12:35:55 +00:00
|
|
|
|
|
|
|
function givenWarehouseWithAddressModels(app) {
|
|
|
|
givenPrivateAppModel(app, 'Address');
|
|
|
|
givenPrivateAppModel(app, 'Warehouse', {
|
|
|
|
shippingAddress: { type: 'Address' }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function expectProductDocIncludesModels(app, modelNames, done) {
|
|
|
|
if (!Array.isArray(modelNames)) modelNames = [modelNames];
|
|
|
|
|
|
|
|
mountExplorer(app);
|
|
|
|
|
|
|
|
getAPIDeclaration(app, 'products').end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
|
|
|
expect(Object.keys(res.body.models)).to.include.members(modelNames);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
2014-07-04 22:09:03 +00:00
|
|
|
});
|