mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 17:59:23 -04:00
[RAM] Cover rewriteRule in unit/integration tests (#150817)
## Summary Closes #149642 - Adds a unit test for `rewriteRule` - Adds actions to the integration tests for the `_find` API ### Checklist - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios
This commit is contained in:
parent
fb1a8ce66d
commit
a608af6425
3 changed files with 176 additions and 10 deletions
|
@ -0,0 +1,88 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License
|
||||
* 2.0; you may not use this file except in compliance with the Elastic License
|
||||
* 2.0.
|
||||
*/
|
||||
import { rewriteRule } from './rewrite_rule';
|
||||
import { RuleTypeParams, SanitizedRule } from '../../types';
|
||||
import { isPlainObject } from 'lodash';
|
||||
|
||||
const DATE_2020 = new Date('1/1/2020');
|
||||
|
||||
const sampleRule: SanitizedRule<RuleTypeParams> & { activeSnoozes?: string[] } = {
|
||||
id: 'aaaa',
|
||||
name: 'Sample Rule',
|
||||
enabled: true,
|
||||
tags: [],
|
||||
consumer: 'xxxx',
|
||||
schedule: { interval: '1m' },
|
||||
params: {},
|
||||
alertTypeId: 'abc123',
|
||||
createdBy: 'user',
|
||||
updatedBy: 'user',
|
||||
createdAt: DATE_2020,
|
||||
updatedAt: DATE_2020,
|
||||
apiKeyOwner: 'owner',
|
||||
notifyWhen: null,
|
||||
muteAll: false,
|
||||
mutedInstanceIds: ['abc', '123'],
|
||||
executionStatus: {
|
||||
status: 'ok',
|
||||
lastExecutionDate: DATE_2020,
|
||||
lastDuration: 1000,
|
||||
},
|
||||
actions: [
|
||||
{
|
||||
group: 'default',
|
||||
id: 'aaa',
|
||||
actionTypeId: 'bbb',
|
||||
params: {},
|
||||
frequency: {
|
||||
summary: false,
|
||||
notifyWhen: 'onThrottleInterval',
|
||||
throttle: '1m',
|
||||
},
|
||||
},
|
||||
],
|
||||
scheduledTaskId: 'xyz456',
|
||||
snoozeSchedule: [],
|
||||
isSnoozedUntil: null,
|
||||
activeSnoozes: [],
|
||||
lastRun: {
|
||||
outcome: 'succeeded',
|
||||
outcomeMsg: null,
|
||||
alertsCount: {
|
||||
active: 1,
|
||||
new: 1,
|
||||
recovered: 1,
|
||||
ignored: 1,
|
||||
},
|
||||
},
|
||||
nextRun: DATE_2020,
|
||||
};
|
||||
|
||||
describe('rewriteRule', () => {
|
||||
it('should not camelCase any property names', () => {
|
||||
const rewritten = rewriteRule(sampleRule);
|
||||
for (const [key, val] of Object.entries(rewritten)) {
|
||||
expect(key.toLowerCase()).toEqual(key);
|
||||
if (isPlainObject(val)) {
|
||||
for (const keyO of Object.keys(rewritten)) {
|
||||
expect(keyO.toLowerCase()).toEqual(keyO);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
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'])
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
|
@ -27,13 +27,39 @@ const findTestUtils = (
|
|||
const { user, space } = scenario;
|
||||
describe(scenario.id, () => {
|
||||
it('should handle find alert request appropriately', async () => {
|
||||
const { body: createdAction } = await supertest
|
||||
.post(`${getUrlPrefix(space.id)}/api/actions/connector`)
|
||||
.set('kbn-xsrf', 'foo')
|
||||
.send({
|
||||
name: 'MY action',
|
||||
connector_type_id: 'test.noop',
|
||||
config: {},
|
||||
secrets: {},
|
||||
})
|
||||
.expect(200);
|
||||
|
||||
const { body: createdAlert } = await supertest
|
||||
.post(`${getUrlPrefix(space.id)}/api/alerting/rule`)
|
||||
.set('kbn-xsrf', 'foo')
|
||||
.send(getTestRuleData())
|
||||
.send(
|
||||
getTestRuleData({
|
||||
actions: [
|
||||
{
|
||||
group: 'default',
|
||||
id: createdAction.id,
|
||||
params: {},
|
||||
frequency: {
|
||||
summary: false,
|
||||
notify_when: 'onThrottleInterval',
|
||||
throttle: '1m',
|
||||
},
|
||||
},
|
||||
],
|
||||
notify_when: undefined,
|
||||
throttle: undefined,
|
||||
})
|
||||
)
|
||||
.expect(200);
|
||||
objectRemover.add(space.id, createdAlert.id, 'rule', 'alerting');
|
||||
|
||||
const response = await supertestWithoutAuth
|
||||
.get(
|
||||
`${getUrlPrefix(space.id)}/${
|
||||
|
@ -73,14 +99,26 @@ const findTestUtils = (
|
|||
consumer: 'alertsFixture',
|
||||
schedule: { interval: '1m' },
|
||||
enabled: true,
|
||||
actions: [],
|
||||
actions: [
|
||||
{
|
||||
group: 'default',
|
||||
id: createdAction.id,
|
||||
connector_type_id: 'test.noop',
|
||||
params: {},
|
||||
frequency: {
|
||||
summary: false,
|
||||
notify_when: 'onThrottleInterval',
|
||||
throttle: '1m',
|
||||
},
|
||||
},
|
||||
],
|
||||
params: {},
|
||||
created_by: 'elastic',
|
||||
scheduled_task_id: match.scheduled_task_id,
|
||||
created_at: match.created_at,
|
||||
updated_at: match.updated_at,
|
||||
throttle: '1m',
|
||||
notify_when: 'onThrottleInterval',
|
||||
throttle: null,
|
||||
notify_when: null,
|
||||
updated_by: 'elastic',
|
||||
api_key_owner: 'elastic',
|
||||
mute_all: false,
|
||||
|
|
|
@ -34,10 +34,38 @@ const findTestUtils = (
|
|||
describe(describeType, () => {
|
||||
afterEach(() => objectRemover.removeAll());
|
||||
it('should handle find alert request appropriately', async () => {
|
||||
const { body: createdAction } = await supertest
|
||||
.post(`${getUrlPrefix(Spaces.space1.id)}/api/actions/connector`)
|
||||
.set('kbn-xsrf', 'foo')
|
||||
.send({
|
||||
name: 'MY action',
|
||||
connector_type_id: 'test.noop',
|
||||
config: {},
|
||||
secrets: {},
|
||||
})
|
||||
.expect(200);
|
||||
|
||||
const { body: createdAlert } = await supertest
|
||||
.post(`${getUrlPrefix(Spaces.space1.id)}/api/alerting/rule`)
|
||||
.set('kbn-xsrf', 'foo')
|
||||
.send(getTestRuleData())
|
||||
.send(
|
||||
getTestRuleData({
|
||||
actions: [
|
||||
{
|
||||
group: 'default',
|
||||
id: createdAction.id,
|
||||
params: {},
|
||||
frequency: {
|
||||
summary: false,
|
||||
notify_when: 'onThrottleInterval',
|
||||
throttle: '1m',
|
||||
},
|
||||
},
|
||||
],
|
||||
notify_when: undefined,
|
||||
throttle: undefined,
|
||||
})
|
||||
)
|
||||
.expect(200);
|
||||
objectRemover.add(Spaces.space1.id, createdAlert.id, 'rule', 'alerting');
|
||||
|
||||
|
@ -63,14 +91,26 @@ const findTestUtils = (
|
|||
consumer: 'alertsFixture',
|
||||
schedule: { interval: '1m' },
|
||||
enabled: true,
|
||||
actions: [],
|
||||
actions: [
|
||||
{
|
||||
group: 'default',
|
||||
id: createdAction.id,
|
||||
connector_type_id: 'test.noop',
|
||||
params: {},
|
||||
frequency: {
|
||||
summary: false,
|
||||
notify_when: 'onThrottleInterval',
|
||||
throttle: '1m',
|
||||
},
|
||||
},
|
||||
],
|
||||
params: {},
|
||||
created_by: null,
|
||||
api_key_owner: null,
|
||||
scheduled_task_id: match.scheduled_task_id,
|
||||
updated_by: null,
|
||||
throttle: '1m',
|
||||
notify_when: 'onThrottleInterval',
|
||||
throttle: null,
|
||||
notify_when: null,
|
||||
mute_all: false,
|
||||
muted_alert_ids: [],
|
||||
created_at: match.created_at,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue