Skip to content

Commit

Permalink
add param validation test
Browse files Browse the repository at this point in the history
  • Loading branch information
jcger committed Oct 14, 2024
1 parent 7e6c06f commit 943f5c9
Showing 1 changed file with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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 { RulesClientContext } from '../../../../rules_client';
import { unmuteAll } from './unmute_all';
import { savedObjectsRepositoryMock } from '@kbn/core-saved-objects-api-server-mocks';

jest.mock('../../../../lib/retry_if_conflicts', () => ({
retryIfConflicts: (_: unknown, id: unknown, asyncFn: () => Promise<unknown>) => {
return asyncFn();
},
}));

jest.mock('../../../../rules_client/lib', () => ({
updateMetaAttributes: () => {},
}));

jest.mock('../../../../saved_objects', () => ({
partiallyUpdateRule: async () => {},
}));

const loggerErrorMock = jest.fn();
const getBulkMock = jest.fn();

const savedObjectsMock = savedObjectsRepositoryMock.create();
savedObjectsMock.get = jest.fn().mockReturnValue({
attributes: {
actions: [],
},
version: '9.0.0',
});

const context = {
logger: { error: loggerErrorMock },
getActionsClient: () => {
return {
getBulk: getBulkMock,
};
},
unsecuredSavedObjectsClient: savedObjectsMock,
authorization: { ensureAuthorized: async () => {} },
ruleTypeRegistry: {
ensureRuleTypeEnabled: () => {},
},
getUserName: async () => {},
} as unknown as RulesClientContext;

describe('validate unmuteAll parameters', () => {
afterEach(() => {
jest.clearAllMocks();
});

it('should not throw an error for valid params', () => {
const validParams = {
id: 'ble',
};

expect(() => unmuteAll(context, validParams)).not.toThrow();
expect(savedObjectsMock.get).toHaveBeenCalled();
});

it('should throw Boom.badRequest for invalid params', async () => {
const invalidParams = {
id: 22 as unknown as string, // type workaround to send wrong data validation
};

await expect(unmuteAll(context, invalidParams)).rejects.toThrowErrorMatchingInlineSnapshot(
`"Error validating unmute all parameters - [id]: expected value of type [string] but got [number]"`
);
});
});

0 comments on commit 943f5c9

Please sign in to comment.