Skip to content

Commit

Permalink
fixed unit tesst and usage of ESO in actions client
Browse files Browse the repository at this point in the history
  • Loading branch information
gmmorris committed Aug 12, 2020
1 parent 1dbf2f2 commit e8748e9
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 41 deletions.
20 changes: 13 additions & 7 deletions x-pack/plugins/actions/server/actions_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from 'src/core/server';

import { i18n } from '@kbn/i18n';
import { omitBy, isUndefined } from 'lodash';
import { ActionTypeRegistry } from './action_type_registry';
import { validateConfig, validateSecrets, ActionExecutorContract } from './lib';
import {
Expand Down Expand Up @@ -150,8 +151,10 @@ export class ActionsClient {
'update'
);
}
const existingObject = await this.unsecuredSavedObjectsClient.get<RawAction>('action', id);
const { actionTypeId } = existingObject.attributes;
const { attributes, references, version } = await this.unsecuredSavedObjectsClient.get<
RawAction
>('action', id);
const { actionTypeId } = attributes;
const { name, config, secrets } = action;
const actionType = this.actionTypeRegistry.get(actionTypeId);
const validatedActionTypeConfig = validateConfig(actionType, config);
Expand All @@ -163,16 +166,19 @@ export class ActionsClient {
'action',
id,
{
...existingObject.attributes,
...attributes,
actionTypeId,
name,
config: validatedActionTypeConfig as SavedObjectAttributes,
secrets: validatedActionTypeSecrets as SavedObjectAttributes,
},
{
references: existingObject.references,
version: existingObject.version,
}
omitBy(
{
references,
version,
},
isUndefined
)
);

