2016-04-24 22:41:37 +02:00
|
|
|
'use strict';
|
2016-10-26 10:43:11 +02:00
|
|
|
|
2014-12-03 15:22:03 +01:00
|
|
|
process.env.NODE_ENV = 'test';
|
|
|
|
|
2016-11-18 20:49:56 +01:00
|
|
|
// because of db-migrate bug (https://github.com/Unleash/unleash/issues/171)
|
|
|
|
process.setMaxListeners(0);
|
|
|
|
|
2016-11-13 15:41:35 +01:00
|
|
|
const supertest = require('supertest');
|
2016-11-10 21:15:16 +01:00
|
|
|
const migrator = require('../../../migrator');
|
|
|
|
const { createStores } = require('../../../lib/db');
|
2016-11-13 15:41:35 +01:00
|
|
|
const { createDb } = require('../../../lib/db/db-pool');
|
2016-11-13 21:07:14 +01:00
|
|
|
const getApp = require('../../../lib/app');
|
2016-11-13 20:33:23 +01:00
|
|
|
require('db-migrate-shared').log.silence(true);
|
2016-11-05 14:08:47 +01:00
|
|
|
|
2016-11-12 11:21:40 +01:00
|
|
|
// because of migrator bug
|
|
|
|
delete process.env.DATABASE_URL;
|
2016-05-01 22:53:09 +02:00
|
|
|
|
2016-11-30 23:41:57 +01:00
|
|
|
const { EventEmitter } = require('events');
|
|
|
|
const eventBus = new EventEmitter();
|
|
|
|
|
2016-11-13 15:41:35 +01:00
|
|
|
function createApp (databaseSchema = 'test') {
|
|
|
|
const options = {
|
|
|
|
databaseUri: require('./database-config').getDatabaseUri(),
|
|
|
|
databaseSchema,
|
|
|
|
minPool: 0,
|
|
|
|
maxPool: 0,
|
|
|
|
};
|
|
|
|
const db = createDb({ databaseUri: options.databaseUri, minPool: 0, maxPool: 0 });
|
2016-11-12 11:21:40 +01:00
|
|
|
|
2016-11-13 16:14:51 +01:00
|
|
|
return db.raw(`CREATE SCHEMA IF NOT EXISTS ${options.databaseSchema}`)
|
2016-11-13 15:41:35 +01:00
|
|
|
.then(() => migrator(options))
|
|
|
|
.then(() => {
|
|
|
|
db.destroy();
|
|
|
|
const stores = createStores(options);
|
2016-11-30 23:41:57 +01:00
|
|
|
const app = getApp({ stores, eventBus });
|
2016-11-13 15:41:35 +01:00
|
|
|
return {
|
|
|
|
stores,
|
|
|
|
request: supertest(app),
|
|
|
|
destroy () {
|
|
|
|
return stores.db.destroy();
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
2014-12-05 17:20:17 +01:00
|
|
|
|
2016-11-10 21:15:16 +01:00
|
|
|
function createStrategies (stores) {
|
2016-11-10 21:32:48 +01:00
|
|
|
return [
|
2014-12-05 17:20:17 +01:00
|
|
|
{
|
2016-06-18 21:55:46 +02:00
|
|
|
name: 'default',
|
|
|
|
description: 'Default on or off Strategy.',
|
|
|
|
parametersTemplate: {},
|
2014-12-05 17:20:17 +01:00
|
|
|
},
|
|
|
|
{
|
2016-06-18 21:55:46 +02:00
|
|
|
name: 'usersWithEmail',
|
|
|
|
description: 'Active for users defined in the comma-separated emails-parameter.',
|
2014-12-05 17:20:17 +01:00
|
|
|
parametersTemplate: {
|
2016-06-18 21:55:46 +02:00
|
|
|
emails: 'String',
|
|
|
|
},
|
|
|
|
},
|
2016-11-10 21:32:48 +01:00
|
|
|
].map(strategy => stores.strategyStore._createStrategy(strategy));
|
2014-12-05 17:20:17 +01:00
|
|
|
}
|
2014-11-25 14:41:11 +01:00
|
|
|
|
2016-11-11 17:39:33 +01:00
|
|
|
function createClientStrategy (stores) {
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
appName: 'demo-sed',
|
|
|
|
instanceId: 'test-1',
|
|
|
|
strategies: ['default'],
|
|
|
|
started: Date.now(),
|
2016-11-13 15:41:35 +01:00
|
|
|
interval: 10,
|
2016-11-11 17:39:33 +01:00
|
|
|
},
|
|
|
|
].map(client => stores.clientStrategyStore.insert(client));
|
|
|
|
}
|
|
|
|
|
|
|
|
function createClientInstance (stores) {
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
appName: 'demo-seed',
|
|
|
|
instanceId: 'test-1',
|
|
|
|
strategies: ['default'],
|
|
|
|
started: Date.now(),
|
2016-11-13 15:41:35 +01:00
|
|
|
interval: 10,
|
2016-11-11 17:39:33 +01:00
|
|
|
},
|
2016-11-28 17:11:11 +01:00
|
|
|
{
|
|
|
|
appName: 'demo-seed-2',
|
|
|
|
instanceId: 'test-2',
|
|
|
|
strategies: ['default'],
|
|
|
|
started: Date.now(),
|
|
|
|
interval: 10,
|
|
|
|
},
|
2016-11-11 17:39:33 +01:00
|
|
|
].map(client => stores.clientInstanceStore.insert(client));
|
|
|
|
}
|
|
|
|
|
2016-11-10 21:15:16 +01:00
|
|
|
function createFeatures (stores) {
|
2016-11-10 21:32:48 +01:00
|
|
|
return [
|
2014-12-05 17:20:17 +01:00
|
|
|
{
|
2016-06-18 21:55:46 +02:00
|
|
|
name: 'featureX',
|
|
|
|
description: 'the #1 feature',
|
2016-06-18 21:53:18 +02:00
|
|
|
enabled: true,
|
2016-07-02 16:25:30 +02:00
|
|
|
strategies: [{ name: 'default', parameters: {} }],
|
2014-12-05 17:20:17 +01:00
|
|
|
},
|
|
|
|
{
|
2016-06-18 21:55:46 +02:00
|
|
|
name: 'featureY',
|
|
|
|
description: 'soon to be the #1 feature',
|
2016-06-18 21:53:18 +02:00
|
|
|
enabled: false,
|
2016-07-02 16:25:30 +02:00
|
|
|
strategies: [{
|
|
|
|
name: 'baz',
|
|
|
|
parameters: {
|
|
|
|
foo: 'bar',
|
|
|
|
},
|
|
|
|
}],
|
2014-12-05 17:20:17 +01:00
|
|
|
},
|
|
|
|
{
|
2016-06-18 21:55:46 +02:00
|
|
|
name: 'featureZ',
|
|
|
|
description: 'terrible feature',
|
2016-06-18 21:53:18 +02:00
|
|
|
enabled: true,
|
2016-07-02 16:25:30 +02:00
|
|
|
strategies: [{
|
|
|
|
name: 'baz',
|
|
|
|
parameters: {
|
|
|
|
foo: 'rab',
|
|
|
|
},
|
|
|
|
}],
|
2014-12-17 21:56:27 +01:00
|
|
|
},
|
|
|
|
{
|
2016-06-18 21:55:46 +02:00
|
|
|
name: 'featureArchivedX',
|
|
|
|
description: 'the #1 feature',
|
2016-06-18 21:53:18 +02:00
|
|
|
enabled: true,
|
|
|
|
archived: true,
|
2016-07-02 16:25:30 +02:00
|
|
|
strategies: [{ name: 'default', parameters: {} }],
|
2014-12-17 21:56:27 +01:00
|
|
|
},
|
|
|
|
{
|
2016-06-18 21:55:46 +02:00
|
|
|
name: 'featureArchivedY',
|
|
|
|
description: 'soon to be the #1 feature',
|
2016-06-18 21:53:18 +02:00
|
|
|
enabled: false,
|
|
|
|
archived: true,
|
2016-07-02 16:25:30 +02:00
|
|
|
strategies: [{
|
|
|
|
name: 'baz',
|
|
|
|
parameters: {
|
|
|
|
foo: 'bar',
|
|
|
|
},
|
|
|
|
}],
|
2014-12-17 21:56:27 +01:00
|
|
|
},
|
|
|
|
{
|
2016-06-18 21:55:46 +02:00
|
|
|
name: 'featureArchivedZ',
|
|
|
|
description: 'terrible feature',
|
2016-06-18 21:53:18 +02:00
|
|
|
enabled: true,
|
|
|
|
archived: true,
|
2016-07-02 16:25:30 +02:00
|
|
|
strategies: [{
|
|
|
|
name: 'baz',
|
|
|
|
parameters: {
|
|
|
|
foo: 'rab',
|
|
|
|
},
|
|
|
|
}],
|
2016-06-18 21:55:46 +02:00
|
|
|
},
|
2016-11-10 21:32:48 +01:00
|
|
|
].map(feature => stores.featureToggleStore._createFeature(feature));
|
2014-12-05 17:20:17 +01:00
|
|
|
}
|
2014-11-25 14:41:11 +01:00
|
|
|
|
2016-11-10 21:15:16 +01:00
|
|
|
function resetDatabase (stores) {
|
2016-11-11 17:39:33 +01:00
|
|
|
return Promise.all([
|
2016-11-13 15:41:35 +01:00
|
|
|
stores.db('strategies').del(),
|
2016-11-11 17:39:33 +01:00
|
|
|
stores.db('features').del(),
|
|
|
|
stores.db('client_strategies').del(),
|
2016-11-13 15:41:35 +01:00
|
|
|
stores.db('client_instances').del(),
|
2016-11-11 17:39:33 +01:00
|
|
|
]);
|
2014-12-05 17:20:17 +01:00
|
|
|
}
|
2014-11-25 14:41:11 +01:00
|
|
|
|
2016-11-10 21:15:16 +01:00
|
|
|
function setupDatabase (stores) {
|
2016-11-11 17:39:33 +01:00
|
|
|
return Promise.all(
|
|
|
|
createStrategies(stores)
|
|
|
|
.concat(createFeatures(stores)
|
|
|
|
.concat(createClientInstance(stores))
|
2016-11-13 15:41:35 +01:00
|
|
|
.concat(createClientStrategy(stores))));
|
2014-11-25 14:41:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2016-11-13 15:41:35 +01:00
|
|
|
setupApp (name) {
|
|
|
|
return createApp(name).then((app) => {
|
2016-11-10 21:15:16 +01:00
|
|
|
return resetDatabase(app.stores)
|
2016-11-13 15:41:35 +01:00
|
|
|
.then(() => setupDatabase(app.stores))
|
|
|
|
.then(() => app);
|
2016-11-10 21:15:16 +01:00
|
|
|
});
|
2016-11-13 15:41:35 +01:00
|
|
|
},
|
2016-04-24 22:41:37 +02:00
|
|
|
};
|