Skip to content

Commit

Permalink
[8.13][Security Solution][Endpoint] Add missing tests for bidirection…
Browse files Browse the repository at this point in the history
…al connector response actions (elastic#176824)

## Summary

Tests for responder action item on alert action menu.

for changes in elastic/pull/176405

### 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

---------

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
2 people authored and fkanout committed Mar 4, 2024
1 parent 78e4b12 commit 9754bf0
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* 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 { useIsExperimentalFeatureEnabled } from '../../../common/hooks/use_experimental_features';
import { useResponderActionData } from './use_responder_action_data';
import { renderHook } from '@testing-library/react-hooks';
import { useGetEndpointDetails } from '../../../management/hooks';
import { HostStatus } from '../../../../common/endpoint/types';

jest.mock('../../../common/hooks/use_experimental_features');
jest.mock('../../../management/hooks', () => ({
useGetEndpointDetails: (jest.fn() as jest.Mock).mockImplementation(() => ({ enabled: false })),
useWithShowResponder: jest.fn(),
}));

const useGetEndpointDetailsMock = useGetEndpointDetails as jest.Mock;
const useIsExperimentalFeatureEnabledMock = useIsExperimentalFeatureEnabled as jest.Mock;

describe('#useResponderActionData', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('should return `responder` menu item as `disabled` if agentType is not `endpoint` and feature flag is enabled', () => {
useIsExperimentalFeatureEnabledMock.mockReturnValue(false);

const { result } = renderHook(() =>
useResponderActionData({
endpointId: 'some-agent-type-id',
// @ts-expect-error this is for testing purpose
agentType: 'some_agent_type',
eventData: [],
})
);
expect(result.current.isDisabled).toEqual(true);
});

describe('when agentType is `endpoint`', () => {
it.each(Object.values(HostStatus).filter((status) => status !== 'unenrolled'))(
'should return `responder` menu item as `enabled `if agentType is `endpoint` when endpoint is %s',
(hostStatus) => {
useGetEndpointDetailsMock.mockReturnValue({
data: {
host_status: hostStatus,
},
isFetching: false,
error: undefined,
});
const { result } = renderHook(() =>
useResponderActionData({
endpointId: 'endpoint-id',
agentType: 'endpoint',
})
);
expect(result.current.isDisabled).toEqual(false);
}
);

it('should return responder menu item `disabled` if agentType is `endpoint` when endpoint is `unenrolled`', () => {
useGetEndpointDetailsMock.mockReturnValue({
data: {
host_status: 'unenrolled',
},
isFetching: false,
error: undefined,
});
const { result } = renderHook(() =>
useResponderActionData({
endpointId: 'endpoint-id',
agentType: 'endpoint',
})
);
expect(result.current.isDisabled).toEqual(true);
});

it('should return responder menu item `disabled` if agentType is `endpoint` when endpoint data has error', () => {
useGetEndpointDetailsMock.mockReturnValue({
data: {
host_status: 'online',
},
isFetching: false,
error: new Error('uh oh!'),
});
const { result } = renderHook(() =>
useResponderActionData({
endpointId: 'endpoint-id',
agentType: 'endpoint',
})
);
expect(result.current.isDisabled).toEqual(true);
});

it('should return responder menu item `disabled` if agentType is `endpoint` and endpoint data is fetching', () => {
useGetEndpointDetailsMock.mockReturnValue({
data: undefined,
isFetching: true,
error: undefined,
});

const { result } = renderHook(() =>
useResponderActionData({
endpointId: 'endpoint-id',
agentType: 'endpoint',
})
);
expect(result.current.isDisabled).toEqual(true);
});
});

describe('when agentType is `sentinel_one`', () => {
it('should return `responder` menu item as `disabled` if agentType is `sentinel_one` and feature flag is disabled', () => {
useIsExperimentalFeatureEnabledMock.mockReturnValue(false);

const { result } = renderHook(() =>
useResponderActionData({
endpointId: 'sentinel-one-id',
agentType: 'sentinel_one',
eventData: [],
})
);
expect(result.current.isDisabled).toEqual(true);
});

it('should return `responder` menu item as `enabled `if agentType is `sentinel_one` and feature flag is enabled', () => {
useIsExperimentalFeatureEnabledMock.mockReturnValue(true);
const { result } = renderHook(() =>
useResponderActionData({
endpointId: 'sentinel-one-id',
agentType: 'sentinel_one',
eventData: [],
})
);
expect(result.current.isDisabled).toEqual(false);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import userEvent from '@testing-library/user-event';
import { act, waitFor } from '@testing-library/react';
import EditConnectorFlyout from '.';
import { ActionConnector, EditConnectorTabs, GenericValidationResult } from '../../../../types';
import { technicalPreviewBadgeProps } from '../beta_badge_props';
import { betaBadgeProps, technicalPreviewBadgeProps } from '../beta_badge_props';
import { AppMockRenderer, createAppMockRenderer } from '../../test_utils';

const updateConnectorResponse = {
Expand Down Expand Up @@ -311,7 +311,7 @@ describe('EditConnectorFlyout', () => {
expect(getByTestId('preconfiguredBadge')).toBeInTheDocument();
});

it('does not show tech preview badge when isExperimental is false', async () => {
it('does not show `tech preview` badge when isExperimental is false', async () => {
const { queryByText } = appMockRenderer.render(
<EditConnectorFlyout
actionTypeRegistry={actionTypeRegistry}
Expand All @@ -324,7 +324,7 @@ describe('EditConnectorFlyout', () => {
expect(queryByText(technicalPreviewBadgeProps.label)).not.toBeInTheDocument();
});

it('shows tech preview badge when isExperimental is true', async () => {
it('shows `tech preview` badge when isExperimental is true', async () => {
actionTypeRegistry.get.mockReturnValue({ ...actionTypeModel, isExperimental: true });
const { getByText } = appMockRenderer.render(
<EditConnectorFlyout
Expand All @@ -337,6 +337,34 @@ describe('EditConnectorFlyout', () => {
await act(() => Promise.resolve());
expect(getByText(technicalPreviewBadgeProps.label)).toBeInTheDocument();
});

it('does not show `beta` badge when `isBeta` is `false`', async () => {
actionTypeRegistry.get.mockReturnValue({ ...actionTypeModel, isBeta: false });
const { queryByText } = appMockRenderer.render(
<EditConnectorFlyout
actionTypeRegistry={actionTypeRegistry}
onClose={onClose}
connector={{ ...connector, isPreconfigured: true }}
onConnectorUpdated={onConnectorUpdated}
/>
);
await act(() => Promise.resolve());
expect(queryByText(betaBadgeProps.label)).not.toBeInTheDocument();
});

it('shows `beta` badge when `isBeta` is `true`', async () => {
actionTypeRegistry.get.mockReturnValue({ ...actionTypeModel, isBeta: true });
const { getByText } = appMockRenderer.render(
<EditConnectorFlyout
actionTypeRegistry={actionTypeRegistry}
onClose={onClose}
connector={{ ...connector, isPreconfigured: true }}
onConnectorUpdated={onConnectorUpdated}
/>
);
await act(() => Promise.resolve());
expect(getByText(betaBadgeProps.label)).toBeInTheDocument();
});
});

describe('Tabs', () => {
Expand Down

0 comments on commit 9754bf0

Please sign in to comment.