1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-01 00:08:27 +01:00
unleash.unleash/test/e2e/util/test-helper.js

153 lines
4.0 KiB
JavaScript
Raw Normal View History

'use strict';
2016-10-26 10:43:11 +02: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
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-10 21:15:16 +01:00
process.env.DATABASE_URL = options.databaseUri
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-11-10 21:15:16 +01:00
function createStrategies (stores) {
return [
{
2016-06-18 21:55:46 +02:00
name: 'default',
description: 'Default on or off Strategy.',
parametersTemplate: {},
},
{
2016-06-18 21:55:46 +02:00
name: 'usersWithEmail',
description: 'Active for users defined in the comma-separated emails-parameter.',
parametersTemplate: {
2016-06-18 21:55:46 +02:00
emails: 'String',
},
},
].map(strategy => stores.strategyStore._createStrategy(strategy));
}
2016-11-11 17:39:33 +01:00
function createClientStrategy (stores) {
return [
{
appName: 'demo-sed',
instanceId: 'test-1',
strategies: ['default'],
started: Date.now(),
interval: 10
},
].map(client => stores.clientStrategyStore.insert(client));
}
function createClientInstance (stores) {
return [
{
appName: 'demo-seed',
instanceId: 'test-1',
strategies: ['default'],
started: Date.now(),
interval: 10
},
].map(client => stores.clientInstanceStore.insert(client));
}
2016-11-10 21:15:16 +01:00
function createFeatures (stores) {
return [
{
2016-06-18 21:55:46 +02:00
name: 'featureX',
description: 'the #1 feature',
2016-06-18 21:53:18 +02:00
enabled: true,
strategies: [{ name: 'default', parameters: {} }],
},
{
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,
strategies: [{
name: 'baz',
parameters: {
foo: 'bar',
},
}],
},
{
2016-06-18 21:55:46 +02:00
name: 'featureZ',
description: 'terrible feature',
2016-06-18 21:53:18 +02:00
enabled: true,
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,
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,
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,
strategies: [{
name: 'baz',
parameters: {
foo: 'rab',
},
}],
2016-06-18 21:55:46 +02:00
},
].map(feature => stores.featureToggleStore._createFeature(feature));
}
2016-11-10 21:15:16 +01:00
function resetDatabase (stores) {
2016-11-11 17:39:33 +01:00
return Promise.all([
stores.db('strategies').del(),
stores.db('features').del(),
stores.db('client_strategies').del(),
stores.db('client_instances').del()
]);
}
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))
.concat(createClientStrategy(stores))))
}
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);
});
}
};