[Cases] Handle undefined connectors config field (#120686) (#121524)

* Marking config as optional

* Filtering out preconfigured connectors

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>

Co-authored-by: Jonathan Buttner <56361221+jonathan-buttner@users.noreply.github.com>
This commit is contained in:
Kibana Machine 2021-12-17 11:02:40 -05:00 committed by GitHub
parent 8f130cf28a
commit e933e118aa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 117 additions and 8 deletions

View file

@ -0,0 +1,104 @@
/*
* 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 { CasesClientArgs } from '../types';
import { loggingSystemMock } from '../../../../../../src/core/server/mocks';
import { getConnectors } from './client';
import { actionsClientMock } from '../../../../actions/server/mocks';
import { ActionType } from '../../../../actions/common/types';
describe('client', () => {
describe('getConnectors', () => {
const logger = loggingSystemMock.createLogger();
const actionsClient = actionsClientMock.create();
const args = { actionsClient, logger } as unknown as CasesClientArgs;
const jiraType: ActionType = {
id: '.jira',
name: '1',
enabled: true,
enabledInConfig: true,
enabledInLicense: true,
minimumLicenseRequired: 'basic',
};
beforeEach(() => {
jest.clearAllMocks();
});
it('removes connectors without a config field defined', async () => {
actionsClient.listTypes.mockImplementation(async () => [jiraType]);
actionsClient.getAll.mockImplementation(async () => [
{
id: '1',
actionTypeId: '.jira',
name: '1',
isPreconfigured: false,
referencedByCount: 1,
},
]);
expect(await getConnectors(args)).toEqual([]);
});
it('removes connectors that are pre configured', async () => {
actionsClient.listTypes.mockImplementation(async () => [jiraType]);
actionsClient.getAll.mockImplementation(async () => [
{
id: '1',
actionTypeId: '.jira',
name: '1',
config: {},
isPreconfigured: true,
referencedByCount: 1,
},
]);
expect(await getConnectors(args)).toEqual([]);
});
it('includes connectors that have a config and are not pre configured', async () => {
actionsClient.listTypes.mockImplementation(async () => [
jiraType,
{
id: '.servicenow',
name: '2',
enabled: true,
enabledInConfig: true,
enabledInLicense: true,
minimumLicenseRequired: 'basic',
},
]);
const connectors = [
{
id: '1',
actionTypeId: '.jira',
name: '1',
config: {},
isPreconfigured: false,
referencedByCount: 1,
},
{
id: '2',
actionTypeId: '.servicenow',
name: '2',
config: {},
isPreconfigured: false,
referencedByCount: 1,
},
];
actionsClient.getAll.mockImplementation(async () => connectors);
expect(await getConnectors(args)).toEqual(connectors);
});
});
});

View file

@ -203,17 +203,10 @@ async function get(
}
}
async function getConnectors({
export async function getConnectors({
actionsClient,
logger,
}: CasesClientArgs): Promise<FindActionResult[]> {
const isConnectorSupported = (
action: FindActionResult,
actionTypes: Record<string, ActionType>
): boolean =>
SUPPORTED_CONNECTORS.includes(action.actionTypeId) &&
actionTypes[action.actionTypeId]?.enabledInLicense;
try {
const actionTypes = (await actionsClient.listTypes()).reduce(
(types, type) => ({ ...types, [type.id]: type }),
@ -228,6 +221,18 @@ async function getConnectors({
}
}
function isConnectorSupported(
action: FindActionResult,
actionTypes: Record<string, ActionType>
): boolean {
return (
SUPPORTED_CONNECTORS.includes(action.actionTypeId) &&
actionTypes[action.actionTypeId]?.enabledInLicense &&
action.config != null &&
!action.isPreconfigured
);
}
async function update(
configurationId: string,
req: CasesConfigurePatch,