Skip to content

Commit

Permalink
Updated unit tests per PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankHassanabad committed Sep 23, 2021
1 parent d7f7f02 commit 14e9394
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,94 @@ describe('utils', () => {
)
).toEqual(NOTIFICATION_THROTTLE_RULE);
});

test('it will use the "rule" and not the "legacyRuleActions" if the rule and actions is defined', () => {
const legacyRuleActions: LegacyRuleActions = {
id: 'id_1',
ruleThrottle: '',
alertThrottle: '',
actions: [
{
id: 'id_2',
group: 'group',
action_type_id: 'actionTypeId',
params: {},
},
],
};

expect(
transformFromAlertThrottle(
{
muteAll: true,
notifyWhen: 'onActiveAlert',
actions: [
{
group: 'group',
id: 'id-123',
actionTypeId: 'id-456',
params: {},
},
],
} as SanitizedAlert<RuleParams>,
legacyRuleActions
)
).toEqual(NOTIFICATION_THROTTLE_NO_ACTIONS);
});

test('it will use the "legacyRuleActions" and not the "rule" if the rule actions is an empty array', () => {
const legacyRuleActions: LegacyRuleActions = {
id: 'id_1',
ruleThrottle: NOTIFICATION_THROTTLE_RULE,
alertThrottle: null,
actions: [
{
id: 'id_2',
group: 'group',
action_type_id: 'actionTypeId',
params: {},
},
],
};

expect(
transformFromAlertThrottle(
{
muteAll: true,
notifyWhen: 'onActiveAlert',
actions: [],
} as unknown as SanitizedAlert<RuleParams>,
legacyRuleActions
)
).toEqual(NOTIFICATION_THROTTLE_RULE);
});

test('it will use the "legacyRuleActions" and not the "rule" if the rule actions is a null', () => {
const legacyRuleActions: LegacyRuleActions = {
id: 'id_1',
ruleThrottle: NOTIFICATION_THROTTLE_RULE,
alertThrottle: null,
actions: [
{
id: 'id_2',
group: 'group',
action_type_id: 'actionTypeId',
params: {},
},
],
};

expect(
transformFromAlertThrottle(
{
muteAll: true,
notifyWhen: 'onActiveAlert',
actions: null,
} as unknown as SanitizedAlert<RuleParams>,
legacyRuleActions
)
).toEqual(NOTIFICATION_THROTTLE_RULE);
});
});

describe('#transformActions', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import { RulesClient } from '../../../../../alerting/server';
// eslint-disable-next-line no-restricted-imports
import { LegacyRuleActions } from '../rule_actions/legacy_types';
import { FullResponseSchema } from '../../../../common/detection_engine/schemas/request';
import { transformAlertToRuleAction } from '../../../../common/detection_engine/transform_actions';

export const calculateInterval = (
interval: string | undefined,
Expand Down Expand Up @@ -230,12 +231,7 @@ export const transformActions = (
legacyRuleActions: LegacyRuleActions | null | undefined
): FullResponseSchema['actions'] => {
if (alertAction != null && alertAction.length !== 0) {
return alertAction.map((action) => ({
group: action.group,
id: action.id,
action_type_id: action.actionTypeId,
params: action.params,
}));
return alertAction.map((action) => transformAlertToRuleAction(action));
} else if (legacyRuleActions != null) {
return legacyRuleActions.actions;
} else {
Expand Down

0 comments on commit 14e9394

Please sign in to comment.