Skip to content

Commit

Permalink
[Security Solution] Template unit tests (elastic#72399)
Browse files Browse the repository at this point in the history
* add unit test for failure cases

* add unit tests

* update wording

* fix error when update template without ttid or ttversion

* fix unit test

* add comment

* review

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
angorayc and elasticmachine committed Jul 28, 2020
1 parent 7d19bca commit 055a582
Show file tree
Hide file tree
Showing 10 changed files with 888 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const PRE_BUILT_MSG = i18n.translate(
export const PRE_BUILT_ACTION = i18n.translate(
'xpack.securitySolution.detectionEngine.rules.prePackagedRules.loadPreBuiltButton',
{
defaultMessage: 'Load prebuilt detection rules',
defaultMessage: 'Load prebuilt detection rules and timeline templates',
}
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { shallow } from 'enzyme';

import { UpdatePrePackagedRulesCallOut } from './update_callout';
import { useKibana } from '../../../../common/lib/kibana';

jest.mock('../../../../common/lib/kibana');

describe('UpdatePrePackagedRulesCallOut', () => {
Expand All @@ -22,6 +23,7 @@ describe('UpdatePrePackagedRulesCallOut', () => {
},
});
});

it('renders correctly', () => {
const wrapper = shallow(
<UpdatePrePackagedRulesCallOut
Expand All @@ -34,4 +36,94 @@ describe('UpdatePrePackagedRulesCallOut', () => {

expect(wrapper.find('EuiCallOut')).toHaveLength(1);
});

it('renders callOutMessage correctly: numberOfUpdatedRules > 0 and numberOfUpdatedTimelines = 0', () => {
const wrapper = shallow(
<UpdatePrePackagedRulesCallOut
loading={false}
numberOfUpdatedRules={1}
numberOfUpdatedTimelines={0}
updateRules={jest.fn()}
/>
);

expect(wrapper.find('[data-test-subj="update-callout"]').find('p').text()).toEqual(
'You can update 1 Elastic prebuilt ruleRelease notes'
);
});

it('renders buttonTitle correctly: numberOfUpdatedRules > 0 and numberOfUpdatedTimelines = 0', () => {
const wrapper = shallow(
<UpdatePrePackagedRulesCallOut
loading={false}
numberOfUpdatedRules={1}
numberOfUpdatedTimelines={0}
updateRules={jest.fn()}
/>
);

expect(wrapper.find('[data-test-subj="update-callout-button"]').prop('children')).toEqual(
'Update 1 Elastic prebuilt rule'
);
});

it('renders callOutMessage correctly: numberOfUpdatedRules = 0 and numberOfUpdatedTimelines > 0', () => {
const wrapper = shallow(
<UpdatePrePackagedRulesCallOut
loading={false}
numberOfUpdatedRules={0}
numberOfUpdatedTimelines={1}
updateRules={jest.fn()}
/>
);

expect(wrapper.find('[data-test-subj="update-callout"]').find('p').text()).toEqual(
'You can update 1 Elastic prebuilt timelineRelease notes'
);
});

it('renders buttonTitle correctly: numberOfUpdatedRules = 0 and numberOfUpdatedTimelines > 0', () => {
const wrapper = shallow(
<UpdatePrePackagedRulesCallOut
loading={false}
numberOfUpdatedRules={0}
numberOfUpdatedTimelines={1}
updateRules={jest.fn()}
/>
);

expect(wrapper.find('[data-test-subj="update-callout-button"]').prop('children')).toEqual(
'Update 1 Elastic prebuilt timeline'
);
});

it('renders callOutMessage correctly: numberOfUpdatedRules > 0 and numberOfUpdatedTimelines > 0', () => {
const wrapper = shallow(
<UpdatePrePackagedRulesCallOut
loading={false}
numberOfUpdatedRules={1}
numberOfUpdatedTimelines={1}
updateRules={jest.fn()}
/>
);

expect(wrapper.find('[data-test-subj="update-callout"]').find('p').text()).toEqual(
'You can update 1 Elastic prebuilt rule and 1 Elastic prebuilt timeline. Note that this will reload deleted Elastic prebuilt rules.Release notes'
);
});

it('renders buttonTitle correctly: numberOfUpdatedRules > 0 and numberOfUpdatedTimelines > 0', () => {
const wrapper = shallow(
<UpdatePrePackagedRulesCallOut
loading={false}
numberOfUpdatedRules={1}
numberOfUpdatedTimelines={1}
updateRules={jest.fn()}
/>
);

expect(wrapper.find('[data-test-subj="update-callout-button"]').prop('children')).toEqual(
'Update 1 Elastic prebuilt rule and 1 Elastic prebuilt timeline'
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const UpdatePrePackagedRulesCallOutComponent: React.FC<UpdatePrePackagedRulesCal
}, [numberOfUpdatedRules, numberOfUpdatedTimelines]);

return (
<EuiCallOut title={i18n.UPDATE_PREPACKAGED_RULES_TITLE}>
<EuiCallOut title={i18n.UPDATE_PREPACKAGED_RULES_TITLE} data-test-subj="update-callout">
<p>
{prepackagedRulesOrTimelines?.callOutMessage}
<br />
Expand All @@ -62,7 +62,12 @@ const UpdatePrePackagedRulesCallOutComponent: React.FC<UpdatePrePackagedRulesCal
{i18n.RELEASE_NOTES_HELP}
</EuiLink>
</p>
<EuiButton onClick={updateRules} size="s" isLoading={loading}>
<EuiButton
onClick={updateRules}
size="s"
isLoading={loading}
data-test-subj="update-callout-button"
>
{prepackagedRulesOrTimelines?.buttonTitle}
</EuiButton>
</EuiCallOut>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ export const usePrePackagedRules = ({
if (
isSubscribed &&
((prePackagedRuleStatusResponse.rules_not_installed === 0 &&
prePackagedRuleStatusResponse.rules_not_updated === 0) ||
prePackagedRuleStatusResponse.rules_not_updated === 0 &&
prePackagedRuleStatusResponse.timelines_not_installed === 0 &&
prePackagedRuleStatusResponse.timelines_not_updated === 0) ||
iterationTryOfFetchingPrePackagedCount > 100)
) {
setLoadingCreatePrePackagedRules(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
getActionsStepsData,
getHumanizedDuration,
getModifiedAboutDetailsData,
getPrePackagedRuleStatus,
getPrePackagedTimelineStatus,
determineDetailsValue,
userHasNoPermissions,
} from './helpers';
Expand Down Expand Up @@ -394,4 +396,138 @@ describe('rule helpers', () => {
expect(result).toEqual(userHasNoPermissionsExpectedResult);
});
});

describe('getPrePackagedRuleStatus', () => {
test('ruleNotInstalled', () => {
const rulesInstalled = 0;
const rulesNotInstalled = 1;
const rulesNotUpdated = 0;
const result: string = getPrePackagedRuleStatus(
rulesInstalled,
rulesNotInstalled,
rulesNotUpdated
);

expect(result).toEqual('ruleNotInstalled');
});

test('ruleInstalled', () => {
const rulesInstalled = 1;
const rulesNotInstalled = 0;
const rulesNotUpdated = 0;
const result: string = getPrePackagedRuleStatus(
rulesInstalled,
rulesNotInstalled,
rulesNotUpdated
);

expect(result).toEqual('ruleInstalled');
});

test('someRuleUninstall', () => {
const rulesInstalled = 1;
const rulesNotInstalled = 1;
const rulesNotUpdated = 0;
const result: string = getPrePackagedRuleStatus(
rulesInstalled,
rulesNotInstalled,
rulesNotUpdated
);

expect(result).toEqual('someRuleUninstall');
});

test('ruleNeedUpdate', () => {
const rulesInstalled = 1;
const rulesNotInstalled = 0;
const rulesNotUpdated = 1;
const result: string = getPrePackagedRuleStatus(
rulesInstalled,
rulesNotInstalled,
rulesNotUpdated
);

expect(result).toEqual('ruleNeedUpdate');
});

test('unknown', () => {
const rulesInstalled = null;
const rulesNotInstalled = null;
const rulesNotUpdated = null;
const result: string = getPrePackagedRuleStatus(
rulesInstalled,
rulesNotInstalled,
rulesNotUpdated
);

expect(result).toEqual('unknown');
});
});

describe('getPrePackagedTimelineStatus', () => {
test('timelinesNotInstalled', () => {
const timelinesInstalled = 0;
const timelinesNotInstalled = 1;
const timelinesNotUpdated = 0;
const result: string = getPrePackagedTimelineStatus(
timelinesInstalled,
timelinesNotInstalled,
timelinesNotUpdated
);

expect(result).toEqual('timelinesNotInstalled');
});

test('timelinesInstalled', () => {
const timelinesInstalled = 1;
const timelinesNotInstalled = 0;
const timelinesNotUpdated = 0;
const result: string = getPrePackagedTimelineStatus(
timelinesInstalled,
timelinesNotInstalled,
timelinesNotUpdated
);

expect(result).toEqual('timelinesInstalled');
});

test('someTimelineUninstall', () => {
const timelinesInstalled = 1;
const timelinesNotInstalled = 1;
const timelinesNotUpdated = 0;
const result: string = getPrePackagedTimelineStatus(
timelinesInstalled,
timelinesNotInstalled,
timelinesNotUpdated
);

expect(result).toEqual('someTimelineUninstall');
});

test('timelineNeedUpdate', () => {
const timelinesInstalled = 1;
const timelinesNotInstalled = 0;
const timelinesNotUpdated = 1;
const result: string = getPrePackagedTimelineStatus(
timelinesInstalled,
timelinesNotInstalled,
timelinesNotUpdated
);

expect(result).toEqual('timelineNeedUpdate');
});

test('unknown', () => {
const timelinesInstalled = null;
const timelinesNotInstalled = null;
const timelinesNotUpdated = null;
const result: string = getPrePackagedTimelineStatus(
timelinesInstalled,
timelinesNotInstalled,
timelinesNotUpdated
);

expect(result).toEqual('unknown');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import {
} from '../__mocks__/request_responses';
import { requestContextMock, serverMock, createMockConfig } from '../__mocks__';
import { SecurityPluginSetup } from '../../../../../../security/server';
import { checkTimelinesStatus } from '../../../timeline/routes/utils/check_timelines_status';
import {
mockCheckTimelinesStatusBeforeInstallResult,
mockCheckTimelinesStatusAfterInstallResult,
} from '../../../timeline/routes/__mocks__/import_timelines';

jest.mock('../../rules/get_prepackaged_rules', () => {
return {
Expand All @@ -38,6 +43,12 @@ jest.mock('../../rules/get_prepackaged_rules', () => {
};
});

jest.mock('../../../timeline/routes/utils/check_timelines_status', () => {
return {
checkTimelinesStatus: jest.fn(),
};
});

describe('get_prepackaged_rule_status_route', () => {
const mockGetCurrentUser = {
user: {
Expand Down Expand Up @@ -126,5 +137,45 @@ describe('get_prepackaged_rule_status_route', () => {
timelines_not_updated: 0,
});
});

test('0 timelines installed, 3 timelines not installed, 0 timelines not updated', async () => {
clients.alertsClient.find.mockResolvedValue(getEmptyFindResult());
(checkTimelinesStatus as jest.Mock).mockResolvedValue(
mockCheckTimelinesStatusBeforeInstallResult
);
const request = getPrepackagedRulesStatusRequest();
const response = await server.inject(request, context);

expect(response.status).toEqual(200);
expect(response.body).toEqual({
rules_custom_installed: 0,
rules_installed: 0,
rules_not_installed: 1,
rules_not_updated: 0,
timelines_installed: 0,
timelines_not_installed: 3,
timelines_not_updated: 0,
});
});

test('3 timelines installed, 0 timelines not installed, 0 timelines not updated', async () => {
clients.alertsClient.find.mockResolvedValue(getEmptyFindResult());
(checkTimelinesStatus as jest.Mock).mockResolvedValue(
mockCheckTimelinesStatusAfterInstallResult
);
const request = getPrepackagedRulesStatusRequest();
const response = await server.inject(request, context);

expect(response.status).toEqual(200);
expect(response.body).toEqual({
rules_custom_installed: 0,
rules_installed: 0,
rules_not_installed: 1,
rules_not_updated: 0,
timelines_installed: 3,
timelines_not_installed: 0,
timelines_not_updated: 0,
});
});
});
});
Loading

0 comments on commit 055a582

Please sign in to comment.