2016-05-06 00:10:55 +00:00
|
|
|
// Copyright IBM Corp. 2013,2016. All Rights Reserved.
|
|
|
|
// Node module: loopback-component-explorer
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2013-11-29 15:17:59 +00:00
|
|
|
var loopback = require('loopback');
|
|
|
|
var explorer = require('../');
|
2013-11-05 19:16:59 +00:00
|
|
|
var request = require('supertest');
|
|
|
|
var assert = require('assert');
|
2014-10-22 08:55:25 +00:00
|
|
|
var path = require('path');
|
2013-11-29 15:17:59 +00:00
|
|
|
var expect = require('chai').expect;
|
2015-01-09 13:00:22 +00:00
|
|
|
var urlJoin = require('../lib/url-join');
|
2015-02-27 09:50:10 +00:00
|
|
|
var os = require('os');
|
2013-11-05 19:16:59 +00:00
|
|
|
|
2013-11-29 15:17:59 +00:00
|
|
|
describe('explorer', function() {
|
|
|
|
describe('with default config', function() {
|
|
|
|
beforeEach(givenLoopBackAppWithExplorer());
|
2013-11-05 19:16:59 +00:00
|
|
|
|
2015-09-16 20:38:09 +00:00
|
|
|
it('should register "loopback-component-explorer" to the app', function() {
|
|
|
|
expect(this.app.get('loopback-component-explorer'))
|
2015-08-28 17:08:05 +00:00
|
|
|
.to.have.property('mountPath', '/explorer');
|
|
|
|
});
|
|
|
|
|
2013-11-29 15:17:59 +00:00
|
|
|
it('should redirect to /explorer/', function(done) {
|
|
|
|
request(this.app)
|
|
|
|
.get('/explorer')
|
2016-06-22 15:49:29 +00:00
|
|
|
.expect(301)
|
2013-11-29 15:17:59 +00:00
|
|
|
.end(done);
|
|
|
|
});
|
2013-11-05 19:16:59 +00:00
|
|
|
|
2013-11-29 15:17:59 +00:00
|
|
|
it('should serve the explorer at /explorer/', function(done) {
|
|
|
|
request(this.app)
|
|
|
|
.get('/explorer/')
|
|
|
|
.expect('Content-Type', /html/)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) throw err;
|
|
|
|
|
2014-10-22 08:55:25 +00:00
|
|
|
assert(!!~res.text.indexOf('<title>StrongLoop API Explorer</title>'),
|
2014-07-05 19:32:00 +00:00
|
|
|
'text does not contain expected string');
|
2016-05-06 14:48:38 +00:00
|
|
|
|
2013-11-29 15:17:59 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should serve correct swagger-ui config', function(done) {
|
|
|
|
request(this.app)
|
|
|
|
.get('/explorer/config.json')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
2016-05-06 14:48:38 +00:00
|
|
|
|
2013-11-29 15:17:59 +00:00
|
|
|
expect(res.body).to
|
2015-08-13 15:20:11 +00:00
|
|
|
.have.property('url', '/explorer/swagger.json');
|
2016-05-06 14:48:38 +00:00
|
|
|
|
2013-11-29 15:17:59 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-01-07 15:56:58 +00:00
|
|
|
});
|
2013-11-05 19:16:59 +00:00
|
|
|
|
2014-07-09 22:38:05 +00:00
|
|
|
describe('with custom explorer base', function() {
|
|
|
|
beforeEach(givenLoopBackAppWithExplorer('/swagger'));
|
2013-11-29 15:17:59 +00:00
|
|
|
|
2015-09-16 20:38:09 +00:00
|
|
|
it('should register "loopback-component-explorer" to the app', function() {
|
|
|
|
expect(this.app.get('loopback-component-explorer'))
|
2015-08-28 17:08:05 +00:00
|
|
|
.to.have.property('mountPath', '/swagger');
|
|
|
|
});
|
|
|
|
|
2013-11-29 15:17:59 +00:00
|
|
|
it('should serve correct swagger-ui config', function(done) {
|
|
|
|
request(this.app)
|
2014-07-09 22:38:05 +00:00
|
|
|
.get('/swagger/config.json')
|
2013-11-29 15:17:59 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
2016-05-06 14:48:38 +00:00
|
|
|
|
2013-11-29 15:17:59 +00:00
|
|
|
expect(res.body).to
|
2015-08-13 15:20:11 +00:00
|
|
|
.have.property('url', '/swagger/swagger.json');
|
2016-05-06 14:48:38 +00:00
|
|
|
|
2013-11-29 15:17:59 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2013-11-05 19:16:59 +00:00
|
|
|
});
|
2013-11-29 15:17:59 +00:00
|
|
|
|
2014-01-07 15:56:58 +00:00
|
|
|
describe('with custom app.restApiRoot', function() {
|
|
|
|
it('should serve correct swagger-ui config', function(done) {
|
|
|
|
var app = loopback();
|
|
|
|
app.set('restApiRoot', '/rest-api-root');
|
|
|
|
configureRestApiAndExplorer(app);
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
.get('/explorer/config.json')
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
2016-05-06 14:48:38 +00:00
|
|
|
|
2014-01-07 15:56:58 +00:00
|
|
|
expect(res.body).to
|
2015-08-13 15:20:11 +00:00
|
|
|
.have.property('url', '/explorer/swagger.json');
|
2016-05-06 14:48:38 +00:00
|
|
|
|
2014-01-07 15:56:58 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-01-09 13:00:22 +00:00
|
|
|
|
|
|
|
it('removes trailing slash from baseUrl', function(done) {
|
|
|
|
// SwaggerUI builds resource URL by concatenating basePath + resourcePath
|
|
|
|
// Since the resource paths are always startign with a slash,
|
|
|
|
// if the basePath ends with a slash too, an incorrect URL is produced
|
|
|
|
var app = loopback();
|
2015-08-13 15:20:11 +00:00
|
|
|
app.set('restApiRoot', '/apis/');
|
2015-01-09 13:00:22 +00:00
|
|
|
configureRestApiAndExplorer(app);
|
|
|
|
|
|
|
|
request(app)
|
2015-08-13 15:20:11 +00:00
|
|
|
.get('/explorer/swagger.json')
|
2015-01-09 13:00:22 +00:00
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
2016-05-06 14:48:38 +00:00
|
|
|
|
2015-01-09 13:00:22 +00:00
|
|
|
var baseUrl = res.body.basePath;
|
2015-08-13 15:20:11 +00:00
|
|
|
var apiPath = Object.keys(res.body.paths)[0];
|
|
|
|
expect(baseUrl + apiPath).to.equal('/apis/products');
|
2016-05-06 14:48:38 +00:00
|
|
|
|
2015-01-09 13:00:22 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-01-07 15:56:58 +00:00
|
|
|
});
|
|
|
|
|
2014-10-22 08:55:25 +00:00
|
|
|
describe('with custom front-end files', function() {
|
|
|
|
var app;
|
|
|
|
beforeEach(function setupExplorerWithUiDirs() {
|
|
|
|
app = loopback();
|
2015-07-01 16:09:38 +00:00
|
|
|
explorer(app, {
|
2016-04-22 21:09:48 +00:00
|
|
|
uiDirs: [path.resolve(__dirname, 'fixtures', 'dummy-swagger-ui')],
|
2015-07-01 16:09:38 +00:00
|
|
|
});
|
2014-10-22 08:55:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('overrides swagger-ui files', function(done) {
|
|
|
|
request(app).get('/explorer/swagger-ui.js')
|
|
|
|
.expect(200)
|
2016-05-06 14:46:02 +00:00
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
|
|
|
|
|
|
|
// expect the content of `dummy-swagger-ui/swagger-ui.js`
|
|
|
|
expect(res.text).to.contain('/* custom swagger-ui file */' + os.EOL);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
2014-10-22 08:55:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('overrides strongloop overrides', function(done) {
|
|
|
|
request(app).get('/explorer/')
|
|
|
|
.expect(200)
|
|
|
|
// expect the content of `dummy-swagger-ui/index.html`
|
2015-02-27 09:50:10 +00:00
|
|
|
.expect('custom index.html' + os.EOL)
|
2014-10-22 08:55:25 +00:00
|
|
|
.end(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-03-08 18:13:08 +00:00
|
|
|
describe('with swaggerUI option', function() {
|
|
|
|
var app;
|
|
|
|
beforeEach(function setupExplorerWithoutUI() {
|
|
|
|
app = loopback();
|
|
|
|
explorer(app, {
|
2016-04-22 21:09:48 +00:00
|
|
|
swaggerUI: false,
|
2016-03-08 18:13:08 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('overrides swagger-ui files', function(done) {
|
|
|
|
request(app).get('/explorer/swagger-ui.js')
|
|
|
|
.expect(404, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should serve config.json', function(done) {
|
|
|
|
request(app)
|
|
|
|
.get('/explorer/config.json')
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
2016-05-06 14:48:38 +00:00
|
|
|
|
2016-03-08 18:13:08 +00:00
|
|
|
expect(res.body).to
|
|
|
|
.have.property('url', '/explorer/swagger.json');
|
2016-05-06 14:48:38 +00:00
|
|
|
|
2016-03-08 18:13:08 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should serve swagger.json', function(done) {
|
|
|
|
request(app)
|
|
|
|
.get('/explorer/swagger.json')
|
|
|
|
.expect(200, done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-07-01 16:09:38 +00:00
|
|
|
describe('explorer.routes API', function() {
|
|
|
|
var app;
|
|
|
|
beforeEach(function() {
|
|
|
|
app = loopback();
|
|
|
|
var Product = loopback.PersistedModel.extend('product');
|
|
|
|
Product.attachTo(loopback.memory());
|
|
|
|
app.model(Product);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('creates explorer routes', function(done) {
|
|
|
|
app.use('/explorer', explorer.routes(app));
|
|
|
|
app.use(app.get('restApiRoot') || '/', loopback.rest());
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
.get('/explorer/config.json')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.end(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-12-31 21:49:06 +00:00
|
|
|
describe('when specifying custom static file root directories', function() {
|
|
|
|
var app;
|
|
|
|
beforeEach(function() {
|
|
|
|
app = loopback();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow `uiDirs` to be defined as an Array', function(done) {
|
2015-07-01 16:09:38 +00:00
|
|
|
explorer(app, {
|
2016-04-22 21:09:48 +00:00
|
|
|
uiDirs: [path.resolve(__dirname, 'fixtures', 'dummy-swagger-ui')],
|
2015-07-01 16:09:38 +00:00
|
|
|
});
|
2014-12-31 21:49:06 +00:00
|
|
|
|
|
|
|
request(app).get('/explorer/')
|
|
|
|
.expect(200)
|
|
|
|
// expect the content of `dummy-swagger-ui/index.html`
|
2015-02-27 09:50:10 +00:00
|
|
|
.expect('custom index.html' + os.EOL)
|
2014-12-31 21:49:06 +00:00
|
|
|
.end(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow `uiDirs` to be defined as an String', function(done) {
|
2015-07-01 16:09:38 +00:00
|
|
|
explorer(app, {
|
2016-04-22 21:09:48 +00:00
|
|
|
uiDirs: path.resolve(__dirname, 'fixtures', 'dummy-swagger-ui'),
|
2015-07-01 16:09:38 +00:00
|
|
|
});
|
2014-12-31 21:49:06 +00:00
|
|
|
|
|
|
|
request(app).get('/explorer/')
|
|
|
|
.expect(200)
|
|
|
|
// expect the content of `dummy-swagger-ui/index.html`
|
2015-02-27 09:50:10 +00:00
|
|
|
.expect('custom index.html' + os.EOL)
|
2014-12-31 21:49:06 +00:00
|
|
|
.end(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-09-03 08:37:09 +00:00
|
|
|
describe('Cross-origin resource sharing', function() {
|
|
|
|
it('allows cross-origin requests by default', function(done) {
|
|
|
|
var app = loopback();
|
|
|
|
configureRestApiAndExplorer(app, '/explorer');
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
.options('/explorer/swagger.json')
|
|
|
|
.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) {
|
|
|
|
var app = loopback();
|
2016-04-22 21:09:48 +00:00
|
|
|
app.set('remoting', { cors: { origin: false }});
|
2015-09-03 08:37:09 +00:00
|
|
|
configureRestApiAndExplorer(app, '/explorer');
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
.options('/explorer/swagger.json')
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
2016-05-06 14:48:38 +00:00
|
|
|
|
2015-09-03 08:37:09 +00:00
|
|
|
var allowOrigin = res.get('Access-Control-Allow-Origin');
|
|
|
|
expect(allowOrigin, 'Access-Control-Allow-Origin')
|
|
|
|
.to.equal(undefined);
|
2016-05-06 14:48:38 +00:00
|
|
|
|
2015-09-03 08:37:09 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-11-18 06:21:27 +00:00
|
|
|
it('updates swagger object when a new model is added', function(done) {
|
|
|
|
var app = loopback();
|
|
|
|
configureRestApiAndExplorer(app, '/explorer');
|
|
|
|
|
|
|
|
// Ensure the swagger object was built
|
|
|
|
request(app)
|
|
|
|
.get('/explorer/swagger.json')
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
|
|
|
|
// Create a new model
|
|
|
|
var Model = loopback.PersistedModel.extend('Customer');
|
|
|
|
Model.attachTo(loopback.memory());
|
|
|
|
app.model(Model);
|
|
|
|
|
|
|
|
// Request swagger.json again
|
|
|
|
request(app)
|
|
|
|
.get('/explorer/swagger.json')
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
2016-05-06 14:48:38 +00:00
|
|
|
|
2015-11-18 06:21:27 +00:00
|
|
|
var modelNames = Object.keys(res.body.definitions);
|
|
|
|
expect(modelNames).to.contain('Customer');
|
|
|
|
var paths = Object.keys(res.body.paths);
|
|
|
|
expect(paths).to.have.contain('/Customers');
|
2016-05-06 14:48:38 +00:00
|
|
|
|
2015-11-18 06:21:27 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-29 21:01:48 +00:00
|
|
|
it('updates swagger object when a remote method is disabled', function(done) {
|
|
|
|
var app = loopback();
|
|
|
|
configureRestApiAndExplorer(app, '/explorer');
|
|
|
|
|
|
|
|
// Ensure the swagger object was built
|
|
|
|
request(app)
|
|
|
|
.get('/explorer/swagger.json')
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
|
|
|
|
|
|
|
// Check the method that will be disabled
|
|
|
|
var paths = Object.keys(res.body.paths);
|
|
|
|
expect(paths).to.contain('/products/findOne');
|
|
|
|
|
|
|
|
var Product = app.models.Product;
|
|
|
|
Product.disableRemoteMethod('findOne', true);
|
|
|
|
|
|
|
|
// Request swagger.json again
|
|
|
|
request(app)
|
|
|
|
.get('/explorer/swagger.json')
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
2016-05-06 14:48:38 +00:00
|
|
|
|
2016-04-29 21:01:48 +00:00
|
|
|
var paths = Object.keys(res.body.paths);
|
|
|
|
expect(paths).to.not.contain('/products/findOne');
|
2016-05-06 14:48:38 +00:00
|
|
|
|
2016-04-29 21:01:48 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-09 22:38:05 +00:00
|
|
|
function givenLoopBackAppWithExplorer(explorerBase) {
|
2013-11-29 15:17:59 +00:00
|
|
|
return function(done) {
|
|
|
|
var app = this.app = loopback();
|
2014-07-09 22:38:05 +00:00
|
|
|
configureRestApiAndExplorer(app, explorerBase);
|
2016-05-06 14:48:38 +00:00
|
|
|
|
2013-11-29 15:17:59 +00:00
|
|
|
done();
|
2014-01-07 15:56:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-07-09 22:38:05 +00:00
|
|
|
function configureRestApiAndExplorer(app, explorerBase) {
|
2015-01-09 13:00:22 +00:00
|
|
|
var Product = loopback.PersistedModel.extend('product');
|
2014-01-07 15:56:58 +00:00
|
|
|
Product.attachTo(loopback.memory());
|
|
|
|
app.model(Product);
|
|
|
|
|
2015-07-01 16:09:38 +00:00
|
|
|
explorer(app, { mountPath: explorerBase });
|
2015-08-13 15:20:11 +00:00
|
|
|
app.set('legacyExplorer', false);
|
2014-07-09 22:38:05 +00:00
|
|
|
app.use(app.get('restApiRoot') || '/', loopback.rest());
|
2013-11-29 15:17:59 +00:00
|
|
|
}
|
2013-11-05 19:16:59 +00:00
|
|
|
});
|