From c7a56604e77c944bf2069096ce2fa71fc1ec1aa0 Mon Sep 17 00:00:00 2001 From: Coen Warmer Date: Tue, 4 Oct 2022 09:10:38 +0200 Subject: [PATCH 1/4] [Actionable Observability] Add Alert Details page header (#140299) Co-authored-by: Faisal Kanout --- .../case_view/components/case_view_alerts.tsx | 1 + x-pack/plugins/cases/public/mocks.ts | 6 +- .../public/hooks/use_fetch_alert_detail.ts | 1 - .../components/alert_details.test.tsx | 55 +++++- .../components/alert_details.tsx | 42 ++++- .../components/alert_summary.tsx | 4 +- .../components/header_actions.test.tsx | 83 +++++++++ .../components/header_actions.tsx | 160 ++++++++++++++++++ .../pages/alert_details/components/index.ts | 2 + .../components/page_title.stories.tsx | 42 +++++ .../components/page_title.test.tsx | 44 +++++ .../alert_details/components/page_title.tsx | 43 +++++ .../public/pages/alert_details/mock/alert.ts | 4 +- .../public/application/sections/index.tsx | 3 + .../components/rule_snooze_modal.tsx | 104 ++++++++++++ .../public/common/get_rule_snooze_modal.tsx | 15 ++ .../triggers_actions_ui/public/mocks.ts | 4 + .../triggers_actions_ui/public/plugin.ts | 6 + 18 files changed, 599 insertions(+), 20 deletions(-) create mode 100644 x-pack/plugins/observability/public/pages/alert_details/components/header_actions.test.tsx create mode 100644 x-pack/plugins/observability/public/pages/alert_details/components/header_actions.tsx create mode 100644 x-pack/plugins/observability/public/pages/alert_details/components/page_title.stories.tsx create mode 100644 x-pack/plugins/observability/public/pages/alert_details/components/page_title.test.tsx create mode 100644 x-pack/plugins/observability/public/pages/alert_details/components/page_title.tsx create mode 100644 x-pack/plugins/triggers_actions_ui/public/application/sections/rules_list/components/rule_snooze_modal.tsx create mode 100644 x-pack/plugins/triggers_actions_ui/public/common/get_rule_snooze_modal.tsx diff --git a/x-pack/plugins/cases/public/components/case_view/components/case_view_alerts.tsx b/x-pack/plugins/cases/public/components/case_view/components/case_view_alerts.tsx index f492240a8f9b..0bd582e1cef6 100644 --- a/x-pack/plugins/cases/public/components/case_view/components/case_view_alerts.tsx +++ b/x-pack/plugins/cases/public/components/case_view/components/case_view_alerts.tsx @@ -28,6 +28,7 @@ export const CaseViewAlerts = ({ caseData }: CaseViewAlertsProps) => { }), [caseData.comments] ); + const alertRegistrationContexts = useMemo( () => getRegistrationContextFromAlerts(caseData.comments), [caseData.comments] diff --git a/x-pack/plugins/cases/public/mocks.ts b/x-pack/plugins/cases/public/mocks.ts index 57eead3b80b1..10a4c1f6fd05 100644 --- a/x-pack/plugins/cases/public/mocks.ts +++ b/x-pack/plugins/cases/public/mocks.ts @@ -21,9 +21,13 @@ const uiMock: jest.Mocked = { getRecentCases: jest.fn(), }; +export const openAddToExistingCaseModalMock = jest.fn(); + const hooksMock: jest.Mocked = { getUseCasesAddToNewCaseFlyout: jest.fn(), - getUseCasesAddToExistingCaseModal: jest.fn(), + getUseCasesAddToExistingCaseModal: jest.fn().mockImplementation(() => ({ + open: openAddToExistingCaseModalMock, + })), }; const helpersMock: jest.Mocked = { diff --git a/x-pack/plugins/observability/public/hooks/use_fetch_alert_detail.ts b/x-pack/plugins/observability/public/hooks/use_fetch_alert_detail.ts index aa604659f8c0..a86b97741f24 100644 --- a/x-pack/plugins/observability/public/hooks/use_fetch_alert_detail.ts +++ b/x-pack/plugins/observability/public/hooks/use_fetch_alert_detail.ts @@ -21,7 +21,6 @@ interface AlertDetailParams { export const useFetchAlertDetail = (id: string): [boolean, TopAlert | null] => { const { observabilityRuleTypeRegistry } = usePluginContext(); - const params = useMemo( () => ({ id, ruleType: observabilityRuleTypeRegistry }), [id, observabilityRuleTypeRegistry] diff --git a/x-pack/plugins/observability/public/pages/alert_details/components/alert_details.test.tsx b/x-pack/plugins/observability/public/pages/alert_details/components/alert_details.test.tsx index d2c67c85c021..eaa253efbc51 100644 --- a/x-pack/plugins/observability/public/pages/alert_details/components/alert_details.test.tsx +++ b/x-pack/plugins/observability/public/pages/alert_details/components/alert_details.test.tsx @@ -7,23 +7,58 @@ import React from 'react'; import * as useUiSettingHook from '@kbn/kibana-react-plugin/public/ui_settings/use_ui_setting'; +import { useParams } from 'react-router-dom'; +import { Chance } from 'chance'; +import { waitFor } from '@testing-library/react'; +import { casesPluginMock } from '@kbn/cases-plugin/public/mocks'; +import { triggersActionsUiMock } from '@kbn/triggers-actions-ui-plugin/public/mocks'; + import { render } from '../../../utils/test_helper'; +import { useKibana } from '../../../utils/kibana_react'; +import { kibanaStartMock } from '../../../utils/kibana_react.mock'; import { useFetchAlertDetail } from '../../../hooks/use_fetch_alert_detail'; -import { AlertDetails } from './alert_details'; -import { Chance } from 'chance'; -import { useParams } from 'react-router-dom'; import { useBreadcrumbs } from '../../../hooks/use_breadcrumbs'; +import { AlertDetails } from './alert_details'; import { ConfigSchema } from '../../../plugin'; import { alert, alertWithNoData } from '../mock/alert'; -import { waitFor } from '@testing-library/react'; -jest.mock('../../../hooks/use_fetch_alert_detail'); -jest.mock('../../../hooks/use_breadcrumbs'); jest.mock('react-router-dom', () => ({ ...jest.requireActual('react-router-dom'), useParams: jest.fn(), })); +jest.mock('../../../utils/kibana_react'); + +const useKibanaMock = useKibana as jest.Mock; + +const mockKibana = () => { + useKibanaMock.mockReturnValue({ + services: { + ...kibanaStartMock.startContract(), + cases: casesPluginMock.createStartContract(), + http: { + basePath: { + prepend: jest.fn(), + }, + }, + triggersActionsUi: triggersActionsUiMock.createStart(), + }, + }); +}; + +jest.mock('../../../hooks/use_fetch_alert_detail'); +jest.mock('../../../hooks/use_breadcrumbs'); +jest.mock('../../../hooks/use_get_user_cases_permissions', () => ({ + useGetUserCasesPermissions: () => ({ + all: true, + create: true, + delete: true, + push: true, + read: true, + update: true, + }), +})); + const useFetchAlertDetailMock = useFetchAlertDetail as jest.Mock; const useParamsMock = useParams as jest.Mock; const useBreadcrumbsMock = useBreadcrumbs as jest.Mock; @@ -49,16 +84,20 @@ describe('Alert details', () => { jest.clearAllMocks(); useParamsMock.mockReturnValue(params); useBreadcrumbsMock.mockReturnValue([]); + mockKibana(); }); - it('should show alert summary', async () => { + it('should show the alert detail page with all necessary components', async () => { useFetchAlertDetailMock.mockReturnValue([false, alert]); const alertDetails = render(, config); - expect(alertDetails.queryByTestId('alertDetails')).toBeTruthy(); await waitFor(() => expect(alertDetails.queryByTestId('centerJustifiedSpinner')).toBeFalsy()); + + expect(alertDetails.queryByTestId('alertDetails')).toBeTruthy(); expect(alertDetails.queryByTestId('alertDetailsError')).toBeFalsy(); + expect(alertDetails.queryByTestId('page-title-container')).toBeTruthy(); + expect(alertDetails.queryByTestId('alert-summary-container')).toBeTruthy(); }); it('should show error loading the alert details', async () => { diff --git a/x-pack/plugins/observability/public/pages/alert_details/components/alert_details.tsx b/x-pack/plugins/observability/public/pages/alert_details/components/alert_details.tsx index a2cd7fd68a2c..1b501e62a7dd 100644 --- a/x-pack/plugins/observability/public/pages/alert_details/components/alert_details.tsx +++ b/x-pack/plugins/observability/public/pages/alert_details/components/alert_details.tsx @@ -9,23 +9,36 @@ import React from 'react'; import { i18n } from '@kbn/i18n'; import { useParams } from 'react-router-dom'; import { EuiEmptyPrompt, EuiPanel } from '@elastic/eui'; + import { useKibana } from '../../../utils/kibana_react'; -import { ObservabilityAppServices } from '../../../application/types'; import { usePluginContext } from '../../../hooks/use_plugin_context'; import { useBreadcrumbs } from '../../../hooks/use_breadcrumbs'; -import { paths } from '../../../config/paths'; -import { AlertDetailsPathParams } from '../types'; +import { useFetchAlertDetail } from '../../../hooks/use_fetch_alert_detail'; + +import { AlertSummary, HeaderActions, PageTitle } from '.'; import { CenterJustifiedSpinner } from '../../rule_details/components/center_justified_spinner'; -import { AlertSummary } from '.'; import PageNotFound from '../../404'; -import { useFetchAlertDetail } from '../../../hooks/use_fetch_alert_detail'; + +import { ObservabilityAppServices } from '../../../application/types'; +import { AlertDetailsPathParams } from '../types'; +import { observabilityFeatureId } from '../../../../common'; +import { paths } from '../../../config/paths'; export function AlertDetails() { - const { http } = useKibana().services; + const { + http, + cases: { + helpers: { canUseCases }, + ui: { getCasesContext }, + }, + } = useKibana().services; const { ObservabilityPageTemplate, config } = usePluginContext(); const { alertId } = useParams(); const [isLoading, alert] = useFetchAlertDetail(alertId); + const CasesContext = getCasesContext(); + const userCasesPermissions = canUseCases(); + useBreadcrumbs([ { href: http.basePath.prepend(paths.observability.alerts), @@ -69,7 +82,22 @@ export function AlertDetails() { ); return ( - + , + rightSideItems: [ + + + , + ], + bottomBorder: false, + }} + data-test-subj="alertDetails" + > ); diff --git a/x-pack/plugins/observability/public/pages/alert_details/components/alert_summary.tsx b/x-pack/plugins/observability/public/pages/alert_details/components/alert_summary.tsx index 4a1d88e928fb..eada6a392552 100644 --- a/x-pack/plugins/observability/public/pages/alert_details/components/alert_summary.tsx +++ b/x-pack/plugins/observability/public/pages/alert_details/components/alert_summary.tsx @@ -30,7 +30,7 @@ export function AlertSummary({ alert }: AlertSummaryProps) { const tags = alert?.fields[ALERT_RULE_TAGS]; return ( - <> +
@@ -161,6 +161,6 @@ export function AlertSummary({ alert }: AlertSummaryProps) {
- + ); } diff --git a/x-pack/plugins/observability/public/pages/alert_details/components/header_actions.test.tsx b/x-pack/plugins/observability/public/pages/alert_details/components/header_actions.test.tsx new file mode 100644 index 000000000000..8bbec59c52b9 --- /dev/null +++ b/x-pack/plugins/observability/public/pages/alert_details/components/header_actions.test.tsx @@ -0,0 +1,83 @@ +/* + * 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 from 'react'; +import { fireEvent } from '@testing-library/react'; +import { triggersActionsUiMock } from '@kbn/triggers-actions-ui-plugin/public/mocks'; +import { casesPluginMock, openAddToExistingCaseModalMock } from '@kbn/cases-plugin/public/mocks'; + +import { render } from '../../../utils/test_helper'; +import { useKibana } from '../../../utils/kibana_react'; +import { kibanaStartMock } from '../../../utils/kibana_react.mock'; +import { alertWithTags, mockAlertUuid } from '../mock/alert'; + +import { HeaderActions } from './header_actions'; + +jest.mock('../../../utils/kibana_react'); + +const useKibanaMock = useKibana as jest.Mock; + +const mockKibana = () => { + useKibanaMock.mockReturnValue({ + services: { + ...kibanaStartMock.startContract(), + triggersActionsUi: triggersActionsUiMock.createStart(), + cases: casesPluginMock.createStartContract(), + }, + }); +}; + +const ruleId = '123'; +const ruleName = '456'; + +jest.mock('../../../hooks/use_fetch_rule', () => { + return { + useFetchRule: () => ({ + reloadRule: jest.fn(), + rule: { + id: ruleId, + name: ruleName, + }, + }), + }; +}); + +describe('Header Actions', () => { + beforeEach(() => { + jest.clearAllMocks(); + mockKibana(); + }); + + it('should display an actions button', () => { + const { queryByTestId } = render(); + expect(queryByTestId('alert-details-header-actions-menu-button')).toBeTruthy(); + }); + + describe('when clicking the actions button', () => { + it('should offer an "add to case" button which opens the add to case modal', async () => { + const { getByTestId, findByRole } = render(); + + fireEvent.click(await findByRole('button', { name: 'Actions' })); + + fireEvent.click(getByTestId('add-to-case-button')); + + expect(openAddToExistingCaseModalMock).toBeCalledWith({ + attachments: [ + { + alertId: mockAlertUuid, + index: '.internal.alerts-observability.metrics.alerts-*', + rule: { + id: ruleId, + name: ruleName, + }, + type: 'alert', + }, + ], + }); + }); + }); +}); diff --git a/x-pack/plugins/observability/public/pages/alert_details/components/header_actions.tsx b/x-pack/plugins/observability/public/pages/alert_details/components/header_actions.tsx new file mode 100644 index 000000000000..e7a2c773dc68 --- /dev/null +++ b/x-pack/plugins/observability/public/pages/alert_details/components/header_actions.tsx @@ -0,0 +1,160 @@ +/* + * 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, { useState } from 'react'; +import { i18n } from '@kbn/i18n'; +import { noop } from 'lodash'; +import { CaseAttachmentsWithoutOwner } from '@kbn/cases-plugin/public/types'; +import { CommentType } from '@kbn/cases-plugin/common'; +import { EuiButton, EuiButtonEmpty, EuiFlexGroup, EuiPopover, EuiText } from '@elastic/eui'; +import { ALERT_RULE_UUID, ALERT_UUID } from '@kbn/rule-data-utils'; + +import { useKibana } from '../../../utils/kibana_react'; +import { useFetchRule } from '../../../hooks/use_fetch_rule'; +import { ObservabilityAppServices } from '../../../application/types'; +import { TopAlert } from '../../alerts'; + +export interface HeaderActionsProps { + alert: TopAlert | null; +} + +export function HeaderActions({ alert }: HeaderActionsProps) { + const { + http, + cases: { + hooks: { getUseCasesAddToExistingCaseModal }, + }, + triggersActionsUi: { getEditAlertFlyout, getRuleSnoozeModal }, + } = useKibana().services; + + const { rule, reloadRule } = useFetchRule({ + http, + ruleId: alert?.fields[ALERT_RULE_UUID] || '', + }); + + const [isPopoverOpen, setIsPopoverOpen] = useState(false); + const [ruleConditionsFlyoutOpen, setRuleConditionsFlyoutOpen] = useState(false); + const [snoozeModalOpen, setSnoozeModalOpen] = useState(false); + + const selectCaseModal = getUseCasesAddToExistingCaseModal(); + + const handleTogglePopover = () => setIsPopoverOpen(!isPopoverOpen); + const handleClosePopover = () => setIsPopoverOpen(false); + + const attachments: CaseAttachmentsWithoutOwner = + alert && rule + ? [ + { + alertId: alert?.fields[ALERT_UUID] || '', + index: '.internal.alerts-observability.metrics.alerts-*', + rule: { + id: rule.id, + name: rule.name, + }, + type: CommentType.alert, + }, + ] + : []; + + const handleAddToCase = () => { + setIsPopoverOpen(false); + selectCaseModal.open({ attachments }); + }; + + const handleViewRuleDetails = () => { + setIsPopoverOpen(false); + setRuleConditionsFlyoutOpen(true); + }; + + const handleOpenSnoozeModal = () => { + setIsPopoverOpen(false); + setSnoozeModalOpen(true); + }; + + return ( + <> + + {i18n.translate('xpack.observability.alertDetails.actionsButtonLabel', { + defaultMessage: 'Actions', + })} + + } + > + + + + {i18n.translate('xpack.observability.alertDetails.viewRuleDetails', { + defaultMessage: 'View rule details', + })} + + + + + + {i18n.translate('xpack.observability.alertDetails.editSnoozeRule', { + defaultMessage: 'Snooze the rule', + })} + + + + + + {i18n.translate('xpack.observability.alertDetails.addToCase', { + defaultMessage: 'Add to case', + })} + + + + + + {rule && ruleConditionsFlyoutOpen + ? getEditAlertFlyout({ + initialRule: rule, + onClose: () => { + setRuleConditionsFlyoutOpen(false); + }, + onSave: reloadRule, + }) + : null} + + {rule && snoozeModalOpen + ? getRuleSnoozeModal({ + rule, + onClose: () => setSnoozeModalOpen(false), + onRuleChanged: reloadRule, + onLoading: noop, + }) + : null} + + ); +} diff --git a/x-pack/plugins/observability/public/pages/alert_details/components/index.ts b/x-pack/plugins/observability/public/pages/alert_details/components/index.ts index f49d7bd3c721..9e2ae5d34dc1 100644 --- a/x-pack/plugins/observability/public/pages/alert_details/components/index.ts +++ b/x-pack/plugins/observability/public/pages/alert_details/components/index.ts @@ -5,5 +5,7 @@ * 2.0. */ +export { HeaderActions } from './header_actions'; export { AlertSummary } from './alert_summary'; export { AlertDetails } from './alert_details'; +export { PageTitle } from './page_title'; diff --git a/x-pack/plugins/observability/public/pages/alert_details/components/page_title.stories.tsx b/x-pack/plugins/observability/public/pages/alert_details/components/page_title.stories.tsx new file mode 100644 index 000000000000..ab0109ef1c21 --- /dev/null +++ b/x-pack/plugins/observability/public/pages/alert_details/components/page_title.stories.tsx @@ -0,0 +1,42 @@ +/* + * 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 from 'react'; +import { ComponentStory } from '@storybook/react'; +import { EuiPageTemplate } from '@elastic/eui'; + +import { PageTitle as Component, PageTitleProps } from './page_title'; + +export default { + component: Component, + title: 'app/AlertDetails/PageTitle', + argTypes: { + title: { control: 'text' }, + active: { control: 'boolean' }, + }, +}; + +const Template: ComponentStory = (props: PageTitleProps) => ( + +); + +const TemplateWithPageTemplate: ComponentStory = (props: PageTitleProps) => ( + + } bottomBorder={false} /> + +); + +const defaultProps = { + title: 'host.cpu.usage is 0.2024 in the last 1 min for all hosts. Alert when > 0.02.', + active: true, +}; + +export const PageTitle = Template.bind({}); +PageTitle.args = defaultProps; + +export const PageTitleUsedWithinPageTemplate = TemplateWithPageTemplate.bind({}); +PageTitleUsedWithinPageTemplate.args = defaultProps; diff --git a/x-pack/plugins/observability/public/pages/alert_details/components/page_title.test.tsx b/x-pack/plugins/observability/public/pages/alert_details/components/page_title.test.tsx new file mode 100644 index 000000000000..bd0b15e8ffc2 --- /dev/null +++ b/x-pack/plugins/observability/public/pages/alert_details/components/page_title.test.tsx @@ -0,0 +1,44 @@ +/* + * 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 from 'react'; +import { render } from '@testing-library/react'; + +import { PageTitle, PageTitleProps } from './page_title'; + +describe('Page Title', () => { + const defaultProps = { + title: 'Great success', + active: true, + }; + + const renderComp = (props: PageTitleProps) => { + return render(); + }; + + it('should display a title when it is passed', () => { + const { getByText } = renderComp(defaultProps); + expect(getByText(defaultProps.title)).toBeTruthy(); + }); + + it('should display an active badge when active is true', async () => { + const { getByText } = renderComp(defaultProps); + expect(getByText('Active')).toBeTruthy(); + }); + + it('should display an inactive badge when active is false', async () => { + const { getByText } = renderComp({ ...defaultProps, active: false }); + + expect(getByText('Recovered')).toBeTruthy(); + }); + + it('should display no badge when active is not passed', async () => { + const { queryByTestId } = renderComp({ title: '123' }); + + expect(queryByTestId('page-title-active-badge')).not.toBeInTheDocument(); + }); +}); diff --git a/x-pack/plugins/observability/public/pages/alert_details/components/page_title.tsx b/x-pack/plugins/observability/public/pages/alert_details/components/page_title.tsx new file mode 100644 index 000000000000..61301f16e37a --- /dev/null +++ b/x-pack/plugins/observability/public/pages/alert_details/components/page_title.tsx @@ -0,0 +1,43 @@ +/* + * 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 from 'react'; +import { EuiBadge, EuiFlexGroup, EuiFlexItem } from '@elastic/eui'; +import { i18n } from '@kbn/i18n'; + +export interface PageTitleProps { + title: string | undefined; + active?: boolean; +} + +export function PageTitle({ title, active }: PageTitleProps) { + const label = active + ? i18n.translate('xpack.observability.alertDetails.alertActiveState', { + defaultMessage: 'Active', + }) + : i18n.translate('xpack.observability.alertDetails.alertRecoveredState', { + defaultMessage: 'Recovered', + }); + + return ( +
+ {title} + + + +
+ {typeof active === 'boolean' ? ( + + {label} + + ) : null} +
+
+
+
+ ); +} diff --git a/x-pack/plugins/observability/public/pages/alert_details/mock/alert.ts b/x-pack/plugins/observability/public/pages/alert_details/mock/alert.ts index ed129fc3d24e..a3031fb0aa18 100644 --- a/x-pack/plugins/observability/public/pages/alert_details/mock/alert.ts +++ b/x-pack/plugins/observability/public/pages/alert_details/mock/alert.ts @@ -32,6 +32,8 @@ import { TopAlert } from '../../alerts'; export const tags: string[] = ['tag1', 'tag2', 'tag3']; +export const mockAlertUuid = '756240e5-92fb-452f-b08e-cd3e0dc51738'; + export const alert: TopAlert = { reason: '1957 log entries (more than 100.25) match the conditions.', fields: { @@ -50,7 +52,7 @@ export const alert: TopAlert = { [ALERT_EVALUATION_VALUE]: 1957, [ALERT_INSTANCE_ID]: '*', [ALERT_RULE_NAME]: 'Log threshold (from logs)', - [ALERT_UUID]: '756240e5-92fb-452f-b08e-cd3e0dc51738', + [ALERT_UUID]: mockAlertUuid, [SPACE_IDS]: ['default'], [VERSION]: '8.0.0', [EVENT_KIND]: 'signal', diff --git a/x-pack/plugins/triggers_actions_ui/public/application/sections/index.tsx b/x-pack/plugins/triggers_actions_ui/public/application/sections/index.tsx index f4ae5d399e6f..2e476855926d 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/sections/index.tsx +++ b/x-pack/plugins/triggers_actions_ui/public/application/sections/index.tsx @@ -49,6 +49,9 @@ export const RulesList = suspendedComponentWithProps( export const RulesListNotifyBadge = suspendedComponentWithProps( lazy(() => import('./rules_list/components/rules_list_notify_badge')) ); +export const RuleSnoozeModal = suspendedComponentWithProps( + lazy(() => import('./rules_list/components/rule_snooze_modal')) +); export const RuleDefinition = suspendedComponentWithProps( lazy(() => import('./rule_details/components/rule_definition')) ); diff --git a/x-pack/plugins/triggers_actions_ui/public/application/sections/rules_list/components/rule_snooze_modal.tsx b/x-pack/plugins/triggers_actions_ui/public/application/sections/rules_list/components/rule_snooze_modal.tsx new file mode 100644 index 000000000000..c4624e7ea8e6 --- /dev/null +++ b/x-pack/plugins/triggers_actions_ui/public/application/sections/rules_list/components/rule_snooze_modal.tsx @@ -0,0 +1,104 @@ +/* + * 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, { useCallback, useMemo } from 'react'; +import { EuiModal, EuiModalBody, EuiSpacer } from '@elastic/eui'; + +import { useKibana } from '../../../../common/lib/kibana'; +import { snoozeRule, unsnoozeRule } from '../../../lib/rule_api'; +import { + SNOOZE_FAILED_MESSAGE, + SNOOZE_SUCCESS_MESSAGE, + UNSNOOZE_SUCCESS_MESSAGE, +} from './rules_list_notify_badge'; +import { SnoozePanel, futureTimeToInterval } from './rule_snooze'; +import { Rule, RuleTypeParams, SnoozeSchedule } from '../../../../types'; + +export interface RuleSnoozeModalProps { + rule: Rule; + onClose: () => void; + onLoading: (isLoading: boolean) => void; + onRuleChanged: () => void; +} + +const isRuleSnoozed = (rule: { isSnoozedUntil?: Date | null; muteAll: boolean }) => + Boolean( + (rule.isSnoozedUntil && new Date(rule.isSnoozedUntil).getTime() > Date.now()) || rule.muteAll + ); + +export const RuleSnoozeModal: React.FunctionComponent = ({ + rule, + onClose, + onLoading, + onRuleChanged, +}) => { + const { + http, + notifications: { toasts }, + } = useKibana().services; + + const isSnoozed = useMemo(() => { + return isRuleSnoozed(rule); + }, [rule]); + + const onApplySnooze = useCallback( + async (snoozeSchedule: SnoozeSchedule) => { + try { + onLoading(true); + onClose(); + + await snoozeRule({ http, id: rule.id, snoozeSchedule }); + + onRuleChanged(); + + toasts.addSuccess(SNOOZE_SUCCESS_MESSAGE); + } catch (e) { + toasts.addDanger(SNOOZE_FAILED_MESSAGE); + } finally { + onLoading(false); + } + }, + [onLoading, onClose, http, rule.id, onRuleChanged, toasts] + ); + + const onApplyUnsnooze = useCallback( + async (scheduleIds?: string[]) => { + try { + onLoading(true); + onClose(); + await unsnoozeRule({ http, id: rule.id, scheduleIds }); + onRuleChanged(); + toasts.addSuccess(UNSNOOZE_SUCCESS_MESSAGE); + } catch (e) { + toasts.addDanger(SNOOZE_FAILED_MESSAGE); + } finally { + onLoading(false); + } + }, + [onLoading, onClose, http, rule.id, onRuleChanged, toasts] + ); + + return ( + + + + + + + ); +}; + +// eslint-disable-next-line import/no-default-export +export { RuleSnoozeModal as default }; diff --git a/x-pack/plugins/triggers_actions_ui/public/common/get_rule_snooze_modal.tsx b/x-pack/plugins/triggers_actions_ui/public/common/get_rule_snooze_modal.tsx new file mode 100644 index 000000000000..b22fe16f7731 --- /dev/null +++ b/x-pack/plugins/triggers_actions_ui/public/common/get_rule_snooze_modal.tsx @@ -0,0 +1,15 @@ +/* + * 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 from 'react'; + +import { RuleSnoozeModal } from '../application/sections'; +import { RuleSnoozeModalProps } from '../application/sections/rules_list/components/rule_snooze_modal'; + +export const getRuleSnoozeModalLazy = (props: RuleSnoozeModalProps) => { + return ; +}; diff --git a/x-pack/plugins/triggers_actions_ui/public/mocks.ts b/x-pack/plugins/triggers_actions_ui/public/mocks.ts index 02722bc0ee73..7c18ea1b6fa2 100644 --- a/x-pack/plugins/triggers_actions_ui/public/mocks.ts +++ b/x-pack/plugins/triggers_actions_ui/public/mocks.ts @@ -43,6 +43,7 @@ import { getFieldBrowserLazy } from './common/get_field_browser'; import { getRuleAlertsSummaryLazy } from './common/get_rule_alerts_summary'; import { getRuleDefinitionLazy } from './common/get_rule_definition'; import { getRuleStatusPanelLazy } from './common/get_rule_status_panel'; +import { getRuleSnoozeModalLazy } from './common/get_rule_snooze_modal'; function createStartMock(): TriggersAndActionsUIPublicPluginStart { const actionTypeRegistry = new TypeRegistry(); @@ -124,6 +125,9 @@ function createStartMock(): TriggersAndActionsUIPublicPluginStart { getRuleStatusPanel: (props) => { return getRuleStatusPanelLazy(props); }, + getRuleSnoozeModal: (props) => { + return getRuleSnoozeModalLazy(props); + }, }; } diff --git a/x-pack/plugins/triggers_actions_ui/public/plugin.ts b/x-pack/plugins/triggers_actions_ui/public/plugin.ts index 10c5e5637f15..1374f10355e1 100644 --- a/x-pack/plugins/triggers_actions_ui/public/plugin.ts +++ b/x-pack/plugins/triggers_actions_ui/public/plugin.ts @@ -78,6 +78,8 @@ import { getRuleDefinitionLazy } from './common/get_rule_definition'; import { RuleStatusPanelProps } from './application/sections/rule_details/components/rule_status_panel'; import { RuleAlertsSummaryProps } from './application/sections/rule_details/components/alert_summary'; import { getRuleAlertsSummaryLazy } from './common/get_rule_alerts_summary'; +import { RuleSnoozeModalProps } from './application/sections/rules_list/components/rule_snooze_modal'; +import { getRuleSnoozeModalLazy } from './common/get_rule_snooze_modal'; export interface TriggersAndActionsUIPublicPluginSetup { actionTypeRegistry: TypeRegistry; @@ -123,6 +125,7 @@ export interface TriggersAndActionsUIPublicPluginStart { getRuleDefinition: (props: RuleDefinitionProps) => ReactElement; getRuleStatusPanel: (props: RuleStatusPanelProps) => ReactElement; getRuleAlertsSummary: (props: RuleAlertsSummaryProps) => ReactElement; + getRuleSnoozeModal: (props: RuleSnoozeModalProps) => ReactElement; } interface PluginsSetup { @@ -352,6 +355,9 @@ export class Plugin getRuleAlertsSummary: (props: RuleAlertsSummaryProps) => { return getRuleAlertsSummaryLazy(props); }, + getRuleSnoozeModal: (props: RuleSnoozeModalProps) => { + return getRuleSnoozeModalLazy(props); + }, }; } From 3c0086b76478f8c011883c0a0ea94354dd0c8f60 Mon Sep 17 00:00:00 2001 From: Pablo Machado Date: Tue, 4 Oct 2022 10:16:54 +0200 Subject: [PATCH 2/4] Add Url state parameter for external alerts checkbox (#142344) * Refactor global_query_string to move reusabel code to helper * Add Url state parameter for external alerts checkbox Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> --- .../events_tab/events_query_tab_body.test.tsx | 6 ++ .../events_tab/events_query_tab_body.tsx | 56 +++++++++++++- .../utils/global_query_string/helpers.ts | 62 ++++++++++++++- .../utils/global_query_string/index.test.tsx | 30 +++++++- .../common/utils/global_query_string/index.ts | 75 +++++++------------ 5 files changed, 178 insertions(+), 51 deletions(-) diff --git a/x-pack/plugins/security_solution/public/common/components/events_tab/events_query_tab_body.test.tsx b/x-pack/plugins/security_solution/public/common/components/events_tab/events_query_tab_body.test.tsx index 0b13363a653a..cb12aaa7f0e7 100644 --- a/x-pack/plugins/security_solution/public/common/components/events_tab/events_query_tab_body.test.tsx +++ b/x-pack/plugins/security_solution/public/common/components/events_tab/events_query_tab_body.test.tsx @@ -15,6 +15,7 @@ import { EventsQueryTabBody, ALERTS_EVENTS_HISTOGRAM_ID } from './events_query_t import { useGlobalFullScreen } from '../../containers/use_full_screen'; import * as tGridActions from '@kbn/timelines-plugin/public/store/t_grid/actions'; import { licenseService } from '../../hooks/use_license'; +import { mockHistory } from '../../mock/router'; const mockGetDefaultControlColumn = jest.fn(); jest.mock('../../../timelines/components/timeline/body/control_columns', () => ({ @@ -39,6 +40,11 @@ jest.mock('../../lib/kibana', () => { }; }); +jest.mock('react-router-dom', () => ({ + ...jest.requireActual('react-router-dom'), + useHistory: () => mockHistory, +})); + const FakeStatefulEventsViewer = ({ additionalFilters }: { additionalFilters: JSX.Element }) => (
{additionalFilters} diff --git a/x-pack/plugins/security_solution/public/common/components/events_tab/events_query_tab_body.tsx b/x-pack/plugins/security_solution/public/common/components/events_tab/events_query_tab_body.tsx index db663c1f0fc6..07c9995ddbd9 100644 --- a/x-pack/plugins/security_solution/public/common/components/events_tab/events_query_tab_body.tsx +++ b/x-pack/plugins/security_solution/public/common/components/events_tab/events_query_tab_body.tsx @@ -42,6 +42,11 @@ import { useLicense } from '../../hooks/use_license'; import { useUiSetting$ } from '../../lib/kibana'; import { defaultAlertsFilters } from '../events_viewer/external_alerts_filter'; +import { + useGetInitialUrlParamValue, + useReplaceUrlParams, +} from '../../utils/global_query_string/helpers'; + export const ALERTS_EVENTS_HISTOGRAM_ID = 'alertsOrEventsHistogramQuery'; type QueryTabBodyProps = UserQueryTabBodyProps | HostQueryTabBodyProps | NetworkQueryTabBodyProps; @@ -55,6 +60,8 @@ export type EventsQueryTabBodyComponentProps = QueryTabBodyProps & { timelineId: TimelineId; }; +const EXTERNAL_ALERTS_URL_PARAM = 'onlyExternalAlerts'; + const EventsQueryTabBodyComponent: React.FC = ({ deleteQuery, endDate, @@ -70,7 +77,6 @@ const EventsQueryTabBodyComponent: React.FC = const { globalFullScreen } = useGlobalFullScreen(); const tGridEnabled = useIsExperimentalFeatureEnabled('tGridEnabled'); const [defaultNumberFormat] = useUiSetting$(DEFAULT_NUMBER_FORMAT); - const [showExternalAlerts, setShowExternalAlerts] = useState(false); const isEnterprisePlus = useLicense().isEnterprise(); const ACTION_BUTTON_COUNT = isEnterprisePlus ? 5 : 4; const leadingControlColumns = useMemo( @@ -78,6 +84,14 @@ const EventsQueryTabBodyComponent: React.FC = [ACTION_BUTTON_COUNT] ); + const showExternalAlertsInitialUrlState = useExternalAlertsInitialUrlState(); + + const [showExternalAlerts, setShowExternalAlerts] = useState( + showExternalAlertsInitialUrlState ?? false + ); + + useSyncExternalAlertsUrlState(showExternalAlerts); + const toggleExternalAlerts = useCallback(() => setShowExternalAlerts((s) => !s), []); const getHistogramSubtitle = useMemo( () => getSubtitleFunction(defaultNumberFormat, showExternalAlerts), @@ -178,3 +192,43 @@ EventsQueryTabBodyComponent.displayName = 'EventsQueryTabBodyComponent'; export const EventsQueryTabBody = React.memo(EventsQueryTabBodyComponent); EventsQueryTabBody.displayName = 'EventsQueryTabBody'; + +const useExternalAlertsInitialUrlState = () => { + const replaceUrlParams = useReplaceUrlParams(); + + const getInitialUrlParamValue = useGetInitialUrlParamValue(EXTERNAL_ALERTS_URL_PARAM); + + const { decodedParam: showExternalAlertsInitialUrlState } = useMemo( + () => getInitialUrlParamValue(), + [getInitialUrlParamValue] + ); + + useEffect(() => { + // Only called on component unmount + return () => { + replaceUrlParams([ + { + key: EXTERNAL_ALERTS_URL_PARAM, + value: null, + }, + ]); + }; + }, [replaceUrlParams]); + + return showExternalAlertsInitialUrlState; +}; + +/** + * Update URL state when showExternalAlerts value changes + */ +const useSyncExternalAlertsUrlState = (showExternalAlerts: boolean) => { + const replaceUrlParams = useReplaceUrlParams(); + useEffect(() => { + replaceUrlParams([ + { + key: EXTERNAL_ALERTS_URL_PARAM, + value: showExternalAlerts ? 'true' : null, + }, + ]); + }, [showExternalAlerts, replaceUrlParams]); +}; diff --git a/x-pack/plugins/security_solution/public/common/utils/global_query_string/helpers.ts b/x-pack/plugins/security_solution/public/common/utils/global_query_string/helpers.ts index 63684f198504..a5f7e9314675 100644 --- a/x-pack/plugins/security_solution/public/common/utils/global_query_string/helpers.ts +++ b/x-pack/plugins/security_solution/public/common/utils/global_query_string/helpers.ts @@ -5,8 +5,12 @@ * 2.0. */ -import { parse } from 'query-string'; import { decode, encode } from 'rison-node'; +import type { ParsedQuery } from 'query-string'; +import { parse, stringify } from 'query-string'; +import { url } from '@kbn/kibana-utils-plugin/public'; +import { useHistory } from 'react-router-dom'; +import { useCallback } from 'react'; import { SecurityPageName } from '../../../app/types'; export const isDetectionsPages = (pageName: string) => @@ -40,3 +44,59 @@ export const getParamFromQueryString = ( return Array.isArray(queryParam) ? queryParam[0] : queryParam; }; + +/** + * + * Gets the value of the URL param from the query string. + * It doesn't update when the URL changes. + * + */ +export const useGetInitialUrlParamValue = (urlParamKey: string) => { + // window.location.search provides the most updated representation of the url search. + // It also guarantees that we don't overwrite URL param managed outside react-router. + const getInitialUrlParamValue = useCallback(() => { + const param = getParamFromQueryString( + getQueryStringFromLocation(window.location.search), + urlParamKey + ); + + const decodedParam = decodeRisonUrlState(param ?? undefined); + + return { param, decodedParam }; + }, [urlParamKey]); + + return getInitialUrlParamValue; +}; + +export const encodeQueryString = (urlParams: ParsedQuery): string => + stringify(url.encodeQuery(urlParams), { sort: false, encode: false }); + +export const useReplaceUrlParams = () => { + const history = useHistory(); + + const replaceUrlParams = useCallback( + (params: Array<{ key: string; value: string | null }>) => { + // window.location.search provides the most updated representation of the url search. + // It prevents unnecessary re-renders which useLocation would create because 'replaceUrlParams' does update the location. + // window.location.search also guarantees that we don't overwrite URL param managed outside react-router. + const search = window.location.search; + const urlParams = parse(search, { sort: false }); + + params.forEach(({ key, value }) => { + if (value == null || value === '') { + delete urlParams[key]; + } else { + urlParams[key] = value; + } + }); + + const newSearch = encodeQueryString(urlParams); + + if (getQueryStringFromLocation(search) !== newSearch) { + history.replace({ search: newSearch }); + } + }, + [history] + ); + return replaceUrlParams; +}; diff --git a/x-pack/plugins/security_solution/public/common/utils/global_query_string/index.test.tsx b/x-pack/plugins/security_solution/public/common/utils/global_query_string/index.test.tsx index a400ab24e06d..dede5125775c 100644 --- a/x-pack/plugins/security_solution/public/common/utils/global_query_string/index.test.tsx +++ b/x-pack/plugins/security_solution/public/common/utils/global_query_string/index.test.tsx @@ -6,7 +6,7 @@ */ import React from 'react'; -import { renderHook } from '@testing-library/react-hooks'; +import { act, renderHook } from '@testing-library/react-hooks'; import { useInitializeUrlParam, useGlobalQueryString, @@ -296,5 +296,33 @@ describe('global query string', () => { expect(mockHistory.replace).not.toHaveBeenCalledWith(); }); + + it('deletes unregistered URL params', async () => { + const urlParamKey = 'testKey'; + const value = '123'; + window.location.search = `?${urlParamKey}=${value}`; + const globalUrlParam = { + [urlParamKey]: value, + }; + const store = makeStore(globalUrlParam); + + const { waitForNextUpdate } = renderHook(() => useSyncGlobalQueryString(), { + wrapper: ({ children }: { children: React.ReactElement }) => ( + {children} + ), + }); + + mockHistory.replace.mockClear(); + + act(() => { + store.dispatch(globalUrlParamActions.deregisterUrlParam({ key: urlParamKey })); + }); + + waitForNextUpdate(); + + expect(mockHistory.replace).toHaveBeenCalledWith({ + search: ``, + }); + }); }); }); diff --git a/x-pack/plugins/security_solution/public/common/utils/global_query_string/index.ts b/x-pack/plugins/security_solution/public/common/utils/global_query_string/index.ts index 09588c7298c0..96834d39fd64 100644 --- a/x-pack/plugins/security_solution/public/common/utils/global_query_string/index.ts +++ b/x-pack/plugins/security_solution/public/common/utils/global_query_string/index.ts @@ -5,20 +5,15 @@ * 2.0. */ -import type * as H from 'history'; -import type { ParsedQuery } from 'query-string'; -import { parse, stringify } from 'query-string'; import { useCallback, useEffect, useMemo } from 'react'; - -import { url } from '@kbn/kibana-utils-plugin/public'; -import { isEmpty, pickBy } from 'lodash/fp'; -import { useHistory } from 'react-router-dom'; +import { difference, isEmpty, pickBy } from 'lodash/fp'; import { useDispatch } from 'react-redux'; +import usePrevious from 'react-use/lib/usePrevious'; import { - decodeRisonUrlState, + encodeQueryString, encodeRisonUrlState, - getParamFromQueryString, - getQueryStringFromLocation, + useGetInitialUrlParamValue, + useReplaceUrlParams, } from './helpers'; import { useShallowEqualSelector } from '../../hooks/use_selector'; import { globalUrlParamActions, globalUrlParamSelectors } from '../../store/global_url_param'; @@ -43,13 +38,10 @@ export const useInitializeUrlParam = ( ) => { const dispatch = useDispatch(); + const getInitialUrlParamValue = useGetInitialUrlParamValue(urlParamKey); + useEffect(() => { - // window.location.search provides the most updated representation of the url search. - // It also guarantees that we don't overwrite URL param managed outside react-router. - const initialValue = getParamFromQueryString( - getQueryStringFromLocation(window.location.search), - urlParamKey - ); + const { param: initialValue, decodedParam: decodedInitialValue } = getInitialUrlParamValue(); dispatch( globalUrlParamActions.registerUrlParam({ @@ -59,7 +51,7 @@ export const useInitializeUrlParam = ( ); // execute consumer initialization - onInitialize(decodeRisonUrlState(initialValue ?? undefined)); + onInitialize(decodedInitialValue); return () => { dispatch(globalUrlParamActions.deregisterUrlParam({ key: urlParamKey })); @@ -103,9 +95,16 @@ export const useGlobalQueryString = (): string => { * - It updates the URL when globalUrlParam store updates. */ export const useSyncGlobalQueryString = () => { - const history = useHistory(); const [{ pageName }] = useRouteSpy(); const globalUrlParam = useShallowEqualSelector(globalUrlParamSelectors.selectGlobalUrlParam); + const previousGlobalUrlParams = usePrevious(globalUrlParam); + const replaceUrlParams = useReplaceUrlParams(); + + // Url params that got deleted from GlobalUrlParams + const unregisteredKeys = useMemo( + () => difference(Object.keys(previousGlobalUrlParams ?? {}), Object.keys(globalUrlParam)), + [previousGlobalUrlParams, globalUrlParam] + ); useEffect(() => { const linkInfo = getLinkInfo(pageName) ?? { skipUrlState: true }; @@ -114,36 +113,16 @@ export const useSyncGlobalQueryString = () => { value: linkInfo.skipUrlState ? null : value, })); - if (params.length > 0) { - // window.location.search provides the most updated representation of the url search. - // It prevents unnecessary re-renders which useLocation would create because 'replaceUrlParams' does update the location. - // window.location.search also guarantees that we don't overwrite URL param managed outside react-router. - replaceUrlParams(params, history, window.location.search); - } - }, [globalUrlParam, pageName, history]); -}; - -const encodeQueryString = (urlParams: ParsedQuery): string => - stringify(url.encodeQuery(urlParams), { sort: false, encode: false }); - -const replaceUrlParams = ( - params: Array<{ key: string; value: string | null }>, - history: H.History, - search: string -) => { - const urlParams = parse(search, { sort: false }); + // Delete unregistered Url params + unregisteredKeys.forEach((key) => { + params.push({ + key, + value: null, + }); + }); - params.forEach(({ key, value }) => { - if (value == null || value === '') { - delete urlParams[key]; - } else { - urlParams[key] = value; + if (params.length > 0) { + replaceUrlParams(params); } - }); - - const newSearch = encodeQueryString(urlParams); - - if (getQueryStringFromLocation(search) !== newSearch) { - history.replace({ search: newSearch }); - } + }, [globalUrlParam, pageName, unregisteredKeys, replaceUrlParams]); }; From f1c12a097acf9c5720bb12676787725b4739fbf7 Mon Sep 17 00:00:00 2001 From: suchcodemuchwow Date: Tue, 4 Oct 2022 11:34:24 +0200 Subject: [PATCH 3/4] Revert "[Synthetics UI] Serialize errors before sending to redux store to prevent warnings (#142259)" This reverts commit b3a749e55a55f5ab1df4d236916dc270209e83fe. --- .../public/apps/synthetics/state/index_status/actions.ts | 5 ++--- .../public/apps/synthetics/state/index_status/index.ts | 4 ++-- .../apps/synthetics/state/monitor_details/index.ts | 7 ++++--- .../public/apps/synthetics/state/monitor_list/actions.ts | 9 ++++----- .../public/apps/synthetics/state/monitor_list/effects.ts | 4 ++-- .../public/apps/synthetics/state/monitor_list/index.ts | 4 ++-- .../public/apps/synthetics/state/overview/index.ts | 6 +++--- .../apps/synthetics/state/service_locations/actions.ts | 5 +---- .../apps/synthetics/state/service_locations/index.ts | 3 +-- .../synthetics/state/synthetics_enablement/actions.ts | 7 +++---- .../apps/synthetics/state/synthetics_enablement/index.ts | 3 +-- .../public/apps/synthetics/state/utils/actions.ts | 4 ++-- .../public/apps/synthetics/state/utils/fetch_effect.ts | 7 +++---- .../legacy_uptime/state/private_locations/index.ts | 6 +++--- 14 files changed, 33 insertions(+), 41 deletions(-) diff --git a/x-pack/plugins/synthetics/public/apps/synthetics/state/index_status/actions.ts b/x-pack/plugins/synthetics/public/apps/synthetics/state/index_status/actions.ts index e522af3bfed7..36e2e2514910 100644 --- a/x-pack/plugins/synthetics/public/apps/synthetics/state/index_status/actions.ts +++ b/x-pack/plugins/synthetics/public/apps/synthetics/state/index_status/actions.ts @@ -5,11 +5,10 @@ * 2.0. */ +import type { IHttpFetchError } from '@kbn/core-http-browser'; import { createAction } from '@reduxjs/toolkit'; import { StatesIndexStatus } from '../../../../../common/runtime_types'; -import { IHttpSerializedFetchError } from '../utils/http_error'; export const getIndexStatus = createAction('[INDEX STATUS] GET'); export const getIndexStatusSuccess = createAction('[INDEX STATUS] GET SUCCESS'); -export const getIndexStatusFail = - createAction('[INDEX STATUS] GET FAIL'); +export const getIndexStatusFail = createAction('[INDEX STATUS] GET FAIL'); diff --git a/x-pack/plugins/synthetics/public/apps/synthetics/state/index_status/index.ts b/x-pack/plugins/synthetics/public/apps/synthetics/state/index_status/index.ts index 19ef8f94938a..f5351c65d0d6 100644 --- a/x-pack/plugins/synthetics/public/apps/synthetics/state/index_status/index.ts +++ b/x-pack/plugins/synthetics/public/apps/synthetics/state/index_status/index.ts @@ -6,7 +6,7 @@ */ import { createReducer } from '@reduxjs/toolkit'; -import { IHttpSerializedFetchError } from '../utils/http_error'; +import { IHttpSerializedFetchError, serializeHttpFetchError } from '../utils/http_error'; import { StatesIndexStatus } from '../../../../../common/runtime_types'; import { getIndexStatus, getIndexStatusSuccess, getIndexStatusFail } from './actions'; @@ -33,7 +33,7 @@ export const indexStatusReducer = createReducer(initialState, (builder) => { state.loading = false; }) .addCase(getIndexStatusFail, (state, action) => { - state.error = action.payload; + state.error = serializeHttpFetchError(action.payload); state.loading = false; }); }); diff --git a/x-pack/plugins/synthetics/public/apps/synthetics/state/monitor_details/index.ts b/x-pack/plugins/synthetics/public/apps/synthetics/state/monitor_details/index.ts index b1fb95d5d5ee..a2d9379df778 100644 --- a/x-pack/plugins/synthetics/public/apps/synthetics/state/monitor_details/index.ts +++ b/x-pack/plugins/synthetics/public/apps/synthetics/state/monitor_details/index.ts @@ -5,8 +5,9 @@ * 2.0. */ +import { IHttpFetchError, ResponseErrorBody } from '@kbn/core/public'; import { createReducer } from '@reduxjs/toolkit'; -import { IHttpSerializedFetchError } from '../utils/http_error'; +import { IHttpSerializedFetchError, serializeHttpFetchError } from '../utils/http_error'; import { getMonitorRecentPingsAction, setMonitorDetailsLocationAction, @@ -46,7 +47,7 @@ export const monitorDetailsReducer = createReducer(initialState, (builder) => { state.loading = false; }) .addCase(getMonitorRecentPingsAction.fail, (state, action) => { - state.error = action.payload; + state.error = serializeHttpFetchError(action.payload as IHttpFetchError); state.loading = false; }) @@ -58,7 +59,7 @@ export const monitorDetailsReducer = createReducer(initialState, (builder) => { state.syntheticsMonitorLoading = false; }) .addCase(getMonitorAction.fail, (state, action) => { - state.error = action.payload; + state.error = serializeHttpFetchError(action.payload as IHttpFetchError); state.syntheticsMonitorLoading = false; }); }); diff --git a/x-pack/plugins/synthetics/public/apps/synthetics/state/monitor_list/actions.ts b/x-pack/plugins/synthetics/public/apps/synthetics/state/monitor_list/actions.ts index 5a8c38284e03..fcfc3d4f22cf 100644 --- a/x-pack/plugins/synthetics/public/apps/synthetics/state/monitor_list/actions.ts +++ b/x-pack/plugins/synthetics/public/apps/synthetics/state/monitor_list/actions.ts @@ -5,13 +5,13 @@ * 2.0. */ +import { IHttpFetchError } from '@kbn/core-http-browser'; import { createAction } from '@reduxjs/toolkit'; import { EncryptedSyntheticsMonitor, MonitorManagementListResult, } from '../../../../../common/runtime_types'; import { createAsyncAction } from '../utils/actions'; -import { IHttpSerializedFetchError } from '../utils/http_error'; import { MonitorListPageState } from './models'; @@ -29,8 +29,7 @@ export const fetchUpsertSuccessAction = createAction<{ id: string; attributes: { enabled: boolean }; }>('fetchUpsertMonitorSuccess'); -export const fetchUpsertFailureAction = createAction<{ - id: string; - error: IHttpSerializedFetchError; -}>('fetchUpsertMonitorFailure'); +export const fetchUpsertFailureAction = createAction<{ id: string; error: IHttpFetchError }>( + 'fetchUpsertMonitorFailure' +); export const clearMonitorUpsertStatus = createAction('clearMonitorUpsertStatus'); diff --git a/x-pack/plugins/synthetics/public/apps/synthetics/state/monitor_list/effects.ts b/x-pack/plugins/synthetics/public/apps/synthetics/state/monitor_list/effects.ts index 67aaa4ec982e..0dee2edfd790 100644 --- a/x-pack/plugins/synthetics/public/apps/synthetics/state/monitor_list/effects.ts +++ b/x-pack/plugins/synthetics/public/apps/synthetics/state/monitor_list/effects.ts @@ -5,10 +5,10 @@ * 2.0. */ +import { IHttpFetchError } from '@kbn/core-http-browser'; import { PayloadAction } from '@reduxjs/toolkit'; import { call, put, takeEvery, takeLeading } from 'redux-saga/effects'; import { fetchEffectFactory } from '../utils/fetch_effect'; -import { serializeHttpFetchError } from '../utils/http_error'; import { fetchMonitorListAction, fetchUpsertFailureAction, @@ -40,7 +40,7 @@ export function* upsertMonitorEffect() { ); } catch (error) { yield put( - fetchUpsertFailureAction({ id: action.payload.id, error: serializeHttpFetchError(error) }) + fetchUpsertFailureAction({ id: action.payload.id, error: error as IHttpFetchError }) ); } } diff --git a/x-pack/plugins/synthetics/public/apps/synthetics/state/monitor_list/index.ts b/x-pack/plugins/synthetics/public/apps/synthetics/state/monitor_list/index.ts index 997f853c9bfc..e1f564c0d0a3 100644 --- a/x-pack/plugins/synthetics/public/apps/synthetics/state/monitor_list/index.ts +++ b/x-pack/plugins/synthetics/public/apps/synthetics/state/monitor_list/index.ts @@ -10,7 +10,7 @@ import { FETCH_STATUS } from '@kbn/observability-plugin/public'; import { ConfigKey, MonitorManagementListResult } from '../../../../../common/runtime_types'; -import { IHttpSerializedFetchError } from '../utils/http_error'; +import { IHttpSerializedFetchError, serializeHttpFetchError } from '../utils/http_error'; import { MonitorListPageState } from './models'; import { @@ -58,7 +58,7 @@ export const monitorListReducer = createReducer(initialState, (builder) => { }) .addCase(fetchMonitorListAction.fail, (state, action) => { state.loading = false; - state.error = action.payload; + state.error = serializeHttpFetchError(action.payload); }) .addCase(fetchUpsertMonitorAction, (state, action) => { state.monitorUpsertStatuses[action.payload.id] = { diff --git a/x-pack/plugins/synthetics/public/apps/synthetics/state/overview/index.ts b/x-pack/plugins/synthetics/public/apps/synthetics/state/overview/index.ts index aa4a8db73b98..49159b29ef46 100644 --- a/x-pack/plugins/synthetics/public/apps/synthetics/state/overview/index.ts +++ b/x-pack/plugins/synthetics/public/apps/synthetics/state/overview/index.ts @@ -9,7 +9,7 @@ import { createReducer } from '@reduxjs/toolkit'; import { MonitorOverviewResult, OverviewStatus } from '../../../../../common/runtime_types'; -import { IHttpSerializedFetchError } from '../utils/http_error'; +import { IHttpSerializedFetchError, serializeHttpFetchError } from '../utils/http_error'; import { MonitorOverviewPageState } from './models'; import { @@ -60,13 +60,13 @@ export const monitorOverviewReducer = createReducer(initialState, (builder) => { }) .addCase(fetchMonitorOverviewAction.fail, (state, action) => { state.loading = false; - state.error = action.payload; + state.error = serializeHttpFetchError(action.payload); }) .addCase(quietFetchOverviewAction.success, (state, action) => { state.data = action.payload; }) .addCase(quietFetchOverviewAction.fail, (state, action) => { - state.error = action.payload; + state.error = serializeHttpFetchError(action.payload); }) .addCase(setOverviewPerPageAction, (state, action) => { state.pageState = { diff --git a/x-pack/plugins/synthetics/public/apps/synthetics/state/service_locations/actions.ts b/x-pack/plugins/synthetics/public/apps/synthetics/state/service_locations/actions.ts index dbdd53d4cbcb..794e16d0292c 100644 --- a/x-pack/plugins/synthetics/public/apps/synthetics/state/service_locations/actions.ts +++ b/x-pack/plugins/synthetics/public/apps/synthetics/state/service_locations/actions.ts @@ -7,13 +7,10 @@ import { createAction } from '@reduxjs/toolkit'; import { ServiceLocations, ThrottlingOptions } from '../../../../../common/runtime_types'; -import { IHttpSerializedFetchError } from '../utils/http_error'; export const getServiceLocations = createAction('[SERVICE LOCATIONS] GET'); export const getServiceLocationsSuccess = createAction<{ throttling: ThrottlingOptions | undefined; locations: ServiceLocations; }>('[SERVICE LOCATIONS] GET SUCCESS'); -export const getServiceLocationsFailure = createAction( - '[SERVICE LOCATIONS] GET FAILURE' -); +export const getServiceLocationsFailure = createAction('[SERVICE LOCATIONS] GET FAILURE'); diff --git a/x-pack/plugins/synthetics/public/apps/synthetics/state/service_locations/index.ts b/x-pack/plugins/synthetics/public/apps/synthetics/state/service_locations/index.ts index 9a338458e603..e13fe756ec7f 100644 --- a/x-pack/plugins/synthetics/public/apps/synthetics/state/service_locations/index.ts +++ b/x-pack/plugins/synthetics/public/apps/synthetics/state/service_locations/index.ts @@ -11,7 +11,6 @@ import { ServiceLocations, ThrottlingOptions, } from '../../../../../common/runtime_types'; -import { IHttpSerializedFetchError } from '../utils/http_error'; import { getServiceLocations, @@ -23,7 +22,7 @@ export interface ServiceLocationsState { locations: ServiceLocations; throttling: ThrottlingOptions | null; loading: boolean; - error: IHttpSerializedFetchError | null; + error: Error | null; locationsLoaded?: boolean; } diff --git a/x-pack/plugins/synthetics/public/apps/synthetics/state/synthetics_enablement/actions.ts b/x-pack/plugins/synthetics/public/apps/synthetics/state/synthetics_enablement/actions.ts index 0c7abffd1b28..c38fadc0952a 100644 --- a/x-pack/plugins/synthetics/public/apps/synthetics/state/synthetics_enablement/actions.ts +++ b/x-pack/plugins/synthetics/public/apps/synthetics/state/synthetics_enablement/actions.ts @@ -7,24 +7,23 @@ import { createAction } from '@reduxjs/toolkit'; import { MonitorManagementEnablementResult } from '../../../../../common/runtime_types'; -import { IHttpSerializedFetchError } from '../utils/http_error'; export const getSyntheticsEnablement = createAction('[SYNTHETICS_ENABLEMENT] GET'); export const getSyntheticsEnablementSuccess = createAction( '[SYNTHETICS_ENABLEMENT] GET SUCCESS' ); -export const getSyntheticsEnablementFailure = createAction( +export const getSyntheticsEnablementFailure = createAction( '[SYNTHETICS_ENABLEMENT] GET FAILURE' ); export const disableSynthetics = createAction('[SYNTHETICS_ENABLEMENT] DISABLE'); export const disableSyntheticsSuccess = createAction<{}>('[SYNTHETICS_ENABLEMENT] DISABLE SUCCESS'); -export const disableSyntheticsFailure = createAction( +export const disableSyntheticsFailure = createAction( '[SYNTHETICS_ENABLEMENT] DISABLE FAILURE' ); export const enableSynthetics = createAction('[SYNTHETICS_ENABLEMENT] ENABLE'); export const enableSyntheticsSuccess = createAction<{}>('[SYNTHETICS_ENABLEMENT] ENABLE SUCCESS'); -export const enableSyntheticsFailure = createAction( +export const enableSyntheticsFailure = createAction( '[SYNTHETICS_ENABLEMENT] ENABLE FAILURE' ); diff --git a/x-pack/plugins/synthetics/public/apps/synthetics/state/synthetics_enablement/index.ts b/x-pack/plugins/synthetics/public/apps/synthetics/state/synthetics_enablement/index.ts index 3bf9ff69bf00..62ed85ad17e8 100644 --- a/x-pack/plugins/synthetics/public/apps/synthetics/state/synthetics_enablement/index.ts +++ b/x-pack/plugins/synthetics/public/apps/synthetics/state/synthetics_enablement/index.ts @@ -18,11 +18,10 @@ import { getSyntheticsEnablementFailure, } from './actions'; import { MonitorManagementEnablementResult } from '../../../../../common/runtime_types'; -import { IHttpSerializedFetchError } from '../utils/http_error'; export interface SyntheticsEnablementState { loading: boolean; - error: IHttpSerializedFetchError | null; + error: Error | null; enablement: MonitorManagementEnablementResult | null; } diff --git a/x-pack/plugins/synthetics/public/apps/synthetics/state/utils/actions.ts b/x-pack/plugins/synthetics/public/apps/synthetics/state/utils/actions.ts index 35e93fd91484..416c3134d603 100644 --- a/x-pack/plugins/synthetics/public/apps/synthetics/state/utils/actions.ts +++ b/x-pack/plugins/synthetics/public/apps/synthetics/state/utils/actions.ts @@ -6,13 +6,13 @@ */ import { createAction } from '@reduxjs/toolkit'; -import type { IHttpSerializedFetchError } from './http_error'; +import type { IHttpFetchError } from '@kbn/core-http-browser'; export function createAsyncAction(actionStr: string) { return { get: createAction(actionStr), success: createAction(`${actionStr}_SUCCESS`), - fail: createAction(`${actionStr}_FAIL`), + fail: createAction(`${actionStr}_FAIL`), }; } diff --git a/x-pack/plugins/synthetics/public/apps/synthetics/state/utils/fetch_effect.ts b/x-pack/plugins/synthetics/public/apps/synthetics/state/utils/fetch_effect.ts index 294da718a6fd..b07f1fa54263 100644 --- a/x-pack/plugins/synthetics/public/apps/synthetics/state/utils/fetch_effect.ts +++ b/x-pack/plugins/synthetics/public/apps/synthetics/state/utils/fetch_effect.ts @@ -8,7 +8,6 @@ import { call, put } from 'redux-saga/effects'; import { PayloadAction } from '@reduxjs/toolkit'; import type { IHttpFetchError } from '@kbn/core-http-browser'; -import { IHttpSerializedFetchError, serializeHttpFetchError } from './http_error'; /** * Factory function for a fetch effect. It expects three action creators, @@ -24,7 +23,7 @@ import { IHttpSerializedFetchError, serializeHttpFetchError } from './http_error export function fetchEffectFactory( fetch: (request: T) => Promise, success: (response: R) => PayloadAction, - fail: (error: IHttpSerializedFetchError) => PayloadAction + fail: (error: IHttpFetchError) => PayloadAction ) { return function* (action: PayloadAction): Generator { try { @@ -33,14 +32,14 @@ export function fetchEffectFactory( // eslint-disable-next-line no-console console.error(response); - yield put(fail(serializeHttpFetchError(response as IHttpFetchError))); + yield put(fail(response as IHttpFetchError)); } else { yield put(success(response as R)); } } catch (error) { // eslint-disable-next-line no-console console.error(error); - yield put(fail(serializeHttpFetchError(error))); + yield put(fail(error as IHttpFetchError)); } }; } diff --git a/x-pack/plugins/synthetics/public/legacy_uptime/state/private_locations/index.ts b/x-pack/plugins/synthetics/public/legacy_uptime/state/private_locations/index.ts index 831f8a9cbf6b..0ff45023143e 100644 --- a/x-pack/plugins/synthetics/public/legacy_uptime/state/private_locations/index.ts +++ b/x-pack/plugins/synthetics/public/legacy_uptime/state/private_locations/index.ts @@ -5,9 +5,9 @@ * 2.0. */ +import { IHttpFetchError, ResponseErrorBody } from '@kbn/core/public'; import { createReducer } from '@reduxjs/toolkit'; import { AgentPolicy } from '@kbn/fleet-plugin/common'; -import { IHttpSerializedFetchError } from '../../../apps/synthetics/state'; import { getAgentPoliciesAction, setAddingNewPrivateLocation, @@ -24,7 +24,7 @@ export interface AgentPoliciesList { export interface AgentPoliciesState { data: AgentPoliciesList | null; loading: boolean; - error: IHttpSerializedFetchError | null; + error: IHttpFetchError | null; isManageFlyoutOpen?: boolean; isAddingNewPrivateLocation?: boolean; } @@ -47,7 +47,7 @@ export const agentPoliciesReducer = createReducer(initialState, (builder) => { state.loading = false; }) .addCase(getAgentPoliciesAction.fail, (state, action) => { - state.error = action.payload; + state.error = action.payload as IHttpFetchError; state.loading = false; }) .addCase(setManageFlyoutOpen, (state, action) => { From 10884e6a5fa57bab81ec61f15c320b1472bc0df9 Mon Sep 17 00:00:00 2001 From: Vitalii Dmyterko <92328789+vitaliidm@users.noreply.github.com> Date: Tue, 4 Oct 2022 10:39:50 +0100 Subject: [PATCH 4/4] [Security Solution][Detections] refactors update rule actions tests (#142464) ## Summary - addresses https://github.com/elastic/kibana/issues/138757 according to proposal in above task: - removes step of updating immutable rule with mock data - makes assertions whether rule properties were not modified against fetched earlier immutable rule ### Checklist - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios --- .../group1/update_actions.ts | 86 +++++++++++-------- .../utils/index.ts | 1 + .../utils/rule_to_update_schema.ts | 36 ++++++++ 3 files changed, 85 insertions(+), 38 deletions(-) create mode 100644 x-pack/test/detection_engine_api_integration/utils/rule_to_update_schema.ts diff --git a/x-pack/test/detection_engine_api_integration/security_and_spaces/group1/update_actions.ts b/x-pack/test/detection_engine_api_integration/security_and_spaces/group1/update_actions.ts index e9e7e18ea318..8ee61c070545 100644 --- a/x-pack/test/detection_engine_api_integration/security_and_spaces/group1/update_actions.ts +++ b/x-pack/test/detection_engine_api_integration/security_and_spaces/group1/update_actions.ts @@ -6,6 +6,7 @@ */ import expect from '@kbn/expect'; +import { omit } from 'lodash'; import { CreateRulesSchema } from '@kbn/security-solution-plugin/common/detection_engine/schemas/request'; import { FtrProviderContext } from '../../common/ftr_provider_context'; @@ -26,14 +27,24 @@ import { findImmutableRuleById, getPrePackagedRulesStatus, getSimpleRuleOutput, + ruleToUpdateSchema, } from '../../utils'; +// Rule id of "9a1a2dae-0b5f-4c3d-8305-a268d404c306" is from the file: +// x-pack/plugins/security_solution/server/lib/detection_engine/rules/prepackaged_rules/elastic_endpoint_security.json +const RULE_ID = '9a1a2dae-0b5f-4c3d-8305-a268d404c306'; + // eslint-disable-next-line import/no-default-export export default ({ getService }: FtrProviderContext) => { const supertest = getService('supertest'); const esArchiver = getService('esArchiver'); const log = getService('log'); + const getImmutableRule = async () => { + await installPrePackagedRules(supertest, log); + return getRule(supertest, log, RULE_ID); + }; + describe('update_actions', () => { describe('updating actions', () => { before(async () => { @@ -105,50 +116,53 @@ export default ({ getService }: FtrProviderContext) => { }); it('should not change properties of immutable rule when applying actions to it', async () => { - await installPrePackagedRules(supertest, log); - // Rule id of "9a1a2dae-0b5f-4c3d-8305-a268d404c306" is from the file: - // x-pack/plugins/security_solution/server/lib/detection_engine/rules/prepackaged_rules/elastic_endpoint.json - const immutableRule = await getRule(supertest, log, '9a1a2dae-0b5f-4c3d-8305-a268d404c306'); + // actions and throttle to be removed from assertion (it asserted in a separate test case) + const actionsProps = ['actions', 'throttle']; + + const immutableRule = await getImmutableRule(); const hookAction = await createNewAction(supertest, log); - const newRuleToUpdate = getSimpleRule(immutableRule.rule_id); - const ruleToUpdate = getRuleWithWebHookAction(hookAction.id, false, newRuleToUpdate); + const ruleToUpdate = getRuleWithWebHookAction( + hookAction.id, + immutableRule.enabled, + ruleToUpdateSchema(immutableRule) + ); const updatedRule = await updateRule(supertest, log, ruleToUpdate); - const bodyToCompare = removeServerGeneratedProperties(updatedRule); + const expected = omit(removeServerGeneratedProperties(updatedRule), actionsProps); - const expected = { - ...getSimpleRuleOutputWithWebHookAction(`${bodyToCompare.actions?.[0].id}`), - rule_id: immutableRule.rule_id, // Rule id should match the same as the immutable rule - version: immutableRule.version, // This version number should not change when an immutable rule is updated - immutable: true, // It should stay immutable true when returning - required_fields: immutableRule.required_fields, // required_fields cannot be modified, so newRuleToUpdate will have required_fields from immutable rule - }; - expect(bodyToCompare).to.eql(expected); + const immutableRuleToAssert = omit( + removeServerGeneratedProperties(immutableRule), + actionsProps + ); + + expect(immutableRuleToAssert).to.eql(expected); + expect(expected.immutable).to.be(true); // It should stay immutable true when returning }); it('should be able to create a new webhook action and attach it to an immutable rule', async () => { - await installPrePackagedRules(supertest, log); - // Rule id of "9a1a2dae-0b5f-4c3d-8305-a268d404c306" is from the file: - // x-pack/plugins/security_solution/server/lib/detection_engine/rules/prepackaged_rules/elastic_endpoint.json - const immutableRule = await getRule(supertest, log, '9a1a2dae-0b5f-4c3d-8305-a268d404c306'); + const immutableRule = await getImmutableRule(); const hookAction = await createNewAction(supertest, log); - const newRuleToUpdate = getSimpleRule(immutableRule.rule_id); - const ruleToUpdate = getRuleWithWebHookAction(hookAction.id, false, newRuleToUpdate); + const ruleToUpdate = getRuleWithWebHookAction( + hookAction.id, + immutableRule.enabled, + ruleToUpdateSchema(immutableRule) + ); const updatedRule = await updateRule(supertest, log, ruleToUpdate); const bodyToCompare = removeServerGeneratedProperties(updatedRule); const expected = getSimpleRuleOutputWithWebHookAction(`${bodyToCompare.actions?.[0].id}`); expect(bodyToCompare.actions).to.eql(expected.actions); + expect(bodyToCompare.throttle).to.eql(expected.throttle); }); it('should be able to create a new webhook action, attach it to an immutable rule and the count of prepackaged rules should not increase. If this fails, suspect the immutable tags are not staying on the rule correctly.', async () => { - await installPrePackagedRules(supertest, log); - // Rule id of "9a1a2dae-0b5f-4c3d-8305-a268d404c306" is from the file: - // x-pack/plugins/security_solution/server/lib/detection_engine/rules/prepackaged_rules/elastic_endpoint.json - const immutableRule = await getRule(supertest, log, '9a1a2dae-0b5f-4c3d-8305-a268d404c306'); + const immutableRule = await getImmutableRule(); const hookAction = await createNewAction(supertest, log); - const newRuleToUpdate = getSimpleRule(immutableRule.rule_id); - const ruleToUpdate = getRuleWithWebHookAction(hookAction.id, false, newRuleToUpdate); + const ruleToUpdate = getRuleWithWebHookAction( + hookAction.id, + immutableRule.enabled, + ruleToUpdateSchema(immutableRule) + ); await updateRule(supertest, log, ruleToUpdate); const status = await getPrePackagedRulesStatus(supertest, log); @@ -156,19 +170,15 @@ export default ({ getService }: FtrProviderContext) => { }); it('should be able to create a new webhook action, attach it to an immutable rule and the rule should stay immutable when searching against immutable tags', async () => { - await installPrePackagedRules(supertest, log); - // Rule id of "9a1a2dae-0b5f-4c3d-8305-a268d404c306" is from the file: - // x-pack/plugins/security_solution/server/lib/detection_engine/rules/prepackaged_rules/elastic_endpoint.json - const immutableRule = await getRule(supertest, log, '9a1a2dae-0b5f-4c3d-8305-a268d404c306'); + const immutableRule = await getImmutableRule(); const hookAction = await createNewAction(supertest, log); - const newRuleToUpdate = getSimpleRule(immutableRule.rule_id); - const ruleToUpdate = getRuleWithWebHookAction(hookAction.id, false, newRuleToUpdate); - await updateRule(supertest, log, ruleToUpdate); - const body = await findImmutableRuleById( - supertest, - log, - '9a1a2dae-0b5f-4c3d-8305-a268d404c306' + const ruleToUpdate = getRuleWithWebHookAction( + hookAction.id, + immutableRule.enabled, + ruleToUpdateSchema(immutableRule) ); + await updateRule(supertest, log, ruleToUpdate); + const body = await findImmutableRuleById(supertest, log, RULE_ID); expect(body.data.length).to.eql(1); // should have only one length to the data set, otherwise we have duplicates or the tags were removed and that is incredibly bad. const bodyToCompare = removeServerGeneratedProperties(body.data[0]); diff --git a/x-pack/test/detection_engine_api_integration/utils/index.ts b/x-pack/test/detection_engine_api_integration/utils/index.ts index 093be64c26d8..866136f172f1 100644 --- a/x-pack/test/detection_engine_api_integration/utils/index.ts +++ b/x-pack/test/detection_engine_api_integration/utils/index.ts @@ -85,6 +85,7 @@ export * from './remove_server_generated_properties'; export * from './remove_server_generated_properties_including_rule_id'; export * from './resolve_simple_rule_output'; export * from './rule_to_ndjson'; +export * from './rule_to_update_schema'; export * from './set_signal_status'; export * from './start_signals_migration'; export * from './update_rule'; diff --git a/x-pack/test/detection_engine_api_integration/utils/rule_to_update_schema.ts b/x-pack/test/detection_engine_api_integration/utils/rule_to_update_schema.ts new file mode 100644 index 000000000000..32766c88978c --- /dev/null +++ b/x-pack/test/detection_engine_api_integration/utils/rule_to_update_schema.ts @@ -0,0 +1,36 @@ +/* + * 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 type { + FullResponseSchema, + UpdateRulesSchema, +} from '@kbn/security-solution-plugin/common/detection_engine/schemas/request'; +import { omit, pickBy } from 'lodash'; + +const propertiesToRemove = [ + 'id', + 'immutable', + 'updated_at', + 'updated_by', + 'created_at', + 'created_by', + 'related_integrations', + 'required_fields', + 'setup', + 'execution_summary', +]; + +/** + * transforms FullResponseSchema rule to UpdateRulesSchema + * returned result can be used in rule update API calls + */ +export const ruleToUpdateSchema = (rule: FullResponseSchema): UpdateRulesSchema => { + const removedProperties = omit(rule, propertiesToRemove); + + // We're only removing undefined values, so this cast correctly narrows the type + return pickBy(removedProperties, (value) => value !== undefined) as UpdateRulesSchema; +};