From 1989c53fb08e4dffad4a1f3d1863eed60f10d707 Mon Sep 17 00:00:00 2001 From: Fredrik Strand Oseberg Date: Fri, 14 Jan 2022 11:16:17 +0100 Subject: [PATCH] fix: enable tags and validate for legacy api (#1264) * fix: enable tags and validate for legacy api * fix: move delete tag * fix: test name * fix: move /api/admin/features --- src/lib/routes/admin-api/feature.ts | 26 ++++++---- src/lib/routes/admin-api/index.ts | 10 ++-- src/test/e2e/api/admin/feature.e2e.test.ts | 60 ++++++++++++++++++++-- 3 files changed, 77 insertions(+), 19 deletions(-) diff --git a/src/lib/routes/admin-api/feature.ts b/src/lib/routes/admin-api/feature.ts index afe86a78ee..7340a09297 100644 --- a/src/lib/routes/admin-api/feature.ts +++ b/src/lib/routes/admin-api/feature.ts @@ -40,17 +40,25 @@ class FeatureController extends Controller { this.tagService = featureTagService; this.service = featureToggleServiceV2; + if (!config.disableLegacyFeaturesApi) { + this.post('/', this.createToggle, CREATE_FEATURE); + this.get('/:featureName', this.getToggle); + this.put('/:featureName', this.updateToggle, UPDATE_FEATURE); + this.delete('/:featureName', this.archiveToggle, DELETE_FEATURE); + this.post('/:featureName/toggle', this.toggle, UPDATE_FEATURE); + this.post('/:featureName/toggle/on', this.toggleOn, UPDATE_FEATURE); + this.post( + '/:featureName/toggle/off', + this.toggleOff, + UPDATE_FEATURE, + ); + + this.post('/:featureName/stale/on', this.staleOn, UPDATE_FEATURE); + this.post('/:featureName/stale/off', this.staleOff, UPDATE_FEATURE); + } + this.get('/', this.getAllToggles); - this.post('/', this.createToggle, CREATE_FEATURE); - this.get('/:featureName', this.getToggle); - this.put('/:featureName', this.updateToggle, UPDATE_FEATURE); - this.delete('/:featureName', this.archiveToggle, DELETE_FEATURE); this.post('/validate', this.validate, NONE); - this.post('/:featureName/toggle', this.toggle, UPDATE_FEATURE); - this.post('/:featureName/toggle/on', this.toggleOn, UPDATE_FEATURE); - this.post('/:featureName/toggle/off', this.toggleOff, UPDATE_FEATURE); - this.post('/:featureName/stale/on', this.staleOn, UPDATE_FEATURE); - this.post('/:featureName/stale/off', this.staleOff, UPDATE_FEATURE); this.get('/:featureName/tags', this.listTags); this.post('/:featureName/tags', this.addTag, UPDATE_FEATURE); this.delete( diff --git a/src/lib/routes/admin-api/index.ts b/src/lib/routes/admin-api/index.ts index 7c29cfb2c9..baaab64880 100644 --- a/src/lib/routes/admin-api/index.ts +++ b/src/lib/routes/admin-api/index.ts @@ -31,12 +31,10 @@ class AdminApi extends Controller { this.app.get('/', this.index); - if (!config.disableLegacyFeaturesApi) { - this.app.use( - '/features', - new FeatureController(config, services).router, - ); - } + this.app.use( + '/features', + new FeatureController(config, services).router, + ); this.app.use( '/feature-types', diff --git a/src/test/e2e/api/admin/feature.e2e.test.ts b/src/test/e2e/api/admin/feature.e2e.test.ts index a8a2182130..98f22fcba8 100644 --- a/src/test/e2e/api/admin/feature.e2e.test.ts +++ b/src/test/e2e/api/admin/feature.e2e.test.ts @@ -694,11 +694,63 @@ test('should not hit endpoints if disable configuration is set', async () => { ); await appWithDisabledLegacyFeatures.request - .get('/api/admin/features') - .expect(404); - - return appWithDisabledLegacyFeatures.request .get('/api/admin/features/featureX') .expect('Content-Type', /json/) .expect(404); + + await appWithDisabledLegacyFeatures.request + .post(`/api/admin/features/featureZ/stale/on`) + .set('Content-Type', 'application/json') + .expect(404); +}); + +test('should hit validate and tags endpoint if legacy api is disabled', async () => { + const appWithDisabledLegacyFeatures = await setupAppWithCustomConfig( + db.stores, + { + disableLegacyFeaturesApi: true, + }, + ); + + const feature = { + name: 'test.feature.disabled.api', + type: 'killswitch', + }; + + await appWithDisabledLegacyFeatures.request + .post('/api/admin/projects/default/features') + .send(feature); + + await appWithDisabledLegacyFeatures.request + .post(`/api/admin/features/${feature.name}/tags`) + .send({ + value: 'TeamGreen', + type: 'simple', + }) + .set('Content-Type', 'application/json'); + + await appWithDisabledLegacyFeatures.request + .get(`/api/admin/features/${feature.name}/tags`) + .expect((res) => { + console.log(res.body); + expect(res.body.tags[0].value).toBe('TeamGreen'); + }); + + await appWithDisabledLegacyFeatures.request + .post('/api/admin/features/validate') + .send({ name: 'validateThis' }) + .expect(200); +}); + +test('should have access to the get all features endpoint even if api is disabled', async () => { + const appWithDisabledLegacyFeatures = await setupAppWithCustomConfig( + db.stores, + { + disableLegacyFeaturesApi: true, + }, + ); + + await appWithDisabledLegacyFeatures.request + .get('/api/admin/features') + .expect(200); });