1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00

fix: BE protection for empty stickiness (#3615)

Add 'default' when creating or throw error when updating a
flexibleRollout strategy with empty stickiness
<!-- Thanks for creating a PR! To make it easier for reviewers and
everyone else to understand what your changes relate to, please add some
relevant content to the headings below. Feel free to ignore or delete
sections that you don't think are relevant. Thank you! ❤️ -->

## About the changes
<!-- Describe the changes introduced. What are they and why are they
being introduced? Feel free to also add screenshots or steps to view the
changes if they're visual. -->

<!-- Does it close an issue? Multiple? -->
Closes #

<!-- (For internal contributors): Does it relate to an issue on public
roadmap? -->
<!--
Relates to [roadmap](https://github.com/orgs/Unleash/projects/10) item:
#
-->

### Important files
<!-- PRs can contain a lot of changes, but not all changes are equally
important. Where should a reviewer start looking to get an overview of
the changes? Are any files particularly important? -->


## Discussion points
<!-- Anything about the PR you'd like to discuss before it gets merged?
Got any questions or doubts? -->

---------

Signed-off-by: andreas-unleash <andreas@getunleash.ai>
This commit is contained in:
andreas-unleash 2023-04-25 12:57:16 +03:00 committed by GitHub
parent 84977768ff
commit e70be0739b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 97 additions and 2 deletions

View File

@ -224,6 +224,16 @@ class FeatureToggleService {
'You can not change the featureName for an activation strategy.',
);
}
if (
strategy.parameters &&
'stickiness' in strategy.parameters &&
strategy.parameters.stickiness === ''
) {
throw new InvalidOperationError(
'You can not have an empty string for stickiness.',
);
}
}
async validateProjectCanAccessSegments(
@ -410,6 +420,14 @@ class FeatureToggleService {
);
}
if (
strategyConfig.parameters &&
'stickiness' in strategyConfig.parameters &&
strategyConfig.parameters.stickiness === ''
) {
strategyConfig.parameters.stickiness = 'default';
}
try {
const newFeatureStrategy =
await this.featureStrategiesStore.createStrategyFeatureEnv({

View File

@ -17,8 +17,11 @@ let app: IUnleashTest;
let db: ITestDb;
const defaultStrategy = {
name: 'default',
parameters: {},
name: 'flexibleRollout',
parameters: {
rollout: '100',
stickiness: '',
},
constraints: [],
};
@ -844,3 +847,77 @@ test('Can add and remove tags at the same time', async () => {
expect(res.body.tags).toHaveLength(1);
});
});
test('Should return "default" for stickiness when creating a flexibleRollout strategy with "" for stickiness', async () => {
const username = 'toggle-feature';
const feature = {
name: 'test-featureA',
description: 'the #1 feature',
};
const projectId = 'default';
await app.services.featureToggleServiceV2.createFeatureToggle(
projectId,
feature,
username,
);
await app.services.featureToggleServiceV2.createStrategy(
defaultStrategy,
{ projectId, featureName: feature.name, environment: DEFAULT_ENV },
username,
);
await app.request
.get(
`/api/admin/projects/${projectId}/features/${feature.name}/environments/${DEFAULT_ENV}`,
)
.expect((res) => {
const toggle = res.body;
expect(toggle.strategies).toHaveLength(1);
expect(toggle.strategies[0].parameters.stickiness).toBe('default');
});
await app.request
.get(`/api/admin/features/${feature.name}`)
.expect((res) => {
const toggle = res.body;
expect(toggle.strategies).toHaveLength(1);
expect(toggle.strategies[0].parameters.stickiness).toBe('default');
});
});
test('Should throw error when updating a flexibleRollout strategy with "" for stickiness', async () => {
const username = 'toggle-feature';
const feature = {
name: 'test-featureB',
description: 'the #1 feature',
};
const projectId = 'default';
await app.services.featureToggleServiceV2.createFeatureToggle(
projectId,
feature,
username,
);
await app.services.featureToggleServiceV2.createStrategy(
defaultStrategy,
{ projectId, featureName: feature.name, environment: DEFAULT_ENV },
username,
);
const featureToggle =
await app.services.featureToggleServiceV2.getFeatureToggle(
feature.name,
);
await app.request
.patch(
`/api/admin/projects/${projectId}/features/${feature.name}/environments/${DEFAULT_ENV}/strategies/${featureToggle.environments[0].strategies[0].id}`,
)
.send(defaultStrategy)
.expect((res) => {
const result = res.body;
expect(res.status).toBe(400);
expect(result.error).toBe('Request validation failed');
});
});