mirror of
https://github.com/Unleash/unleash.git
synced 2025-03-23 00:16:25 +01:00
parent
2c46672784
commit
63037b35c3
@ -1,6 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
const { STRATEGY_CREATED, STRATEGY_DELETED } = require('../event-type');
|
||||
const { STRATEGY_CREATED, STRATEGY_DELETED, STRATEGY_UPDATED } = require('../event-type');
|
||||
const logger = require('../logger');
|
||||
const NotFoundError = require('../error/notfound-error');
|
||||
const STRATEGY_COLUMNS = ['name', 'description', 'parameters'];
|
||||
@ -10,6 +10,7 @@ class StrategyStore {
|
||||
constructor (db, eventStore) {
|
||||
this.db = db;
|
||||
eventStore.on(STRATEGY_CREATED, event => this._createStrategy(event.data));
|
||||
eventStore.on(STRATEGY_UPDATED, event => this._updateStrategy(event.data));
|
||||
eventStore.on(STRATEGY_DELETED, event => {
|
||||
db(TABLE)
|
||||
.where('name', event.data.name)
|
||||
@ -61,6 +62,13 @@ class StrategyStore {
|
||||
.insert(this.eventDataToRow(data))
|
||||
.catch(err => logger.error('Could not insert strategy, error was: ', err));
|
||||
}
|
||||
|
||||
_updateStrategy (data) {
|
||||
this.db(TABLE)
|
||||
.where({ name: data.name })
|
||||
.update(this.eventDataToRow(data))
|
||||
.catch(err => logger.error('Could not update strategy, error was: ', err));
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = StrategyStore;
|
||||
|
@ -3,6 +3,7 @@
|
||||
const {
|
||||
STRATEGY_CREATED,
|
||||
STRATEGY_DELETED,
|
||||
STRATEGY_UPDATED,
|
||||
FEATURE_CREATED,
|
||||
FEATURE_UPDATED,
|
||||
FEATURE_ARCHIVED,
|
||||
@ -13,6 +14,7 @@ const diff = require('deep-diff').diff;
|
||||
const strategyTypes = [
|
||||
STRATEGY_CREATED,
|
||||
STRATEGY_DELETED,
|
||||
STRATEGY_UPDATED,
|
||||
];
|
||||
|
||||
const featureTypes = [
|
||||
|
@ -7,4 +7,5 @@ module.exports = {
|
||||
FEATURE_REVIVED: 'feature-revived',
|
||||
STRATEGY_CREATED: 'strategy-created',
|
||||
STRATEGY_DELETED: 'strategy-deleted',
|
||||
STRATEGY_UPDATED: 'strategy-updated',
|
||||
};
|
||||
|
@ -75,6 +75,23 @@ module.exports = function (app, config) {
|
||||
.catch(error => handleError(req, res, error));
|
||||
});
|
||||
|
||||
app.put('/strategies/:strategyName', (req, res) => {
|
||||
const strategyName = req.params.strategyName;
|
||||
const updatedStrategy = req.body;
|
||||
|
||||
updatedStrategy.name = strategyName;
|
||||
|
||||
strategyStore.getStrategy(strategyName)
|
||||
.then(() => validateInput(updatedStrategy))
|
||||
.then(() => eventStore.store({
|
||||
type: eventType.STRATEGY_UPDATED,
|
||||
createdBy: extractUser(req),
|
||||
data: updatedStrategy,
|
||||
}))
|
||||
.then(() => res.status(200).end())
|
||||
.catch(error => handleError(req, res, error));
|
||||
});
|
||||
|
||||
function validateStrategyName (data) {
|
||||
return new Promise((resolve, reject) => {
|
||||
strategyStore.getStrategy(data.name)
|
||||
|
@ -82,3 +82,23 @@ test.serial('can\'t delete a strategy that dose not exist', async (t) => {
|
||||
.delete('/api/strategies/unknown')
|
||||
.expect(404);
|
||||
});
|
||||
|
||||
test.serial('updates a exiting strategy', async (t) => {
|
||||
const { request, destroy } = await setupApp('strategy_api_serial');
|
||||
return request
|
||||
.put('/api/strategies/default')
|
||||
.send({ name: 'default', description: 'Default is the best!', parameters: [] })
|
||||
.set('Content-Type', 'application/json')
|
||||
.expect(200)
|
||||
.then(destroy);
|
||||
});
|
||||
|
||||
test.serial('cant update a unknown strategy', async (t) => {
|
||||
const { request, destroy } = await setupApp('strategy_api_serial');
|
||||
return request
|
||||
.put('/api/strategies/unknown')
|
||||
.send({ name: 'unkown', parameters: [] })
|
||||
.set('Content-Type', 'application/json')
|
||||
.expect(404)
|
||||
.then(destroy);
|
||||
});
|
||||
|
@ -78,3 +78,35 @@ test('should not be possible to override name', () => {
|
||||
.send({ name: 'Testing', parameters: [] })
|
||||
.expect(403);
|
||||
});
|
||||
|
||||
test('should update strategy', () => {
|
||||
const name = 'AnotherStrat';
|
||||
const { request, base, strategyStore } = getSetup();
|
||||
strategyStore.addStrategy({ name, parameters: [] });
|
||||
|
||||
return request
|
||||
.put(`${base}/api/strategies/${name}`)
|
||||
.send({ name, parameters: [], description: 'added' })
|
||||
.expect(200);
|
||||
});
|
||||
|
||||
test('should not update uknown strategy', () => {
|
||||
const name = 'UnknownStrat';
|
||||
const { request, base } = getSetup();
|
||||
|
||||
return request
|
||||
.put(`${base}/api/strategies/${name}`)
|
||||
.send({ name, parameters: [], description: 'added' })
|
||||
.expect(404);
|
||||
});
|
||||
|
||||
test('should validate format when updating strategy', () => {
|
||||
const name = 'AnotherStrat';
|
||||
const { request, base, strategyStore } = getSetup();
|
||||
strategyStore.addStrategy({ name, parameters: [] });
|
||||
|
||||
return request
|
||||
.put(`${base}/api/strategies/${name}`)
|
||||
.send({ })
|
||||
.expect(400);
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user