[Security Solution][Endpoint][Host Isolation] Host isolation cases view unit test (#104579)

This commit is contained in:
Candace Park 2021-07-12 15:37:16 -04:00 committed by GitHub
parent 8f7df03472
commit 8acdcff840
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 190 additions and 2 deletions

View file

@ -13,7 +13,14 @@ import routeData from 'react-router';
import { getFormMock, useFormMock, useFormDataMock } from '../__mock__/form';
import { useUpdateComment } from '../../containers/use_update_comment';
import { basicCase, basicPush, getUserAction } from '../../containers/mock';
import {
basicCase,
basicPush,
getUserAction,
getHostIsolationUserAction,
hostIsolationComment,
hostReleaseComment,
} from '../../containers/mock';
import { UserActionTree } from '.';
import { TestProviders } from '../../common/mock';
import { Ecs } from '../../../common';
@ -368,4 +375,82 @@ describe(`UserActionTree`, () => {
).toEqual(true);
});
});
describe('Host isolation action', () => {
it('renders in the cases details view', async () => {
const isolateAction = [getHostIsolationUserAction()];
const props = {
...defaultProps,
caseUserActions: isolateAction,
data: { ...defaultProps.data, comments: [...basicCase.comments, hostIsolationComment()] },
};
const wrapper = mount(
<TestProviders>
<UserActionTree {...props} />
</TestProviders>
);
await waitFor(() => {
expect(wrapper.find(`[data-test-subj="endpoint-action"]`).exists()).toBe(true);
});
});
it('shows the correct username', async () => {
const isolateAction = [getHostIsolationUserAction()];
const props = {
...defaultProps,
caseUserActions: isolateAction,
data: { ...defaultProps.data, comments: [hostIsolationComment()] },
};
const wrapper = mount(
<TestProviders>
<UserActionTree {...props} />
</TestProviders>
);
await waitFor(() => {
expect(wrapper.find(`[data-test-subj="user-action-avatar"]`).first().prop('name')).toEqual(
defaultProps.data.createdBy.fullName
);
});
});
it('shows a lock icon if the action is isolate', async () => {
const isolateAction = [getHostIsolationUserAction()];
const props = {
...defaultProps,
caseUserActions: isolateAction,
data: { ...defaultProps.data, comments: [hostIsolationComment()] },
};
const wrapper = mount(
<TestProviders>
<UserActionTree {...props} />
</TestProviders>
);
await waitFor(() => {
expect(
wrapper.find(`[data-test-subj="endpoint-action"]`).first().prop('timelineIcon')
).toBe('lock');
});
});
it('shows a lockOpen icon if the action is unisolate/release', async () => {
const isolateAction = [getHostIsolationUserAction()];
const props = {
...defaultProps,
caseUserActions: isolateAction,
data: { ...defaultProps.data, comments: [hostReleaseComment()] },
};
const wrapper = mount(
<TestProviders>
<UserActionTree {...props} />
</TestProviders>
);
await waitFor(() => {
expect(
wrapper.find(`[data-test-subj="endpoint-action"]`).first().prop('timelineIcon')
).toBe('lockOpen');
});
});
});
});

View file

@ -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 { mount } from 'enzyme';
import { HostIsolationCommentEvent } from './user_action_host_isolation_comment_event';
const defaultProps = () => {
return {
type: 'isolate',
endpoints: [{ endpointId: 'e1', hostname: 'host1' }],
href: jest.fn(),
onClick: jest.fn(),
};
};
describe('UserActionHostIsolationCommentEvent', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('renders with the correct action and hostname', async () => {
const wrapper = mount(<HostIsolationCommentEvent {...defaultProps()} />);
expect(wrapper.find(`[data-test-subj="actions-link-e1"]`).first().exists()).toBeTruthy();
expect(wrapper.text()).toBe('isolated host host1');
});
it('navigates to app on link click', async () => {
const onActionsLinkClick = jest.fn();
const wrapper = mount(
<HostIsolationCommentEvent {...defaultProps()} onClick={onActionsLinkClick} />
);
wrapper.find(`[data-test-subj="actions-link-e1"]`).first().simulate('click');
expect(onActionsLinkClick).toHaveBeenCalled();
});
});

View file

@ -44,7 +44,7 @@ const HostIsolationCommentEventComponent: React.FC<Props> = ({
<LinkAnchor
onClick={onLinkClick}
href={endpointDetailsHref}
data-test-subj={`endpointDetails-activity-log-link-${endpoints[0].endpointId}`}
data-test-subj={`actions-link-${endpoints[0].endpointId}`}
>
{endpoints[0].hostname}
</LinkAnchor>

View file

@ -76,6 +76,58 @@ export const alertComment: Comment = {
version: 'WzQ3LDFc',
};
export const hostIsolationComment: () => Comment = () => {
return {
type: CommentType.actions,
comment: 'I just isolated the host!',
id: 'isolate-comment-id',
actions: {
targets: [
{
hostname: 'host1',
endpointId: '001',
},
],
type: 'isolate',
},
associationType: AssociationType.case,
createdAt: basicCreatedAt,
createdBy: elasticUser,
owner: SECURITY_SOLUTION_OWNER,
pushedAt: null,
pushedBy: null,
updatedAt: null,
updatedBy: null,
version: 'WzQ3LDFc',
};
};
export const hostReleaseComment: () => Comment = () => {
return {
type: CommentType.actions,
comment: 'I just released the host!',
id: 'isolate-comment-id',
actions: {
targets: [
{
hostname: 'host1',
endpointId: '001',
},
],
type: 'unisolate',
},
associationType: AssociationType.case,
createdAt: basicCreatedAt,
createdBy: elasticUser,
owner: SECURITY_SOLUTION_OWNER,
pushedAt: null,
pushedBy: null,
updatedAt: null,
updatedBy: null,
version: 'WzQ3LDFc',
};
};
export const basicCase: Case = {
type: CaseType.individual,
owner: SECURITY_SOLUTION_OWNER,
@ -374,6 +426,15 @@ export const getAlertUserAction = () => ({
newValue: '{"type":"alert","alertId":"alert-id-1","index":"index-id-1"}',
});
export const getHostIsolationUserAction = () => ({
...basicAction,
actionId: 'isolate-action-id',
actionField: ['comment'] as UserActionField,
action: 'create' as UserAction,
commentId: 'isolate-comment-id',
newValue: 'some value',
});
export const caseUserActions: CaseUserActions[] = [
getUserAction(['description'], 'create'),
getUserAction(['comment'], 'create'),