Skip to content

Commit

Permalink
Create unit test for registerBuiltInActionTypes and move common code (#…
Browse files Browse the repository at this point in the history
…45648)

* Create unit test for registerBuiltInActionTypes and move common code
  • Loading branch information
YulNaumenko authored Sep 13, 2019
1 parent 68ffe7e commit c6e7859
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,67 +9,34 @@ jest.mock('./lib/send_email', () => ({
}));

import { ActionType, ActionTypeExecutorOptions } from '../types';
import { ActionsConfigurationUtilities } from '../actions_config';
import { ActionTypeRegistry } from '../action_type_registry';
import { encryptedSavedObjectsMock } from '../../../encrypted_saved_objects/server/plugin.mock';
import { taskManagerMock } from '../../../task_manager/task_manager.mock';
import { validateParams, validateConfig, validateSecrets } from '../lib';
import { validateConfig, validateSecrets, validateParams } from '../lib';
import { SavedObjectsClientMock } from '../../../../../../src/core/server/mocks';
import { registerBuiltInActionTypes } from './index';
import { createActionTypeRegistry } from './index.test';
import { sendEmail } from './lib/send_email';
import { ActionParamsType, ActionTypeConfigType, ActionTypeSecretsType } from './email';

const sendEmailMock = sendEmail as jest.Mock;

const ACTION_TYPE_ID = '.email';
const NO_OP_FN = () => {};
const MOCK_KIBANA_CONFIG_UTILS: ActionsConfigurationUtilities = {
isWhitelistedHostname: _ => true,
isWhitelistedUri: _ => true,
ensureWhitelistedHostname: _ => {},
ensureWhitelistedUri: _ => {},
};

const services = {
log: NO_OP_FN,
callCluster: async (path: string, opts: any) => {},
savedObjectsClient: SavedObjectsClientMock.create(),
};

function getServices() {
return services;
}

let actionTypeRegistry: ActionTypeRegistry;
let actionType: ActionType;

const mockEncryptedSavedObjectsPlugin = encryptedSavedObjectsMock.create();

beforeAll(() => {
actionTypeRegistry = new ActionTypeRegistry({
getServices,
isSecurityEnabled: true,
taskManager: taskManagerMock.create(),
encryptedSavedObjectsPlugin: mockEncryptedSavedObjectsPlugin,
spaceIdToNamespace: jest.fn().mockReturnValue(undefined),
getBasePath: jest.fn().mockReturnValue(undefined),
});

registerBuiltInActionTypes(actionTypeRegistry, MOCK_KIBANA_CONFIG_UTILS);

const actionTypeRegistry = createActionTypeRegistry();
actionType = actionTypeRegistry.get(ACTION_TYPE_ID);
});

beforeEach(() => {
jest.resetAllMocks();
});

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

