-
Notifications
You must be signed in to change notification settings - Fork 555
fix(Utils): coerce non-string values before split in validateRule #8198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bardock-2393
wants to merge
1
commit into
Flagsmith:main
Choose a base branch
from
bardock-2393:fix/create-segment-condition-value-split-crash
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+98
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| // These stores pull in `window` globals (via the flux dispatcher) that | ||
| // aren't available under the jest `node` test environment, so they're | ||
| // mocked out to allow `common/utils/utils` to load for real. | ||
| jest.mock('common/stores/account-store', () => ({})) | ||
| jest.mock('common/stores/project-store', () => ({})) | ||
| jest.mock('common/store', () => ({ getStore: () => ({}) })) | ||
| jest.mock('@flagsmith/flagsmith', () => ({ | ||
| getValue: (_key: string, opts: { fallback: unknown }) => opts.fallback, | ||
| })) | ||
|
|
||
| import Utils from 'common/utils/utils' | ||
| import { SegmentCondition } from 'common/types/responses' | ||
|
|
||
| const buildRule = (overrides: Partial<SegmentCondition>): SegmentCondition => ({ | ||
| operator: 'EQUAL', | ||
| property: 'trait', | ||
| value: '', | ||
| ...overrides, | ||
| }) | ||
|
|
||
| describe('validateRule', () => { | ||
| it('returns false for a falsy rule', () => { | ||
| expect(Utils.validateRule(undefined as unknown as SegmentCondition)).toBe( | ||
| false, | ||
| ) | ||
| }) | ||
|
|
||
| it('returns true for a rule marked as deleted regardless of its value', () => { | ||
| const rule = buildRule({ | ||
| delete: true, | ||
| operator: 'MODULO', | ||
| value: undefined as unknown as string, | ||
| }) | ||
| expect(() => Utils.validateRule(rule)).not.toThrow() | ||
| expect(Utils.validateRule(rule)).toBe(true) | ||
| }) | ||
|
|
||
| describe('does not throw and returns false when value is missing', () => { | ||
| it.each<[string, SegmentCondition['value']]>([ | ||
| ['undefined', undefined as unknown as string], | ||
| ['null', null], | ||
| ['empty string', ''], | ||
| ])('MODULO with %s value', (_label, value) => { | ||
| const rule = buildRule({ operator: 'MODULO', value }) | ||
| expect(() => Utils.validateRule(rule)).not.toThrow() | ||
| expect(Utils.validateRule(rule)).toBe(false) | ||
| }) | ||
|
|
||
| it.each<[string, SegmentCondition['value']]>([ | ||
| ['undefined', undefined as unknown as string], | ||
| ['null', null], | ||
| ['empty string', ''], | ||
| ])('semver operator with %s value', (_label, value) => { | ||
| const rule = buildRule({ operator: 'GREATER_THAN:semver', value }) | ||
| expect(() => Utils.validateRule(rule)).not.toThrow() | ||
| expect(Utils.validateRule(rule)).toBe(false) | ||
| }) | ||
| }) | ||
|
|
||
| it('does not throw when value is a non-string type for MODULO', () => { | ||
| const rule = buildRule({ operator: 'MODULO', value: true }) | ||
| expect(() => Utils.validateRule(rule)).not.toThrow() | ||
| }) | ||
|
|
||
| it('does not throw when value is a non-string type for a semver operator', () => { | ||
| const rule = buildRule({ operator: 'GREATER_THAN:semver', value: true }) | ||
| expect(() => Utils.validateRule(rule)).not.toThrow() | ||
| }) | ||
|
|
||
| it('is valid for a hideValue operator (e.g. IS_SET) even with a null value', () => { | ||
| const rule = buildRule({ operator: 'IS_SET', value: null }) | ||
| expect(() => Utils.validateRule(rule)).not.toThrow() | ||
| expect(Utils.validateRule(rule)).toBe(true) | ||
| }) | ||
|
|
||
| it('validates a correct MODULO value', () => { | ||
| const rule = buildRule({ operator: 'MODULO', value: '2|1' }) | ||
| expect(Utils.validateRule(rule)).toBe(true) | ||
| }) | ||
|
|
||
| it('validates a correct semver value', () => { | ||
| const rule = buildRule({ | ||
| operator: 'GREATER_THAN:semver', | ||
| value: '1.0.0:semver', | ||
| }) | ||
| expect(Utils.validateRule(rule)).toBe(true) | ||
| }) | ||
|
|
||
| it('rejects an invalid semver value', () => { | ||
| const rule = buildRule({ | ||
| operator: 'GREATER_THAN:semver', | ||
| value: 'not-a-version', | ||
| }) | ||
| expect(Utils.validateRule(rule)).toBe(false) | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Assert the validation result for non-string values.
These tests only verify that
Utils.validateRuledoes not throw. They do not verify that the suppliedtruevalue is rejected. AddtoBe(false)assertions for both theMODULOand semver cases. TheSegmentCondition.valuecontract permits booleans, so the validation result is part of this regression contract.Suggested assertions
expect(() => Utils.validateRule(rule)).not.toThrow() + expect(Utils.validateRule(rule)).toBe(false)Apply the same assertion to both tests.
📝 Committable suggestion