Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
JiaweiWu committed Apr 22, 2024
1 parent 416c550 commit 02afccb
Show file tree
Hide file tree
Showing 2 changed files with 236 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { rulesClientMock } from '../../../../rules_client.mock';
import { RuleTypeDisabledError } from '../../../../lib/errors/rule_type_disabled';
import { verifyApiAccess } from '../../../../lib/license_api_access';
import { RuleAction, RuleSystemAction } from '../../../../types';
import { Rule } from '../../../../application/rule/types';
import { Rule, RuleParams } from '../../../../application/rule/types';

const rulesClient = rulesClientMock.create();

Expand Down Expand Up @@ -129,15 +129,15 @@ describe('bulkEnableRulesRoute', () => {
});

describe('actions', () => {
const mockedRule: Rule<{}> = {
const mockedRule: Rule<RuleParams> = {
id: '1',
alertTypeId: '1',
schedule: { interval: '10s' },
params: {
bar: true,
},
createdAt: new Date(),
updatedAt: new Date(),
createdAt: new Date('2020-08-20T19:23:38Z'),
updatedAt: new Date('2020-08-20T19:23:38Z'),
actions: [
{
group: 'default',
Expand Down Expand Up @@ -186,7 +186,7 @@ describe('bulkEnableRulesRoute', () => {
uuid: '123-456',
};

const mockedRules: Array<Rule<{}>> = [
const mockedRules: Array<Rule<RuleParams>> = [
{
...mockedRule,
actions: [action],
Expand All @@ -211,7 +211,7 @@ describe('bulkEnableRulesRoute', () => {
const [_, handler] = router.patch.mock.calls[0];

rulesClient.bulkEnableRules.mockResolvedValueOnce(
bulkEnableActionsResult as unknown as BulkEnableRulesResult
bulkEnableActionsResult as BulkEnableRulesResult<RuleParams>
);

const [context, req, res] = mockHandlerArguments(
Expand All @@ -225,25 +225,60 @@ describe('bulkEnableRulesRoute', () => {
const routeRes = await handler(context, req, res);

// @ts-expect-error: body exists
expect(routeRes.body.rules[0].actions).toEqual([
{
connector_type_id: 'test',
group: 'default',
id: '2',
params: {
foo: true,
expect(routeRes.body).toEqual({
rules: [
{
id: '1',
enabled: true,
name: 'abc',
tags: ['foo'],
rule_type_id: '1',
consumer: 'bar',
schedule: {
interval: '10s',
},
actions: [
{
group: 'default',
id: '2',
params: {
foo: true,
},
connector_type_id: 'test',
uuid: '123-456',
},
{
id: 'system_action-id',
params: {
foo: true,
},
uuid: '123-456',
connector_type_id: 'test-2',
},
],
params: {
bar: true,
},
created_by: '',
updated_by: '',
created_at: '2020-08-20T19:23:38.000Z',
updated_at: '2020-08-20T19:23:38.000Z',
api_key_owner: '',
throttle: '30s',
mute_all: false,
notify_when: 'onActionGroupChange',
muted_alert_ids: [],
execution_status: {
status: 'unknown',
last_execution_date: '2020-08-20T19:23:38.000Z',
},
revision: 0,
},
uuid: '123-456',
},
{
connector_type_id: 'test-2',
id: 'system_action-id',
params: {
foo: true,
},
uuid: '123-456',
},
]);
],
errors: [],
total: 1,
task_ids_failed_to_be_enabled: [],
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
/*
* 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 { transformBulkEnableResponse } from './v1';
import { RuleAction, RuleSystemAction } from '../../../../../../../common';

describe('transformBulkEnableResponse', () => {
const defaultAction: RuleAction = {
id: '1',
uuid: '111',
params: { foo: 'bar' },
group: 'default',
actionTypeId: '.test',
frequency: { notifyWhen: 'onThrottleInterval', summary: true, throttle: '1h' },
alertsFilter: {
query: { dsl: '{test:1}', kql: 'test:1s', filters: [] },
timeframe: {
days: [1, 2, 3],
hours: { end: '15:00', start: '00:00' },
timezone: 'UTC',
},
},
};

const systemAction: RuleSystemAction = {
id: '2',
uuid: '222',
params: { foo: 'bar' },
actionTypeId: '.test',
};

const rule = {
id: '3d534c70-582b-11ec-8995-2b1578a3bc5d',
enabled: true,
name: 'stressing index-threshold 37/200',
tags: [],
alertTypeId: '.index-threshold',
consumer: 'alerts',
schedule: {
interval: '1s',
},
actions: [defaultAction],
systemActions: [systemAction],
params: {},
createdBy: 'elastic',
updatedBy: '2889684073',
createdAt: new Date('2023-08-01T09:16:35.368Z'),
updatedAt: new Date('2023-08-01T09:16:35.368Z'),
notifyWhen: 'onActiveAlert' as const,
throttle: null,
apiKey: null,
apiKeyOwner: '2889684073',
muteAll: false,
mutedInstanceIds: [],
scheduledTaskId: '52125fb0-5895-11ec-ae69-bb65d1a71b72',
executionStatus: {
status: 'ok' as const,
lastExecutionDate: new Date('2023-08-01T09:16:35.368Z'),
lastDuration: 1194,
},
revision: 0,
};
it('should transform bulk enable result', () => {
expect(
transformBulkEnableResponse({
rules: [rule],
errors: [
{
message: 'test error',
status: 400,
rule: {
id: 'error-rule-id',
name: 'error-rule',
},
},
],

total: 1,
taskIdsFailedToBeEnabled: ['error-rule-id'],
})
).toMatchInlineSnapshot(`
Object {
"errors": Array [
Object {
"message": "test error",
"rule": Object {
"id": "error-rule-id",
"name": "error-rule",
},
"status": 400,
},
],
"rules": Array [
Object {
"actions": Array [
Object {
"alerts_filter": Object {
"query": Object {
"dsl": "{test:1}",
"filters": Array [],
"kql": "test:1s",
},
"timeframe": Object {
"days": Array [
1,
2,
3,
],
"hours": Object {
"end": "15:00",
"start": "00:00",
},
"timezone": "UTC",
},
},
"connector_type_id": ".test",
"frequency": Object {
"notify_when": "onThrottleInterval",
"summary": true,
"throttle": "1h",
},
"group": "default",
"id": "1",
"params": Object {
"foo": "bar",
},
"uuid": "111",
},
Object {
"connector_type_id": ".test",
"id": "2",
"params": Object {
"foo": "bar",
},
"uuid": "222",
},
],
"api_key_owner": "2889684073",
"consumer": "alerts",
"created_at": "2023-08-01T09:16:35.368Z",
"created_by": "elastic",
"enabled": true,
"execution_status": Object {
"last_duration": 1194,
"last_execution_date": "2023-08-01T09:16:35.368Z",
"status": "ok",
},
"id": "3d534c70-582b-11ec-8995-2b1578a3bc5d",
"mute_all": false,
"muted_alert_ids": Array [],
"name": "stressing index-threshold 37/200",
"notify_when": "onActiveAlert",
"params": Object {},
"revision": 0,
"rule_type_id": ".index-threshold",
"schedule": Object {
"interval": "1s",
},
"scheduled_task_id": "52125fb0-5895-11ec-ae69-bb65d1a71b72",
"tags": Array [],
"throttle": null,
"updated_at": "2023-08-01T09:16:35.368Z",
"updated_by": "2889684073",
},
],
"task_ids_failed_to_be_enabled": Array [
"error-rule-id",
],
"total": 1,
}
`);
});
});

0 comments on commit 02afccb

Please sign in to comment.