2015-08-31 22:16:53 +00:00
|
|
|
var async = require('async');
|
2014-06-02 16:47:46 +00:00
|
|
|
var boot = require('../');
|
|
|
|
var path = require('path');
|
|
|
|
var loopback = require('loopback');
|
|
|
|
var assert = require('assert');
|
2014-11-13 14:31:35 +00:00
|
|
|
var expect = require('chai').expect;
|
2014-10-09 19:18:36 +00:00
|
|
|
var fs = require('fs-extra');
|
2014-06-02 16:47:46 +00:00
|
|
|
var sandbox = require('./helpers/sandbox');
|
|
|
|
var appdir = require('./helpers/appdir');
|
2014-11-12 15:56:01 +00:00
|
|
|
var supertest = require('supertest');
|
2015-03-04 13:14:22 +00:00
|
|
|
var os = require('os');
|
2014-06-02 16:47:46 +00:00
|
|
|
|
|
|
|
var SIMPLE_APP = path.join(__dirname, 'fixtures', 'simple-app');
|
2015-09-25 05:25:34 +00:00
|
|
|
var ENV_APP = path.join(__dirname, 'fixtures', 'env-app');
|
2014-06-02 16:47:46 +00:00
|
|
|
|
|
|
|
var app;
|
|
|
|
|
|
|
|
describe('executor', function() {
|
|
|
|
beforeEach(sandbox.reset);
|
|
|
|
|
|
|
|
beforeEach(appdir.init);
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
app = loopback();
|
2014-11-12 15:56:01 +00:00
|
|
|
|
|
|
|
// process.bootFlags is used by simple-app/boot/*.js scripts
|
|
|
|
process.bootFlags = [];
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {
|
|
|
|
delete process.bootFlags;
|
2014-06-02 16:47:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var dummyInstructions = someInstructions({
|
2014-06-25 06:09:24 +00:00
|
|
|
config: {
|
2014-06-02 16:47:46 +00:00
|
|
|
port: 3000,
|
|
|
|
host: '127.0.0.1',
|
|
|
|
restApiRoot: '/rest-api',
|
|
|
|
foo: { bar: 'bat' },
|
|
|
|
baz: true
|
|
|
|
},
|
2014-06-13 11:14:43 +00:00
|
|
|
models: [
|
|
|
|
{
|
|
|
|
name: 'User',
|
|
|
|
config: {
|
|
|
|
dataSource: 'the-db'
|
|
|
|
}
|
2014-06-02 16:47:46 +00:00
|
|
|
}
|
2014-06-13 11:14:43 +00:00
|
|
|
],
|
2014-06-02 16:47:46 +00:00
|
|
|
dataSources: {
|
|
|
|
'the-db': {
|
|
|
|
connector: 'memory',
|
|
|
|
defaultForType: 'db'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-12-03 08:31:40 +00:00
|
|
|
describe('when booting', function() {
|
2015-02-20 21:50:41 +00:00
|
|
|
it('should set the `booting` flag during execution', function(done) {
|
2014-12-03 08:31:40 +00:00
|
|
|
expect(app.booting).to.be.undefined();
|
2015-02-20 21:50:41 +00:00
|
|
|
boot.execute(app, simpleAppInstructions(), function(err) {
|
2014-12-03 08:31:40 +00:00
|
|
|
expect(err).to.be.undefined();
|
2015-02-23 21:10:12 +00:00
|
|
|
expect(process.bootingFlagSet).to.be.true();
|
|
|
|
expect(app.booting).to.be.false();
|
2014-12-03 08:31:40 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should emit the `booted` event', function(done) {
|
|
|
|
app.on('booted', function() {
|
|
|
|
// This test fails with a timeout when the `booted` event has not been
|
|
|
|
// emitted correctly
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
boot.execute(app, dummyInstructions, function(err) {
|
|
|
|
expect(err).to.be.undefined();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work when called synchronously', function() {
|
|
|
|
boot.execute(app, dummyInstructions);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-06-13 11:14:43 +00:00
|
|
|
it('configures models', function() {
|
2014-06-02 16:47:46 +00:00
|
|
|
boot.execute(app, dummyInstructions);
|
|
|
|
assert(app.models);
|
2014-06-09 12:43:44 +00:00
|
|
|
assert(app.models.User);
|
2015-03-19 07:54:49 +00:00
|
|
|
assert.equal(app.models.User, app.registry.getModel('User'),
|
|
|
|
'Boot should not have extended built-in User model');
|
2014-06-09 12:43:44 +00:00
|
|
|
assertValidDataSource(app.models.User.dataSource);
|
|
|
|
assert.isFunc(app.models.User, 'find');
|
|
|
|
assert.isFunc(app.models.User, 'create');
|
2014-06-02 16:47:46 +00:00
|
|
|
});
|
|
|
|
|
2014-06-13 11:14:43 +00:00
|
|
|
it('defines and customizes models', function() {
|
|
|
|
appdir.writeFileSync('models/Customer.js', 'module.exports = ' +
|
2014-07-22 08:58:18 +00:00
|
|
|
function(Customer) {
|
2014-06-13 11:14:43 +00:00
|
|
|
Customer.settings._customized = 'Customer';
|
2014-07-22 08:58:18 +00:00
|
|
|
Customer.base.settings._customized = 'Base';
|
2014-06-13 11:14:43 +00:00
|
|
|
}.toString());
|
|
|
|
|
|
|
|
boot.execute(app, someInstructions({
|
|
|
|
models: [
|
|
|
|
{
|
|
|
|
name: 'Customer',
|
|
|
|
config: { dataSource: 'db' },
|
|
|
|
definition: {
|
|
|
|
name: 'Customer',
|
|
|
|
base: 'User',
|
|
|
|
},
|
|
|
|
sourceFile: path.resolve(appdir.PATH, 'models', 'Customer.js')
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}));
|
|
|
|
|
|
|
|
expect(app.models.Customer).to.exist();
|
|
|
|
expect(app.models.Customer.settings._customized).to.be.equal('Customer');
|
2015-03-19 07:54:49 +00:00
|
|
|
var UserModel = app.registry.getModel('User');
|
|
|
|
expect(UserModel.settings._customized).to.equal('Base');
|
2014-06-13 11:14:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('defines model without attaching it', function() {
|
|
|
|
boot.execute(app, someInstructions({
|
|
|
|
models: [
|
|
|
|
{
|
|
|
|
name: 'Vehicle',
|
|
|
|
config: undefined,
|
|
|
|
definition: {
|
|
|
|
name: 'Vehicle'
|
|
|
|
},
|
|
|
|
sourceFile: undefined
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Car',
|
|
|
|
config: { dataSource: 'db' },
|
|
|
|
definition: {
|
|
|
|
name: 'Car',
|
|
|
|
base: 'Vehicle',
|
|
|
|
},
|
|
|
|
sourceFile: undefined
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}));
|
|
|
|
|
|
|
|
expect(Object.keys(app.models)).to.eql(['Car']);
|
|
|
|
});
|
|
|
|
|
2014-06-02 16:47:46 +00:00
|
|
|
it('attaches models to data sources', function() {
|
|
|
|
boot.execute(app, dummyInstructions);
|
2014-06-09 12:43:44 +00:00
|
|
|
assert.equal(app.models.User.dataSource, app.dataSources.theDb);
|
2014-06-02 16:47:46 +00:00
|
|
|
});
|
|
|
|
|
2014-06-16 08:56:55 +00:00
|
|
|
it('defines all models first before running the config phase', function() {
|
|
|
|
appdir.writeFileSync('models/Customer.js', 'module.exports = ' +
|
|
|
|
function(Customer/*, Base*/) {
|
|
|
|
Customer.on('attached', function() {
|
|
|
|
Customer._modelsWhenAttached =
|
|
|
|
Object.keys(Customer.modelBuilder.models);
|
|
|
|
});
|
|
|
|
}.toString());
|
|
|
|
|
|
|
|
boot.execute(app, someInstructions({
|
|
|
|
models: [
|
|
|
|
{
|
|
|
|
name: 'Customer',
|
|
|
|
config: { dataSource: 'db' },
|
|
|
|
definition: { name: 'Customer' },
|
|
|
|
sourceFile: path.resolve(appdir.PATH, 'models', 'Customer.js')
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'UniqueName',
|
|
|
|
config: { dataSource: 'db' },
|
|
|
|
definition: { name: 'UniqueName' },
|
|
|
|
sourceFile: undefined
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}));
|
|
|
|
|
|
|
|
expect(app.models.Customer._modelsWhenAttached).to.include('UniqueName');
|
|
|
|
});
|
|
|
|
|
2015-03-19 07:54:49 +00:00
|
|
|
it('defines models in the local app registry', function() {
|
|
|
|
app = loopback({ localRegistry: true });
|
|
|
|
boot.execute(app, someInstructions({
|
|
|
|
models: [
|
|
|
|
{
|
|
|
|
name: 'LocalCustomer',
|
|
|
|
config: { dataSource: 'db' },
|
|
|
|
definition: { name: 'LocalCustomer' },
|
|
|
|
sourceFile: undefined
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}));
|
|
|
|
|
|
|
|
expect(Object.keys(loopback.registry.modelBuilder.models), 'global models')
|
|
|
|
.to.not.contain('LocalCustomer');
|
|
|
|
expect(Object.keys(app.registry.modelBuilder.models), 'local models')
|
|
|
|
.to.contain('LocalCustomer');
|
|
|
|
});
|
|
|
|
|
2015-01-08 21:07:23 +00:00
|
|
|
it('throws on bad require() call inside boot script', function() {
|
|
|
|
var file = appdir.writeFileSync('boot/badScript.js',
|
|
|
|
'require("doesnt-exist"); module.exports = {};');
|
|
|
|
|
|
|
|
function doBoot() {
|
|
|
|
boot.execute(app, someInstructions({ files: { boot: [file] } }));
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(doBoot).to.throw(/Cannot find module \'doesnt-exist\'/);
|
|
|
|
});
|
|
|
|
|
2014-06-02 16:47:46 +00:00
|
|
|
it('instantiates data sources', function() {
|
|
|
|
boot.execute(app, dummyInstructions);
|
|
|
|
assert(app.dataSources);
|
|
|
|
assert(app.dataSources.theDb);
|
|
|
|
assertValidDataSource(app.dataSources.theDb);
|
|
|
|
assert(app.dataSources.TheDb);
|
|
|
|
});
|
|
|
|
|
2014-06-09 14:31:43 +00:00
|
|
|
it('does not call autoAttach', function() {
|
|
|
|
boot.execute(app, dummyInstructions);
|
|
|
|
|
|
|
|
// loopback-datasource-juggler quirk:
|
|
|
|
// Model.dataSources has modelBuilder as the default value,
|
|
|
|
// therefore it's not enough to assert a false-y value
|
|
|
|
var actual = loopback.Email.dataSource instanceof loopback.DataSource ?
|
|
|
|
'attached' : 'not attached';
|
|
|
|
expect(actual).to.equal('not attached');
|
|
|
|
});
|
|
|
|
|
2014-10-21 08:06:28 +00:00
|
|
|
it('skips definition of already defined LoopBack models', function() {
|
|
|
|
var builtinModel = {
|
|
|
|
name: 'User',
|
|
|
|
definition: fs.readJsonFileSync(
|
|
|
|
require.resolve('loopback/common/models/user.json')
|
|
|
|
),
|
|
|
|
config: { dataSource: 'db' },
|
|
|
|
sourceFile: require.resolve('loopback/common/models/user.js')
|
|
|
|
};
|
|
|
|
builtinModel.definition.redefined = true;
|
|
|
|
|
2014-11-13 14:54:59 +00:00
|
|
|
boot.execute(app, someInstructions({ models: [builtinModel] }));
|
2014-10-21 08:06:28 +00:00
|
|
|
|
|
|
|
expect(app.models.User.settings.redefined, 'redefined').to.not.equal(true);
|
|
|
|
});
|
|
|
|
|
2014-06-02 16:47:46 +00:00
|
|
|
describe('with boot and models files', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
boot.execute(app, simpleAppInstructions());
|
|
|
|
});
|
|
|
|
|
2014-10-09 19:18:36 +00:00
|
|
|
afterEach(function() {
|
|
|
|
delete process.bootFlags;
|
2014-06-02 16:47:46 +00:00
|
|
|
});
|
2014-10-09 19:18:36 +00:00
|
|
|
|
|
|
|
it('should run `boot/*` files', function(done) {
|
|
|
|
// scripts are loaded by the order of file names
|
|
|
|
expect(process.bootFlags).to.eql([
|
|
|
|
'barLoaded',
|
|
|
|
'barSyncLoaded',
|
|
|
|
'fooLoaded',
|
|
|
|
'barStarted'
|
|
|
|
]);
|
|
|
|
|
|
|
|
// bar finished happens in the next tick
|
|
|
|
// barSync executed after bar finished
|
|
|
|
setTimeout(function() {
|
|
|
|
expect(process.bootFlags).to.eql([
|
|
|
|
'barLoaded',
|
|
|
|
'barSyncLoaded',
|
|
|
|
'fooLoaded',
|
|
|
|
'barStarted',
|
|
|
|
'barFinished',
|
|
|
|
'barSyncExecuted'
|
|
|
|
]);
|
|
|
|
done();
|
|
|
|
}, 10);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('with boot with callback', function() {
|
|
|
|
it('should run `boot/*` files asynchronously', function(done) {
|
|
|
|
boot.execute(app, simpleAppInstructions(), function() {
|
|
|
|
expect(process.bootFlags).to.eql([
|
|
|
|
'barLoaded',
|
|
|
|
'barSyncLoaded',
|
|
|
|
'fooLoaded',
|
|
|
|
'barStarted',
|
|
|
|
'barFinished',
|
|
|
|
'barSyncExecuted'
|
|
|
|
]);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-04-14 09:02:09 +00:00
|
|
|
|
2015-08-13 10:02:31 +00:00
|
|
|
describe('for mixins', function() {
|
2015-04-30 05:53:44 +00:00
|
|
|
var options;
|
|
|
|
beforeEach(function() {
|
|
|
|
appdir.writeFileSync('custom-mixins/example.js',
|
2015-04-14 09:02:09 +00:00
|
|
|
'module.exports = ' +
|
|
|
|
'function(Model, options) {}');
|
|
|
|
|
2015-04-30 05:53:44 +00:00
|
|
|
appdir.writeFileSync('custom-mixins/time-stamps.js',
|
2015-04-14 09:02:09 +00:00
|
|
|
'module.exports = ' +
|
|
|
|
'function(Model, options) {}');
|
|
|
|
|
2015-04-30 05:53:44 +00:00
|
|
|
appdir.writeConfigFileSync('custom-mixins/time-stamps.json', {
|
2015-04-14 09:02:09 +00:00
|
|
|
name: 'Timestamping'
|
|
|
|
});
|
|
|
|
|
2015-04-30 05:53:44 +00:00
|
|
|
options = {
|
|
|
|
appRootDir: appdir.PATH
|
2015-04-14 09:02:09 +00:00
|
|
|
};
|
2015-04-30 05:53:44 +00:00
|
|
|
});
|
2015-04-14 09:02:09 +00:00
|
|
|
|
2015-04-30 05:53:44 +00:00
|
|
|
it('defines mixins from instructions - using `mixinDirs`', function() {
|
|
|
|
options.mixinDirs = ['./custom-mixins'];
|
2015-04-14 09:02:09 +00:00
|
|
|
boot(app, options);
|
|
|
|
|
|
|
|
var modelBuilder = app.registry.modelBuilder;
|
2015-04-30 05:53:44 +00:00
|
|
|
var registry = modelBuilder.mixins.mixins;
|
|
|
|
expect(Object.keys(registry)).to.eql(['Example', 'Timestamping']);
|
|
|
|
});
|
2015-04-14 09:02:09 +00:00
|
|
|
|
2015-04-30 05:53:44 +00:00
|
|
|
it('defines mixins from instructions - using `mixinSources`', function() {
|
|
|
|
options.mixinSources = ['./custom-mixins'];
|
|
|
|
boot(app, options);
|
|
|
|
|
|
|
|
var modelBuilder = app.registry.modelBuilder;
|
2015-04-14 09:02:09 +00:00
|
|
|
var registry = modelBuilder.mixins.mixins;
|
|
|
|
expect(Object.keys(registry)).to.eql(['Example', 'Timestamping']);
|
|
|
|
});
|
|
|
|
});
|
2014-06-02 16:47:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('with PaaS and npm env variables', function() {
|
2015-04-22 07:08:51 +00:00
|
|
|
beforeEach(function cleanEnvironment() {
|
|
|
|
// jscs:disable requireCamelCaseOrUpperCaseIdentifiers
|
|
|
|
delete process.env.npm_config_host;
|
|
|
|
delete process.env.OPENSHIFT_SLS_IP;
|
|
|
|
delete process.env.OPENSHIFT_NODEJS_IP;
|
|
|
|
delete process.env.HOST;
|
|
|
|
delete process.env.npm_package_config_host;
|
|
|
|
|
|
|
|
delete process.env.npm_config_port;
|
|
|
|
delete process.env.OPENSHIFT_SLS_PORT;
|
|
|
|
delete process.env.OPENSHIFT_NODEJS_PORT;
|
|
|
|
delete process.env.PORT;
|
|
|
|
delete process.env.npm_package_config_port;
|
|
|
|
});
|
|
|
|
|
2014-06-02 16:47:46 +00:00
|
|
|
function bootWithDefaults() {
|
|
|
|
app = loopback();
|
|
|
|
boot.execute(app, someInstructions({
|
2014-06-25 06:09:24 +00:00
|
|
|
config: {
|
2014-06-02 16:47:46 +00:00
|
|
|
port: undefined,
|
|
|
|
host: undefined
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should honor host and port', function() {
|
|
|
|
function assertHonored(portKey, hostKey) {
|
|
|
|
process.env[hostKey] = randomPort();
|
|
|
|
process.env[portKey] = randomHost();
|
|
|
|
bootWithDefaults();
|
|
|
|
assert.equal(app.get('port'), process.env[portKey], portKey);
|
|
|
|
assert.equal(app.get('host'), process.env[hostKey], hostKey);
|
|
|
|
delete process.env[portKey];
|
|
|
|
delete process.env[hostKey];
|
|
|
|
}
|
|
|
|
|
|
|
|
assertHonored('OPENSHIFT_SLS_PORT', 'OPENSHIFT_NODEJS_IP');
|
|
|
|
assertHonored('npm_config_port', 'npm_config_host');
|
|
|
|
assertHonored('npm_package_config_port', 'npm_package_config_host');
|
|
|
|
assertHonored('OPENSHIFT_SLS_PORT', 'OPENSHIFT_SLS_IP');
|
|
|
|
assertHonored('PORT', 'HOST');
|
|
|
|
});
|
|
|
|
|
2015-04-22 07:08:51 +00:00
|
|
|
it('should prioritize host sources', function() {
|
2014-11-13 14:54:59 +00:00
|
|
|
// jscs:disable requireCamelCaseOrUpperCaseIdentifiers
|
2014-06-02 16:47:46 +00:00
|
|
|
process.env.npm_config_host = randomHost();
|
|
|
|
process.env.OPENSHIFT_SLS_IP = randomHost();
|
|
|
|
process.env.OPENSHIFT_NODEJS_IP = randomHost();
|
|
|
|
process.env.HOST = randomHost();
|
|
|
|
process.env.npm_package_config_host = randomHost();
|
|
|
|
|
|
|
|
bootWithDefaults();
|
|
|
|
assert.equal(app.get('host'), process.env.npm_config_host);
|
2015-04-22 07:08:51 +00:00
|
|
|
});
|
2014-06-02 16:47:46 +00:00
|
|
|
|
2015-04-22 07:08:51 +00:00
|
|
|
it('should prioritize port sources', function() {
|
2014-06-02 16:47:46 +00:00
|
|
|
process.env.npm_config_port = randomPort();
|
|
|
|
process.env.OPENSHIFT_SLS_PORT = randomPort();
|
|
|
|
process.env.OPENSHIFT_NODEJS_PORT = randomPort();
|
|
|
|
process.env.PORT = randomPort();
|
|
|
|
process.env.npm_package_config_port = randomPort();
|
|
|
|
|
|
|
|
bootWithDefaults();
|
|
|
|
assert.equal(app.get('port'), process.env.npm_config_port);
|
|
|
|
});
|
|
|
|
|
|
|
|
function randomHost() {
|
|
|
|
return Math.random().toString().split('.')[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
function randomPort() {
|
|
|
|
return Math.floor(Math.random() * 10000);
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should honor 0 for free port', function() {
|
2014-06-25 06:09:24 +00:00
|
|
|
boot.execute(app, someInstructions({ config: { port: 0 } }));
|
2014-06-02 16:47:46 +00:00
|
|
|
assert.equal(app.get('port'), 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should default to port 3000', function() {
|
2014-06-25 06:09:24 +00:00
|
|
|
boot.execute(app, someInstructions({ config: { port: undefined } }));
|
2014-06-02 16:47:46 +00:00
|
|
|
assert.equal(app.get('port'), 3000);
|
|
|
|
});
|
2015-04-22 07:08:51 +00:00
|
|
|
|
2015-04-29 20:37:26 +00:00
|
|
|
it('should respect named pipes port values in ENV', function() {
|
|
|
|
var NAMED_PORT = '\\.\\pipe\\test';
|
|
|
|
process.env.PORT = NAMED_PORT;
|
2015-04-22 07:08:51 +00:00
|
|
|
boot.execute(app, someInstructions({ config: { port: 3000 } }));
|
2015-04-29 20:37:26 +00:00
|
|
|
assert.equal(app.get('port'), NAMED_PORT);
|
2015-04-22 07:08:51 +00:00
|
|
|
});
|
2014-06-02 16:47:46 +00:00
|
|
|
});
|
|
|
|
|
2015-08-13 10:02:31 +00:00
|
|
|
describe('with middleware.json', function() {
|
|
|
|
it('should parse a simple config variable', function(done) {
|
|
|
|
boot.execute(app, simpleMiddlewareConfig('routes',
|
|
|
|
{ path: '${restApiRoot}' }
|
|
|
|
));
|
|
|
|
|
|
|
|
supertest(app).get('/').end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
|
|
|
expect(res.body.path).to.equal(app.get('restApiRoot'));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should parse multiple config variables', function(done) {
|
|
|
|
boot.execute(app, simpleMiddlewareConfig('routes',
|
|
|
|
{ path: '${restApiRoot}', env: '${env}' }
|
|
|
|
));
|
|
|
|
|
|
|
|
supertest(app).get('/').end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
|
|
|
expect(res.body.path).to.equal(app.get('restApiRoot'));
|
|
|
|
expect(res.body.env).to.equal(app.get('env'));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should parse config variables in an array', function(done) {
|
|
|
|
boot.execute(app, simpleMiddlewareConfig('routes',
|
|
|
|
{ paths: ['${restApiRoot}'] }
|
|
|
|
));
|
|
|
|
|
|
|
|
supertest(app).get('/').end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
|
|
|
expect(res.body.paths).to.eql(
|
|
|
|
[app.get('restApiRoot')]
|
|
|
|
);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should parse config variables in an object', function(done) {
|
|
|
|
boot.execute(app, simpleMiddlewareConfig('routes',
|
|
|
|
{ info: { path: '${restApiRoot}' } }
|
|
|
|
));
|
|
|
|
|
|
|
|
supertest(app).get('/').end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
|
|
|
expect(res.body.info).to.eql({
|
|
|
|
path: app.get('restApiRoot')
|
|
|
|
});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should parse config variables in a nested object', function(done) {
|
|
|
|
boot.execute(app, simpleMiddlewareConfig('routes',
|
|
|
|
{ nested: { info: { path: '${restApiRoot}' } } }
|
|
|
|
));
|
|
|
|
|
|
|
|
supertest(app).get('/').end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
|
|
|
expect(res.body.nested).to.eql({
|
|
|
|
info: { path: app.get('restApiRoot') }
|
|
|
|
});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-08-31 22:16:53 +00:00
|
|
|
it('should not parse invalid config variables', function(done) {
|
|
|
|
var invalidDataTypes = [undefined, function() {}];
|
|
|
|
async.each(invalidDataTypes, function(invalidDataType, cb) {
|
|
|
|
var config = simpleMiddlewareConfig('routes', {
|
|
|
|
path: invalidDataType
|
|
|
|
});
|
|
|
|
boot.execute(app, config);
|
|
|
|
|
|
|
|
supertest(app)
|
|
|
|
.get('/')
|
|
|
|
.end(function(err, res) {
|
|
|
|
expect(err).to.be.null();
|
|
|
|
expect(res.body.path).to.be.undefined();
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
}, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should parse valid config variables', function(done) {
|
|
|
|
var config = simpleMiddlewareConfig('routes', {
|
|
|
|
props: ['a', '${vVar}', 1, true, function() {}, {x:1, y: '${y}'}]
|
|
|
|
});
|
|
|
|
boot.execute(app, config);
|
|
|
|
|
|
|
|
supertest(app)
|
|
|
|
.get('/')
|
|
|
|
.end(function(err, res) {
|
|
|
|
expect(err).to.be.null();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-08-13 10:02:31 +00:00
|
|
|
});
|
|
|
|
|
2015-08-28 06:19:46 +00:00
|
|
|
describe('with component-config.json', function() {
|
|
|
|
|
|
|
|
it('should parse a simple config variable', function(done) {
|
|
|
|
boot.execute(app, simpleComponentConfig(
|
|
|
|
{ path: '${restApiRoot}' }
|
|
|
|
));
|
|
|
|
|
|
|
|
supertest(app).get('/component').end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
|
|
|
expect(res.body.path).to.equal(app.get('restApiRoot'));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should parse multiple config variables', function(done) {
|
|
|
|
boot.execute(app, simpleComponentConfig(
|
|
|
|
{ path: '${restApiRoot}', env: '${env}' }
|
|
|
|
));
|
|
|
|
|
|
|
|
supertest(app).get('/component').end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
|
|
|
expect(res.body.path).to.equal(app.get('restApiRoot'));
|
|
|
|
expect(res.body.env).to.equal(app.get('env'));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should parse config variables in an array', function(done) {
|
|
|
|
boot.execute(app, simpleComponentConfig(
|
|
|
|
{ paths: ['${restApiRoot}'] }
|
|
|
|
));
|
|
|
|
|
|
|
|
supertest(app).get('/component').end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
|
|
|
expect(res.body.paths).to.eql(
|
|
|
|
[app.get('restApiRoot')]
|
|
|
|
);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should parse config variables in an object', function(done) {
|
|
|
|
boot.execute(app, simpleComponentConfig(
|
|
|
|
{ info: { path: '${restApiRoot}' } }
|
|
|
|
));
|
|
|
|
|
|
|
|
supertest(app).get('/component').end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
|
|
|
expect(res.body.info).to.eql({
|
|
|
|
path: app.get('restApiRoot')
|
|
|
|
});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should parse config variables in a nested object', function(done) {
|
|
|
|
boot.execute(app, simpleComponentConfig(
|
|
|
|
{ nested: { info: { path: '${restApiRoot}' } } }
|
|
|
|
));
|
|
|
|
|
|
|
|
supertest(app).get('/component').end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
|
|
|
expect(res.body.nested).to.eql({
|
|
|
|
info: { path: app.get('restApiRoot') }
|
|
|
|
});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2014-06-02 16:47:46 +00:00
|
|
|
it('calls function exported by boot/init.js', function() {
|
|
|
|
var file = appdir.writeFileSync('boot/init.js',
|
|
|
|
'module.exports = function(app) { app.fnCalled = true; };');
|
|
|
|
|
|
|
|
delete app.fnCalled;
|
2014-11-13 14:54:59 +00:00
|
|
|
boot.execute(app, someInstructions({ files: { boot: [file] } }));
|
2014-06-02 16:47:46 +00:00
|
|
|
expect(app.fnCalled, 'exported fn was called').to.be.true();
|
|
|
|
});
|
2014-11-12 15:56:01 +00:00
|
|
|
|
|
|
|
it('configures middleware', function(done) {
|
|
|
|
var pushNamePath = require.resolve('./helpers/push-name-middleware');
|
|
|
|
|
|
|
|
boot.execute(app, someInstructions({
|
|
|
|
middleware: {
|
|
|
|
phases: ['initial', 'custom'],
|
|
|
|
middleware: [
|
|
|
|
{
|
|
|
|
sourceFile: pushNamePath,
|
|
|
|
config: {
|
|
|
|
phase: 'initial',
|
|
|
|
params: 'initial'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
sourceFile: pushNamePath,
|
|
|
|
config: {
|
|
|
|
phase: 'custom',
|
|
|
|
params: 'custom'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
sourceFile: pushNamePath,
|
|
|
|
config: {
|
|
|
|
phase: 'routes',
|
|
|
|
params: 'routes'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
sourceFile: pushNamePath,
|
|
|
|
config: {
|
|
|
|
phase: 'routes',
|
|
|
|
enabled: false,
|
|
|
|
params: 'disabled'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
supertest(app)
|
|
|
|
.get('/')
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
|
|
|
var names = (res.headers.names || '').split(',');
|
|
|
|
expect(names).to.eql(['initial', 'custom', 'routes']);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 21:56:28 +00:00
|
|
|
it('configures middleware using shortform', function(done) {
|
|
|
|
|
|
|
|
boot.execute(app, someInstructions({
|
|
|
|
middleware: {
|
|
|
|
middleware: [
|
|
|
|
{
|
|
|
|
sourceFile: require.resolve('loopback'),
|
|
|
|
fragment: 'static',
|
|
|
|
config: {
|
|
|
|
phase: 'files',
|
|
|
|
params: path.join(__dirname, './fixtures/simple-app/client/')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
supertest(app)
|
|
|
|
.get('/')
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
2015-03-04 13:14:22 +00:00
|
|
|
expect(res.text).to.eql(('<!DOCTYPE html>\n<html>\n<head lang="en">\n' +
|
2014-11-21 21:56:28 +00:00
|
|
|
' <meta charset="UTF-8">\n <title>simple-app</title>\n' +
|
2015-03-04 13:14:22 +00:00
|
|
|
'</head>\n<body>\n<h1>simple-app</h1>\n' +
|
|
|
|
'</body>\n</html>').replace(/\n/g, os.EOL));
|
2014-11-21 21:56:28 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-12 15:56:01 +00:00
|
|
|
it('configures middleware (end-to-end)', function(done) {
|
|
|
|
boot.execute(app, simpleAppInstructions());
|
|
|
|
|
|
|
|
supertest(app)
|
|
|
|
.get('/')
|
|
|
|
.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
|
|
|
expect(res.headers.names).to.equal('custom-middleware');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-01-06 13:00:26 +00:00
|
|
|
|
|
|
|
it('configures components', function() {
|
|
|
|
appdir.writeConfigFileSync('component-config.json', {
|
|
|
|
'./components/test-component': {
|
|
|
|
option: 'value'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
appdir.writeFileSync('components/test-component/index.js',
|
|
|
|
'module.exports = ' +
|
|
|
|
'function(app, options) { app.componentOptions = options; }');
|
|
|
|
|
|
|
|
boot(app, appdir.PATH);
|
|
|
|
|
2015-03-05 09:38:27 +00:00
|
|
|
expect(Object.keys(require.cache)).to.include(
|
|
|
|
appdir.resolve('components/test-component/index.js'));
|
|
|
|
|
2015-01-06 13:00:26 +00:00
|
|
|
expect(app.componentOptions).to.eql({ option: 'value' });
|
|
|
|
});
|
2015-01-01 09:57:09 +00:00
|
|
|
|
2015-03-05 09:38:27 +00:00
|
|
|
it('disables component when configuration is not set', function() {
|
|
|
|
appdir.writeConfigFileSync('component-config.json', {
|
|
|
|
'./components/test-component': false
|
|
|
|
});
|
|
|
|
|
|
|
|
appdir.writeFileSync('components/test-component/index.js',
|
|
|
|
'module.exports = ' +
|
|
|
|
'function(app, options) { app.componentOptions = options; }');
|
|
|
|
|
|
|
|
boot(app, appdir.PATH);
|
|
|
|
|
|
|
|
expect(Object.keys(require.cache)).to.not.include(
|
|
|
|
appdir.resolve('components/test-component/index.js'));
|
|
|
|
});
|
|
|
|
|
2015-01-01 09:57:09 +00:00
|
|
|
it('configures middleware (that requires `this`)', function(done) {
|
|
|
|
var passportPath = require.resolve('./fixtures/passport');
|
|
|
|
|
|
|
|
boot.execute(app, someInstructions({
|
|
|
|
middleware: {
|
|
|
|
phases: ['auth'],
|
|
|
|
middleware: [
|
|
|
|
{
|
|
|
|
sourceFile: passportPath,
|
|
|
|
fragment: 'initialize',
|
|
|
|
config: {
|
|
|
|
phase: 'auth:before'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
supertest(app)
|
|
|
|
.get('/')
|
|
|
|
.expect('passport', 'initialized', done);
|
|
|
|
});
|
2015-09-25 05:25:34 +00:00
|
|
|
|
|
|
|
describe('when booting with env', function() {
|
|
|
|
it('should set the `booting` flag during execution', function(done) {
|
|
|
|
expect(app.booting).to.be.undefined();
|
|
|
|
boot.execute(app, envAppInstructions(), function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
expect(app.booting).to.be.false();
|
|
|
|
expect(process.bootFlags).to.not.have.property('barLoadedInTest');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-06-02 16:47:46 +00:00
|
|
|
});
|
|
|
|
|
2015-08-13 10:02:31 +00:00
|
|
|
function simpleMiddlewareConfig(phase, params) {
|
|
|
|
return someInstructions({
|
|
|
|
middleware: {
|
|
|
|
phases: [phase],
|
|
|
|
middleware: [
|
|
|
|
{
|
|
|
|
sourceFile: path.join(__dirname, './fixtures/simple-middleware.js'),
|
|
|
|
config: {
|
|
|
|
phase: phase,
|
|
|
|
params: params
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-08-28 06:19:46 +00:00
|
|
|
function simpleComponentConfig(config) {
|
|
|
|
return someInstructions({
|
|
|
|
components: [
|
|
|
|
{
|
|
|
|
sourceFile: path.join(__dirname, './fixtures/simple-component.js'),
|
|
|
|
config: config
|
|
|
|
}
|
|
|
|
]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-06-02 16:47:46 +00:00
|
|
|
function assertValidDataSource(dataSource) {
|
|
|
|
// has methods
|
|
|
|
assert.isFunc(dataSource, 'createModel');
|
|
|
|
assert.isFunc(dataSource, 'discoverModelDefinitions');
|
|
|
|
assert.isFunc(dataSource, 'discoverSchema');
|
|
|
|
assert.isFunc(dataSource, 'enableRemote');
|
|
|
|
assert.isFunc(dataSource, 'disableRemote');
|
|
|
|
assert.isFunc(dataSource, 'defineOperation');
|
|
|
|
assert.isFunc(dataSource, 'operations');
|
|
|
|
}
|
|
|
|
|
2014-11-13 14:54:59 +00:00
|
|
|
assert.isFunc = function(obj, name) {
|
2014-06-02 16:47:46 +00:00
|
|
|
assert(obj, 'cannot assert function ' + name +
|
|
|
|
' on object that does not exist');
|
|
|
|
assert(typeof obj[name] === 'function', name + ' is not a function');
|
|
|
|
};
|
|
|
|
|
|
|
|
function someInstructions(values) {
|
|
|
|
var result = {
|
2014-06-25 06:09:24 +00:00
|
|
|
config: values.config || {},
|
2014-06-13 11:14:43 +00:00
|
|
|
models: values.models || [],
|
2014-06-16 08:56:55 +00:00
|
|
|
dataSources: values.dataSources || { db: { connector: 'memory' } },
|
2014-11-12 15:56:01 +00:00
|
|
|
middleware: values.middleware || { phases: [], middleware: [] },
|
2015-01-06 13:00:26 +00:00
|
|
|
components: values.components || [],
|
2014-06-02 16:47:46 +00:00
|
|
|
files: {
|
|
|
|
boot: []
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (values.files) {
|
|
|
|
for (var k in values.files)
|
|
|
|
result.files[k] = values.files[k];
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function simpleAppInstructions() {
|
2014-10-09 19:18:36 +00:00
|
|
|
// Copy it so that require will happend again
|
|
|
|
fs.copySync(SIMPLE_APP, appdir.PATH);
|
|
|
|
return boot.compile(appdir.PATH);
|
2014-06-02 16:47:46 +00:00
|
|
|
}
|
2015-09-25 05:25:34 +00:00
|
|
|
|
|
|
|
function envAppInstructions() {
|
|
|
|
fs.copySync(ENV_APP, appdir.PATH);
|
|
|
|
return boot.compile({
|
|
|
|
appRootDir: appdir.PATH,
|
|
|
|
env: 'test'
|
|
|
|
});
|
|
|
|
}
|