2016-04-24 22:41:37 +02:00
|
|
|
'use strict';
|
2014-12-03 15:22:03 +01:00
|
|
|
process.env.NODE_ENV = 'test';
|
|
|
|
|
2016-05-01 18:20:10 +02:00
|
|
|
var Promise = require('bluebird');
|
|
|
|
var request = require('supertest');
|
2016-05-01 22:53:09 +02:00
|
|
|
var databaseUri = require('./databaseConfig').getDatabaseUri();
|
|
|
|
var knex = require('../lib/db/dbPool')(databaseUri);
|
|
|
|
var eventDb = require('../lib/db/event')(knex);
|
|
|
|
var EventStore = require('../lib/eventStore');
|
|
|
|
var eventStore = new EventStore(eventDb);
|
|
|
|
var featureDb = require('../lib/db/feature')(knex, eventStore);
|
|
|
|
var strategyDb = require('../lib/db/strategy')(knex, eventStore);
|
|
|
|
|
|
|
|
var app = require('../app')({
|
|
|
|
baseUriPath: '',
|
|
|
|
db: knex,
|
|
|
|
eventDb: eventDb,
|
|
|
|
eventStore: eventStore,
|
|
|
|
featureDb: featureDb,
|
|
|
|
strategyDb: strategyDb
|
|
|
|
});
|
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-04-24 22:41:37 +02:00
|
|
|
], function (strategy) {
|
|
|
|
return 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([
|
|
|
|
{
|
|
|
|
"name": "featureX",
|
|
|
|
"description": "the #1 feature",
|
|
|
|
"enabled": true,
|
|
|
|
"strategy": "default"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "featureY",
|
|
|
|
"description": "soon to be the #1 feature",
|
|
|
|
"enabled": false,
|
|
|
|
"strategy": "baz",
|
|
|
|
"parameters": {
|
|
|
|
"foo": "bar"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "featureZ",
|
|
|
|
"description": "terrible feature",
|
|
|
|
"enabled": true,
|
|
|
|
"strategy": "baz",
|
|
|
|
"parameters": {
|
|
|
|
"foo": "rab"
|
|
|
|
}
|
2014-12-17 21:56:27 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "featureArchivedX",
|
|
|
|
"description": "the #1 feature",
|
|
|
|
"enabled": true,
|
|
|
|
"archived": true,
|
|
|
|
"strategy": "default"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "featureArchivedY",
|
|
|
|
"description": "soon to be the #1 feature",
|
|
|
|
"enabled": false,
|
|
|
|
"archived": true,
|
|
|
|
"strategy": "baz",
|
|
|
|
"parameters": {
|
|
|
|
"foo": "bar"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "featureArchivedZ",
|
|
|
|
"description": "terrible feature",
|
|
|
|
"enabled": true,
|
|
|
|
"archived": true,
|
|
|
|
"strategy": "baz",
|
|
|
|
"parameters": {
|
|
|
|
"foo": "rab"
|
|
|
|
}
|
2014-12-05 17:20:17 +01:00
|
|
|
}
|
2016-04-24 22:41:37 +02:00
|
|
|
], function (feature) {
|
|
|
|
return 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 = {
|
2014-12-05 17:20:17 +01:00
|
|
|
request: request,
|
|
|
|
db: {
|
|
|
|
reset: resetDatabase,
|
|
|
|
setup: setupDatabase,
|
2016-04-24 22:41:37 +02:00
|
|
|
resetAndSetup: function () {
|
|
|
|
return resetDatabase().then(setupDatabase);
|
|
|
|
}
|
2014-12-05 17:20:17 +01:00
|
|
|
}
|
2016-04-24 22:41:37 +02:00
|
|
|
};
|