Skip to content

Commit

Permalink
added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
YulNaumenko committed Apr 1, 2021
1 parent 8c8a5d9 commit eac81e0
Show file tree
Hide file tree
Showing 37 changed files with 1,333 additions and 716 deletions.
4 changes: 2 additions & 2 deletions x-pack/plugins/alerting/server/alerts_client/alerts_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export interface CreateOptions<Params extends AlertTypeParams> {
| 'mutedInstanceIds'
| 'actions'
| 'executionStatus'
> & { actions: NormalizedAlertAction[] };
> & { actions: AlertAction[] };
options?: {
id?: string;
migrationVersion?: Record<string, string>;
Expand All @@ -163,7 +163,7 @@ export interface UpdateOptions<Params extends AlertTypeParams> {
name: string;
tags: string[];
schedule: IntervalSchedule;
actions: NormalizedAlertAction[];
actions: AlertAction[];
params: Params;
throttle: string | null;
notifyWhen: AlertNotifyWhenType | null;
Expand Down
3 changes: 3 additions & 0 deletions x-pack/plugins/alerting/server/routes/create_rule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ describe('createRuleRoute', () => {
group: mockedAlert.actions[0].group,
id: mockedAlert.actions[0].id,
params: mockedAlert.actions[0].params,
connector_type_id: mockedAlert.actions[0].actionTypeId,
},
],
};
Expand Down Expand Up @@ -131,6 +132,7 @@ describe('createRuleRoute', () => {
"data": Object {
"actions": Array [
Object {
"actionTypeId": "test",
"group": "default",
"id": "2",
"params": Object {
Expand Down Expand Up @@ -203,6 +205,7 @@ describe('createRuleRoute', () => {
"data": Object {
"actions": Array [
Object {
"actionTypeId": "test",
"group": "default",
"id": "2",
"params": Object {
Expand Down
11 changes: 11 additions & 0 deletions x-pack/plugins/alerting/server/routes/create_rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
AlertingRequestHandlerContext,
BASE_ALERTING_API_PATH,
AlertNotifyWhenType,
AlertAction,
} from '../types';

export const bodySchema = schema.object({
Expand All @@ -40,12 +41,21 @@ export const bodySchema = schema.object({
group: schema.string(),
id: schema.string(),
params: schema.recordOf(schema.string(), schema.any(), { defaultValue: {} }),
connector_type_id: schema.string(),
}),
{ defaultValue: [] }
),
notify_when: schema.string({ validate: validateNotifyWhenType }),
});

const rewriteBodyReqActions: RewriteRequestCase<AlertAction> = ({
connector_type_id: actionTypeId,
...rest
}) => ({
actionTypeId,
...rest,
});

const rewriteBodyReq: RewriteRequestCase<CreateOptions<AlertTypeParams>['data']> = ({
rule_type_id: alertTypeId,
notify_when: notifyWhen,
Expand Down Expand Up @@ -121,6 +131,7 @@ export const createRuleRoute = (
data: rewriteBodyReq({
...rule,
notify_when: rule.notify_when as AlertNotifyWhenType,
actions: rule.actions.map((action) => rewriteBodyReqActions(action)),
}),
options: { id: params?.id },
}
Expand Down
11 changes: 7 additions & 4 deletions x-pack/plugins/alerting/server/routes/update_rule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ describe('updateRuleRoute', () => {
group: mockedAlert.actions[0].group,
id: mockedAlert.actions[0].id,
params: mockedAlert.actions[0].params,
connector_type_id: mockedAlert.actions[0].actionTypeId,
},
],
};
Expand Down Expand Up @@ -109,6 +110,7 @@ describe('updateRuleRoute', () => {
"data": Object {
"actions": Array [
Object {
"actionTypeId": "test",
"group": "default",
"id": "2",
"params": Object {
Expand Down Expand Up @@ -203,10 +205,11 @@ describe('updateRuleRoute', () => {

alertsClient.update.mockRejectedValue(new AlertTypeDisabledError('Fail', 'license_invalid'));

const [context, req, res] = mockHandlerArguments({ alertsClient }, { params: {}, body: {} }, [
'ok',
'forbidden',
]);
const [context, req, res] = mockHandlerArguments(
{ alertsClient },
{ params: {}, body: updateRequest },
['ok', 'forbidden']
);

await handler(context, req, res);

Expand Down
12 changes: 11 additions & 1 deletion x-pack/plugins/alerting/server/routes/update_rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { schema } from '@kbn/config-schema';
import { IRouter } from 'kibana/server';
import { ILicenseState, AlertTypeDisabledError, validateDurationSchema } from '../lib';
import { AlertNotifyWhenType } from '../../common';
import { AlertAction, AlertNotifyWhenType } from '../../common';
import { UpdateOptions } from '../alerts_client';
import {
verifyAccessAndContext,
Expand Down Expand Up @@ -41,12 +41,21 @@ const bodySchema = schema.object({
group: schema.string(),
id: schema.string(),
params: schema.recordOf(schema.string(), schema.any(), { defaultValue: {} }),
connector_type_id: schema.string(),
}),
{ defaultValue: [] }
),
notify_when: schema.string({ validate: validateNotifyWhenType }),
});

const rewriteBodyReqActions: RewriteRequestCase<AlertAction> = ({
connector_type_id: actionTypeId,
...rest
}) => ({
actionTypeId,
...rest,
});

const rewriteBodyReq: RewriteRequestCase<UpdateOptions<AlertTypeParams>> = (result) => {
const { notify_when: notifyWhen, ...rest } = result.data;
return {
Expand Down Expand Up @@ -128,6 +137,7 @@ export const updateRuleRoute = (
data: {
...rule,
notify_when: rule.notify_when as AlertNotifyWhenType,
actions: rule.actions.map((action) => rewriteBodyReqActions(action)),
},
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,13 @@ describe('health check', () => {

it('renders children if keys are enabled', async () => {
useKibanaMock().services.http.get = jest.fn().mockResolvedValue({
isSufficientlySecure: true,
hasPermanentEncryptionKey: true,
is_sufficiently_secure: true,
has_permanent_encryption_key: true,
alerting_framework_heath: {
decryption_health: { status: 'ok', timestamp: '2021-04-01T21:29:22.991Z' },
execution_health: { status: 'ok', timestamp: '2021-04-01T21:29:22.991Z' },
read_health: { status: 'ok', timestamp: '2021-04-01T21:29:22.991Z' },
},
isAlertsAvailable: true,
});
const { queryByText } = render(
Expand All @@ -78,8 +83,13 @@ describe('health check', () => {

test('renders warning if TLS is required', async () => {
useKibanaMock().services.http.get = jest.fn().mockImplementation(async () => ({
isSufficientlySecure: false,
hasPermanentEncryptionKey: true,
is_sufficiently_secure: false,
has_permanent_encryption_key: true,
alerting_framework_heath: {
decryption_health: { status: 'ok', timestamp: '2021-04-01T21:29:22.991Z' },
execution_health: { status: 'ok', timestamp: '2021-04-01T21:29:22.991Z' },
read_health: { status: 'ok', timestamp: '2021-04-01T21:29:22.991Z' },
},
isAlertsAvailable: true,
}));
const { queryAllByText } = render(
Expand Down Expand Up @@ -110,8 +120,13 @@ describe('health check', () => {

test('renders warning if encryption key is ephemeral', async () => {
useKibanaMock().services.http.get = jest.fn().mockImplementation(async () => ({
isSufficientlySecure: true,
hasPermanentEncryptionKey: false,
is_sufficiently_secure: true,
has_permanent_encryption_key: false,
alerting_framework_heath: {
decryption_health: { status: 'ok', timestamp: '2021-04-01T21:29:22.991Z' },
execution_health: { status: 'ok', timestamp: '2021-04-01T21:29:22.991Z' },
read_health: { status: 'ok', timestamp: '2021-04-01T21:29:22.991Z' },
},
isAlertsAvailable: true,
}));
const { queryByText, queryByRole } = render(
Expand Down Expand Up @@ -139,8 +154,13 @@ describe('health check', () => {

test('renders warning if encryption key is ephemeral and keys are disabled', async () => {
useKibanaMock().services.http.get = jest.fn().mockImplementation(async () => ({
isSufficientlySecure: false,
hasPermanentEncryptionKey: false,
is_sufficiently_secure: false,
has_permanent_encryption_key: false,
alerting_framework_heath: {
decryption_health: { status: 'ok', timestamp: '2021-04-01T21:29:22.991Z' },
execution_health: { status: 'ok', timestamp: '2021-04-01T21:29:22.991Z' },
read_health: { status: 'ok', timestamp: '2021-04-01T21:29:22.991Z' },
},
isAlertsAvailable: true,
}));

Expand Down
Loading

0 comments on commit eac81e0

Please sign in to comment.