Skip to content

Commit

Permalink
fix: feature type is now validated (#7769)
Browse files Browse the repository at this point in the history
Previously people were able to send random data to feature type. Now it
is validated.

Fixes #7751

---------

Co-authored-by: Thomas Heartman <[email protected]>
  • Loading branch information
sjaanus and thomasheartman authored Aug 6, 2024
1 parent cd5a458 commit 0118f88
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/lib/features/feature-search/feature.search.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,11 @@ test('should filter features by type', async () => {
});
await app.createFeature({
name: 'my_feature_b',
type: 'experimental',
type: 'experiment',
});

const { body } = await filterFeaturesByType(
'IS_ANY_OF:experimental,kill-switch',
'IS_ANY_OF:experiment,kill-switch',
);

expect(body).toMatchObject({
Expand All @@ -263,7 +263,7 @@ test('should filter features by created by', async () => {
});
await app.createFeature({
name: 'my_feature_b',
type: 'experimental',
type: 'experiment',
});

const { body } = await filterFeaturesByCreatedBy('IS:1');
Expand Down
18 changes: 14 additions & 4 deletions src/lib/features/feature-toggle/tests/feature-toggles.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2030,7 +2030,7 @@ test('should clone feature flag without strategies', async () => {
const envName = 'some-env-3';
const featureName = 'feature.flag.base';
const cloneName = 'feature.flag.clone';
const type = 'eExperiment';
const type = 'experiment';
const description = 'Lorem ipsum...';

// Create environment
Expand Down Expand Up @@ -2069,7 +2069,7 @@ test('should clone feature flag WITH strategies', async () => {
const envName = 'some-env-4';
const featureName = 'feature.flag.base.2';
const cloneName = 'feature.flag.clone.2';
const type = 'eExperiment';
const type = 'experiment';
const description = 'Lorem ipsum...';

// Create environment
Expand Down Expand Up @@ -2124,7 +2124,7 @@ test('should clone feature flag WITH variants', async () => {
const envName = 'some-env-5';
const featureName = 'feature.flag.base.3';
const cloneName = 'feature.flag.clone.3';
const type = 'eExperiment';
const type = 'experiment';
const description = 'Lorem ipsum...';
const variants = [
{ name: 'variant1', weight: 50 },
Expand Down Expand Up @@ -2211,7 +2211,7 @@ test('should clone feature flag without replacing groupId', async () => {
test('should clone feature flag WITHOUT createdAt field', async () => {
const featureName = 'feature.flag.base.5';
const cloneName = 'feature.flag.clone.5';
const type = 'eExperiment';
const type = 'experiment';
const description = 'Lorem ipsum...';
const originalCreatedAt = new Date(2011, 11, 11);

Expand Down Expand Up @@ -3707,3 +3707,13 @@ test('can get evaluation metrics', async () => {
],
});
});

test("Should not be able to create flag with a type that doesn't exist", async () => {
await app.request
.post('/api/admin/projects/default/features')
.send({
type: 'random',
name: 'random.type.flag',
})
.expect(400);
});
4 changes: 2 additions & 2 deletions src/lib/features/project/projects.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ test('response for project overview should include feature type counts', async (
});
await app.createFeature({
name: 'my-new-development-toggle',
type: 'development',
type: 'experiment',
});
const { body } = await app.request
.get('/api/admin/projects/default/overview')
Expand All @@ -159,7 +159,7 @@ test('response for project overview should include feature type counts', async (
expect(body).toMatchObject({
featureTypeCounts: [
{
type: 'development',
type: 'experiment',
count: 1,
},
{
Expand Down
8 changes: 7 additions & 1 deletion src/lib/openapi/spec/create-feature-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ export const createFeatureSchema = {
description: 'Unique feature name',
},
type: {
type: 'string',
enum: [
'experiment',
'kill-switch',
'release',
'operational',
'permission',
],
example: 'release',
description:
"The feature flag's [type](https://docs.getunleash.io/reference/feature-toggle-types). One of experiment, kill-switch, release, operational, or permission",
Expand Down
8 changes: 7 additions & 1 deletion src/lib/openapi/spec/update-feature-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ export const updateFeatureSchema = {
description: 'Detailed description of the feature',
},
type: {
type: 'string',
enum: [
'experiment',
'kill-switch',
'release',
'operational',
'permission',
],
example: 'kill-switch',
description:
'Type of the flag e.g. experiment, kill-switch, release, operational, permission',
Expand Down
21 changes: 12 additions & 9 deletions src/test/e2e/api/admin/tags.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ test('Can tag features', async () => {
};
await app.request.post('/api/admin/projects/default/features').send({
name: featureName,
type: 'killswitch',
type: 'kill-switch',
enabled: true,
strategies: [{ name: 'default' }],
});
Expand All @@ -141,7 +141,7 @@ test('Can tag features', async () => {

await app.request.post('/api/admin/projects/default/features').send({
name: featureName2,
type: 'killswitch',
type: 'kill-switch',
enabled: true,
strategies: [{ name: 'default' }],
});
Expand Down Expand Up @@ -175,17 +175,20 @@ test('Can bulk remove tags', async () => {

await app.request.post('/api/admin/projects/default/features').send({
name: featureName,
type: 'killswitch',
type: 'kill-switch',
enabled: true,
strategies: [{ name: 'default' }],
});

await app.request.post('/api/admin/projects/default/features').send({
name: featureName2,
type: 'killswitch',
enabled: true,
strategies: [{ name: 'default' }],
});
await app.request
.post('/api/admin/projects/default/features')
.send({
name: featureName2,
type: 'kill-switch',
enabled: true,
strategies: [{ name: 'default' }],
})
.expect(201);

await app.request
.put('/api/admin/projects/default/tags')
Expand Down
8 changes: 4 additions & 4 deletions src/test/e2e/api/client/feature.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ test('Can get strategies for specific environment', async () => {
// Create feature flag
await app.request.post('/api/admin/projects/default/features').send({
name: featureName,
type: 'killswitch',
type: 'kill-switch',
});

// Add global strategy
Expand Down Expand Up @@ -277,13 +277,13 @@ test('Can use multiple filters', async () => {

await app.request.post('/api/admin/projects/default/features').send({
name: 'test.feature',
type: 'killswitch',
type: 'kill-switch',
enabled: true,
strategies: [{ name: 'default' }],
});
await app.request.post('/api/admin/projects/default/features').send({
name: 'test.feature2',
type: 'killswitch',
type: 'kill-switch',
enabled: true,
strategies: [{ name: 'default' }],
});
Expand Down Expand Up @@ -367,7 +367,7 @@ test('Can add tags while creating feature flag', async () => {

await app.request.post('/api/admin/projects/default/features').send({
name: featureName,
type: 'killswitch',
type: 'kill-switch',
tags,
});

Expand Down

0 comments on commit 0118f88

Please sign in to comment.