Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SecuritySolution] Retrieve onboarding steps #181442

Merged
merged 43 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
6d4b37e
Retrieve onboarding steps
angorayc Apr 23, 2024
7366052
compatible with legacy flyout
angorayc Apr 23, 2024
f2afbaa
Merge branch 'main' of github.com:elastic/kibana into issues/181190
angorayc Apr 30, 2024
6e15bde
hide onboarding steps when cases opened
angorayc May 1, 2024
91e636a
remove debugger
angorayc May 1, 2024
5675be7
hide all tours when irrelevant flyout open
angorayc May 2, 2024
2d503bf
open correlation tab after case submission
angorayc May 3, 2024
818ffa2
fix types
angorayc May 3, 2024
da45950
Merge branch 'main' of github.com:elastic/kibana into issues/181190
angorayc May 7, 2024
2b1ad24
unit tests
angorayc May 7, 2024
31d997b
unit tests
angorayc May 7, 2024
c6c8a8d
fix unit tests
angorayc May 7, 2024
e0b59eb
unit tests
angorayc May 7, 2024
3162e3e
Merge branch 'main' into issues/181190
angorayc May 7, 2024
d910941
use state observable
semd May 10, 2024
205ab83
cleaning
semd May 10, 2024
b74b20e
set behavior subject default value
semd May 10, 2024
30a5fa3
get context state util
semd May 10, 2024
2ed8fdb
unit tests
angorayc May 10, 2024
6ef8cae
fix UI error
angorayc May 10, 2024
c75c144
Merge branch 'main' into issues/181190
angorayc May 10, 2024
2c5063c
unit tests
angorayc May 13, 2024
7b91f9e
Merge branch 'main' into issues/181190
angorayc May 13, 2024
4a9b90c
unit tests
angorayc May 13, 2024
edfc5d3
code review
angorayc May 14, 2024
3273700
unit tests
angorayc May 14, 2024
a6cd062
unit tests
angorayc May 15, 2024
7a40a60
Merge branch 'main' into issues/181190
angorayc May 15, 2024
0c2b7ba
unit tests
angorayc May 15, 2024
caa3121
Merge branch 'main' into issues/181190
angorayc May 16, 2024
f7b2411
Merge branch 'main' into issues/181190
angorayc May 20, 2024
05784f5
memo SecurityTourStep
semd May 21, 2024
2933d68
Merge remote-tracking branch 'upstream/main' into issues/181190
semd May 21, 2024
2534c07
prevent cells without anchor to update
semd May 21, 2024
690f59b
type failure
angorayc May 21, 2024
39e1468
Merge branch 'main' into issues/181190
angorayc May 22, 2024
96f5924
use separate cases state context
semd May 24, 2024
e3433d9
remove type not used
semd May 24, 2024
a5e9085
clean tests
semd May 24, 2024
5337e47
improve test
semd May 24, 2024
c80bdc8
Merge remote-tracking branch 'upstream/main' into issues/181190
semd May 24, 2024
1e6f738
Merge branch 'main' into issues/181190
kibanamachine May 24, 2024
fc02346
Merge branch 'main' into issues/181190
kibanamachine May 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { useCasesToast } from '../../../common/use_cases_toast';
import { alertComment } from '../../../containers/mock';
import { useCreateAttachments } from '../../../containers/use_create_attachments';
import { CasesContext } from '../../cases_context';
import { CasesContextStoreActionsList } from '../../cases_context/cases_context_reducer';
import { CasesContextStoreActionsList } from '../../cases_context/state/cases_context_reducer';
import { ExternalReferenceAttachmentTypeRegistry } from '../../../client/attachment_framework/external_reference_registry';
import type { AddToExistingCaseModalProps } from './use_cases_add_to_existing_case_modal';
import { useCasesAddToExistingCaseModal } from './use_cases_add_to_existing_case_modal';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { CaseStatuses } from '../../../../common/types/domain';
import type { AllCasesSelectorModalProps } from '.';
import { useCasesToast } from '../../../common/use_cases_toast';
import type { CaseUI } from '../../../containers/types';
import { CasesContextStoreActionsList } from '../../cases_context/cases_context_reducer';
import { CasesContextStoreActionsList } from '../../cases_context/state/cases_context_reducer';
import { useCasesContext } from '../../cases_context/use_cases_context';
import { useCasesAddToNewCaseFlyout } from '../../create/flyout/use_cases_add_to_new_case_flyout';
import type { CaseAttachmentsWithoutOwner } from '../../../types';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { getAllCasesSelectorModalNoProviderLazy } from '../../client/ui/get_all_
import { getCreateCaseFlyoutLazyNoProvider } from '../../client/ui/get_create_case_flyout';
import type { AppMockRenderer } from '../../common/mock';
import { createAppMockRenderer } from '../../common/mock';
import { getInitialCasesContextState } from './cases_context_reducer';
import { getInitialCasesContextState } from './state/cases_context_reducer';
import { CasesGlobalComponents } from './cases_global_components';

