Skip to content

Commit

Permalink
[SIEM][Detection Engine] Fixes export of single rule and the icons
Browse files Browse the repository at this point in the history
## Summary

Fixes export of single rule and the icons.
* #62378
* Single export of rules was using the `rule.id` instead of the `rule.rule_id` where now it flips it and works as expected.
* This adds data-test-subj for testing
* This adds jest unit tests to the menu component

Icons Before:
<img width="396" alt="Screen Shot 2020-04-02 at 5 12 43 PM" src="https://user-images.githubusercontent.com/1151048/78315482-5b533280-751a-11ea-8378-d5e106ebd36f.png">

Icons After:
<img width="407" alt="Screen Shot 2020-04-02 at 7 40 28 PM" src="https://user-images.githubusercontent.com/1151048/78315449-3fe82780-751a-11ea-9d16-2f8c2ea22a78.png">

### Checklist

- [x] [Unit or functional tests](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#cross-browser-compatibility) were updated or added to match the most common scenarios
  • Loading branch information
FrankHassanabad authored Apr 3, 2020
1 parent 4cbf6d2 commit 85c665a
Show file tree
Hide file tree
Showing 3 changed files with 295 additions and 13 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { shallow } from 'enzyme';
import { shallow, mount } from 'enzyme';
import React from 'react';

import { deleteRulesAction, duplicateRulesAction } from '../../all/actions';
import { RuleActionsOverflow } from './index';
import { mockRule } from '../../all/__mocks__/mock';

Expand All @@ -16,11 +17,279 @@ jest.mock('react-router-dom', () => ({
}),
}));

jest.mock('../../all/actions', () => ({
deleteRulesAction: jest.fn(),
duplicateRulesAction: jest.fn(),
}));

