Skip to content

Commit b7804ce

Browse files
authored
Merge branch 'security/feature/alert-user-assignment' into security/feature/alert-user-assignment-7820
2 parents f1e9b94 + b9c3f9f commit b7804ce

3 files changed

Lines changed: 84 additions & 78 deletions

File tree

x-pack/plugins/security_solution/public/common/components/toolbar/bulk_actions/alert_bulk_assignees.test.tsx

Lines changed: 72 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,57 @@ import type { TimelineItem } from '@kbn/timelines-plugin/common';
99
import { act, fireEvent, render } from '@testing-library/react';
1010
import React from 'react';
1111
import { TestProviders } from '../../../mock';
12+
import { useGetUserProfiles } from '../../../../detections/containers/detection_engine/alerts/use_get_user_profiles';
1213
import { useSuggestUsers } from '../../../../detections/containers/detection_engine/alerts/use_suggest_users';
1314

1415
import { BulkAlertAssigneesPanel } from './alert_bulk_assignees';
1516
import { ALERT_WORKFLOW_ASSIGNEE_IDS } from '@kbn/rule-data-utils';
1617

18+
jest.mock('../../../../detections/containers/detection_engine/alerts/use_get_user_profiles');
1719
jest.mock('../../../../detections/containers/detection_engine/alerts/use_suggest_users');
1820

1921
const mockUserProfiles = [
20-
{ uid: 'default-test-assignee-id-1', enabled: true, user: { username: 'user1' }, data: {} },
21-
{ uid: 'default-test-assignee-id-2', enabled: true, user: { username: 'user2' }, data: {} },
22+
{ uid: 'user-id-1', enabled: true, user: { username: 'user1' }, data: {} },
23+
{ uid: 'user-id-2', enabled: true, user: { username: 'user2' }, data: {} },
2224
];
2325

