-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Security solution][Endpoint] Users can filter trusted apps by policy…
… name (#106710) * Allow users select policies from a dropdown * Policy filters are passed throguh the API call and the results are now filtered by policy * Moved policies selector inside search component and triggers search only when refresh button is clicked * Fixes tests * Triggers policy filter when policy is selected. Also fix unit test because now policies are loaded at the trusted apps list * Renamed components and added an index.ts for the exports * Adds unit tests for policies selector component * Fix unit tests and changed camelcase by snack case for url params * adds multilang * Fixes i18n keys * Move mock resonse to the mocks file * Use string templating in test * remove === true from boolean comparison * Set function in useCallback. Renames some variables and types. Use reourceState helper function to get the prev state. Use generated data for policies in tests * Fix ts errors * Removes unused type and fix type name for Item * Puts exclude clause on policy dropdown behind a feature flag * Adds missing feature flags in some tests and in global reducer * Fix test adding useExperimentalValua mock for FF * Wrapp handlers in a useCallback in order to prevent useless rerenders Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
- Loading branch information
1 parent
3b387e2
commit d56b22f
Showing
26 changed files
with
768 additions
and
101 deletions.
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
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
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
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
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
8 changes: 8 additions & 0 deletions
8
x-pack/plugins/security_solution/public/management/components/policies_selector/index.ts
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,8 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export { PoliciesSelector, PolicySelectionItem, PoliciesSelectorProps } from './policies_selector'; |
128 changes: 128 additions & 0 deletions
128
...curity_solution/public/management/components/policies_selector/policies_selector.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,128 @@ | ||
/* | ||
* 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 { I18nProvider } from '@kbn/i18n/react'; | ||
import { render, act, fireEvent, RenderResult } from '@testing-library/react'; | ||
import React from 'react'; | ||
import { EndpointDocGenerator } from '../../../../common/endpoint/generate_data'; | ||
|
||
import { PoliciesSelector, PoliciesSelectorProps } from '.'; | ||
import { useIsExperimentalFeatureEnabled } from '../../../common/hooks/use_experimental_features'; | ||
|
||
// TODO: remove this mock when feature flag is removed | ||
jest.mock('../../../common/hooks/use_experimental_features'); | ||
const useIsExperimentalFeatureEnabledMock = useIsExperimentalFeatureEnabled as jest.Mock; | ||
|
||
let onChangeSelectionMock: jest.Mock; | ||
|
||
describe('Policies selector', () => { | ||
let getElement: (params: Partial<PoliciesSelectorProps>) => RenderResult; | ||
beforeEach(() => { | ||
onChangeSelectionMock = jest.fn(); | ||
useIsExperimentalFeatureEnabledMock.mockReturnValue(false); | ||
getElement = (params: Partial<PoliciesSelectorProps>) => { | ||
return render( | ||
<I18nProvider> | ||
<PoliciesSelector | ||
policies={[policy]} | ||
onChangeSelection={onChangeSelectionMock} | ||
{...params} | ||
/> | ||
</I18nProvider> | ||
); | ||
}; | ||
}); | ||
const generator = new EndpointDocGenerator('policy-list'); | ||
const policy = generator.generatePolicyPackagePolicy(); | ||
policy.name = 'test policy A'; | ||
policy.id = 'abc123'; | ||
|
||
describe('When click on policy', () => { | ||
it('should have a default value', () => { | ||
const defaultIncludedPolicies = 'abc123'; | ||
const defaultExcludedPolicies = 'global'; | ||
const element = getElement({ defaultExcludedPolicies, defaultIncludedPolicies }); | ||
act(() => { | ||
fireEvent.click(element.getByTestId('policiesSelectorButton')); | ||
}); | ||
expect(element.getByText(policy.name)).toHaveTextContent(policy.name); | ||
act(() => { | ||
fireEvent.click(element.getByText('Unassigned entries')); | ||
}); | ||
expect(onChangeSelectionMock).toHaveBeenCalledWith([ | ||
{ checked: 'on', id: 'abc123', name: 'test policy A' }, | ||
{ checked: 'off', id: 'global', name: 'Global entries' }, | ||
{ checked: 'on', id: 'unassigned', name: 'Unassigned entries' }, | ||
]); | ||
}); | ||
|
||
it('should disable enabled default value', () => { | ||
useIsExperimentalFeatureEnabledMock.mockReturnValue(true); | ||
const defaultIncludedPolicies = 'abc123'; | ||
const defaultExcludedPolicies = 'global'; | ||
const element = getElement({ defaultExcludedPolicies, defaultIncludedPolicies }); | ||
act(() => { | ||
fireEvent.click(element.getByTestId('policiesSelectorButton')); | ||
}); | ||
act(() => { | ||
fireEvent.click(element.getByText(policy.name)); | ||
}); | ||
expect(onChangeSelectionMock).toHaveBeenCalledWith([ | ||
{ checked: 'off', id: 'abc123', name: 'test policy A' }, | ||
{ checked: 'off', id: 'global', name: 'Global entries' }, | ||
{ checked: undefined, id: 'unassigned', name: 'Unassigned entries' }, | ||
]); | ||
}); | ||
|
||
it('should remove disabled default value', () => { | ||
const defaultIncludedPolicies = 'abc123'; | ||
const defaultExcludedPolicies = 'global'; | ||
const element = getElement({ defaultExcludedPolicies, defaultIncludedPolicies }); | ||
act(() => { | ||
fireEvent.click(element.getByTestId('policiesSelectorButton')); | ||
}); | ||
act(() => { | ||
fireEvent.click(element.getByText('Global entries')); | ||
}); | ||
expect(onChangeSelectionMock).toHaveBeenCalledWith([ | ||
{ checked: 'on', id: 'abc123', name: 'test policy A' }, | ||
{ checked: undefined, id: 'global', name: 'Global entries' }, | ||
{ checked: undefined, id: 'unassigned', name: 'Unassigned entries' }, | ||
]); | ||
}); | ||
}); | ||
|
||
describe('When filter policy', () => { | ||
it('should filter policy by name', () => { | ||
const element = getElement({}); | ||
act(() => { | ||
fireEvent.click(element.getByTestId('policiesSelectorButton')); | ||
}); | ||
act(() => { | ||
fireEvent.change(element.getByTestId('policiesSelectorSearch'), { | ||
target: { value: policy.name }, | ||
}); | ||
}); | ||
expect(element.queryAllByText('Global entries')).toStrictEqual([]); | ||
expect(element.getByText(policy.name)).toHaveTextContent(policy.name); | ||
}); | ||
it('should filter with no results', () => { | ||
const element = getElement({}); | ||
act(() => { | ||
fireEvent.click(element.getByTestId('policiesSelectorButton')); | ||
}); | ||
act(() => { | ||
fireEvent.change(element.getByTestId('policiesSelectorSearch'), { | ||
target: { value: 'no results' }, | ||
}); | ||
}); | ||
expect(element.queryAllByText('Global entries')).toStrictEqual([]); | ||
expect(element.queryAllByText('Unassigned entries')).toStrictEqual([]); | ||
expect(element.queryAllByText(policy.name)).toStrictEqual([]); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.