-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[Security Solution][Detections] Adds exception modal tests #74596
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2b458dc
adds modal tests
dplumlee 8774520
adds comments to tests
dplumlee ecc0dea
splits up into code blocks
dplumlee a17eaa6
switches tests to use internal mock
dplumlee 18ab207
mocks onchange
dplumlee 73fd344
adds more tests
dplumlee 5ef03ec
fixes typecheck
dplumlee 4cf0023
addresses comments
dplumlee 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 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
338 changes: 338 additions & 0 deletions
338
.../security_solution/public/common/components/exceptions/add_exception_modal/index.test.tsx
This file contains 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,338 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { ThemeProvider } from 'styled-components'; | ||
import { mount, ReactWrapper } from 'enzyme'; | ||
import euiLightVars from '@elastic/eui/dist/eui_theme_light.json'; | ||
import { act } from 'react-dom/test-utils'; | ||
|
||
import { AddExceptionModal } from './'; | ||
import { useKibana, useCurrentUser } from '../../../../common/lib/kibana'; | ||
import { getExceptionListSchemaMock } from '../../../../../../lists/common/schemas/response/exception_list_schema.mock'; | ||
import { useFetchIndexPatterns } from '../../../../detections/containers/detection_engine/rules'; | ||
import { stubIndexPattern } from 'src/plugins/data/common/index_patterns/index_pattern.stub'; | ||
import { useAddOrUpdateException } from '../use_add_exception'; | ||
import { useFetchOrCreateRuleExceptionList } from '../use_fetch_or_create_rule_exception_list'; | ||
import { useSignalIndex } from '../../../../detections/containers/detection_engine/alerts/use_signal_index'; | ||
import { createUseKibanaMock } from '../../../mock/kibana_react'; | ||
import { TimelineNonEcsData, Ecs } from '../../../../graphql/types'; | ||
import * as builder from '../builder'; | ||
import * as helpers from '../helpers'; | ||
import { getExceptionListItemSchemaMock } from '../../../../../../lists/common/schemas/response/exception_list_item_schema.mock'; | ||
import { EntriesArray } from '../../../../../../lists/common/schemas/types'; | ||
import { ExceptionListItemSchema } from '../../../../../../lists/common'; | ||
|
||
jest.mock('../../../../detections/containers/detection_engine/alerts/use_signal_index'); | ||
jest.mock('../../../../common/lib/kibana'); | ||
jest.mock('../../../../detections/containers/detection_engine/rules'); | ||
jest.mock('../use_add_exception'); | ||
jest.mock('../use_fetch_or_create_rule_exception_list'); | ||
jest.mock('../builder'); | ||
|
||
const useKibanaMock = useKibana as jest.Mock; | ||
|
||
describe('When the add exception modal is opened', () => { | ||
const ruleName = 'test rule'; | ||
let defaultEndpointItems: jest.SpyInstance<ReturnType< | ||
typeof helpers.defaultEndpointExceptionItems | ||
>>; | ||
let ExceptionBuilderComponent: jest.SpyInstance<ReturnType< | ||
typeof builder.ExceptionBuilderComponent | ||
>>; | ||
beforeEach(() => { | ||
defaultEndpointItems = jest.spyOn(helpers, 'defaultEndpointExceptionItems'); | ||
ExceptionBuilderComponent = jest | ||
.spyOn(builder, 'ExceptionBuilderComponent') | ||
.mockReturnValue(<></>); | ||
|
||
const kibanaMock = createUseKibanaMock()(); | ||
useKibanaMock.mockImplementation(() => ({ | ||
...kibanaMock, | ||
})); | ||
(useAddOrUpdateException as jest.Mock).mockImplementation(() => [ | ||
{ isLoading: false }, | ||
jest.fn(), | ||
]); | ||
(useFetchOrCreateRuleExceptionList as jest.Mock).mockImplementation(() => [ | ||
false, | ||
getExceptionListSchemaMock(), | ||
]); | ||
(useSignalIndex as jest.Mock).mockImplementation(() => ({ | ||
loading: false, | ||
signalIndexName: 'mock-siem-signals-index', | ||
})); | ||
(useFetchIndexPatterns as jest.Mock).mockImplementation(() => [ | ||
{ | ||
isLoading: false, | ||
indexPatterns: stubIndexPattern, | ||
}, | ||
]); | ||
(useCurrentUser as jest.Mock).mockReturnValue({ username: 'test-username' }); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
describe('when the modal is loading', () => { | ||
let wrapper: ReactWrapper; | ||
beforeEach(() => { | ||
// Mocks one of the hooks as loading | ||
(useFetchIndexPatterns as jest.Mock).mockImplementation(() => [ | ||
{ | ||
isLoading: true, | ||
indexPatterns: stubIndexPattern, | ||
}, | ||
]); | ||
wrapper = mount( | ||
<ThemeProvider theme={() => ({ eui: euiLightVars, darkMode: false })}> | ||
<AddExceptionModal | ||
ruleId={'123'} | ||
ruleIndices={[]} | ||
ruleName={ruleName} | ||
exceptionListType={'endpoint'} | ||
onCancel={jest.fn()} | ||
onConfirm={jest.fn()} | ||
/> | ||
</ThemeProvider> | ||
); | ||
}); | ||
it('should show the loading spinner', () => { | ||
expect(wrapper.find('[data-test-subj="loadingAddExceptionModal"]').exists()).toBeTruthy(); | ||
}); | ||
}); | ||
|
||
describe('when there is no alert data passed to an endpoint list exception', () => { | ||
let wrapper: ReactWrapper; | ||
beforeEach(() => { | ||
wrapper = mount( | ||
<ThemeProvider theme={() => ({ eui: euiLightVars, darkMode: false })}> | ||
<AddExceptionModal | ||
ruleId={'123'} | ||
ruleIndices={['filebeat-*']} | ||
ruleName={ruleName} | ||
exceptionListType={'endpoint'} | ||
onCancel={jest.fn()} | ||
onConfirm={jest.fn()} | ||
/> | ||
</ThemeProvider> | ||
); | ||
const callProps = ExceptionBuilderComponent.mock.calls[0][0]; | ||
act(() => callProps.onChange({ exceptionItems: [] })); | ||
}); | ||
it('has the add exception button disabled', () => { | ||
expect( | ||
wrapper.find('button[data-test-subj="add-exception-confirm-button"]').getDOMNode() | ||
).toBeDisabled(); | ||
}); | ||
it('should render the exception builder', () => { | ||
expect(wrapper.find('[data-test-subj="alert-exception-builder"]').exists()).toBeTruthy(); | ||
}); | ||
it('should not render the close on add exception checkbox', () => { | ||
expect( | ||
wrapper.find('[data-test-subj="close-alert-on-add-add-exception-checkbox"]').exists() | ||
).toBeFalsy(); | ||
}); | ||
it('should contain the endpoint specific documentation text', () => { | ||
expect(wrapper.find('[data-test-subj="add-exception-endpoint-text"]').exists()).toBeTruthy(); | ||
}); | ||
}); | ||
|
||
describe('when there is alert data passed to an endpoint list exception', () => { | ||
let wrapper: ReactWrapper; | ||
beforeEach(() => { | ||
const alertDataMock: { ecsData: Ecs; nonEcsData: TimelineNonEcsData[] } = { | ||
ecsData: { _id: 'test-id' }, | ||
nonEcsData: [{ field: 'file.path', value: ['test/path'] }], | ||
}; | ||
wrapper = mount( | ||
<ThemeProvider theme={() => ({ eui: euiLightVars, darkMode: false })}> | ||
<AddExceptionModal | ||
ruleId={'123'} | ||
ruleIndices={['filebeat-*']} | ||
ruleName={ruleName} | ||
exceptionListType={'endpoint'} | ||
onCancel={jest.fn()} | ||
onConfirm={jest.fn()} | ||
alertData={alertDataMock} | ||
/> | ||
</ThemeProvider> | ||
); | ||
const callProps = ExceptionBuilderComponent.mock.calls[0][0]; | ||
act(() => callProps.onChange({ exceptionItems: [...callProps.exceptionListItems] })); | ||
}); | ||
it('has the add exception button enabled', () => { | ||
expect( | ||
wrapper.find('button[data-test-subj="add-exception-confirm-button"]').getDOMNode() | ||
).not.toBeDisabled(); | ||
}); | ||
it('should render the exception builder', () => { | ||
expect(wrapper.find('[data-test-subj="alert-exception-builder"]').exists()).toBeTruthy(); | ||
}); | ||
it('should prepopulate endpoint items', () => { | ||
expect(defaultEndpointItems).toHaveBeenCalled(); | ||
}); | ||
it('should render the close on add exception checkbox', () => { | ||
expect( | ||
wrapper.find('[data-test-subj="close-alert-on-add-add-exception-checkbox"]').exists() | ||
).toBeTruthy(); | ||
}); | ||
it('should have the bulk close checkbox disabled', () => { | ||
expect( | ||
wrapper | ||
.find('input[data-test-subj="bulk-close-alert-on-add-add-exception-checkbox"]') | ||
.getDOMNode() | ||
).toBeDisabled(); | ||
}); | ||
it('should contain the endpoint specific documentation text', () => { | ||
expect(wrapper.find('[data-test-subj="add-exception-endpoint-text"]').exists()).toBeTruthy(); | ||
}); | ||
}); | ||
|
||
describe('when there is alert data passed to a detection list exception', () => { | ||
let wrapper: ReactWrapper; | ||
beforeEach(() => { | ||
const alertDataMock: { ecsData: Ecs; nonEcsData: TimelineNonEcsData[] } = { | ||
ecsData: { _id: 'test-id' }, | ||
nonEcsData: [{ field: 'file.path', value: ['test/path'] }], | ||
}; | ||
wrapper = mount( | ||
<ThemeProvider theme={() => ({ eui: euiLightVars, darkMode: false })}> | ||
<AddExceptionModal | ||
ruleId={'123'} | ||
ruleIndices={['filebeat-*']} | ||
ruleName={ruleName} | ||
exceptionListType={'detection'} | ||
onCancel={jest.fn()} | ||
onConfirm={jest.fn()} | ||
alertData={alertDataMock} | ||
/> | ||
</ThemeProvider> | ||
); | ||
const callProps = ExceptionBuilderComponent.mock.calls[0][0]; | ||
act(() => callProps.onChange({ exceptionItems: [getExceptionListItemSchemaMock()] })); | ||
}); | ||
it('has the add exception button enabled', () => { | ||
expect( | ||
wrapper.find('button[data-test-subj="add-exception-confirm-button"]').getDOMNode() | ||
).not.toBeDisabled(); | ||
}); | ||
it('should render the exception builder', () => { | ||
expect(wrapper.find('[data-test-subj="alert-exception-builder"]').exists()).toBeTruthy(); | ||
}); | ||
it('should not prepopulate endpoint items', () => { | ||
expect(defaultEndpointItems).not.toHaveBeenCalled(); | ||
}); | ||
it('should render the close on add exception checkbox', () => { | ||
expect( | ||
wrapper.find('[data-test-subj="close-alert-on-add-add-exception-checkbox"]').exists() | ||
).toBeTruthy(); | ||
}); | ||
it('should have the bulk close checkbox disabled', () => { | ||
expect( | ||
wrapper | ||
.find('input[data-test-subj="bulk-close-alert-on-add-add-exception-checkbox"]') | ||
.getDOMNode() | ||
).toBeDisabled(); | ||
}); | ||
}); | ||
|
||
describe('when there is bulk-closeable alert data passed to an endpoint list exception', () => { | ||
let wrapper: ReactWrapper; | ||
let callProps: { | ||
onChange: (props: { exceptionItems: ExceptionListItemSchema[] }) => void; | ||
exceptionListItems: ExceptionListItemSchema[]; | ||
}; | ||
beforeEach(() => { | ||
// Mocks the index patterns to contain the pre-populated endpoint fields so that the exception qualifies as bulk closable | ||
(useFetchIndexPatterns as jest.Mock).mockImplementation(() => [ | ||
{ | ||
isLoading: false, | ||
indexPatterns: { | ||
...stubIndexPattern, | ||
fields: [ | ||
{ name: 'file.path.text', type: 'string' }, | ||
{ name: 'subject_name', type: 'string' }, | ||
{ name: 'trusted', type: 'string' }, | ||
{ name: 'file.hash.sha256', type: 'string' }, | ||
{ name: 'event.code', type: 'string' }, | ||
], | ||
}, | ||
}, | ||
]); | ||
const alertDataMock: { ecsData: Ecs; nonEcsData: TimelineNonEcsData[] } = { | ||
ecsData: { _id: 'test-id' }, | ||
nonEcsData: [{ field: 'file.path', value: ['test/path'] }], | ||
}; | ||
wrapper = mount( | ||
<ThemeProvider theme={() => ({ eui: euiLightVars, darkMode: false })}> | ||
<AddExceptionModal | ||
ruleId={'123'} | ||
ruleIndices={['filebeat-*']} | ||
ruleName={ruleName} | ||
exceptionListType={'endpoint'} | ||
onCancel={jest.fn()} | ||
onConfirm={jest.fn()} | ||
alertData={alertDataMock} | ||
/> | ||
</ThemeProvider> | ||
); | ||
callProps = ExceptionBuilderComponent.mock.calls[0][0]; | ||
act(() => callProps.onChange({ exceptionItems: [...callProps.exceptionListItems] })); | ||
}); | ||
it('has the add exception button enabled', () => { | ||
expect( | ||
wrapper.find('button[data-test-subj="add-exception-confirm-button"]').getDOMNode() | ||
).not.toBeDisabled(); | ||
}); | ||
it('should render the exception builder', () => { | ||
expect(wrapper.find('[data-test-subj="alert-exception-builder"]').exists()).toBeTruthy(); | ||
}); | ||
it('should prepopulate endpoint items', () => { | ||
expect(defaultEndpointItems).toHaveBeenCalled(); | ||
}); | ||
it('should render the close on add exception checkbox', () => { | ||
expect( | ||
wrapper.find('[data-test-subj="close-alert-on-add-add-exception-checkbox"]').exists() | ||
).toBeTruthy(); | ||
}); | ||
it('should contain the endpoint specific documentation text', () => { | ||
expect(wrapper.find('[data-test-subj="add-exception-endpoint-text"]').exists()).toBeTruthy(); | ||
}); | ||
it('should have the bulk close checkbox enabled', () => { | ||
expect( | ||
wrapper | ||
.find('input[data-test-subj="bulk-close-alert-on-add-add-exception-checkbox"]') | ||
.getDOMNode() | ||
).not.toBeDisabled(); | ||
}); | ||
describe('when a "is in list" entry is added', () => { | ||
it('should have the bulk close checkbox disabled', () => { | ||
act(() => | ||
callProps.onChange({ | ||
exceptionItems: [ | ||
...callProps.exceptionListItems, | ||
{ | ||
...getExceptionListItemSchemaMock(), | ||
entries: [ | ||
{ field: 'event.code', operator: 'included', type: 'list' }, | ||
] as EntriesArray, | ||
}, | ||
], | ||
}) | ||
); | ||
|
||
expect( | ||
wrapper | ||
.find('input[data-test-subj="bulk-close-alert-on-add-add-exception-checkbox"]') | ||
.getDOMNode() | ||
).toBeDisabled(); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains 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.
Nice catch! 👍