From 13e431ed17de50f949eee6d46298e438fdf0cfd1 Mon Sep 17 00:00:00 2001 From: ivaosthu Date: Thu, 24 Jan 2019 11:26:07 +0100 Subject: [PATCH] fix: Update variant protocol --- docs/api/admin/feature-toggles-api.md | 2 ++ lib/options.js | 2 +- lib/routes/admin-api/feature-schema.js | 14 +++++++++++++- lib/routes/admin-api/feature.test.js | 19 +++++++++++++------ test/e2e/api/admin/feature.e2e.test.js | 5 ++++- test/e2e/helpers/database.json | 5 ++++- 6 files changed, 37 insertions(+), 10 deletions(-) diff --git a/docs/api/admin/feature-toggles-api.md b/docs/api/admin/feature-toggles-api.md index 68192282c8..6ac816a925 100644 --- a/docs/api/admin/feature-toggles-api.md +++ b/docs/api/admin/feature-toggles-api.md @@ -28,9 +28,11 @@ This endpoint is the one all admin ui should use to fetch all available feature "variants": [ { "name": "variant1", + "weight": 50 }, { "name": "variant2", + "weight": 50 } ] }, diff --git a/lib/options.js b/lib/options.js index ab9ef02a44..3fffbd8e65 100644 --- a/lib/options.js +++ b/lib/options.js @@ -18,7 +18,7 @@ const DEFAULT_OPTIONS = { enableRequestLogger: isDev(), secret: 'UNLEASH-SECRET', sessionAge: THIRTY_DAYS, - adminAuthentication: 'none', + adminAuthentication: 'unsecure', }; module.exports = { diff --git a/lib/routes/admin-api/feature-schema.js b/lib/routes/admin-api/feature-schema.js index bc635d43d1..fc730624fc 100644 --- a/lib/routes/admin-api/feature-schema.js +++ b/lib/routes/admin-api/feature-schema.js @@ -12,7 +12,18 @@ const strategiesSchema = joi.object().keys({ const variantsSchema = joi.object().keys({ name: nameType, - percentage: joi.number(), + weight: joi + .number() + .min(0) + .max(100) + .required(), + payload: joi + .object() + .keys({ + type: joi.string().required(), + value: joi.string().required(), + }) + .optional(), }); const featureShema = joi @@ -28,6 +39,7 @@ const featureShema = joi .items(strategiesSchema), variants: joi .array() + .unique((a, b) => a.name === b.name) .optional() .items(variantsSchema), }) diff --git a/lib/routes/admin-api/feature.test.js b/lib/routes/admin-api/feature.test.js index 4c6590e079..31bc964958 100644 --- a/lib/routes/admin-api/feature.test.js +++ b/lib/routes/admin-api/feature.test.js @@ -241,22 +241,29 @@ test('invalid feature names should have error msg', t => { test('should not allow variants with same name when creating feature flag', t => { t.plan(0); - const { request, base } = getSetup(); + const { request, base, perms } = getSetup(); + perms.withPermissions(CREATE_FEATURE); return request .post(`${base}/api/admin/features`) .send({ - name: 'ts', + name: 't.variant', + enabled: true, strategies: [{ name: 'default' }], - variants: [{ name: 'variant1' }, { name: 'variant1' }], + variants: [ + { name: 'variant1', weight: 50 }, + { name: 'variant1', weight: 50 }, + ], }) .set('Content-Type', 'application/json') - .expect(403); + .expect(400); }); test('should not allow variants with same name when updating feature flag', t => { t.plan(0); - const { request, featureToggleStore, base } = getSetup(); + const { request, featureToggleStore, base, perms } = getSetup(); + perms.withPermissions(UPDATE_FEATURE); + featureToggleStore.addFeature({ name: 'ts', strategies: [{ name: 'default' }], @@ -270,5 +277,5 @@ test('should not allow variants with same name when updating feature flag', t => variants: [{ name: 'variant1' }, { name: 'variant1' }], }) .set('Content-Type', 'application/json') - .expect(403); + .expect(400); }); diff --git a/test/e2e/api/admin/feature.e2e.test.js b/test/e2e/api/admin/feature.e2e.test.js index e8d2cf8298..4067aab271 100644 --- a/test/e2e/api/admin/feature.e2e.test.js +++ b/test/e2e/api/admin/feature.e2e.test.js @@ -59,7 +59,10 @@ test.serial('creates new feature toggle with variants', async t => { name: 'com.test.variants', enabled: false, strategies: [{ name: 'default' }], - variants: [{ name: 'variant1' }, { name: 'variant2' }], + variants: [ + { name: 'variant1', weight: 50 }, + { name: 'variant2', weight: 50 }, + ], }) .set('Content-Type', 'application/json') .expect(201) diff --git a/test/e2e/helpers/database.json b/test/e2e/helpers/database.json index d9560b9de6..576a44e105 100644 --- a/test/e2e/helpers/database.json +++ b/test/e2e/helpers/database.json @@ -127,7 +127,10 @@ "enabled": true, "archived": false, "strategies": [{ "name": "default" }], - "variants": [{ "name": "control" }, { "name": "new" }] + "variants": [ + { "name": "control", "weight": 50 }, + { "name": "new", "weight": 50 } + ] } ] }