jest.mock('../../client/ui/get_create_case_flyout');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import React from 'react';
import { getAllCasesSelectorModalNoProviderLazy } from '../../client/ui/get_all_cases_selector_modal';
import { getCreateCaseFlyoutLazyNoProvider } from '../../client/ui/get_create_case_flyout';
import type { CasesContextState } from './cases_context_reducer';
import type { CasesContextState } from './state/cases_context_reducer';

export const CasesGlobalComponents = React.memo(({ state }: { state: CasesContextState }) => {
return (
Expand Down
23 changes: 13 additions & 10 deletions x-pack/plugins/cases/public/components/cases_context/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { FilesContext } from '@kbn/shared-ux-file-context';

import type { QueryClient } from '@tanstack/react-query';
import { QueryClientProvider } from '@tanstack/react-query';
import type { CasesContextStoreAction } from './cases_context_reducer';
import type {
CasesFeaturesAllRequired,
CasesFeatures,
Expand All @@ -29,7 +28,9 @@ import { CasesGlobalComponents } from './cases_global_components';
import { DEFAULT_FEATURES } from '../../../common/constants';
import { constructFileKindIdByOwner } from '../../../common/files';
import { DEFAULT_BASE_PATH } from '../../common/navigation';
import { casesContextReducer, getInitialCasesContextState } from './cases_context_reducer';
import type { CasesContextStoreAction } from './state/cases_context_reducer';
import { casesContextReducer, getInitialCasesContextState } from './state/cases_context_reducer';
import { CasesStateContext } from './state/cases_state_context';
import { isRegisteredOwner } from '../../files';
import { casesQueryClient } from './query_client';

Expand Down Expand Up @@ -152,14 +153,16 @@ export const CasesProvider: FC<

return (
<QueryClientProvider client={queryClient}>
<CasesContext.Provider value={value}>
{applyFilesContext(
<>
<CasesGlobalComponents state={state} />
{children}
</>
)}
</CasesContext.Provider>
<CasesStateContext.Provider value={state}>
<CasesContext.Provider value={value}>
{applyFilesContext(
<>
<CasesGlobalComponents state={state} />
{children}
</>
)}
</CasesContext.Provider>
</CasesStateContext.Provider>
</QueryClientProvider>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/

import { assertNever } from '@kbn/std';
import type { AllCasesSelectorModalProps } from '../all_cases/selector_modal';
import type { CreateCaseFlyoutProps } from '../create/flyout';
import type { AllCasesSelectorModalProps } from '../../all_cases/selector_modal';
import type { CreateCaseFlyoutProps } from '../../create/flyout';

export const getInitialCasesContextState = (): CasesContextState => {
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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, { useContext } from 'react';
import type { CasesContextState } from './cases_context_reducer';

export const CasesStateContext = React.createContext<CasesContextState | undefined>(undefined);

export const useCasesStateContext = () => {
const casesStateContext = useContext(CasesStateContext);
if (!casesStateContext) {
throw new Error(
'useCasesStateContext must be used within a CasesProvider and have a defined value. See https://github.com/elastic/kibana/blob/main/x-pack/plugins/cases/README.md#cases-ui'
);
}
return casesStateContext;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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 { act, renderHook } from '@testing-library/react-hooks';
import { useCasesAddToExistingCaseModal } from '../../all_cases/selector_modal/use_cases_add_to_existing_case_modal';
import { createAppMockRenderer } from '../../../common/mock';
import { useIsAddToCaseOpen } from './use_is_add_to_case_open';
import { useCasesToast } from '../../../common/use_cases_toast';
import { useCasesAddToNewCaseFlyout } from '../../create/flyout/use_cases_add_to_new_case_flyout';

jest.mock('../../../common/use_cases_toast');
const useCasesToastMock = useCasesToast as jest.Mock;
useCasesToastMock.mockReturnValue({
showInfoToast: jest.fn(),
});

const { AppWrapper } = createAppMockRenderer();

describe('use is add to existing case modal open hook', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('should throw if called outside of a cases context', () => {
const { result } = renderHook(useIsAddToCaseOpen);
expect(result.error?.message).toContain(
'useCasesStateContext must be used within a CasesProvider and have a defined value'
);
});

it('should return false when the add to case modal and flyout are not open', async () => {
const { result } = renderHook(useIsAddToCaseOpen, { wrapper: AppWrapper });
expect(result.current).toEqual(false);
});

it('should return true when the add to existing case modal opens', async () => {
const { result, rerender } = renderHook(
() => {
return {
modal: useCasesAddToExistingCaseModal(),
isOpen: useIsAddToCaseOpen(),
};
},
{ wrapper: AppWrapper }
);

expect(result.current.isOpen).toEqual(false);
act(() => {
result.current.modal.open();
});
rerender();
expect(result.current.isOpen).toEqual(true);
});

it('should return true when the add to new case flyout opens', async () => {
const { result, rerender } = renderHook(
() => {
return {
flyout: useCasesAddToNewCaseFlyout(),
isOpen: useIsAddToCaseOpen(),
};
},
{ wrapper: AppWrapper }
);

expect(result.current.isOpen).toEqual(false);
act(() => {
result.current.flyout.open();
});
rerender();
expect(result.current.isOpen).toEqual(true);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* 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 { useCasesStateContext } from './cases_state_context';

export type UseIsAddToCaseOpen = () => boolean;

/**
* This hook is to check if the "add to case" is open, either the modal or the flyout
*/
export const useIsAddToCaseOpen: UseIsAddToCaseOpen = () => {
angorayc marked this conversation as resolved.
Show resolved Hide resolved
const { selectCaseModal, createCaseFlyout } = useCasesStateContext();
return selectCaseModal.isModalOpen || createCaseFlyout.isFlyoutOpen;
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { renderHook } from '@testing-library/react-hooks';
import type { FC, PropsWithChildren } from 'react';
import React from 'react';
import { CasesContext } from '../../cases_context';
import { CasesContextStoreActionsList } from '../../cases_context/cases_context_reducer';
import { CasesContextStoreActionsList } from '../../cases_context/state/cases_context_reducer';
import { useCasesAddToNewCaseFlyout } from './use_cases_add_to_new_case_flyout';
import { allCasesPermissions } from '../../../common/mock';
import { ExternalReferenceAttachmentTypeRegistry } from '../../../client/attachment_framework/external_reference_registry';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useCallback, useMemo } from 'react';
import type { CaseAttachmentsWithoutOwner } from '../../../types';
import { useCasesToast } from '../../../common/use_cases_toast';
import type { CaseUI } from '../../../containers/types';
import { CasesContextStoreActionsList } from '../../cases_context/cases_context_reducer';
import { CasesContextStoreActionsList } from '../../cases_context/state/cases_context_reducer';
import { useCasesContext } from '../../cases_context/use_cases_context';
import type { CreateCaseFlyoutProps } from './create_case_flyout';

Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/cases/public/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const uiMock: jest.Mocked<CasesPublicStart['ui']> = {

export const openAddToExistingCaseModalMock = jest.fn();
export const openAddToNewCaseFlyoutMock = jest.fn();
export const isAddToCaseOpenMock = jest.fn();

const hooksMock: jest.Mocked<CasesPublicStart['hooks']> = {
useCasesAddToNewCaseFlyout: jest.fn().mockImplementation(() => ({
Expand All @@ -35,6 +36,7 @@ const hooksMock: jest.Mocked<CasesPublicStart['hooks']> = {
useCasesAddToExistingCaseModal: jest.fn().mockImplementation(() => ({
open: openAddToExistingCaseModalMock,
})),
useIsAddToCaseOpen: isAddToCaseOpenMock,
};

const helpersMock: jest.Mocked<CasesPublicStart['helpers']> = {
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/cases/public/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ describe('Cases Ui Plugin', () => {
hooks: {
useCasesAddToExistingCaseModal: expect.any(Function),
useCasesAddToNewCaseFlyout: expect.any(Function),
useIsAddToCaseOpen: expect.any(Function),
},
ui: {
getAllCasesSelectorModal: expect.any(Function),
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/cases/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { APP_ID, APP_PATH } from '../common/constants';
import { APP_TITLE, APP_DESC } from './common/translations';
import { useCasesAddToExistingCaseModal } from './components/all_cases/selector_modal/use_cases_add_to_existing_case_modal';
import { useCasesAddToNewCaseFlyout } from './components/create/flyout/use_cases_add_to_new_case_flyout';
import { useIsAddToCaseOpen } from './components/cases_context/state/use_is_add_to_case_open';
import { createClientAPI } from './client/api';
import { canUseCases } from './client/helpers/can_use_cases';
import { getRuleIdFromEvent } from './client/helpers/get_rule_id_from_event';
Expand Down Expand Up @@ -190,6 +191,7 @@ export class CasesUiPlugin
hooks: {
useCasesAddToNewCaseFlyout,
useCasesAddToExistingCaseModal,
useIsAddToCaseOpen,
},
helpers: {
canUseCases: canUseCases(core.application.capabilities),
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/cases/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import type { ServerlessPluginSetup, ServerlessPluginStart } from '@kbn/serverle

import type { UseCasesAddToExistingCaseModal } from './components/all_cases/selector_modal/use_cases_add_to_existing_case_modal';
import type { UseCasesAddToNewCaseFlyout } from './components/create/flyout/use_cases_add_to_new_case_flyout';
import type { UseIsAddToCaseOpen } from './components/cases_context/state/use_is_add_to_case_open';
import type { canUseCases } from './client/helpers/can_use_cases';
import type { getRuleIdFromEvent } from './client/helpers/get_rule_id_from_event';
import type { GetCasesContextProps } from './client/ui/get_cases_context';
Expand Down Expand Up @@ -154,6 +155,7 @@ export interface CasesPublicStart {
hooks: {
useCasesAddToNewCaseFlyout: UseCasesAddToNewCaseFlyout;
useCasesAddToExistingCaseModal: UseCasesAddToExistingCaseModal;
useIsAddToCaseOpen: UseIsAddToCaseOpen;
};
helpers: {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ jest.mock('../../../utils/route/use_route_spy');
jest.mock('@kbn/expandable-flyout', () => {
return {
useExpandableFlyoutApi: () => ({ openFlyout: mockOpenFlyout }),
useExpandableFlyoutState: () => ({ left: false }),
};
});

Expand All @@ -55,6 +56,7 @@ jest.mock('@kbn/kibana-react-plugin/public', () => {
...original,
};
});
jest.mock('../../guided_onboarding_tour/tour_step');

const mockRouteSpy: RouteSpyState = {
pageName: SecurityPageName.overview,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import type { TimelineItem, TimelineNonEcsData } from '../../../../../common/sea
import type { ColumnHeaderOptions, OnRowSelected } from '../../../../../common/types/timeline';
import { TimelineId } from '../../../../../common/types';
import { useIsExperimentalFeatureEnabled } from '../../../hooks/use_experimental_features';
import { useTourContext } from '../../guided_onboarding_tour';
import { AlertsCasesTourSteps, SecurityStepId } from '../../guided_onboarding_tour/tour_config';

type Props = EuiDataGridCellValueElementProps & {
columnHeaders: ColumnHeaderOptions[];
Expand Down Expand Up @@ -83,6 +85,11 @@ const RowActionComponent = ({
const isExpandableFlyoutInCreateRuleEnabled = useIsExperimentalFeatureEnabled(
'expandableFlyoutInCreateRuleEnabled'
);
const { activeStep, isTourShown } = useTourContext();
const shouldFocusOnOverviewTab =
(activeStep === AlertsCasesTourSteps.expandEvent ||
activeStep === AlertsCasesTourSteps.reviewAlertDetailsFlyout) &&
isTourShown(SecurityStepId.alertsCases);

const columnValues = useMemo(
() =>
Expand Down Expand Up @@ -123,6 +130,7 @@ const RowActionComponent = ({
openFlyout({
right: {
id: DocumentDetailsRightPanelKey,
path: shouldFocusOnOverviewTab ? { tab: 'overview' } : undefined,
params: {
id: eventId,
indexName,
Expand Down Expand Up @@ -156,7 +164,17 @@ const RowActionComponent = ({
})
);
}
}, [dispatch, eventId, indexName, openFlyout, tabType, tableId, showExpandableFlyout, telemetry]);
}, [
eventId,
indexName,
showExpandableFlyout,
tableId,
openFlyout,
shouldFocusOnOverviewTab,
telemetry,
dispatch,
tabType,
]);

const Action = controlColumn.rowCellRender;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ jest.mock('../../../detection_engine/rule_management/logic/use_rule_with_fallbac
};
});

jest.mock('../guided_onboarding_tour/tour_step', () => ({
GuidedOnboardingTourStep: jest.fn(({ children }) => (
<div data-test-subj="guided-onboarding">{children}</div>
)),
}));

jest.mock('../link_to');
describe('EventDetails', () => {
const defaultProps = {
Expand Down Expand Up @@ -168,6 +174,10 @@ describe('EventDetails', () => {
EVENT_DETAILS_CONTEXT_ID
);
});

test('renders GuidedOnboardingTourStep', () => {
expect(alertsWrapper.find('[data-test-subj="guided-onboarding"]').exists()).toEqual(true);
});
});

describe('threat intel tab', () => {
Expand Down
Loading