describe('actionTypeRegistry.get() works', () => {
test('action type static data is as expected', () => {
expect(actionType.id).toEqual(ACTION_TYPE_ID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,64 +9,31 @@ jest.mock('./lib/send_email', () => ({
}));

import { ActionType, ActionTypeExecutorOptions } from '../types';
import { ActionsConfigurationUtilities } from '../actions_config';
import { ActionTypeRegistry } from '../action_type_registry';
import { encryptedSavedObjectsMock } from '../../../encrypted_saved_objects/server/plugin.mock';
import { taskManagerMock } from '../../../task_manager/task_manager.mock';
import { validateConfig, validateParams } from '../lib';
import { SavedObjectsClientMock } from '../../../../../../src/core/server/mocks';
import { registerBuiltInActionTypes } from './index';
import { createActionTypeRegistry } from './index.test';
import { ActionParamsType, ActionTypeConfigType } from './es_index';

const ACTION_TYPE_ID = '.index';
const NO_OP_FN = () => {};
const MOCK_KIBANA_CONFIG_UTILS: ActionsConfigurationUtilities = {
isWhitelistedHostname: _ => true,
isWhitelistedUri: _ => true,
ensureWhitelistedHostname: _ => {},
ensureWhitelistedUri: _ => {},
};

const services = {
log: NO_OP_FN,
callCluster: jest.fn(),
savedObjectsClient: SavedObjectsClientMock.create(),
};

function getServices() {
return services;
}

let actionTypeRegistry: ActionTypeRegistry;
let actionType: ActionType;

const mockEncryptedSavedObjectsPlugin = encryptedSavedObjectsMock.create();

beforeAll(() => {
actionTypeRegistry = new ActionTypeRegistry({
getServices,
isSecurityEnabled: true,
taskManager: taskManagerMock.create(),
encryptedSavedObjectsPlugin: mockEncryptedSavedObjectsPlugin,
spaceIdToNamespace: jest.fn().mockReturnValue(undefined),
getBasePath: jest.fn().mockReturnValue(undefined),
});

registerBuiltInActionTypes(actionTypeRegistry, MOCK_KIBANA_CONFIG_UTILS);

const actionTypeRegistry = createActionTypeRegistry();
actionType = actionTypeRegistry.get(ACTION_TYPE_ID);
});

beforeEach(() => {
jest.resetAllMocks();
});

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

describe('actionTypeRegistry.get() works', () => {
test('action type static data is as expected', () => {
expect(actionType.id).toEqual(ACTION_TYPE_ID);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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 { ActionsConfigurationUtilities } from '../actions_config';
import { ActionTypeRegistry } from '../action_type_registry';
import { encryptedSavedObjectsMock } from '../../../encrypted_saved_objects/server/plugin.mock';
import { taskManagerMock } from '../../../task_manager/task_manager.mock';
import { SavedObjectsClientMock } from '../../../../../../src/core/server/mocks';
import { registerBuiltInActionTypes } from './index';

const ACTION_TYPE_IDS = ['.index', '.email', '.pagerduty', '.server-log', '.slack', '.webhook'];
const NO_OP_FN = () => {};
const MOCK_KIBANA_CONFIG_UTILS: ActionsConfigurationUtilities = {
isWhitelistedHostname: _ => true,
isWhitelistedUri: _ => true,
ensureWhitelistedHostname: _ => {},
ensureWhitelistedUri: _ => {},
};

const services = {
log: NO_OP_FN,
callCluster: jest.fn(),
savedObjectsClient: SavedObjectsClientMock.create(),
};

function getServices() {
return services;
}

const mockEncryptedSavedObjectsPlugin = encryptedSavedObjectsMock.create();

export function createActionTypeRegistry(): ActionTypeRegistry {
const actionTypeRegistry = new ActionTypeRegistry({
getServices,
isSecurityEnabled: true,
taskManager: taskManagerMock.create(),
encryptedSavedObjectsPlugin: mockEncryptedSavedObjectsPlugin,
spaceIdToNamespace: jest.fn().mockReturnValue(undefined),
getBasePath: jest.fn().mockReturnValue(undefined),
});
registerBuiltInActionTypes(actionTypeRegistry, MOCK_KIBANA_CONFIG_UTILS);
return actionTypeRegistry;
}

beforeEach(() => {
jest.resetAllMocks();
});

describe('action is registered', () => {
test('gets registered with builtin actions', () => {
const actionTypeRegistry = createActionTypeRegistry();
ACTION_TYPE_IDS.forEach(ACTION_TYPE_ID =>
expect(actionTypeRegistry.has(ACTION_TYPE_ID)).toEqual(true)
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,64 +9,33 @@ jest.mock('./lib/post_pagerduty', () => ({
}));

import { ActionType, Services, ActionTypeExecutorOptions } from '../types';
import { ActionsConfigurationUtilities } from '../actions_config';
import { ActionTypeRegistry } from '../action_type_registry';
import { taskManagerMock } from '../../../task_manager/task_manager.mock';
import { encryptedSavedObjectsMock } from '../../../encrypted_saved_objects/server/plugin.mock';
import { validateConfig, validateSecrets, validateParams } from '../lib';
import { SavedObjectsClientMock } from '../../../../../../src/core/server/mocks';
import { postPagerduty } from './lib/post_pagerduty';
import { registerBuiltInActionTypes } from './index';
import { createActionTypeRegistry } from './index.test';

const postPagerdutyMock = postPagerduty as jest.Mock;

const ACTION_TYPE_ID = '.pagerduty';
const NO_OP_FN = () => {};
const MOCK_KIBANA_CONFIG_UTILS: ActionsConfigurationUtilities = {
isWhitelistedHostname: _ => true,
isWhitelistedUri: _ => true,
ensureWhitelistedHostname: _ => {},
ensureWhitelistedUri: _ => {},
};

const services: Services = {
log: NO_OP_FN,
callCluster: async (path: string, opts: any) => {},
savedObjectsClient: SavedObjectsClientMock.create(),
};

function getServices(): Services {
return services;
}

let actionType: ActionType;
let actionTypeRegistry: ActionTypeRegistry;

const mockEncryptedSavedObjectsPlugin = encryptedSavedObjectsMock.create();

beforeAll(() => {
actionTypeRegistry = new ActionTypeRegistry({
getServices,
isSecurityEnabled: true,
taskManager: taskManagerMock.create(),
encryptedSavedObjectsPlugin: mockEncryptedSavedObjectsPlugin,
spaceIdToNamespace: jest.fn().mockReturnValue(undefined),
getBasePath: jest.fn().mockReturnValue(undefined),
});
registerBuiltInActionTypes(actionTypeRegistry, MOCK_KIBANA_CONFIG_UTILS);
const actionTypeRegistry = createActionTypeRegistry();
actionType = actionTypeRegistry.get(ACTION_TYPE_ID);
});

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

describe('action registation', () => {
test('should be successful', () => {
expect(actionTypeRegistry.has(ACTION_TYPE_ID)).toEqual(true);
});
});

describe('get()', () => {
test('should return correct action type', () => {
expect(actionType.id).toEqual(ACTION_TYPE_ID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,76 +5,39 @@
*/

import { ActionType, Services } from '../types';
import { ActionsConfigurationUtilities } from '../actions_config';
import { ActionTypeRegistry } from '../action_type_registry';
import { taskManagerMock } from '../../../task_manager/task_manager.mock';
import { encryptedSavedObjectsMock } from '../../../encrypted_saved_objects/server/plugin.mock';
import { validateParams } from '../lib';
import { SavedObjectsClientMock } from '../../../../../../src/core/server/mocks';

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

const ACTION_TYPE_ID = '.server-log';
const NO_OP_FN = () => {};
const MOCK_KIBANA_CONFIG_UTILS: ActionsConfigurationUtilities = {
isWhitelistedHostname: _ => true,
isWhitelistedUri: _ => true,
ensureWhitelistedHostname: _ => {},
ensureWhitelistedUri: _ => {},
};

const services: Services = {
log: NO_OP_FN,
callCluster: async (path: string, opts: any) => {},
savedObjectsClient: SavedObjectsClientMock.create(),
};

function getServices(): Services {
return services;
}

let actionTypeRegistry: ActionTypeRegistry;

const mockEncryptedSavedObjectsPlugin = encryptedSavedObjectsMock.create();
let actionType: ActionType;

beforeAll(() => {
actionTypeRegistry = new ActionTypeRegistry({
getServices,
isSecurityEnabled: true,
taskManager: taskManagerMock.create(),
encryptedSavedObjectsPlugin: mockEncryptedSavedObjectsPlugin,
spaceIdToNamespace: jest.fn().mockReturnValue(undefined),
getBasePath: jest.fn().mockReturnValue(undefined),
});
registerBuiltInActionTypes(actionTypeRegistry, MOCK_KIBANA_CONFIG_UTILS);
const actionTypeRegistry = createActionTypeRegistry();
actionType = actionTypeRegistry.get(ACTION_TYPE_ID);
expect(actionType).toBeTruthy();
});

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

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

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

describe('validateParams()', () => {
let actionType: ActionType;

beforeAll(() => {
actionType = actionTypeRegistry.get(ACTION_TYPE_ID);
expect(actionType).toBeTruthy();
});

test('should validate and pass when params is valid', () => {
expect(validateParams(actionType, { message: 'a message' })).toEqual({
message: 'a message',
Expand Down Expand Up @@ -123,7 +86,6 @@ describe('execute()', () => {
const mockLog = jest.fn().mockResolvedValueOnce({ success: true });

services.log = mockLog;
const actionType = actionTypeRegistry.get(ACTION_TYPE_ID);
const id = 'some-id';
await actionType.executor({
id,
Expand Down
Loading

0 comments on commit c6e7859

Please sign in to comment.