Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
andreas-unleash committed Apr 25, 2023
1 parent 8497776 commit e70be07
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/lib/services/feature-toggle-service.ts
Expand Up @@ -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(
Expand Down Expand Up @@ -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({
Expand Down
81 changes: 79 additions & 2 deletions src/test/e2e/api/admin/feature.e2e.test.ts
Expand Up @@ -17,8 +17,11 @@ let app: IUnleashTest;
let db: ITestDb;

const defaultStrategy = {
name: 'default',
parameters: {},
name: 'flexibleRollout',
parameters: {
rollout: '100',
stickiness: '',
},
constraints: [],
};

Expand Down Expand Up @@ -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');
});
});

0 comments on commit e70be07

Please sign in to comment.