2016-04-24 22:41:37 +02:00
|
|
|
'use strict';
|
2016-10-26 10:43:11 +02:00
|
|
|
|
2014-12-03 15:22:03 +01:00
|
|
|
process.env.NODE_ENV = 'test';
|
|
|
|
|
2016-11-05 15:28:33 +01:00
|
|
|
// Mute migrator
|
|
|
|
require('db-migrate-shared').log.silence(true);
|
|
|
|
|
2016-08-22 15:12:05 +02:00
|
|
|
const BPromise = require('bluebird');
|
2016-06-18 21:53:18 +02:00
|
|
|
let request = require('supertest');
|
2016-11-05 15:28:33 +01:00
|
|
|
const migrator = require('../../migrator');
|
2016-10-27 21:02:12 +02:00
|
|
|
|
2016-11-05 14:08:47 +01:00
|
|
|
const options = {
|
|
|
|
databaseUri: require('./database-config').getDatabaseUri(),
|
|
|
|
};
|
|
|
|
|
|
|
|
const { createStores } = require('../../lib/db');
|
|
|
|
|
|
|
|
const stores = createStores(options);
|
2016-05-01 22:53:09 +02:00
|
|
|
|
2016-08-23 01:29:47 +02:00
|
|
|
const app = require('../../app')({
|
2016-05-01 22:53:09 +02:00
|
|
|
baseUriPath: '',
|
2016-11-05 14:08:47 +01:00
|
|
|
stores,
|
2016-05-01 22:53:09 +02:00
|
|
|
});
|
2014-12-05 17:20:17 +01:00
|
|
|
|
2016-08-22 15:12:05 +02:00
|
|
|
BPromise.promisifyAll(request);
|
2014-12-05 17:20:17 +01:00
|
|
|
request = request(app);
|
2014-11-25 14:41:11 +01:00
|
|
|
|
2016-07-02 11:54:50 +02:00
|
|
|
function createStrategies () {
|
2016-08-22 15:12:05 +02:00
|
|
|
return BPromise.map([
|
2014-12-05 17:20:17 +01:00
|
|
|
{
|
2016-06-18 21:55:46 +02:00
|
|
|
name: 'default',
|
|
|
|
description: 'Default on or off Strategy.',
|
|
|
|
parametersTemplate: {},
|
2014-12-05 17:20:17 +01:00
|
|
|
},
|
|
|
|
{
|
2016-06-18 21:55:46 +02:00
|
|
|
name: 'usersWithEmail',
|
|
|
|
description: 'Active for users defined in the comma-separated emails-parameter.',
|
2014-12-05 17:20:17 +01:00
|
|
|
parametersTemplate: {
|
2016-06-18 21:55:46 +02:00
|
|
|
emails: 'String',
|
|
|
|
},
|
|
|
|
},
|
2016-11-05 14:08:47 +01:00
|
|
|
], strategy => stores.strategyStore._createStrategy(strategy));
|
2014-12-05 17:20:17 +01:00
|
|
|
}
|
2014-11-25 14:41:11 +01:00
|
|
|
|
2016-07-02 11:54:50 +02:00
|
|
|
function createFeatures () {
|
2016-08-22 15:12:05 +02:00
|
|
|
return BPromise.map([
|
2014-12-05 17:20:17 +01:00
|
|
|
{
|
2016-06-18 21:55:46 +02:00
|
|
|
name: 'featureX',
|
|
|
|
description: 'the #1 feature',
|
2016-06-18 21:53:18 +02:00
|
|
|
enabled: true,
|
2016-07-02 16:25:30 +02:00
|
|
|
strategies: [{ name: 'default', parameters: {} }],
|
2014-12-05 17:20:17 +01:00
|
|
|
},
|
|
|
|
{
|
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,
|
2016-07-02 16:25:30 +02:00
|
|
|
strategies: [{
|
|
|
|
name: 'baz',
|
|
|
|
parameters: {
|
|
|
|
foo: 'bar',
|
|
|
|
},
|
|
|
|
}],
|
2014-12-05 17:20:17 +01:00
|
|
|
},
|
|
|
|
{
|
2016-06-18 21:55:46 +02:00
|
|
|
name: 'featureZ',
|
|
|
|
description: 'terrible feature',
|
2016-06-18 21:53:18 +02:00
|
|
|
enabled: true,
|
2016-07-02 16:25:30 +02:00
|
|
|
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,
|
2016-07-02 16:25:30 +02:00
|
|
|
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,
|
2016-07-02 16:25:30 +02:00
|
|
|
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,
|
2016-07-02 16:25:30 +02:00
|
|
|
strategies: [{
|
|
|
|
name: 'baz',
|
|
|
|
parameters: {
|
|
|
|
foo: 'rab',
|
|
|
|
},
|
|
|
|
}],
|
2016-06-18 21:55:46 +02:00
|
|
|
},
|
2016-11-05 14:08:47 +01:00
|
|
|
], feature => stores.featureToggleStore._createFeature(feature));
|
2014-12-05 17:20:17 +01:00
|
|
|
}
|
2014-11-25 14:41:11 +01:00
|
|
|
|
2016-07-02 11:54:50 +02:00
|
|
|
function destroyStrategies () {
|
2016-11-05 14:08:47 +01:00
|
|
|
return stores.db('strategies').del();
|
2014-12-05 17:20:17 +01:00
|
|
|
}
|
2014-11-25 14:41:11 +01:00
|
|
|
|
2016-07-02 11:54:50 +02:00
|
|
|
function destroyFeatures () {
|
2016-11-05 14:08:47 +01:00
|
|
|
return stores.db('features').del();
|
2014-11-25 14:41:11 +01:00
|
|
|
}
|
|
|
|
|
2016-07-02 11:54:50 +02:00
|
|
|
function resetDatabase () {
|
2016-08-22 15:12:05 +02:00
|
|
|
return BPromise.all([destroyStrategies(), destroyFeatures()]);
|
2014-12-05 17:20:17 +01:00
|
|
|
}
|
2014-11-25 14:41:11 +01:00
|
|
|
|
2016-07-02 11:54:50 +02:00
|
|
|
function setupDatabase () {
|
2016-11-05 15:28:33 +01:00
|
|
|
return BPromise.all([migrator(options.databaseUri), 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-07-02 11:54:50 +02:00
|
|
|
resetAndSetup () {
|
2016-04-24 22:41:37 +02:00
|
|
|
return resetDatabase().then(setupDatabase);
|
2016-06-18 21:55:46 +02:00
|
|
|
},
|
|
|
|
},
|
2016-04-24 22:41:37 +02:00
|
|
|
};
|