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-10 21:15:16 +01:00
|
|
|
let supertest = require('supertest');
|
2016-10-27 21:02:12 +02:00
|
|
|
|
2016-11-05 14:08:47 +01:00
|
|
|
const options = {
|
|
|
|
databaseUri: require('./database-config').getDatabaseUri(),
|
|
|
|
};
|
|
|
|
|
2016-11-10 21:15:16 +01:00
|
|
|
const migrator = require('../../../migrator');
|
|
|
|
const { createStores } = require('../../../lib/db');
|
2016-11-05 14:08:47 +01:00
|
|
|
|
2016-11-10 21:15:16 +01:00
|
|
|
process.env.DATABASE_URL = options.databaseUri
|
2016-05-01 22:53:09 +02:00
|
|
|
|
2016-11-10 21:15:16 +01:00
|
|
|
const createApp = migrator(options.databaseUri).then(() => {
|
|
|
|
const stores = createStores(options);
|
|
|
|
const app = require('../../../app')({stores});
|
|
|
|
return { stores, request: supertest(app) };
|
2016-05-01 22:53:09 +02:00
|
|
|
});
|
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-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 destroyStrategies (stores) {
|
2016-11-05 14:08:47 +01:00
|
|
|
return stores.db('strategies').del();
|
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 destroyFeatures (stores) {
|
2016-11-05 14:08:47 +01:00
|
|
|
return stores.db('features').del();
|
2014-11-25 14:41:11 +01:00
|
|
|
}
|
|
|
|
|
2016-11-10 21:15:16 +01:00
|
|
|
function resetDatabase (stores) {
|
2016-11-10 21:32:48 +01:00
|
|
|
return Promise.all([destroyStrategies(stores), destroyFeatures(stores)]);
|
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-10 21:32:48 +01:00
|
|
|
return Promise.all(createStrategies(stores).concat(createFeatures(stores)))
|
2014-11-25 14:41:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2016-11-10 21:15:16 +01:00
|
|
|
setupApp () {
|
|
|
|
return createApp.then((app) => {
|
|
|
|
return resetDatabase(app.stores)
|
|
|
|
.then(() => setupDatabase(app.stores))
|
|
|
|
.then(() => app);
|
|
|
|
});
|
|
|
|
}
|
2016-04-24 22:41:37 +02:00
|
|
|
};
|