From 4999bb93cf64bec7bd1e11a98cbd4ea138a33bae Mon Sep 17 00:00:00 2001 From: ivaosthu Date: Mon, 18 Dec 2017 09:08:04 +0100 Subject: [PATCH] Should not be allow creation of archived toggle closes #284 --- lib/db/feature-toggle-store.js | 8 ++++++++ lib/routes/admin-api/feature.js | 13 +++++++------ test/e2e/api/admin/feature.e2e.test.js | 15 +++++++++++++++ test/fixtures/fake-feature-toggle-store.js | 1 + 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/lib/db/feature-toggle-store.js b/lib/db/feature-toggle-store.js index 31ae86b46e..c09fc470c0 100644 --- a/lib/db/feature-toggle-store.js +++ b/lib/db/feature-toggle-store.js @@ -51,6 +51,14 @@ class FeatureToggleStore { .then(this.rowToFeature); } + hasFeatureName(name) { + return this.db + .first('name') + .from(TABLE) + .where({ name }) + .then(n => !!n); + } + getArchivedFeatures() { return this.db .select(FEATURE_COLUMNS) diff --git a/lib/routes/admin-api/feature.js b/lib/routes/admin-api/feature.js index 954f8c2938..f3e09becb3 100644 --- a/lib/routes/admin-api/feature.js +++ b/lib/routes/admin-api/feature.js @@ -97,12 +97,13 @@ module.exports.router = function(config) { function validateUniqueName(req) { return new Promise((resolve, reject) => { - featureToggleStore - .getFeature(req.body.name) - .then(() => - reject(new NameExistsError('Feature name already exist')) - ) - .catch(() => resolve(req)); + featureToggleStore.hasFeatureName(req.body.name).then(hasName => { + if (hasName) { + reject(new NameExistsError('Feature name already exist')); + } else { + resolve(req); + } + }); }); } diff --git a/test/e2e/api/admin/feature.e2e.test.js b/test/e2e/api/admin/feature.e2e.test.js index 4532a026a5..f9c9cc0815 100644 --- a/test/e2e/api/admin/feature.e2e.test.js +++ b/test/e2e/api/admin/feature.e2e.test.js @@ -193,3 +193,18 @@ test.serial( .then(destroy); } ); + +test.serial('should not be possible to create archived toggle', async t => { + t.plan(0); + const { request, destroy } = await setupApp('feature_api_serial'); + return request + .post('/api/admin/features') + .send({ + name: 'featureArchivedX', + enabled: false, + strategies: [{ name: 'default' }], + }) + .set('Content-Type', 'application/json') + .expect(403) + .then(destroy); +}); diff --git a/test/fixtures/fake-feature-toggle-store.js b/test/fixtures/fake-feature-toggle-store.js index acfb5523ce..8e348efeb2 100644 --- a/test/fixtures/fake-feature-toggle-store.js +++ b/test/fixtures/fake-feature-toggle-store.js @@ -12,6 +12,7 @@ module.exports = () => { } }, getFeatures: () => Promise.resolve(_features), + hasFeatureName: () => Promise.resolve(false), addFeature: feature => _features.push(feature), }; };