forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Security Solution][Notes] - fix user filter not checking correct lic…
…ense in notes management page (elastic#197149)
- Loading branch information
1 parent
6d7fecd
commit dcd8e0c
Showing
8 changed files
with
202 additions
and
61 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
69 changes: 69 additions & 0 deletions
69
x-pack/plugins/security_solution/public/notes/components/user_filter_dropdown.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,69 @@ | ||
/* | ||
* 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 { fireEvent, render, screen } from '@testing-library/react'; | ||
import React from 'react'; | ||
import { UserFilterDropdown } from './user_filter_dropdown'; | ||
import { USER_SELECT_TEST_ID } from './test_ids'; | ||
import { useSuggestUsers } from '../../common/components/user_profiles/use_suggest_users'; | ||
import { useLicense } from '../../common/hooks/use_license'; | ||
import { useUpsellingMessage } from '../../common/hooks/use_upselling'; | ||
|
||
jest.mock('../../common/components/user_profiles/use_suggest_users'); | ||
jest.mock('../../common/hooks/use_license'); | ||
jest.mock('../../common/hooks/use_upselling'); | ||
|
||
const mockDispatch = jest.fn(); | ||
jest.mock('react-redux', () => { | ||
const original = jest.requireActual('react-redux'); | ||
|
||
return { | ||
...original, | ||
useDispatch: () => mockDispatch, | ||
}; | ||
}); | ||
|
||
describe('UserFilterDropdown', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
(useSuggestUsers as jest.Mock).mockReturnValue({ | ||
isLoading: false, | ||
data: [{ user: { username: 'test' } }, { user: { username: 'elastic' } }], | ||
}); | ||
(useLicense as jest.Mock).mockReturnValue({ isPlatinumPlus: () => true }); | ||
(useUpsellingMessage as jest.Mock).mockReturnValue('upsellingMessage'); | ||
}); | ||
|
||
it('should render the component enabled', () => { | ||
const { getByTestId } = render(<UserFilterDropdown />); | ||
|
||
const dropdown = getByTestId(USER_SELECT_TEST_ID); | ||
|
||
expect(dropdown).toBeInTheDocument(); | ||
expect(dropdown).not.toHaveClass('euiComboBox-isDisabled'); | ||
}); | ||
|
||
it('should render the dropdown disabled', async () => { | ||
(useLicense as jest.Mock).mockReturnValue({ isPlatinumPlus: () => false }); | ||
|
||
const { getByTestId } = render(<UserFilterDropdown />); | ||
|
||
expect(getByTestId(USER_SELECT_TEST_ID)).toHaveClass('euiComboBox-isDisabled'); | ||
}); | ||
|
||
it('should call the correct action when select a user', async () => { | ||
const { getByTestId } = render(<UserFilterDropdown />); | ||
|
||
const userSelect = getByTestId('comboBoxSearchInput'); | ||
userSelect.focus(); | ||
|
||
const option = await screen.findByText('test'); | ||
fireEvent.click(option); | ||
|
||
expect(mockDispatch).toHaveBeenCalled(); | ||
}); | ||
}); |
79 changes: 79 additions & 0 deletions
79
x-pack/plugins/security_solution/public/notes/components/user_filter_dropdown.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,79 @@ | ||
/* | ||
* 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 React, { useMemo, useCallback, useState } from 'react'; | ||
import { EuiComboBox, EuiToolTip } from '@elastic/eui'; | ||
import { useDispatch } from 'react-redux'; | ||
import type { UserProfileWithAvatar } from '@kbn/user-profile-components'; | ||
import { i18n } from '@kbn/i18n'; | ||
import type { EuiComboBoxOptionOption } from '@elastic/eui/src/components/combo_box/types'; | ||
import { useLicense } from '../../common/hooks/use_license'; | ||
import { useUpsellingMessage } from '../../common/hooks/use_upselling'; | ||
import { USER_SELECT_TEST_ID } from './test_ids'; | ||
import { useSuggestUsers } from '../../common/components/user_profiles/use_suggest_users'; | ||
import { userFilterUsers } from '..'; | ||
|
||
export const USERS_DROPDOWN = i18n.translate('xpack.securitySolution.notes.usersDropdownLabel', { | ||
defaultMessage: 'Users', | ||
}); | ||
|
||
export const UserFilterDropdown = React.memo(() => { | ||
const dispatch = useDispatch(); | ||
const isPlatinumPlus = useLicense().isPlatinumPlus(); | ||
const upsellingMessage = useUpsellingMessage('note_management_user_filter'); | ||
|
||
const { isLoading, data } = useSuggestUsers({ | ||
searchTerm: '', | ||
enabled: isPlatinumPlus, | ||
}); | ||
const users = useMemo( | ||
() => | ||
(data || []).map((userProfile: UserProfileWithAvatar) => ({ | ||
label: userProfile.user.full_name || userProfile.user.username, | ||
})), | ||
[data] | ||
); | ||
|
||
const [selectedUser, setSelectedUser] = useState<Array<EuiComboBoxOptionOption<string>>>(); | ||
const onChange = useCallback( | ||
(user: Array<EuiComboBoxOptionOption<string>>) => { | ||
setSelectedUser(user); | ||
dispatch(userFilterUsers(user.length > 0 ? user[0].label : '')); | ||
}, | ||
[dispatch] | ||
); | ||
|
||
const dropdown = useMemo( | ||
() => ( | ||
<EuiComboBox | ||
prepend={USERS_DROPDOWN} | ||
singleSelection={{ asPlainText: true }} | ||
options={users} | ||
selectedOptions={selectedUser} | ||
onChange={onChange} | ||
isLoading={isPlatinumPlus && isLoading} | ||
isDisabled={!isPlatinumPlus} | ||
data-test-subj={USER_SELECT_TEST_ID} | ||
/> | ||
), | ||
[isLoading, isPlatinumPlus, onChange, selectedUser, users] | ||
); | ||
|
||
return ( | ||
<> | ||
{isPlatinumPlus ? ( | ||
<>{dropdown}</> | ||
) : ( | ||
<EuiToolTip position="bottom" content={upsellingMessage}> | ||
{dropdown} | ||
</EuiToolTip> | ||
)} | ||
</> | ||
); | ||
}); | ||
|
||
UserFilterDropdown.displayName = 'UserFilterDropdown'; |
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