-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[Security Solution] Adds tests for coverage overview page #168058
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
Changes from all commits
fffc5f5
c5063f4
146e173
2c1b76f
053909a
0058dc6
d456e10
7f102aa
d845cbe
4ecf9a9
85008c5
1bcf6c0
4c58bcd
e9f8e15
e585ede
eeb1d23
7539e30
ab56cd7
a0f1cc4
7067309
51f4bbd
b64e55a
d8b2620
b48dc43
72aca4d
ad2d829
a0343bd
aea805b
f248349
e2dc4b3
e66c647
a240bc9
a37bbc3
a06fb5e
2172170
2c79610
6d471f6
089ee4b
f24d4f8
879594d
00b5f5f
e57b430
72d4166
76b4469
2cfc275
4b13726
a8b6d95
e18f573
19282d8
6cd40c3
4442f7c
636311c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| import { left } from 'fp-ts/lib/Either'; | ||
| import { exactCheck, foldLeftRight, getPaths } from '@kbn/securitysolution-io-ts-utils'; | ||
| import { | ||
| CoverageOverviewRequestBody, | ||
| CoverageOverviewRuleActivity, | ||
| CoverageOverviewRuleSource, | ||
| } from './coverage_overview_route'; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Obviously not part of this PR, but these types need to be migrated to Zod. It makes testing these request schemas SO much easier and understandable.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, I saw the validation tests Dmitrii wrote with zod while writing these and thought the same. In a later PR these types definitely need to be switched over. |
||
|
|
||
| describe('Coverage overview request schema', () => { | ||
| test('empty object validates', () => { | ||
| const payload: CoverageOverviewRequestBody = {}; | ||
|
|
||
| const decoded = CoverageOverviewRequestBody.decode(payload); | ||
| const checked = exactCheck(payload, decoded); | ||
| const message = foldLeftRight(checked); | ||
|
|
||
| expect(getPaths(left(message.errors))).toEqual([]); | ||
| expect(message.schema).toEqual(payload); | ||
| }); | ||
|
|
||
| test('validates with all fields populated', () => { | ||
| const payload: CoverageOverviewRequestBody = { | ||
| filter: { | ||
| activity: [CoverageOverviewRuleActivity.Enabled, CoverageOverviewRuleActivity.Disabled], | ||
| source: [CoverageOverviewRuleSource.Custom, CoverageOverviewRuleSource.Prebuilt], | ||
| search_term: 'search term', | ||
| }, | ||
| }; | ||
|
|
||
| const decoded = CoverageOverviewRequestBody.decode(payload); | ||
| const checked = exactCheck(payload, decoded); | ||
| const message = foldLeftRight(checked); | ||
|
|
||
| expect(getPaths(left(message.errors))).toEqual([]); | ||
| expect(message.schema).toEqual(payload); | ||
| }); | ||
|
|
||
| test('does NOT validate with extra fields', () => { | ||
| const payload: CoverageOverviewRequestBody & { invalid_field: string } = { | ||
| filter: { | ||
| activity: [CoverageOverviewRuleActivity.Enabled, CoverageOverviewRuleActivity.Disabled], | ||
| source: [CoverageOverviewRuleSource.Custom, CoverageOverviewRuleSource.Prebuilt], | ||
| search_term: 'search term', | ||
| }, | ||
| invalid_field: 'invalid field', | ||
| }; | ||
|
|
||
| const decoded = CoverageOverviewRequestBody.decode(payload); | ||
| const checked = exactCheck(payload, decoded); | ||
| const message = foldLeftRight(checked); | ||
|
|
||
| expect(getPaths(left(message.errors))).toEqual(['invalid keys "invalid_field"']); | ||
| expect(message.schema).toEqual({}); | ||
| }); | ||
|
|
||
| test('does NOT validate with invalid filter values', () => { | ||
| const payload: CoverageOverviewRequestBody = { | ||
| filter: { | ||
| // @ts-expect-error | ||
| activity: ['Wrong activity field'], | ||
| // @ts-expect-error | ||
| source: ['Wrong source field'], | ||
| search_term: 'search term', | ||
| }, | ||
| }; | ||
|
|
||
| const decoded = CoverageOverviewRequestBody.decode(payload); | ||
| const checked = exactCheck(payload, decoded); | ||
| const message = foldLeftRight(checked); | ||
|
|
||
| expect(getPaths(left(message.errors))).toEqual([ | ||
| 'Invalid value "Wrong activity field" supplied to "filter,activity"', | ||
| 'Invalid value "Wrong source field" supplied to "filter,source"', | ||
| ]); | ||
| expect(message.schema).toEqual({}); | ||
| }); | ||
|
|
||
| test('does NOT validate with empty filter arrays', () => { | ||
| const payload: CoverageOverviewRequestBody = { | ||
| filter: { | ||
| activity: [], | ||
| source: [], | ||
| search_term: 'search term', | ||
| }, | ||
| }; | ||
|
|
||
| const decoded = CoverageOverviewRequestBody.decode(payload); | ||
| const checked = exactCheck(payload, decoded); | ||
| const message = foldLeftRight(checked); | ||
|
|
||
| expect(getPaths(left(message.errors))).toEqual([ | ||
| 'Invalid value "[]" supplied to "filter,activity"', | ||
| 'Invalid value "[]" supplied to "filter,source"', | ||
| ]); | ||
| expect(message.schema).toEqual({}); | ||
| }); | ||
|
|
||
| test('does NOT validate with empty search_term', () => { | ||
| const payload: CoverageOverviewRequestBody = { | ||
| filter: { | ||
| search_term: '', | ||
| }, | ||
| }; | ||
|
|
||
| const decoded = CoverageOverviewRequestBody.decode(payload); | ||
| const checked = exactCheck(payload, decoded); | ||
| const message = foldLeftRight(checked); | ||
|
|
||
| expect(getPaths(left(message.errors))).toEqual([ | ||
| 'Invalid value "" supplied to "filter,search_term"', | ||
| ]); | ||
| expect(message.schema).toEqual({}); | ||
| }); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.