Skip to content

Commit

Permalink
rename console log action to server log
Browse files Browse the repository at this point in the history
- actually use the server log (passed in to executor)
- add some jest tests
  • Loading branch information
pmuellr committed May 30, 2019
1 parent bf223b4 commit 6e26161
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 31 deletions.
2 changes: 1 addition & 1 deletion x-pack/plugins/actions/server/action_type_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface Services {
log: (tags: string | string[], data?: string | object | (() => any), timestamp?: number) => void;
}

interface ExecutorOptions {
export interface ExecutorOptions {
actionTypeConfig: Record<string, any>;
params: Record<string, any>;
services: Services;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { ActionTypeService } from '../../action_type_service';

import { registerBuiltInActionTypes } from '../index';

const ACTION_TYPE_ID = 'kibana.server-log';
const NO_OP_FN = () => {};

const services = {
log: NO_OP_FN,
};

let actionTypeService: ActionTypeService;

beforeAll(() => {
actionTypeService = new ActionTypeService({ services });
registerBuiltInActionTypes(actionTypeService);
});

beforeEach(() => {
services.log = NO_OP_FN;
});

describe('action is registered', () => {
test('gets registered with builtin actions', () => {
expect(actionTypeService.has(ACTION_TYPE_ID)).toEqual(true);
});
});

describe('get()', () => {
test('returns action type', () => {
const actionType = actionTypeService.get(ACTION_TYPE_ID);
expect(actionType.id).toEqual(ACTION_TYPE_ID);
expect(actionType.name).toEqual('server-log');
});
});

describe('validateParams()', () => {
test('should validate and pass when params is valid', () => {
actionTypeService.validateParams(ACTION_TYPE_ID, { message: 'a message' });
actionTypeService.validateParams(ACTION_TYPE_ID, {
message: 'a message',
tags: ['info', 'blorg'],
});
});

test('should validate and throw error when params is invalid', () => {
expect(() => {
actionTypeService.validateParams(ACTION_TYPE_ID, {});
}).toThrowErrorMatchingInlineSnapshot(
`"child \\"message\\" fails because [\\"message\\" is required]"`
);

expect(() => {
actionTypeService.validateParams(ACTION_TYPE_ID, { message: 1 });
}).toThrowErrorMatchingInlineSnapshot(
`"child \\"message\\" fails because [\\"message\\" must be a string]"`
);

expect(() => {
actionTypeService.validateParams(ACTION_TYPE_ID, { message: 'x', tags: 2 });
}).toThrowErrorMatchingInlineSnapshot(
`"child \\"tags\\" fails because [\\"tags\\" must be an array]"`
);

expect(() => {
actionTypeService.validateParams(ACTION_TYPE_ID, { message: 'x', tags: [2] });
}).toThrowErrorMatchingInlineSnapshot(
`"child \\"tags\\" fails because [\\"tags\\" at position 0 fails because [\\"0\\" must be a string]]"`
);
});
});

describe('execute()', () => {
test('calls the executor with proper params', async () => {
const mockLog = jest.fn().mockResolvedValueOnce({ success: true });

services.log = mockLog;
await actionTypeService.execute({
id: ACTION_TYPE_ID,
actionTypeConfig: {},
params: { message: 'message text here', tags: ['tag1', 'tag2'] },
});
expect(mockLog).toMatchInlineSnapshot(`
[MockFunction] {
"calls": Array [
Array [
Array [
"tag1",
"tag2",
],
"message text here",
],
],
"results": Array [
Object {
"type": "return",
"value": Promise {},
},
],
}
`);
});
});
20 changes: 0 additions & 20 deletions x-pack/plugins/actions/server/builtin_action_types/console_log.ts

This file was deleted.

4 changes: 2 additions & 2 deletions x-pack/plugins/actions/server/builtin_action_types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

import { ActionTypeService } from '../action_type_service';

import { actionType as consoleLogActionType } from './console_log';
import { actionType as serverLogActionType } from '././server_log';

export function registerBuiltInActionTypes(actionTypeService: ActionTypeService) {
actionTypeService.register(consoleLogActionType);
actionTypeService.register(serverLogActionType);
}
34 changes: 34 additions & 0 deletions x-pack/plugins/actions/server/builtin_action_types/server_log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import Joi from 'joi';

import { ActionType, ExecutorOptions } from '../action_type_service';

const DEFAULT_TAGS = ['info', 'alerting'];

const PARAMS_SCHEMA = Joi.object().keys({
message: Joi.string().required(),
tags: Joi.array()
.items(Joi.string())
.optional()
.default(DEFAULT_TAGS),
});

export const actionType: ActionType = {
id: 'kibana.server-log',
name: 'server-log',
validate: {
params: PARAMS_SCHEMA,
},
executor,
};

async function executor({ params, services }: ExecutorOptions): Promise<any> {
const { message, tags } = params;

services.log(tags, message);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ import expect from '@kbn/expect';
import { KibanaFunctionalTestDefaultProviders } from '../../../../types/providers';

// eslint-disable-next-line import/no-default-export
export default function consoleLogTest({ getService }: KibanaFunctionalTestDefaultProviders) {
export default function serverLogTest({ getService }: KibanaFunctionalTestDefaultProviders) {
const supertest = getService('supertest');

describe('console_log', () => {
it('should return 200 when creating a builtin console-log action', async () => {
describe('create server-log action', () => {
it('should return 200 when creating a builtin server-log action', async () => {
await supertest
.post('/api/action')
.set('kbn-xsrf', 'foo')
.send({
attributes: {
description: 'A console.log action',
actionTypeId: 'kibana.console-log',
description: 'A server.log action',
actionTypeId: 'kibana.server-log',
actionTypeConfig: {},
},
})
Expand All @@ -30,8 +30,8 @@ export default function consoleLogTest({ getService }: KibanaFunctionalTestDefau
type: 'action',
id: resp.body.id,
attributes: {
description: 'A console.log action',
actionTypeId: 'kibana.console-log',
description: 'A server.log action',
actionTypeId: 'kibana.server-log',
actionTypeConfig: {},
},
references: [],
Expand Down
2 changes: 1 addition & 1 deletion x-pack/test/api_integration/apis/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ export default function alertingTests({ loadTestFile }: KibanaFunctionalTestDefa
loadTestFile(require.resolve('./get'));
loadTestFile(require.resolve('./list_action_types'));
loadTestFile(require.resolve('./update'));
loadTestFile(require.resolve('./builtin_action_types/console_log'));
loadTestFile(require.resolve('./builtin_action_types/server_log'));
});
}

0 comments on commit 6e26161

Please sign in to comment.