From 55bc634b662549ff5d2ff6cb8b386586656e828b Mon Sep 17 00:00:00 2001 From: Ivar Date: Tue, 23 Aug 2016 00:04:11 +0200 Subject: [PATCH] User should not be allowed to post both 'strategy' and 'strategies' at the same time Relates #102 --- packages/unleash-api/lib/routes/feature.js | 9 +++++++++ packages/unleash-api/test/featureApiSpec.js | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/packages/unleash-api/lib/routes/feature.js b/packages/unleash-api/lib/routes/feature.js index 38d2cd17226..a5c6bb082f9 100644 --- a/packages/unleash-api/lib/routes/feature.js +++ b/packages/unleash-api/lib/routes/feature.js @@ -32,6 +32,7 @@ module.exports = function (app, config) { req.checkBody('name', 'Name must match format ^[0-9a-zA-Z\\.\\-]+$').matches(/^[0-9a-zA-Z\\.\\-]+$/i); validateRequest(req) + .then(validateFormat) .then(validateUniqueName) .then(() => eventStore.create({ type: eventType.featureCreated, @@ -102,4 +103,12 @@ module.exports = function (app, config) { }); }); } + + function validateFormat (req) { + if (req.body.strategy && req.body.strategies) { + return BPromise.reject(new ValidationError('Cannot use both "strategy" and "strategies".')); + } + + return BPromise.resolve(req); + } }; diff --git a/packages/unleash-api/test/featureApiSpec.js b/packages/unleash-api/test/featureApiSpec.js index 4f348b7bfda..0dc7ab85370 100644 --- a/packages/unleash-api/test/featureApiSpec.js +++ b/packages/unleash-api/test/featureApiSpec.js @@ -141,5 +141,25 @@ describe('The features api', () => { .set('Content-Type', 'application/json') .expect(200, done); }); + + it('should not be allowed to post both strategy and strategies', function (done) { + logger.setLevel('FATAL'); + request + .post('/features') + .send({ + name: 'featureConfusing', + description: 'soon to be the #14 feature', + enabled: false, + strategy: 'baz', + parameters: {}, + strategies: [ + { + name: 'baz', + parameters: { foo: 'bar' }, + }, + ] }) + .set('Content-Type', 'application/json') + .expect(400, done); + }); }); });