Skip to content

Commit

Permalink
Marking config as optional
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-buttner committed Dec 7, 2021
1 parent d0f7d7c commit 3a8164b
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 7 deletions.
4 changes: 3 additions & 1 deletion x-pack/plugins/cases/common/api/connectors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ export * from './resilient';
export * from './mappings';
export * from './swimlane';

export type ActionConnector = ActionResult;
// This marks the config field as partial because preconfigured connectors will not have a config section
// see: https://github.com/elastic/kibana/issues/119696 for more details
export type ActionConnector = Omit<ActionResult, 'config'> & Partial<Pick<ActionResult, 'config'>>;
export type ActionTypeConnector = ActionType;

export const ConnectorFieldsRt = rt.union([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,14 @@ describe('ServiceNow validator', () => {

expect(connectorValidator(invalidConnector)).toBeFalsy();
});

test('it returns an error message if the connector does not have a config field', () => {
const { config, ...connectorWithoutConfig } = connector;
expect(connectorValidator(connectorWithoutConfig)).toBeFalsy();
});

test('it returns an error message if the connector config is undefined', () => {
expect(connectorValidator({ ...connector, config: undefined })).toBeFalsy();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ import { CaseActionConnector } from '../../types';
export const connectorValidator = (
connector: CaseActionConnector
): ReturnType<ValidationConfig['validator']> => {
const {
config: { usesTableApi },
} = connector;
if (usesTableApi) {
if (connector.config?.usesTableApi) {
return {
message: 'Deprecated connector',
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ describe('Swimlane validator', () => {
expect(connectorValidator(invalidConnector)).toEqual({ message: 'Invalid connector' });
});

test('it returns an error message if the connector config is undefined', () => {
const invalidConnector = {
...connector,
config: undefined,
};
expect(connectorValidator(invalidConnector)).toEqual({ message: 'Invalid connector' });
});

test('it returns an error message if the connector config is not present', () => {
const { config, ...invalidConnector } = connector;

expect(connectorValidator(invalidConnector)).toEqual({ message: 'Invalid connector' });
});

test.each([SwimlaneConnectorType.Cases, SwimlaneConnectorType.All])(
'it does not return an error message if the connector is of type %s',
(connectorType) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ export const connectorValidator = (
connector: CaseActionConnector
): ReturnType<ValidationConfig['validator']> => {
const {
config: { mappings, connectorType },
config: { mappings, connectorType } = { mappings: undefined, connectorType: undefined },
} = connector;

if (connectorType === SwimlaneConnectorType.Alerts || isAnyRequiredFieldNotSet(mappings)) {
return {
message: 'Invalid connector',
Expand Down
20 changes: 20 additions & 0 deletions x-pack/plugins/cases/public/components/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,26 @@ describe('Utils', () => {
expect(isDeprecatedConnector({ ...connector, actionTypeId: '.servicenow-sir' })).toBe(false);
});

it('returns false if the connector is .servicenow but does not have a config property', () => {
const { config, ...connectorWithoutConfig } = connector;
expect(
isDeprecatedConnector({
...connectorWithoutConfig,
actionTypeId: '.servicenow',
})
).toBe(false);
});

it('returns false if the connector is .servicenow but does not have a usesTableApi property', () => {
expect(
isDeprecatedConnector({
...connector,
actionTypeId: '.servicenow',
config: {},
})
).toBe(false);
});

it('returns true if the connector is .servicenow and the usesTableApi=true', () => {
expect(
isDeprecatedConnector({
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/cases/public/components/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const isDeprecatedConnector = (connector?: CaseActionConnector): boolean
* Import Set API (https://developer.servicenow.com/dev.do#!/reference/api/rome/rest/c_ImportSetAPI)
* A ServiceNow connector is considered deprecated if it uses the Table API.
*/
return !!connector.config.usesTableApi;
return !!connector.config?.usesTableApi;
}

return false;
Expand Down

0 comments on commit 3a8164b

Please sign in to comment.