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

[Security] [Cases] Manage timeline UI API #67719

Merged
merged 16 commits into from
Jun 3, 2020
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,9 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import React from 'react';
import { mount, ReactWrapper } from 'enzyme';
import { EuiButtonIcon, EuiToolTip } from '@elastic/eui';

import { Filter } from '../../../../../../../src/plugins/data/common/es_query';
import { TimelineAction } from '../../../timelines/components/timeline/body/actions';
import { buildAlertsRuleIdFilter, getAlertActions } from './default_config';
import {
CreateTimeline,
SetEventsDeletedProps,
SetEventsLoadingProps,
UpdateTimelineLoading,
} from './types';
import { mockEcsDataWithAlert } from '../../../common/mock/mock_ecs';
import { sendAlertToTimelineAction, updateAlertStatusAction } from './actions';
import * as i18n from './translations';
import { buildAlertsRuleIdFilter } from './default_config';

jest.mock('./actions');

Expand Down Expand Up @@ -47,162 +34,162 @@ describe('alerts default_config', () => {
expect(filters[0]).toEqual(expectedFilter);
});
});

describe('getAlertActions', () => {
let setEventsLoading: ({ eventIds, isLoading }: SetEventsLoadingProps) => void;
let setEventsDeleted: ({ eventIds, isDeleted }: SetEventsDeletedProps) => void;
let createTimeline: CreateTimeline;
let updateTimelineIsLoading: UpdateTimelineLoading;

let onAlertStatusUpdateSuccess: (count: number, status: string) => void;
let onAlertStatusUpdateFailure: (status: string, error: Error) => void;

beforeEach(() => {
setEventsLoading = jest.fn();
setEventsDeleted = jest.fn();
createTimeline = jest.fn();
updateTimelineIsLoading = jest.fn();
onAlertStatusUpdateSuccess = jest.fn();
onAlertStatusUpdateFailure = jest.fn();
});

describe('timeline tooltip', () => {
test('it invokes sendAlertToTimelineAction when button clicked', () => {
const alertsActions = getAlertActions({
canUserCRUD: true,
hasIndexWrite: true,
setEventsLoading,
setEventsDeleted,
createTimeline,
status: 'open',
updateTimelineIsLoading,
onAlertStatusUpdateSuccess,
onAlertStatusUpdateFailure,
});
const timelineAction = alertsActions[0].getAction({
eventId: 'even-id',
ecsData: mockEcsDataWithAlert,
});
const wrapper = mount<React.ReactElement>(timelineAction as React.ReactElement);
wrapper.find(EuiButtonIcon).simulate('click');

expect(sendAlertToTimelineAction).toHaveBeenCalled();
});
});

describe('alert open action', () => {
let alertsActions: TimelineAction[];
let alertOpenAction: JSX.Element;
let wrapper: ReactWrapper<React.ReactElement, unknown>;

beforeEach(() => {
alertsActions = getAlertActions({
canUserCRUD: true,
hasIndexWrite: true,
setEventsLoading,
setEventsDeleted,
createTimeline,
status: 'open',
updateTimelineIsLoading,
onAlertStatusUpdateSuccess,
onAlertStatusUpdateFailure,
});

alertOpenAction = alertsActions[1].getAction({
eventId: 'event-id',
ecsData: mockEcsDataWithAlert,
});

wrapper = mount<React.ReactElement>(alertOpenAction as React.ReactElement);
});

afterEach(() => {
wrapper.unmount();
});

test('it invokes updateAlertStatusAction when button clicked', () => {
wrapper.find(EuiButtonIcon).simulate('click');

expect(updateAlertStatusAction).toHaveBeenCalledWith({
alertIds: ['event-id'],
status: 'open',
setEventsLoading,
setEventsDeleted,
onAlertStatusUpdateSuccess,
onAlertStatusUpdateFailure,
});
});

test('it displays expected text on hover', () => {
const openAlert = wrapper.find(EuiToolTip);
openAlert.simulate('mouseOver');
const tooltip = wrapper.find('.euiToolTipPopover').text();

expect(tooltip).toEqual(i18n.ACTION_OPEN_ALERT);
});

test('it displays expected icon', () => {
const icon = wrapper.find(EuiButtonIcon).props().iconType;

expect(icon).toEqual('securityAlertDetected');
});
});

describe('alert close action', () => {
let alertsActions: TimelineAction[];
let alertCloseAction: JSX.Element;
let wrapper: ReactWrapper<React.ReactElement, unknown>;

beforeEach(() => {
alertsActions = getAlertActions({
canUserCRUD: true,
hasIndexWrite: true,
setEventsLoading,
setEventsDeleted,
createTimeline,
status: 'closed',
updateTimelineIsLoading,
onAlertStatusUpdateSuccess,
onAlertStatusUpdateFailure,
});

alertCloseAction = alertsActions[1].getAction({
eventId: 'event-id',
ecsData: mockEcsDataWithAlert,
});

wrapper = mount<React.ReactElement>(alertCloseAction as React.ReactElement);
});

afterEach(() => {
wrapper.unmount();
});

test('it invokes updateAlertStatusAction when status button clicked', () => {
wrapper.find(EuiButtonIcon).simulate('click');

expect(updateAlertStatusAction).toHaveBeenCalledWith({
alertIds: ['event-id'],
status: 'closed',
setEventsLoading,
setEventsDeleted,
onAlertStatusUpdateSuccess,
onAlertStatusUpdateFailure,
});
});

test('it displays expected text on hover', () => {
const closeAlert = wrapper.find(EuiToolTip);
closeAlert.simulate('mouseOver');
const tooltip = wrapper.find('.euiToolTipPopover').text();
expect(tooltip).toEqual(i18n.ACTION_CLOSE_ALERT);
});

test('it displays expected icon', () => {
const icon = wrapper.find(EuiButtonIcon).props().iconType;

expect(icon).toEqual('securityAlertResolved');
});
});
});
// TODO: move these tests to ../timelines/components/timeline/body/events/event_column_view.tsx
// describe.skip('getAlertActions', () => {
// let setEventsLoading: ({ eventIds, isLoading }: SetEventsLoadingProps) => void;
// let setEventsDeleted: ({ eventIds, isDeleted }: SetEventsDeletedProps) => void;
// let createTimeline: CreateTimeline;
// let updateTimelineIsLoading: UpdateTimelineLoading;
//
// let onAlertStatusUpdateSuccess: (count: number, status: string) => void;
// let onAlertStatusUpdateFailure: (status: string, error: Error) => void;
//
// beforeEach(() => {
// setEventsLoading = jest.fn();
// setEventsDeleted = jest.fn();
// createTimeline = jest.fn();
// updateTimelineIsLoading = jest.fn();
// onAlertStatusUpdateSuccess = jest.fn();
// onAlertStatusUpdateFailure = jest.fn();
// });
//
// describe('timeline tooltip', () => {
// test('it invokes sendAlertToTimelineAction when button clicked', () => {
// const alertsActions = getAlertActions({
// canUserCRUD: true,
// hasIndexWrite: true,
// setEventsLoading,
// setEventsDeleted,
// createTimeline,
// status: 'open',
// updateTimelineIsLoading,
// onAlertStatusUpdateSuccess,
// onAlertStatusUpdateFailure,
// });
// const timelineAction = alertsActions[0].getAction({
// eventId: 'even-id',
// ecsData: mockEcsDataWithAlert,
// });
// const wrapper = mount<React.ReactElement>(timelineAction as React.ReactElement);
// wrapper.find(EuiButtonIcon).simulate('click');
//
// expect(sendAlertToTimelineAction).toHaveBeenCalled();
// });
// });
//
// describe('alert open action', () => {
// let alertsActions: TimelineAction[];
// let alertOpenAction: JSX.Element;
// let wrapper: ReactWrapper<React.ReactElement, unknown>;
//
// beforeEach(() => {
// alertsActions = getAlertActions({
// canUserCRUD: true,
// hasIndexWrite: true,
// setEventsLoading,
// setEventsDeleted,
// createTimeline,
// status: 'open',
// updateTimelineIsLoading,
// onAlertStatusUpdateSuccess,
// onAlertStatusUpdateFailure,
// });
//
// alertOpenAction = alertsActions[1].getAction({
// eventId: 'event-id',
// ecsData: mockEcsDataWithAlert,
// });
//
// wrapper = mount<React.ReactElement>(alertOpenAction as React.ReactElement);
// });
//
// afterEach(() => {
// wrapper.unmount();
// });
//
// test('it invokes updateAlertStatusAction when button clicked', () => {
// wrapper.find(EuiButtonIcon).simulate('click');
//
// expect(updateAlertStatusAction).toHaveBeenCalledWith({
// alertIds: ['event-id'],
// status: 'open',
// setEventsLoading,
// setEventsDeleted,
// onAlertStatusUpdateSuccess,
// onAlertStatusUpdateFailure,
// });
// });
//
// test('it displays expected text on hover', () => {
// const openAlert = wrapper.find(EuiToolTip);
// openAlert.simulate('mouseOver');
// const tooltip = wrapper.find('.euiToolTipPopover').text();
//
// expect(tooltip).toEqual(i18n.ACTION_OPEN_ALERT);
// });
//
// test('it displays expected icon', () => {
// const icon = wrapper.find(EuiButtonIcon).props().iconType;
//
// expect(icon).toEqual('securityAlertDetected');
// });
// });
//
// describe('alert close action', () => {
// let alertsActions: TimelineAction[];
// let alertCloseAction: JSX.Element;
// let wrapper: ReactWrapper<React.ReactElement, unknown>;
//
// beforeEach(() => {
// alertsActions = getAlertActions({
// canUserCRUD: true,
// hasIndexWrite: true,
// setEventsLoading,
// setEventsDeleted,
// createTimeline,
// status: 'closed',
// updateTimelineIsLoading,
// onAlertStatusUpdateSuccess,
// onAlertStatusUpdateFailure,
// });
//
// alertCloseAction = alertsActions[1].getAction({
// eventId: 'event-id',
// ecsData: mockEcsDataWithAlert,
// });
//
// wrapper = mount<React.ReactElement>(alertCloseAction as React.ReactElement);
// });
//
// afterEach(() => {
// wrapper.unmount();
// });
//
// test('it invokes updateAlertStatusAction when status button clicked', () => {
// wrapper.find(EuiButtonIcon).simulate('click');
//
// expect(updateAlertStatusAction).toHaveBeenCalledWith({
// alertIds: ['event-id'],
// status: 'closed',
// setEventsLoading,
// setEventsDeleted,
// onAlertStatusUpdateSuccess,
// onAlertStatusUpdateFailure,
// });
// });
//
// test('it displays expected text on hover', () => {
// const closeAlert = wrapper.find(EuiToolTip);
// closeAlert.simulate('mouseOver');
// const tooltip = wrapper.find('.euiToolTipPopover').text();
// expect(tooltip).toEqual(i18n.ACTION_CLOSE_ALERT);
// });
//
// test('it displays expected icon', () => {
// const icon = wrapper.find(EuiButtonIcon).props().iconType;
//
// expect(icon).toEqual('securityAlertResolved');
// });
// });
// });
});
Loading