Skip to content

Commit

Permalink
Fixed more issues from PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankHassanabad committed Jul 20, 2021
1 parent d0f8274 commit 2793a8f
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,28 @@ const createMockClients = () => ({
appClient: siemMock.createClient(),
});

/**
* Adds mocking to the interface so we don't have to cast everywhere
*/
type SecuritySolutionRequestHandlerContextMock = SecuritySolutionRequestHandlerContext & {
core: {
elasticsearch: {
client: {
asCurrentUser: {
updateByQuery: jest.Mock;
search: jest.Mock;
transport: {
request: jest.Mock;
};
};
};
};
};
};

const createRequestContextMock = (
clients: ReturnType<typeof createMockClients> = createMockClients()
) => {
): SecuritySolutionRequestHandlerContextMock => {
const coreContext = coreMock.createRequestHandlerContext();
return ({
alerting: { getAlertsClient: jest.fn(() => clients.alertsClient) },
Expand All @@ -39,7 +58,7 @@ const createRequestContextMock = (
},
licensing: clients.licensing,
securitySolution: { getAppClient: jest.fn(() => clients.appClient) },
} as unknown) as SecuritySolutionRequestHandlerContext;
} as unknown) as SecuritySolutionRequestHandlerContextMock;
};

const createTools = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ describe('read_privileges route', () => {
server = serverMock.create();
({ context } = requestContextMock.createTools());

// eslint-disable-next-line @typescript-eslint/no-explicit-any
(context.core.elasticsearch.client.asCurrentUser.transport.request as any).mockResolvedValue({
context.core.elasticsearch.client.asCurrentUser.transport.request.mockResolvedValue({
body: getMockPrivilegesResult(),
});

Expand Down Expand Up @@ -66,8 +65,7 @@ describe('read_privileges route', () => {
});

test('returns 500 when bad response from cluster', async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(context.core.elasticsearch.client.asCurrentUser.transport.request as any).mockResolvedValue(
context.core.elasticsearch.client.asCurrentUser.transport.request.mockResolvedValue(
elasticsearchClientMock.createErrorTransportRequestPromise(new Error('Test error'))
);
const response = await server.inject(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ describe('add_prepackaged_rules_route', () => {
errors: [],
});

// eslint-disable-next-line @typescript-eslint/no-explicit-any
(context.core.elasticsearch.client.asCurrentUser.search as any).mockResolvedValue(
context.core.elasticsearch.client.asCurrentUser.search.mockResolvedValue(
elasticsearchClientMock.createSuccessTransportRequestPromise({ _shards: { total: 1 } })
);
addPrepackedRulesRoute(server.router, createMockConfig(), securitySetup);
Expand Down Expand Up @@ -129,8 +128,7 @@ describe('add_prepackaged_rules_route', () => {

test('it returns a 400 if the index does not exist', async () => {
const request = addPrepackagedRulesRequest();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(context.core.elasticsearch.client.asCurrentUser.search as any).mockResolvedValueOnce(
context.core.elasticsearch.client.asCurrentUser.search.mockResolvedValueOnce(
elasticsearchClientMock.createSuccessTransportRequestPromise({ _shards: { total: 0 } })
);
const response = await server.inject(request, context);
Expand Down Expand Up @@ -185,8 +183,7 @@ describe('add_prepackaged_rules_route', () => {
});

test('catches errors if payloads cause errors to be thrown', async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(context.core.elasticsearch.client.asCurrentUser.search as any).mockResolvedValue(
context.core.elasticsearch.client.asCurrentUser.search.mockResolvedValue(
elasticsearchClientMock.createErrorTransportRequestPromise(new Error('Test error'))
);
const request = addPrepackagedRulesRequest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ describe('create_rules_bulk', () => {
clients.alertsClient.find.mockResolvedValue(getEmptyFindResult()); // no existing rules
clients.alertsClient.create.mockResolvedValue(getAlertMock(getQueryRuleParams())); // successful creation

// eslint-disable-next-line @typescript-eslint/no-explicit-any
(context.core.elasticsearch.client.asCurrentUser.search as any).mockResolvedValue(
context.core.elasticsearch.client.asCurrentUser.search.mockResolvedValue(
elasticsearchClientMock.createSuccessTransportRequestPromise({ _shards: { total: 1 } })
);
createRulesBulkRoute(server.router, ml);
Expand Down Expand Up @@ -88,8 +87,7 @@ describe('create_rules_bulk', () => {
});

it('returns an error object if the index does not exist', async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(context.core.elasticsearch.client.asCurrentUser.search as any).mockResolvedValueOnce(
context.core.elasticsearch.client.asCurrentUser.search.mockResolvedValueOnce(
elasticsearchClientMock.createSuccessTransportRequestPromise({ _shards: { total: 0 } })
);
const response = await server.inject(getReadBulkRequest(), context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ describe('create_rules', () => {
clients.alertsClient.create.mockResolvedValue(getAlertMock(getQueryRuleParams())); // creation succeeds
clients.savedObjectsClient.find.mockResolvedValue(getFindResultStatus()); // needed to transform

// eslint-disable-next-line @typescript-eslint/no-explicit-any
(context.core.elasticsearch.client.asCurrentUser.search as any).mockResolvedValue(
context.core.elasticsearch.client.asCurrentUser.search.mockResolvedValue(
elasticsearchClientMock.createSuccessTransportRequestPromise({ _shards: { total: 1 } })
);
createRulesRoute(server.router, ml);
Expand Down Expand Up @@ -106,8 +105,7 @@ describe('create_rules', () => {

describe('unhappy paths', () => {
test('it returns a 400 if the index does not exist', async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(context.core.elasticsearch.client.asCurrentUser.search as any).mockResolvedValueOnce(
context.core.elasticsearch.client.asCurrentUser.search.mockResolvedValueOnce(
elasticsearchClientMock.createSuccessTransportRequestPromise({ _shards: { total: 0 } })
);
const response = await server.inject(getCreateRequest(), context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ describe('import_rules_route', () => {

clients.alertsClient.find.mockResolvedValue(getEmptyFindResult()); // no extant rules

// eslint-disable-next-line @typescript-eslint/no-explicit-any
(context.core.elasticsearch.client.asCurrentUser.search as any).mockResolvedValue(
context.core.elasticsearch.client.asCurrentUser.search.mockResolvedValue(
elasticsearchClientMock.createSuccessTransportRequestPromise({ _shards: { total: 1 } })
);
importRulesRoute(server.router, config, ml);
Expand Down Expand Up @@ -128,8 +127,7 @@ describe('import_rules_route', () => {

test('returns an error if the index does not exist', async () => {
clients.appClient.getSignalsIndex.mockReturnValue('mockSignalsIndex');
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(context.core.elasticsearch.client.asCurrentUser.search as any).mockResolvedValueOnce(
context.core.elasticsearch.client.asCurrentUser.search.mockResolvedValueOnce(
elasticsearchClientMock.createSuccessTransportRequestPromise({ _shards: { total: 0 } })
);
const response = await server.inject(request, context);
Expand All @@ -142,8 +140,7 @@ describe('import_rules_route', () => {
});

test('returns an error when cluster throws error', async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(context.core.elasticsearch.client.asCurrentUser.search as any).mockResolvedValue(
context.core.elasticsearch.client.asCurrentUser.search.mockResolvedValue(
elasticsearchClientMock.createErrorTransportRequestPromise({
body: new Error('Test error'),
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ describe('set signal status', () => {
beforeEach(() => {
server = serverMock.create();
({ context } = requestContextMock.createTools());
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(context.core.elasticsearch.client.asCurrentUser.search as any).mockResolvedValue(
context.core.elasticsearch.client.asCurrentUser.search.mockResolvedValue(
elasticsearchClientMock.createSuccessTransportRequestPromise(
getSuccessfulSignalUpdateResponse()
)
Expand Down Expand Up @@ -58,8 +57,7 @@ describe('set signal status', () => {
});

test('catches error if asCurrentUser throws error', async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(context.core.elasticsearch.client.asCurrentUser.updateByQuery as any).mockResolvedValue(
context.core.elasticsearch.client.asCurrentUser.updateByQuery.mockResolvedValue(
elasticsearchClientMock.createErrorTransportRequestPromise(new Error('Test error'))
);
const response = await server.inject(getSetSignalStatusByQueryRequest(), context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ describe('query for signal', () => {
server = serverMock.create();
({ context } = requestContextMock.createTools());

// eslint-disable-next-line @typescript-eslint/no-explicit-any
(context.core.elasticsearch.client.asCurrentUser.search as any).mockResolvedValue(
context.core.elasticsearch.client.asCurrentUser.search.mockResolvedValue(
elasticsearchClientMock.createSuccessTransportRequestPromise(getEmptySignalsResponse())
);

Expand Down Expand Up @@ -71,8 +70,7 @@ describe('query for signal', () => {
});

test('catches error if query throws error', async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(context.core.elasticsearch.client.asCurrentUser.search as any).mockResolvedValue(
context.core.elasticsearch.client.asCurrentUser.search.mockResolvedValue(
elasticsearchClientMock.createErrorTransportRequestPromise(new Error('Test error'))
);
const response = await server.inject(getSignalsAggsQueryRequest(), context);
Expand Down

0 comments on commit 2793a8f

Please sign in to comment.