describe('RuleActionsOverflow', () => {
test('renders correctly against snapshot', () => {
const wrapper = shallow(
<RuleActionsOverflow rule={mockRule('id')} userHasNoPermissions={false} />
);
expect(wrapper).toMatchSnapshot();
describe('snapshots', () => {
test('renders correctly against snapshot', () => {
const wrapper = shallow(
<RuleActionsOverflow rule={mockRule('id')} userHasNoPermissions={false} />
);
expect(wrapper).toMatchSnapshot();
});
});

describe('rules details menu panel', () => {
test('there is at least one item when there is a rule within the rules-details-menu-panel', () => {
const wrapper = mount(
<RuleActionsOverflow rule={mockRule('id')} userHasNoPermissions={false} />
);
wrapper.find('[data-test-subj="rules-details-popover-button-icon"] button').simulate('click');
wrapper.update();
const items: unknown[] = wrapper
.find('[data-test-subj="rules-details-menu-panel"]')
.first()
.prop('items');

expect(items.length).toBeGreaterThan(0);
});

test('items are empty when there is a null rule within the rules-details-menu-panel', () => {
const wrapper = mount(<RuleActionsOverflow rule={null} userHasNoPermissions={false} />);
wrapper.find('[data-test-subj="rules-details-popover-button-icon"] button').simulate('click');
wrapper.update();
expect(
wrapper
.find('[data-test-subj="rules-details-menu-panel"]')
.first()
.prop('items')
).toEqual([]);
});

test('items are empty when there is an undefined rule within the rules-details-menu-panel', () => {
const wrapper = mount(<RuleActionsOverflow rule={null} userHasNoPermissions={false} />);
wrapper.find('[data-test-subj="rules-details-popover-button-icon"] button').simulate('click');
wrapper.update();
expect(
wrapper
.find('[data-test-subj="rules-details-menu-panel"]')
.first()
.prop('items')
).toEqual([]);
});

test('it opens the popover when rules-details-popover-button-icon is clicked', () => {
const wrapper = mount(
<RuleActionsOverflow rule={mockRule('id')} userHasNoPermissions={false} />
);
wrapper.find('[data-test-subj="rules-details-popover-button-icon"] button').simulate('click');
wrapper.update();
expect(
wrapper
.find('[data-test-subj="rules-details-popover"]')
.first()
.prop('isOpen')
).toEqual(true);
});
});

describe('rules details pop over button icon', () => {
test('it does not open the popover when rules-details-popover-button-icon is clicked when the user does not have permission', () => {
const wrapper = mount(
<RuleActionsOverflow rule={mockRule('id')} userHasNoPermissions={true} />
);
wrapper.find('[data-test-subj="rules-details-popover-button-icon"] button').simulate('click');
wrapper.update();
expect(
wrapper
.find('[data-test-subj="rules-details-popover"]')
.first()
.prop('isOpen')
).toEqual(false);
});
});

describe('rules details duplicate rule', () => {
test('it does not open the popover when rules-details-popover-button-icon is clicked and the user does not have permission', () => {
const rule = mockRule('id');
const wrapper = mount(<RuleActionsOverflow rule={rule} userHasNoPermissions={true} />);
wrapper.find('[data-test-subj="rules-details-popover-button-icon"] button').simulate('click');
wrapper.update();
expect(wrapper.find('[data-test-subj="rules-details-delete-rule"] button').exists()).toEqual(
false
);
});

test('it opens the popover when rules-details-popover-button-icon is clicked', () => {
const wrapper = mount(
<RuleActionsOverflow rule={mockRule('id')} userHasNoPermissions={false} />
);
wrapper.find('[data-test-subj="rules-details-popover-button-icon"] button').simulate('click');
wrapper.update();
expect(
wrapper
.find('[data-test-subj="rules-details-popover"]')
.first()
.prop('isOpen')
).toEqual(true);
});

test('it closes the popover when rules-details-duplicate-rule is clicked', () => {
const wrapper = mount(
<RuleActionsOverflow rule={mockRule('id')} userHasNoPermissions={false} />
);
wrapper.find('[data-test-subj="rules-details-popover-button-icon"] button').simulate('click');
wrapper.update();
wrapper.find('[data-test-subj="rules-details-duplicate-rule"] button').simulate('click');
wrapper.update();
expect(
wrapper
.find('[data-test-subj="rules-details-popover"]')
.first()
.prop('isOpen')
).toEqual(false);
});

test('it calls duplicateRulesAction when rules-details-duplicate-rule is clicked', () => {
const wrapper = mount(
<RuleActionsOverflow rule={mockRule('id')} userHasNoPermissions={false} />
);
wrapper.find('[data-test-subj="rules-details-popover-button-icon"] button').simulate('click');
wrapper.update();
wrapper.find('[data-test-subj="rules-details-duplicate-rule"] button').simulate('click');
wrapper.update();
expect(duplicateRulesAction).toHaveBeenCalled();
});

test('it calls duplicateRulesAction with the rule and rule.id when rules-details-duplicate-rule is clicked', () => {
const rule = mockRule('id');
const wrapper = mount(<RuleActionsOverflow rule={rule} userHasNoPermissions={false} />);
wrapper.find('[data-test-subj="rules-details-popover-button-icon"] button').simulate('click');
wrapper.update();
wrapper.find('[data-test-subj="rules-details-duplicate-rule"] button').simulate('click');
wrapper.update();
expect(duplicateRulesAction).toHaveBeenCalledWith(
[rule],
[rule.id],
expect.anything(),
expect.anything()
);
});
});

describe('rules details export rule', () => {
test('it does not open the popover when rules-details-popover-button-icon is clicked and the user does not have permission', () => {
const rule = mockRule('id');
const wrapper = mount(<RuleActionsOverflow rule={rule} userHasNoPermissions={true} />);
wrapper.find('[data-test-subj="rules-details-popover-button-icon"] button').simulate('click');
wrapper.update();
expect(wrapper.find('[data-test-subj="rules-details-export-rule"] button').exists()).toEqual(
false
);
});

test('it closes the popover when rules-details-export-rule is clicked', () => {
const wrapper = mount(
<RuleActionsOverflow rule={mockRule('id')} userHasNoPermissions={false} />
);
wrapper.find('[data-test-subj="rules-details-popover-button-icon"] button').simulate('click');
wrapper.update();
wrapper.find('[data-test-subj="rules-details-export-rule"] button').simulate('click');
wrapper.update();
expect(
wrapper
.find('[data-test-subj="rules-details-popover"]')
.first()
.prop('isOpen')
).toEqual(false);
});

test('it sets the rule.rule_id on the generic downloader when rules-details-export-rule is clicked', () => {
const rule = mockRule('id');
const wrapper = mount(<RuleActionsOverflow rule={rule} userHasNoPermissions={false} />);
wrapper.find('[data-test-subj="rules-details-popover-button-icon"] button').simulate('click');
wrapper.update();
wrapper.find('[data-test-subj="rules-details-export-rule"] button').simulate('click');
wrapper.update();
expect(
wrapper.find('[data-test-subj="rules-details-generic-downloader"]').prop('ids')
).toEqual([rule.rule_id]);
});

test('it does not close the pop over on rules-details-export-rule when the rule is an immutable rule and the user does a click', () => {
const rule = mockRule('id');
rule.immutable = true;
const wrapper = mount(<RuleActionsOverflow rule={rule} userHasNoPermissions={false} />);
wrapper.find('[data-test-subj="rules-details-popover-button-icon"] button').simulate('click');
wrapper.update();
wrapper.find('[data-test-subj="rules-details-export-rule"] button').simulate('click');
wrapper.update();
expect(
wrapper
.find('[data-test-subj="rules-details-popover"]')
.first()
.prop('isOpen')
).toEqual(true);
});

test('it does not set the rule.rule_id on rules-details-export-rule when the rule is an immutable rule', () => {
const rule = mockRule('id');
rule.immutable = true;
const wrapper = mount(<RuleActionsOverflow rule={rule} userHasNoPermissions={false} />);
wrapper.find('[data-test-subj="rules-details-popover-button-icon"] button').simulate('click');
wrapper.update();
wrapper.find('[data-test-subj="rules-details-export-rule"] button').simulate('click');
wrapper.update();
expect(
wrapper.find('[data-test-subj="rules-details-generic-downloader"]').prop('ids')
).toEqual([]);
});
});

describe('rules details delete rule', () => {
test('it does not open the popover when rules-details-popover-button-icon is clicked and the user does not have permission', () => {
const rule = mockRule('id');
const wrapper = mount(<RuleActionsOverflow rule={rule} userHasNoPermissions={true} />);
wrapper.find('[data-test-subj="rules-details-popover-button-icon"] button').simulate('click');
wrapper.update();
expect(wrapper.find('[data-test-subj="rules-details-delete-rule"] button').exists()).toEqual(
false
);
});

test('it closes the popover when rules-details-delete-rule is clicked', () => {
const wrapper = mount(
<RuleActionsOverflow rule={mockRule('id')} userHasNoPermissions={false} />
);
wrapper.find('[data-test-subj="rules-details-popover-button-icon"] button').simulate('click');
wrapper.update();
wrapper.find('[data-test-subj="rules-details-delete-rule"] button').simulate('click');
wrapper.update();
expect(
wrapper
.find('[data-test-subj="rules-details-popover"]')
.first()
.prop('isOpen')
).toEqual(false);
});

test('it calls deleteRulesAction when rules-details-delete-rule is clicked', () => {
const wrapper = mount(
<RuleActionsOverflow rule={mockRule('id')} userHasNoPermissions={false} />
);
wrapper.find('[data-test-subj="rules-details-popover-button-icon"] button').simulate('click');
wrapper.update();
wrapper.find('[data-test-subj="rules-details-delete-rule"] button').simulate('click');
wrapper.update();
expect(deleteRulesAction).toHaveBeenCalled();
});

test('it calls deleteRulesAction with the rule.id when rules-details-delete-rule is clicked', () => {
const rule = mockRule('id');
const wrapper = mount(<RuleActionsOverflow rule={rule} userHasNoPermissions={false} />);
wrapper.find('[data-test-subj="rules-details-popover-button-icon"] button').simulate('click');
wrapper.update();
wrapper.find('[data-test-subj="rules-details-delete-rule"] button').simulate('click');
wrapper.update();
expect(deleteRulesAction).toHaveBeenCalledWith(
[rule.id],
expect.anything(),
expect.anything(),
expect.anything()
);
});
});
});
Loading

0 comments on commit 85c665a

Please sign in to comment.