return {
Expand Down
93 changes: 61 additions & 32 deletions x-pack/plugins/alerts/server/alerts_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,27 +244,33 @@ describe('create()', () => {

test('creates an alert', async () => {
const data = getMockData();
const createdAttributes = {
...data,
alertTypeId: '123',
schedule: { interval: '10s' },
params: {
bar: true,
},
createdAt: '2019-02-12T21:01:22.479Z',
createdBy: 'elastic',
updatedBy: 'elastic',
muteAll: false,
mutedInstanceIds: [],
actions: [
{
group: 'default',
actionRef: 'action_0',
actionTypeId: 'test',
params: {
foo: true,
},
},
],
};
unsecuredSavedObjectsClient.create.mockResolvedValueOnce({
id: '1',
type: 'alert',
attributes: {
alertTypeId: '123',
schedule: { interval: '10s' },
params: {
bar: true,
},
createdAt: '2019-02-12T21:01:22.479Z',
actions: [
{
group: 'default',
actionRef: 'action_0',
actionTypeId: 'test',
params: {
foo: true,
},
},
],
},
attributes: createdAttributes,
references: [
{
name: 'action_0',
Expand All @@ -290,7 +296,7 @@ describe('create()', () => {
id: '1',
type: 'alert',
attributes: {
actions: [],
...createdAttributes,
scheduledTaskId: 'task-123',
},
references: [
Expand All @@ -316,16 +322,27 @@ describe('create()', () => {
},
],
"alertTypeId": "123",
"consumer": "bar",
"createdAt": 2019-02-12T21:01:22.479Z,
"createdBy": "elastic",
"enabled": true,
"id": "1",
"muteAll": false,
"mutedInstanceIds": Array [],
"name": "abc",
"params": Object {
"bar": true,
},
"schedule": Object {
"interval": "10s",
},
"scheduledTaskId": "task-123",
"tags": Array [
"foo",
],
"throttle": null,
"updatedAt": 2019-02-12T21:01:22.479Z,
"updatedBy": "elastic",
}
`);
expect(unsecuredSavedObjectsClient.create).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -414,14 +431,27 @@ describe('create()', () => {
},
],
"alertTypeId": "123",
"apiKey": null,
"apiKeyOwner": null,
"consumer": "bar",
"createdAt": "2019-02-12T21:01:22.479Z",
"createdBy": "elastic",
"enabled": true,
"muteAll": false,
"mutedInstanceIds": Array [],
"name": "abc",
"params": Object {
"bar": true,
},
"schedule": Object {
"interval": "10s",
},
"scheduledTaskId": "task-123",
"tags": Array [
"foo",
],
"throttle": null,
"updatedBy": "elastic",
}
`);
expect(unsecuredSavedObjectsClient.update.mock.calls[0][3]).toMatchInlineSnapshot(`
Expand All @@ -433,7 +463,6 @@ describe('create()', () => {
"type": "action",
},
],
"version": undefined,
}
`);
});
Expand Down Expand Up @@ -1589,7 +1618,7 @@ describe('disable()', () => {
describe('muteAll()', () => {
test('mutes an alert', async () => {
const alertsClient = new AlertsClient(alertsClientParams);
unsecuredSavedObjectsClient.get.mockResolvedValueOnce({
encryptedSavedObjects.getDecryptedAsInternalUser.mockResolvedValueOnce({
id: '1',
type: 'alert',
attributes: {
Expand Down Expand Up @@ -1635,7 +1664,7 @@ describe('muteAll()', () => {

describe('authorization', () => {
beforeEach(() => {
unsecuredSavedObjectsClient.get.mockResolvedValueOnce({
encryptedSavedObjects.getDecryptedAsInternalUser.mockResolvedValueOnce({
id: '1',
type: 'alert',
attributes: {
Expand Down Expand Up @@ -1690,7 +1719,7 @@ describe('muteAll()', () => {
describe('unmuteAll()', () => {
test('unmutes an alert', async () => {
const alertsClient = new AlertsClient(alertsClientParams);
unsecuredSavedObjectsClient.get.mockResolvedValueOnce({
encryptedSavedObjects.getDecryptedAsInternalUser.mockResolvedValueOnce({
id: '1',
type: 'alert',
attributes: {
Expand Down Expand Up @@ -1736,7 +1765,7 @@ describe('unmuteAll()', () => {

describe('authorization', () => {
beforeEach(() => {
unsecuredSavedObjectsClient.get.mockResolvedValueOnce({
encryptedSavedObjects.getDecryptedAsInternalUser.mockResolvedValueOnce({
id: '1',
type: 'alert',
attributes: {
Expand Down Expand Up @@ -1791,7 +1820,7 @@ describe('unmuteAll()', () => {
describe('muteInstance()', () => {
test('mutes an alert instance', async () => {
const alertsClient = new AlertsClient(alertsClientParams);
unsecuredSavedObjectsClient.get.mockResolvedValueOnce({
encryptedSavedObjects.getDecryptedAsInternalUser.mockResolvedValueOnce({
id: '1',
type: 'alert',
attributes: {
Expand Down Expand Up @@ -1825,7 +1854,7 @@ describe('muteInstance()', () => {

test('skips muting when alert instance already muted', async () => {
const alertsClient = new AlertsClient(alertsClientParams);
unsecuredSavedObjectsClient.get.mockResolvedValueOnce({
encryptedSavedObjects.getDecryptedAsInternalUser.mockResolvedValueOnce({
id: '1',
type: 'alert',
attributes: {
Expand All @@ -1845,7 +1874,7 @@ describe('muteInstance()', () => {

test('skips muting when alert is muted', async () => {
const alertsClient = new AlertsClient(alertsClientParams);
unsecuredSavedObjectsClient.get.mockResolvedValueOnce({
encryptedSavedObjects.getDecryptedAsInternalUser.mockResolvedValueOnce({
id: '1',
type: 'alert',
attributes: {
Expand All @@ -1866,7 +1895,7 @@ describe('muteInstance()', () => {

describe('authorization', () => {
beforeEach(() => {
unsecuredSavedObjectsClient.get.mockResolvedValueOnce({
encryptedSavedObjects.getDecryptedAsInternalUser.mockResolvedValueOnce({
id: '1',
type: 'alert',
attributes: {
Expand Down Expand Up @@ -1929,7 +1958,7 @@ describe('muteInstance()', () => {
describe('unmuteInstance()', () => {
test('unmutes an alert instance', async () => {
const alertsClient = new AlertsClient(alertsClientParams);
unsecuredSavedObjectsClient.get.mockResolvedValueOnce({
encryptedSavedObjects.getDecryptedAsInternalUser.mockResolvedValueOnce({
id: '1',
type: 'alert',
attributes: {
Expand Down Expand Up @@ -1966,7 +1995,7 @@ describe('unmuteInstance()', () => {

test('skips unmuting when alert instance not muted', async () => {
const alertsClient = new AlertsClient(alertsClientParams);
unsecuredSavedObjectsClient.get.mockResolvedValueOnce({
encryptedSavedObjects.getDecryptedAsInternalUser.mockResolvedValueOnce({
id: '1',
type: 'alert',
attributes: {
Expand All @@ -1986,7 +2015,7 @@ describe('unmuteInstance()', () => {

test('skips unmuting when alert is muted', async () => {
const alertsClient = new AlertsClient(alertsClientParams);
unsecuredSavedObjectsClient.get.mockResolvedValueOnce({
encryptedSavedObjects.getDecryptedAsInternalUser.mockResolvedValueOnce({
id: '1',
type: 'alert',
attributes: {
Expand All @@ -2007,7 +2036,7 @@ describe('unmuteInstance()', () => {

describe('authorization', () => {
beforeEach(() => {
unsecuredSavedObjectsClient.get.mockResolvedValueOnce({
encryptedSavedObjects.getDecryptedAsInternalUser.mockResolvedValueOnce({
id: '1',
type: 'alert',
attributes: {
Expand Down
5 changes: 3 additions & 2 deletions x-pack/plugins/alerts/server/alerts_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,10 @@ export class AlertsClient {
this.validateActions(alertType, data.actions);

const { references, actions } = await this.denormalizeActions(data.actions);
const encryptedAttributes = this.apiKeyAsAlertAttributes(createdAPIKey, username);
const rawAlert: RawAlert = {
...data,
...this.apiKeyAsAlertAttributes(createdAPIKey, username),
...encryptedAttributes,
actions,
createdBy: username,
updatedBy: username,
Expand Down Expand Up @@ -234,8 +235,8 @@ export class AlertsClient {
'alert',
createdAlert.id,
{
...rawAlert,
...createdAlert.attributes,
...encryptedAttributes,
scheduledTaskId: scheduledTask.id,
},
{
Expand Down

0 comments on commit e8748e9

Please sign in to comment.