Skip to content

Commit

Permalink
Update rewrite tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Zacqary committed Dec 21, 2023
1 parent c157663 commit 1057ce6
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 69 deletions.
124 changes: 63 additions & 61 deletions x-pack/plugins/alerting/server/routes/lib/rewrite_actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@
* 2.0.
*/

import { rewriteActionsReq, rewriteActionsRes } from './rewrite_actions';
import { RuleActionTypes } from '../../../common';
import { rewriteActionsReq } from './rewrite_actions';

describe('rewrite Actions', () => {
describe('rewriteActionsRes', () => {
it('rewrites the actions response correctly', () => {
expect(
rewriteActionsRes([
describe('rewriteActionsReq', () => {
it('should rewrite actions correctly', () => {
expect(
rewriteActionsReq(
[
{
uuid: '111',
group: 'default',
id: '1',
actionTypeId: '2',
params: { foo: 'bar' },
frequency: {
summary: true,
notifyWhen: 'onThrottleInterval',
notify_when: 'onThrottleInterval',
throttle: '1h',
},
alertsFilter: {
alerts_filter: {
query: {
kql: 'test:1s',
dsl: '{test:1}',
Expand All @@ -39,60 +39,12 @@ describe('rewrite Actions', () => {
},
},
},
])
).toEqual([
{
alerts_filter: {
query: { dsl: '{test:1}', kql: 'test:1s', filters: [] },
timeframe: {
days: [1, 2, 3],
hours: { end: '15:00', start: '00:00' },
timezone: 'UTC',
},
},
connector_type_id: '2',
frequency: { notify_when: 'onThrottleInterval', summary: true, throttle: '1h' },
group: 'default',
id: '1',
params: { foo: 'bar' },
uuid: '111',
},
]);
});
});

describe('rewriteActionsReq', () => {
expect(
rewriteActionsReq([
{
uuid: '111',
group: 'default',
id: '1',
params: { foo: 'bar' },
frequency: {
summary: true,
notify_when: 'onThrottleInterval',
throttle: '1h',
},
alerts_filter: {
query: {
kql: 'test:1s',
dsl: '{test:1}',
filters: [],
},
timeframe: {
days: [1, 2, 3],
timezone: 'UTC',
hours: {
start: '00:00',
end: '15:00',
},
},
},
},
])
],
() => false
)
).toEqual([
{
type: RuleActionTypes.DEFAULT,
uuid: '111',
group: 'default',
id: '1',
Expand Down Expand Up @@ -120,4 +72,54 @@ describe('rewrite Actions', () => {
},
]);
});
it('should rewrite system actions correctly', () => {
expect(
rewriteActionsReq(
[
{
uuid: '111',
group: 'default',
id: '1',
params: { foo: 'bar' },
frequency: {
summary: true,
notify_when: 'onThrottleInterval',
throttle: '1h',
},
},
{
uuid: '111',
group: 'default',
id: 'system-1',
params: { foo: 'bar' },
frequency: {
summary: true,
notify_when: 'onThrottleInterval',
throttle: '1h',
},
},
],
(id) => id.startsWith('system')
)
).toEqual([
{
type: RuleActionTypes.DEFAULT,
uuid: '111',
group: 'default',
id: '1',
params: { foo: 'bar' },
frequency: {
summary: true,
notifyWhen: 'onThrottleInterval',
throttle: '1h',
},
},
{
type: RuleActionTypes.SYSTEM,
uuid: '111',
id: 'system-1',
params: { foo: 'bar' },
},
]);
});
});
26 changes: 18 additions & 8 deletions x-pack/plugins/alerting/server/routes/lib/rewrite_rule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,21 @@ const sampleRule: SanitizedRule<RuleTypeParams> & { activeSnoozes?: string[] } =
id: 'aaa',
actionTypeId: 'bbb',
params: {},
type: 'default',
frequency: {
summary: false,
notifyWhen: 'onThrottleInterval',
throttle: '1m',
},
alertsFilter: { query: { kql: 'test:1', dsl: '{}', filters: [] } },
},
{
id: 'ccc',
actionTypeId: 'ddd',
params: {},
type: 'system',
useAlertDataForTemplate: true,
},
],
scheduledTaskId: 'xyz456',
snoozeSchedule: [],
Expand Down Expand Up @@ -78,13 +86,15 @@ describe('rewriteRule', () => {
});
it('should rewrite actions correctly', () => {
const rewritten = rewriteRule(sampleRule);
for (const action of rewritten.actions) {
expect(Object.keys(action)).toEqual(
expect.arrayContaining(['group', 'id', 'connector_type_id', 'params', 'frequency'])
);
expect(Object.keys(action.frequency!)).toEqual(
expect.arrayContaining(['summary', 'notify_when', 'throttle'])
);
}
const [rewrittenDefaultAction, rewrittenSystemAction] = rewritten.actions;
expect(Object.keys(rewrittenDefaultAction)).toEqual(
expect.arrayContaining(['group', 'id', 'connector_type_id', 'params', 'frequency'])
);
expect(Object.keys(rewrittenDefaultAction.frequency!)).toEqual(
expect.arrayContaining(['summary', 'notify_when', 'throttle'])
);
expect(Object.keys(rewrittenSystemAction)).toEqual(
expect.arrayContaining(['use_alert_data_for_template', 'connector_type_id'])
);
});
});

0 comments on commit 1057ce6

Please sign in to comment.