Skip to content

Commit

Permalink
Fix types and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
banderror committed Nov 1, 2021
1 parent a668b2c commit a4027e2
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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';

Expand Down Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export const deleteRulesBulkRoute = (
return transformValidateBulkError(
idOrRuleIdOrUnknown,
rule,
[ruleStatus],
ruleStatus,
isRuleRegistryEnabled
);
} catch (err) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 });
});
Expand All @@ -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',
Expand All @@ -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');
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ export const newTransformValidate = (
export const transformValidateBulkError = (
ruleId: string,
alert: PartialAlert<RuleParams>,
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({
Expand Down

0 comments on commit a4027e2

Please sign in to comment.