From a4027e2a89c83621d4b7258693fd50dd65227ec4 Mon Sep 17 00:00:00 2001 From: Georgii Gorbachev Date: Wed, 20 Oct 2021 05:40:45 +0200 Subject: [PATCH] Fix types and tests --- .../detection_engine/rules/api.test.ts | 2 +- .../routes/__mocks__/request_responses.ts | 9 ++++++- .../routes/rules/delete_rules_bulk_route.ts | 2 +- .../internal_find_rule_status_route.test.ts | 27 ++++++++++--------- .../routes/rules/patch_rules_bulk_route.ts | 2 +- .../routes/rules/update_rules_bulk_route.ts | 2 +- .../routes/rules/validate.test.ts | 6 ++--- .../detection_engine/routes/rules/validate.ts | 6 ++--- 8 files changed, 33 insertions(+), 23 deletions(-) diff --git a/x-pack/plugins/security_solution/public/detections/containers/detection_engine/rules/api.test.ts b/x-pack/plugins/security_solution/public/detections/containers/detection_engine/rules/api.test.ts index e5b9808bf1e46..e812f5d10e6e9 100644 --- a/x-pack/plugins/security_solution/public/detections/containers/detection_engine/rules/api.test.ts +++ b/x-pack/plugins/security_solution/public/detections/containers/detection_engine/rules/api.test.ts @@ -669,7 +669,7 @@ describe('Detections Rules API', () => { test('check parameter url, query', async () => { await getRuleStatusById({ id: 'mySuperRuleId', signal: abortCtrl.signal }); - expect(fetchMock).toHaveBeenCalledWith('/api/detection_engine/rules/_find_statuses', { + expect(fetchMock).toHaveBeenCalledWith('/internal/detection_engine/rules/_find_statuses', { body: '{"ids":["mySuperRuleId"]}', method: 'POST', signal: abortCtrl.signal, diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/routes/__mocks__/request_responses.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/routes/__mocks__/request_responses.ts index b78f0b5d64082..fa0bac82da08b 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/routes/__mocks__/request_responses.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/routes/__mocks__/request_responses.ts @@ -23,6 +23,7 @@ import { DETECTION_ENGINE_SIGNALS_FINALIZE_MIGRATION_URL, DETECTION_ENGINE_SIGNALS_MIGRATION_STATUS_URL, DETECTION_ENGINE_RULES_BULK_ACTION, + INTERNAL_DETECTION_ENGINE_RULE_STATUS_URL, } from '../../../../../common/constants'; import { RuleAlertType, @@ -43,7 +44,6 @@ import { getQueryRuleParams } from '../../schemas/rule_schemas.mock'; import { getPerformBulkActionSchemaMock } from '../../../../../common/detection_engine/schemas/request/perform_bulk_action_schema.mock'; import { RuleExecutionStatus } from '../../../../../common/detection_engine/schemas/common/schemas'; import { GetCurrentStatusBulkResult } from '../../rule_execution_log/types'; -import { ruleTypeMappings } from '../../signals/utils'; // eslint-disable-next-line no-restricted-imports import type { LegacyRuleNotificationAlertType } from '../../notifications/legacy_types'; @@ -233,6 +233,13 @@ export const ruleStatusRequest = () => body: { ids: ['04128c15-0d1b-4716-a4c5-46997ac7f3bd'] }, }); +export const internalRuleStatusRequest = () => + requestMock.create({ + method: 'post', + path: INTERNAL_DETECTION_ENGINE_RULE_STATUS_URL, + body: { ids: ['04128c15-0d1b-4716-a4c5-46997ac7f3bd'] }, + }); + export const getImportRulesRequest = (hapiStream?: HapiReadableStream) => requestMock.create({ method: 'post', diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/routes/rules/delete_rules_bulk_route.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/routes/rules/delete_rules_bulk_route.ts index 20bf9cc2f45cc..054238cf6fa45 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/routes/rules/delete_rules_bulk_route.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/routes/rules/delete_rules_bulk_route.ts @@ -92,7 +92,7 @@ export const deleteRulesBulkRoute = ( return transformValidateBulkError( idOrRuleIdOrUnknown, rule, - [ruleStatus], + ruleStatus, isRuleRegistryEnabled ); } catch (err) { diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/routes/rules/internal_find_rule_status_route.test.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/routes/rules/internal_find_rule_status_route.test.ts index 6aa0350e3e16b..f1ad41dfd4368 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/routes/rules/internal_find_rule_status_route.test.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/routes/rules/internal_find_rule_status_route.test.ts @@ -7,13 +7,13 @@ import { INTERNAL_DETECTION_ENGINE_RULE_STATUS_URL } from '../../../../../common/constants'; import { - ruleStatusRequest, + internalRuleStatusRequest, getAlertMock, getRuleExecutionStatusSucceeded, getRuleExecutionStatusFailed, } from '../__mocks__/request_responses'; import { serverMock, requestContextMock, requestMock } from '../__mocks__'; -import { findRulesStatusesRoute } from './find_rules_status_route'; +import { internalFindRuleStatusRoute } from './internal_find_rule_status_route'; import { RuleStatusResponse } from '../../rules/types'; import { AlertExecutionStatusErrorReasons } from '../../../../../../alerting/common'; import { getQueryRuleParams } from '../../schemas/rule_schemas.mock'; @@ -39,18 +39,18 @@ describe.each([ getAlertMock(isRuleRegistryEnabled, getQueryRuleParams()) ); - findRulesStatusesRoute(server.router); + internalFindRuleStatusRoute(server.router); }); describe('status codes with actionClient and alertClient', () => { test('returns 200 when finding a single rule status with a valid rulesClient', async () => { - const response = await server.inject(ruleStatusRequest(), context); + const response = await server.inject(internalRuleStatusRequest(), context); expect(response.status).toEqual(200); }); test('returns 404 if alertClient is not available on the route', async () => { context.alerting.getRulesClient = jest.fn(); - const response = await server.inject(ruleStatusRequest(), context); + const response = await server.inject(internalRuleStatusRequest(), context); expect(response.status).toEqual(404); expect(response.body).toEqual({ message: 'Not Found', status_code: 404 }); }); @@ -59,7 +59,7 @@ describe.each([ clients.ruleExecutionLogClient.getCurrentStatus.mockImplementation(async () => { throw new Error('Test error'); }); - const response = await server.inject(ruleStatusRequest(), context); + const response = await server.inject(internalRuleStatusRequest(), context); expect(response.status).toEqual(500); expect(response.body).toEqual({ message: 'Test error', @@ -85,13 +85,16 @@ describe.each([ ...failingExecutionRule, }); - const response = await server.inject(ruleStatusRequest(), context); - const body: RuleStatusResponse = response.body; + const request = internalRuleStatusRequest(); + const ruleId = request.body.ids[0]; + + const response = await server.inject(request, context); + const responseBody: RuleStatusResponse = response.body; + const ruleStatus = responseBody[ruleId].current_status; + expect(response.status).toEqual(200); - expect(body[ruleStatusRequest().body.ids[0]].current_status?.status).toEqual('failed'); - expect(body[ruleStatusRequest().body.ids[0]].current_status?.last_failure_message).toEqual( - 'Reason: read Message: oops' - ); + expect(ruleStatus?.status).toEqual('failed'); + expect(ruleStatus?.last_failure_message).toEqual('Reason: read Message: oops'); }); }); diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/routes/rules/patch_rules_bulk_route.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/routes/rules/patch_rules_bulk_route.ts index eb334e90e5fbd..838bfe63782c8 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/routes/rules/patch_rules_bulk_route.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/routes/rules/patch_rules_bulk_route.ts @@ -198,7 +198,7 @@ export const patchRulesBulkRoute = ( ruleId: rule.id, spaceId: context.securitySolution.getSpaceId(), }); - return transformValidateBulkError(rule.id, rule, [ruleStatus], isRuleRegistryEnabled); + return transformValidateBulkError(rule.id, rule, ruleStatus, isRuleRegistryEnabled); } else { return getIdBulkError({ id, ruleId }); } diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/routes/rules/update_rules_bulk_route.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/routes/rules/update_rules_bulk_route.ts index 000c87e5b3f9b..80b77722e79b0 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/routes/rules/update_rules_bulk_route.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/routes/rules/update_rules_bulk_route.ts @@ -99,7 +99,7 @@ export const updateRulesBulkRoute = ( ruleId: rule.id, spaceId: context.securitySolution.getSpaceId(), }); - return transformValidateBulkError(rule.id, rule, [ruleStatus], isRuleRegistryEnabled); + return transformValidateBulkError(rule.id, rule, ruleStatus, isRuleRegistryEnabled); } else { return getIdBulkError({ id: payloadRule.id, ruleId: payloadRule.rule_id }); } diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/routes/rules/validate.test.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/routes/rules/validate.test.ts index a7ba1ac77b7bf..032988bcca8be 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/routes/rules/validate.test.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/routes/rules/validate.test.ts @@ -8,7 +8,7 @@ import { transformValidate, transformValidateBulkError } from './validate'; import { BulkError } from '../utils'; import { RulesSchema } from '../../../../../common/detection_engine/schemas/response'; -import { getAlertMock, getRuleExecutionStatuses } from '../__mocks__/request_responses'; +import { getAlertMock, getRuleExecutionStatusSucceeded } from '../__mocks__/request_responses'; import { getListArrayMock } from '../../../../../common/detection_engine/schemas/types/lists.mock'; import { getThreatMock } from '../../../../../common/detection_engine/schemas/types/threat.mock'; import { getQueryRuleParams } from '../../schemas/rule_schemas.mock'; @@ -121,12 +121,12 @@ describe.each([ }); test('it should do a validation correctly of a rule id with ruleStatus passed in', () => { - const ruleStatuses = getRuleExecutionStatuses(); + const ruleStatus = getRuleExecutionStatusSucceeded(); const ruleAlert = getAlertMock(isRuleRegistryEnabled, getQueryRuleParams()); const validatedOrError = transformValidateBulkError( 'rule-1', ruleAlert, - ruleStatuses, + ruleStatus, isRuleRegistryEnabled ); const expected: RulesSchema = { diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/routes/rules/validate.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/routes/rules/validate.ts index f1c2eff1f898f..d4bb020cfb672 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/routes/rules/validate.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/routes/rules/validate.ts @@ -57,12 +57,12 @@ export const newTransformValidate = ( export const transformValidateBulkError = ( ruleId: string, alert: PartialAlert, - ruleStatus?: IRuleStatusSOAttributes[], + ruleStatus?: IRuleStatusSOAttributes, isRuleRegistryEnabled?: boolean ): RulesSchema | BulkError => { if (isAlertType(isRuleRegistryEnabled ?? false, alert)) { - if (ruleStatus && ruleStatus?.length > 0 && isRuleStatusSavedObjectAttributes(ruleStatus[0])) { - const transformed = transformAlertToRule(alert, ruleStatus[0]); + if (ruleStatus && isRuleStatusSavedObjectAttributes(ruleStatus)) { + const transformed = transformAlertToRule(alert, ruleStatus); const [validated, errors] = validateNonExact(transformed, rulesSchema); if (errors != null || validated == null) { return createBulkErrorObject({