24-
const mockAssigneeItems = [
26+
const mockSuggestedUserProfiles: UserProfileWithAvatar[] = [
27+
...mockUserProfiles,
28+
{ uid: 'user-id-3', enabled: true, user: { username: 'user3' }, data: {} },
29+
{ uid: 'user-id-4', enabled: true, user: { username: 'user4' }, data: {} },
30+
];
31+
32+
const mockAlertsWithAssignees = [
2533
{
2634
_id: 'test-id',
27-
data: [{ field: ALERT_WORKFLOW_ASSIGNEE_IDS, value: ['assignee-id-1', 'assignee-id-2'] }],
35+
data: [
36+
{
37+
field: ALERT_WORKFLOW_ASSIGNEE_IDS,
38+
value: ['user-id-1', 'user-id-2'],
39+
},
40+
],
41+
ecs: { _id: 'test-id' },
42+
},
43+
{
44+
_id: 'test-id',
45+
data: [
46+
{
47+
field: ALERT_WORKFLOW_ASSIGNEE_IDS,
48+
value: ['user-id-1', 'user-id-2'],
49+
},
50+
],
2851
ecs: { _id: 'test-id' },
2952
},
3053
];
3154

32-
(useSuggestUsers as jest.Mock).mockReturnValue({ loading: false, userProfiles: mockUserProfiles });
55+
(useGetUserProfiles as jest.Mock).mockReturnValue({
56+
loading: false,
57+
userProfiles: mockUserProfiles,
58+
});
59+
(useSuggestUsers as jest.Mock).mockReturnValue({
60+
loading: false,
61+
userProfiles: mockSuggestedUserProfiles,
62+
});
3363

3464
const renderAssigneesMenu = (
3565
items: TimelineItem[],
@@ -55,7 +85,7 @@ describe('BulkAlertAssigneesPanel', () => {
5585
});
5686

5787
test('it renders', () => {
58-
const wrapper = renderAssigneesMenu(mockAssigneeItems);
88+
const wrapper = renderAssigneesMenu(mockAlertsWithAssignees);
5989

6090
expect(wrapper.getByTestId('alert-assignees-update-button')).toBeInTheDocument();
6191
expect(useSuggestUsers).toHaveBeenCalled();
@@ -66,25 +96,8 @@ describe('BulkAlertAssigneesPanel', () => {
6696
const mockedOnSubmit = jest.fn();
6797
const mockedSetIsLoading = jest.fn();
6898

69-
const mockAssignees = [
70-
{
71-
_id: 'test-id',
72-
data: [{ field: ALERT_WORKFLOW_ASSIGNEE_IDS, value: ['default-test-assignee-id-1'] }],
73-
ecs: { _id: 'test-id' },
74-
},
75-
{
76-
_id: 'test-id',
77-
data: [
78-
{
79-
field: ALERT_WORKFLOW_ASSIGNEE_IDS,
80-
value: ['default-test-assignee-id-1', 'default-test-assignee-id-2'],
81-
},
82-
],
83-
ecs: { _id: 'test-id' },
84-
},
85-
];
8699
const wrapper = renderAssigneesMenu(
87-
mockAssignees,
100+
mockAlertsWithAssignees,
88101
mockedClosePopover,
89102
mockedOnSubmit,
90103
mockedSetIsLoading
@@ -99,66 +112,41 @@ describe('BulkAlertAssigneesPanel', () => {
99112
});
100113

101114
test('it updates state correctly', () => {
102-
const mockAssignees = [
103-
{
104-
_id: 'test-id',
105-
data: [{ field: ALERT_WORKFLOW_ASSIGNEE_IDS, value: ['default-test-assignee-id-1'] }],
106-
ecs: { _id: 'test-id' },
107-
},
108-
{
109-
_id: 'test-id',
110-
data: [
111-
{
112-
field: ALERT_WORKFLOW_ASSIGNEE_IDS,
113-
value: ['default-test-assignee-id-1', 'default-test-assignee-id-2'],
114-
},
115-
],
116-
ecs: { _id: 'test-id' },
117-
},
118-
];
119-
const wrapper = renderAssigneesMenu(mockAssignees);
120-
121-
expect(wrapper.getAllByRole('option')[0]).toHaveAttribute('title', 'user1');
122-
expect(wrapper.getAllByRole('option')[0]).toBeChecked();
123-
act(() => {
124-
fireEvent.click(wrapper.getByText('user1'));
125-
});
126-
expect(wrapper.getAllByRole('option')[0]).toHaveAttribute('title', 'user1');
127-
expect(wrapper.getAllByRole('option')[0]).not.toBeChecked();
128-
129-
expect(wrapper.getAllByRole('option')[1]).toHaveAttribute('title', 'user2');
130-
expect(wrapper.getAllByRole('option')[1]).not.toBeChecked();
131-
act(() => {
132-
fireEvent.click(wrapper.getByText('user2'));
133-
});
134-
expect(wrapper.getAllByRole('option')[1]).toHaveAttribute('title', 'user2');
135-
expect(wrapper.getAllByRole('option')[1]).toBeChecked();
115+
const wrapper = renderAssigneesMenu(mockAlertsWithAssignees);
116+
117+
const deselectUser = (userName: string, index: number) => {
118+
expect(wrapper.getAllByRole('option')[index]).toHaveAttribute('title', userName);
119+
expect(wrapper.getAllByRole('option')[index]).toBeChecked();
120+
act(() => {
121+
fireEvent.click(wrapper.getByText(userName));
122+
});
123+
expect(wrapper.getAllByRole('option')[index]).toHaveAttribute('title', userName);
124+
expect(wrapper.getAllByRole('option')[index]).not.toBeChecked();
125+
};
126+
127+
const selectUser = (userName: string, index = 0) => {
128+
expect(wrapper.getAllByRole('option')[index]).toHaveAttribute('title', userName);
129+
expect(wrapper.getAllByRole('option')[index]).not.toBeChecked();
130+
act(() => {
131+
fireEvent.click(wrapper.getByText(userName));
132+
});
133+
expect(wrapper.getAllByRole('option')[index]).toHaveAttribute('title', userName);
134+
expect(wrapper.getAllByRole('option')[index]).toBeChecked();
135+
};
136+
137+
deselectUser('user1', 0);
138+
deselectUser('user2', 1);
139+
selectUser('user3', 2);
140+
selectUser('user4', 3);
136141
});
137142

138143
test('it calls expected functions on submit when alerts have changed', () => {
139144
const mockedClosePopover = jest.fn();
140145
const mockedOnSubmit = jest.fn();
141146
const mockedSetIsLoading = jest.fn();
142147

143-
const mockAssignees = [
144-
{
145-
_id: 'test-id',
146-
data: [{ field: ALERT_WORKFLOW_ASSIGNEE_IDS, value: ['default-test-assignee-id-1'] }],
147-
ecs: { _id: 'test-id' },
148-
},
149-
{
150-
_id: 'test-id',
151-
data: [
152-
{
153-
field: ALERT_WORKFLOW_ASSIGNEE_IDS,
154-
value: ['default-test-assignee-id-1', 'default-test-assignee-id-2'],
155-
},
156-
],
157-
ecs: { _id: 'test-id' },
158-
},
159-
];
160148
const wrapper = renderAssigneesMenu(
161-
mockAssignees,
149+
mockAlertsWithAssignees,
162150
mockedClosePopover,
163151
mockedOnSubmit,
164152
mockedSetIsLoading
@@ -169,6 +157,12 @@ describe('BulkAlertAssigneesPanel', () => {
169157
act(() => {
170158
fireEvent.click(wrapper.getByText('user2'));
171159
});
160+
act(() => {
161+
fireEvent.click(wrapper.getByText('user3'));
162+
});
163+
act(() => {
164+
fireEvent.click(wrapper.getByText('user4'));
165+
});
172166

173167
act(() => {
174168
fireEvent.click(wrapper.getByTestId('alert-assignees-update-button'));
@@ -177,8 +171,8 @@ describe('BulkAlertAssigneesPanel', () => {
177171
expect(mockedOnSubmit).toHaveBeenCalled();
178172
expect(mockedOnSubmit).toHaveBeenCalledWith(
179173
{
180-
assignees_to_add: ['default-test-assignee-id-2'],
181-
assignees_to_remove: ['default-test-assignee-id-1'],
174+
assignees_to_add: ['user-id-4', 'user-id-3'],
175+
assignees_to_remove: ['user-id-1', 'user-id-2'],
182176
},
183177
['test-id', 'test-id'],
184178
expect.anything(), // An anonymous callback defined in the onSubmit function

x-pack/plugins/security_solution/public/common/components/toolbar/bulk_actions/use_bulk_alert_assignees_items.test.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ import type {
1515
} from './use_bulk_alert_assignees_items';
1616
import { useBulkAlertAssigneesItems } from './use_bulk_alert_assignees_items';
1717
import { useSetAlertAssignees } from './use_set_alert_assignees';
18+
import { useGetUserProfiles } from '../../../../detections/containers/detection_engine/alerts/use_get_user_profiles';
1819
import { useSuggestUsers } from '../../../../detections/containers/detection_engine/alerts/use_suggest_users';
1920

2021
jest.mock('./use_set_alert_assignees');
22+
jest.mock('../../../../detections/containers/detection_engine/alerts/use_get_user_profiles');
2123
jest.mock('../../../../detections/containers/detection_engine/alerts/use_suggest_users');
2224

2325
const mockUserProfiles = [
@@ -49,6 +51,10 @@ const renderPanel = (panel: UseBulkAlertAssigneesPanel) => {
4951
describe('useBulkAlertAssigneesItems', () => {
5052
beforeEach(() => {
5153
(useSetAlertAssignees as jest.Mock).mockReturnValue(jest.fn());
54+
(useGetUserProfiles as jest.Mock).mockReturnValue({
55+
loading: false,
56+
userProfiles: mockUserProfiles,
57+
});
5258
(useSuggestUsers as jest.Mock).mockReturnValue({
5359
loading: false,
5460
userProfiles: mockUserProfiles,

x-pack/plugins/security_solution/public/detections/components/alerts_table/timeline_actions/use_alert_assignees_actions.test.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ import React from 'react';
1616
import type { EuiContextMenuPanelDescriptor } from '@elastic/eui';
1717
import { EuiPopover, EuiContextMenu } from '@elastic/eui';
1818
import { useSetAlertAssignees } from '../../../../common/components/toolbar/bulk_actions/use_set_alert_assignees';
19+
import { useGetUserProfiles } from '../../../containers/detection_engine/alerts/use_get_user_profiles';
1920
import { useSuggestUsers } from '../../../containers/detection_engine/alerts/use_suggest_users';
2021

2122
jest.mock('../../../containers/detection_engine/alerts/use_alerts_privileges');
2223
jest.mock('../../../../common/components/toolbar/bulk_actions/use_set_alert_assignees');
24+
jest.mock('../../../containers/detection_engine/alerts/use_get_user_profiles');
2325
jest.mock('../../../containers/detection_engine/alerts/use_suggest_users');
2426

2527
const mockUserProfiles = [
@@ -159,6 +161,10 @@ describe('useAlertAssigneesActions', () => {
159161

160162
it('should render the nested panel', async () => {
161163
(useSetAlertAssignees as jest.Mock).mockReturnValue(jest.fn());
164+
(useGetUserProfiles as jest.Mock).mockReturnValue({
165+
loading: false,
166+
userProfiles: mockUserProfiles,
167+
});
162168
(useSuggestUsers as jest.Mock).mockReturnValue({
163169
loading: false,
164170
userProfiles: mockUserProfiles,

0 commit comments

Comments
 (0)