1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00
unleash.unleash/packages/unleash-api/test/e2e/specHelper.js

133 lines
3.4 KiB
JavaScript
Raw Normal View History

'use strict';
process.env.NODE_ENV = 'test';
const BPromise = require('bluebird');
2016-06-18 21:53:18 +02:00
let request = require('supertest');
const databaseUri = require('./databaseConfig').getDatabaseUri();
2016-08-23 01:29:47 +02:00
const knex = require('../../lib/db/dbPool')(databaseUri);
const eventDb = require('../../lib/db/event')(knex);
const EventStore = require('../../lib/eventStore');
2016-06-18 21:53:18 +02:00
const eventStore = new EventStore(eventDb);
2016-08-23 01:29:47 +02:00
const featureDb = require('../../lib/db/feature')(knex, eventStore);
const strategyDb = require('../../lib/db/strategy')(knex, eventStore);
2016-08-23 01:29:47 +02:00
const app = require('../../app')({
baseUriPath: '',
db: knex,
2016-06-18 21:53:18 +02:00
eventDb,
eventStore,
featureDb,
2016-06-18 21:55:46 +02:00
strategyDb,
});
BPromise.promisifyAll(request);
request = request(app);
2016-07-02 11:54:50 +02:00
function createStrategies () {
return BPromise.map([
{
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',
},
},
2016-06-18 21:53:18 +02:00
], strategy => strategyDb._createStrategy(strategy));
}
2016-07-02 11:54:50 +02:00
function createFeatures () {
return BPromise.map([
{
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
},
2016-06-18 21:53:18 +02:00
], feature => featureDb._createFeature(feature));
}
2016-07-02 11:54:50 +02:00
function destroyStrategies () {
return knex('strategies').del();
}
2016-07-02 11:54:50 +02:00
function destroyFeatures () {
return knex('features').del();
}
2016-07-02 11:54:50 +02:00
function resetDatabase () {
return BPromise.all([destroyStrategies(), destroyFeatures()]);
}
2016-07-02 11:54:50 +02:00
function setupDatabase () {
return BPromise.all([createStrategies(), createFeatures()]);
}
module.exports = {
2016-06-18 21:53:18 +02:00
request,
db: {
reset: resetDatabase,
setup: setupDatabase,
2016-07-02 11:54:50 +02:00
resetAndSetup () {
return resetDatabase().then(setupDatabase);
2016-06-18 21:55:46 +02:00
},
},
};