2016-04-24 22:41:37 +02:00
|
|
|
'use strict';
|
2014-12-03 15:22:03 +01:00
|
|
|
process.env.NODE_ENV = 'test';
|
|
|
|
|
2016-06-18 21:53:18 +02:00
|
|
|
const Promise = require('bluebird');
|
|
|
|
let request = require('supertest');
|
|
|
|
const databaseUri = require('./databaseConfig').getDatabaseUri();
|
|
|
|
const knex = require('../lib/db/dbPool')(databaseUri);
|
|
|
|
const eventDb = require('../lib/db/event')(knex);
|
|
|
|
const EventStore = require('../lib/eventStore');
|
|
|
|
const eventStore = new EventStore(eventDb);
|
|
|
|
const featureDb = require('../lib/db/feature')(knex, eventStore);
|
|
|
|
const strategyDb = require('../lib/db/strategy')(knex, eventStore);
|
2016-05-01 22:53:09 +02:00
|
|
|
|
2016-06-18 21:53:18 +02:00
|
|
|
const app = require('../app')({
|
2016-05-01 22:53:09 +02:00
|
|
|
baseUriPath: '',
|
|
|
|
db: knex,
|
2016-06-18 21:53:18 +02:00
|
|
|
eventDb,
|
|
|
|
eventStore,
|
|
|
|
featureDb,
|
|
|
|
strategyDb
|
2016-05-01 22:53:09 +02:00
|
|
|
});
|
2014-12-05 17:20:17 +01:00
|
|
|
|
|
|
|
Promise.promisifyAll(request);
|
|
|
|
request = request(app);
|
2014-11-25 14:41:11 +01:00
|
|
|
|
2014-12-05 17:20:17 +01:00
|
|
|
function createStrategies() {
|
|
|
|
return Promise.map([
|
|
|
|
{
|
|
|
|
name: "default",
|
2016-05-06 11:11:32 +02:00
|
|
|
description: "Default on or off Strategy.",
|
|
|
|
parametersTemplate: {}
|
2014-12-05 17:20:17 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "usersWithEmail",
|
|
|
|
description: "Active for users defined in the comma-separated emails-parameter.",
|
|
|
|
parametersTemplate: {
|
|
|
|
emails: "String"
|
|
|
|
}
|
|
|
|
}
|
2016-06-18 21:53:18 +02:00
|
|
|
], strategy => strategyDb._createStrategy(strategy));
|
2014-12-05 17:20:17 +01:00
|
|
|
}
|
2014-11-25 14:41:11 +01:00
|
|
|
|
2014-12-05 17:20:17 +01:00
|
|
|
function createFeatures() {
|
|
|
|
return Promise.map([
|
|
|
|
{
|
2016-06-18 21:53:18 +02:00
|
|
|
name: "featureX",
|
|
|
|
description: "the #1 feature",
|
|
|
|
enabled: true,
|
|
|
|
strategy: "default"
|
2014-12-05 17:20:17 +01:00
|
|
|
},
|
|
|
|
{
|
2016-06-18 21:53:18 +02:00
|
|
|
name: "featureY",
|
|
|
|
description: "soon to be the #1 feature",
|
|
|
|
enabled: false,
|
|
|
|
strategy: "baz",
|
|
|
|
parameters: {
|
|
|
|
foo: "bar"
|
2014-12-05 17:20:17 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2016-06-18 21:53:18 +02:00
|
|
|
name: "featureZ",
|
|
|
|
description: "terrible feature",
|
|
|
|
enabled: true,
|
|
|
|
strategy: "baz",
|
|
|
|
parameters: {
|
|
|
|
foo: "rab"
|
2014-12-05 17:20:17 +01:00
|
|
|
}
|
2014-12-17 21:56:27 +01:00
|
|
|
},
|
|
|
|
{
|
2016-06-18 21:53:18 +02:00
|
|
|
name: "featureArchivedX",
|
|
|
|
description: "the #1 feature",
|
|
|
|
enabled: true,
|
|
|
|
archived: true,
|
|
|
|
strategy: "default"
|
2014-12-17 21:56:27 +01:00
|
|
|
},
|
|
|
|
{
|
2016-06-18 21:53:18 +02:00
|
|
|
name: "featureArchivedY",
|
|
|
|
description: "soon to be the #1 feature",
|
|
|
|
enabled: false,
|
|
|
|
archived: true,
|
|
|
|
strategy: "baz",
|
|
|
|
parameters: {
|
|
|
|
foo: "bar"
|
2014-12-17 21:56:27 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2016-06-18 21:53:18 +02:00
|
|
|
name: "featureArchivedZ",
|
|
|
|
description: "terrible feature",
|
|
|
|
enabled: true,
|
|
|
|
archived: true,
|
|
|
|
strategy: "baz",
|
|
|
|
parameters: {
|
|
|
|
foo: "rab"
|
2014-12-17 21:56:27 +01:00
|
|
|
}
|
2014-12-05 17:20:17 +01:00
|
|
|
}
|
2016-06-18 21:53:18 +02:00
|
|
|
], feature => featureDb._createFeature(feature));
|
2014-12-05 17:20:17 +01:00
|
|
|
}
|
2014-11-25 14:41:11 +01:00
|
|
|
|
2014-12-05 17:20:17 +01:00
|
|
|
function destroyStrategies() {
|
|
|
|
return knex('strategies').del();
|
|
|
|
}
|
2014-11-25 14:41:11 +01:00
|
|
|
|
2014-12-05 17:20:17 +01:00
|
|
|
function destroyFeatures() {
|
|
|
|
return knex('features').del();
|
2014-11-25 14:41:11 +01:00
|
|
|
}
|
|
|
|
|
2014-12-05 17:20:17 +01:00
|
|
|
function resetDatabase() {
|
2014-12-12 14:11:09 +01:00
|
|
|
return Promise.all([destroyStrategies(), destroyFeatures()]);
|
2014-12-05 17:20:17 +01:00
|
|
|
}
|
2014-11-25 14:41:11 +01:00
|
|
|
|
2014-12-05 17:20:17 +01:00
|
|
|
function setupDatabase() {
|
|
|
|
return Promise.all([createStrategies(), createFeatures()]);
|
2014-11-25 14:41:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2016-06-18 21:53:18 +02:00
|
|
|
request,
|
2014-12-05 17:20:17 +01:00
|
|
|
db: {
|
|
|
|
reset: resetDatabase,
|
|
|
|
setup: setupDatabase,
|
2016-06-18 21:53:18 +02:00
|
|
|
resetAndSetup() {
|
2016-04-24 22:41:37 +02:00
|
|
|
return resetDatabase().then(setupDatabase);
|
|
|
|
}
|
2014-12-05 17:20:17 +01:00
|
|
|
}
|
2016-04-24 22:41:37 +02:00
|
|
|
};
|