Skip to content

Commit

Permalink
Tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
adcoelho committed Oct 10, 2024
1 parent 758185c commit 0c71513
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { createStartServicesMock } from '../../../common/lib/kibana/kibana_react
import { useGetAllCaseConfigurations } from '../../../containers/configure/use_get_all_case_configurations';
import { useGetAllCaseConfigurationsResponse } from '../../configure_cases/__mock__';
import { templatesConfigurationMock } from '../../../containers/mock';
import * as utils from '../../../containers/configure/utils';

jest.mock('@kbn/alerts-ui-shared/src/common/hooks/use_alerts_data_view');
jest.mock('../../../common/lib/kibana/use_application');
Expand All @@ -29,10 +30,6 @@ const useAlertsDataViewMock = jest.mocked(useAlertsDataView);
const useApplicationMock = useApplication as jest.Mock;
const useGetAllCaseConfigurationsMock = useGetAllCaseConfigurations as jest.Mock;

useKibanaMock.mockReturnValue({
services: { ...createStartServicesMock(), data: { dataViews: {} } },
} as unknown as ReturnType<typeof useKibana>);

const actionParams = {
subAction: 'run',
subActionParams: {
Expand Down Expand Up @@ -98,6 +95,9 @@ describe('CasesParamsFields renders', () => {
},
});
useGetAllCaseConfigurationsMock.mockImplementation(() => useGetAllCaseConfigurationsResponse);
useKibanaMock.mockReturnValue({
services: { ...createStartServicesMock(), data: { dataViews: {} } },
} as unknown as ReturnType<typeof useKibana>);
});

afterEach(() => {
Expand Down Expand Up @@ -268,6 +268,54 @@ describe('CasesParamsFields renders', () => {
expect(await screen.findByText(templatesConfigurationMock[1].name)).toBeInTheDocument();
});

it('renders security templates if the project is serverless security', async () => {
useKibanaMock.mockReturnValue({
services: {
...createStartServicesMock(),
// simulate a serverless security project
cloud: { isServerlessEnabled: true, serverless: { projectType: 'security' } },
data: { dataViews: {} },
},
} as unknown as ReturnType<typeof useKibana>);

const configuration = {
...useGetAllCaseConfigurationsResponse.data[0],
templates: templatesConfigurationMock,
};
useGetAllCaseConfigurationsMock.mockImplementation(() => ({
...useGetAllCaseConfigurationsResponse,
data: [configuration],
}));
const getConfigurationByOwnerSpy = jest
.spyOn(utils, 'getConfigurationByOwner')
.mockImplementation(() => configuration);

const observabilityOwnedRule = {
...defaultProps,
// these two would normally produce an observability owner
producerId: 'observability',
featureId: 'observability',
actionParams: {
subAction: 'run',
subActionParams: {
...actionParams.subActionParams,
templateId: templatesConfigurationMock[1].key,
},
},
};

render(<CasesParamsFields {...observabilityOwnedRule} />);

expect(getConfigurationByOwnerSpy).toHaveBeenCalledWith(
expect.objectContaining({
// the security owner was forced
owner: 'securitySolution',
})
);

getConfigurationByOwnerSpy.mockRestore();
});

it('updates template correctly', async () => {
useGetAllCaseConfigurationsMock.mockReturnValueOnce({
...useGetAllCaseConfigurationsResponse,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ export const CasesParamsFieldsComponent: React.FunctionComponent<
const owner = getOwnerFromRuleConsumerProducer({
consumer: featureId,
producer: producerId,
// This is a workaround for a very specific bug with the cases action in serverless security
// More info here: https://github.com/elastic/kibana/issues/195599
isServerlessSecurity:
cloud?.isServerlessEnabled && cloud?.serverless.projectType === 'security',
});
Expand Down

0 comments on commit 0c71513

Please sign in to comment.