1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/test/e2e/helpers/test-helper.js

200 lines
5.3 KiB
JavaScript
Raw Normal View History

'use strict';
2016-10-26 10:43:11 +02:00
process.env.NODE_ENV = 'test';
// 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');
const getApp = require('../../../lib/app');
require('db-migrate-shared').log.silence(true);
2016-11-12 11:21:40 +01:00
// because of migrator bug
delete process.env.DATABASE_URL;
const { EventEmitter } = require('events');
const eventBus = new EventEmitter();
function createApp(databaseSchema = 'test') {
2016-11-13 15:41:35 +01:00
const options = {
2016-12-03 13:45:22 +01:00
databaseUrl: require('./database-config').getDatabaseUrl(),
2016-11-13 15:41:35 +01:00
databaseSchema,
minPool: 0,
maxPool: 0,
};
const db = createDb({
databaseUrl: options.databaseUrl,
minPool: 0,
maxPool: 0,
});
2016-11-12 11:21:40 +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);
const app = getApp({ stores, eventBus });
2016-11-13 15:41:35 +01:00
return {
stores,
request: supertest(app),
destroy() {
2016-11-13 15:41:35 +01:00
return stores.db.destroy();
},
};
});
}
function createStrategies(stores) {
return [
{
2016-06-18 21:55:46 +02:00
name: 'default',
description: 'Default on or off Strategy.',
2016-12-12 17:09:44 +01:00
parameters: [],
},
{
2016-06-18 21:55:46 +02:00
name: 'usersWithEmail',
description:
'Active for users defined in the comma-separated emails-parameter.',
parameters: [{ name: 'emails', type: 'string' }],
2016-06-18 21:55:46 +02:00
},
].map(strategy => stores.strategyStore._createStrategy(strategy));
}
function createApplications(stores) {
2016-11-11 17:39:33 +01:00
return [
{
appName: 'demo-app-1',
2016-11-11 17:39:33 +01:00
strategies: ['default'],
},
{
appName: 'demo-app-2',
strategies: ['default', 'extra'],
description: 'hello',
},
].map(client => stores.clientApplicationsStore.upsert(client));
2016-11-11 17:39:33 +01:00
}
function createClientInstance(stores) {
2016-11-11 17:39:33 +01:00
return [
{
appName: 'demo-app-1',
2016-11-11 17:39:33 +01:00
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
},
{
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));
}
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));
}
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_applications').del(),
2016-11-13 15:41:35 +01:00
stores.db('client_instances').del(),
2016-11-11 17:39:33 +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(createApplications(stores))
)
);
}
module.exports = {
setupApp(name) {
return createApp(name).then(app =>
resetDatabase(app.stores)
2016-11-13 15:41:35 +01:00
.then(() => setupDatabase(app.stores))
.then(() => app)
);
2016-11-13 15:41:35 +01:00
},
};