Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cases] Handle undefined connectors config field #120686

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions x-pack/plugins/cases/server/client/configure/client.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
21 changes: 13 additions & 8 deletions x-pack/plugins/cases/server/client/configure/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,17 +203,10 @@ async function get(
}
}

async function getConnectors({
export async function getConnectors({
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm exporting this so I can test it directly 😬 I could test via the createConfigurationSubClient but it's a little more difficult.

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 }),
Expand All @@ -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,
Expand Down