Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
YulNaumenko committed Oct 25, 2021
1 parent b3c4044 commit 0b78c4b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
47 changes: 47 additions & 0 deletions x-pack/plugins/actions/server/lib/action_executor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,53 @@ test('throws an error when config is invalid', async () => {
});
});

test('throws an error when connector is invalid', async () => {
const actionType: jest.Mocked<ActionType> = {
id: 'test',
name: 'Test',
minimumLicenseRequired: 'basic',
validate: {
config: schema.object({
param1: schema.string(),
}),
connector: schema.object({
config: schema.object({
param1: schema.string(),
}),
secrets: schema.object({
param2: schema.string(),
}),
}),
},
executor: jest.fn(),
};
const actionSavedObject = {
id: '1',
type: 'action',
attributes: {
actionTypeId: 'test',
},
references: [],
};
const actionResult = {
id: actionSavedObject.id,
name: actionSavedObject.id,
actionTypeId: actionSavedObject.attributes.actionTypeId,
isPreconfigured: false,
};
actionsClient.get.mockResolvedValueOnce(actionResult);
encryptedSavedObjectsClient.getDecryptedAsInternalUser.mockResolvedValueOnce(actionSavedObject);
actionTypeRegistry.get.mockReturnValueOnce(actionType);

const result = await actionExecutor.execute(executeParams);
expect(result).toEqual({
actionId: '1',
status: 'error',
retry: false,
message: `error validating action type config: [param1]: expected value of type [string] but got [undefined]`,
});
});

test('throws an error when params is invalid', async () => {
const actionType: jest.Mocked<ActionType> = {
id: 'test',
Expand Down
16 changes: 10 additions & 6 deletions x-pack/plugins/actions/server/lib/action_executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,21 @@ export class ActionExecutor {
let validatedSecrets: Record<string, unknown>;
try {
validatedParams = validateParams(actionType, params);

validatedConfig = validateConfig(actionType, config as Record<string, unknown>);
validatedSecrets = validateSecrets(actionType, secrets as Record<string, unknown>);
if (actionType.validate?.connector) {
const validateActionTypeConnector = validateConnector(actionType, {
config: config as Record<string, unknown>,
secrets: secrets as Record<string, unknown>,
});
validatedConfig = validateActionTypeConnector.config as Record<string, unknown>;
validatedSecrets = validateActionTypeConnector.secrets as Record<string, unknown>;
} else {
validatedConfig = validateConfig(actionType, config as Record<string, unknown>);
validatedSecrets = validateSecrets(actionType, secrets as Record<string, unknown>);
validatedConfig = {
...validatedConfig,
...(validateActionTypeConnector.config as Record<string, unknown>),
};
validatedSecrets = {
...validatedSecrets,
...(validateActionTypeConnector.secrets as Record<string, unknown>),
};
}
} catch (err) {
span?.setOutcome('failure');
Expand Down

0 comments on commit 0b78c4b

Please sign in to comment.