Skip to content

Commit

Permalink
add tests for export by id
Browse files Browse the repository at this point in the history
  • Loading branch information
WafaaNasr committed Feb 1, 2023
1 parent f192914 commit 7b2c13e
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export const ImportDataModalComponent = ({
showToasterMessage({
importResponse,
exceptionsIncluded: showExceptionsCheckBox,
actionConnectorsIncluded: showActionConnectorsCheckBox && !!connectorsCount,
actionConnectorsIncluded: showActionConnectorsCheckBox,
successMessage,
errorMessage,
errorMessageDetailed: failedDetailed,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,7 @@ describe('getExportAll', () => {
exceptionsClient,
clients.savedObjectsClient,
logger,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
exporterMockWithConnector as any,
exporterMockWithConnector as never,
requestMock
);
const rulesJson = JSON.parse(exports.rulesNdjson);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { Readable } from 'stream';
import type { RulesErrors } from './get_export_by_object_ids';
import { getExportByObjectIds, getRulesFromObjects } from './get_export_by_object_ids';
import type { FindHit } from '../../../routes/__mocks__/request_responses';
Expand All @@ -29,7 +29,6 @@ const exceptionsClient = getExceptionListClientMock();
import type { loggingSystemMock } from '@kbn/core/server/mocks';
import { requestContextMock } from '../../../routes/__mocks__/request_context';

// TODO add tests for connectors
describe('get_export_by_object_ids', () => {
let logger: ReturnType<typeof loggingSystemMock.createLogger>;
const { clients } = requestContextMock.createTools();
Expand Down Expand Up @@ -164,6 +163,179 @@ describe('get_export_by_object_ids', () => {
actionConnectors: '',
});
});

test('it will export with rule and action connectors', async () => {
const rulesClient = rulesClientMock.create();
const result = getFindResultWithSingleHit();
const alert = {
...getRuleMock(getQueryRuleParams()),
actions: [
{
group: 'default',
id: '123',
params: {
message: 'Rule {{context.rule.name}} generated {{state.signals_count}} alerts',
},
actionTypeId: '.slack',
},
],
};

alert.params = {
...alert.params,
filters: [{ query: { match_phrase: { 'host.name': 'some-host' } } }],
threat: getThreatMock(),
meta: { someMeta: 'someField' },
timelineId: 'some-timeline-id',
timelineTitle: 'some-timeline-title',
};
result.data = [alert];
rulesClient.find.mockResolvedValue(result);
let eventCount = 0;
const readable = new Readable({
objectMode: true,
read() {
if (eventCount === 0) {
eventCount += 1;
return this.push({
id: 'cabc78e0-9031-11ed-b076-53cc4d57aaf1',
type: 'action',
updated_at: '2023-01-11T11:30:31.683Z',
created_at: '2023-01-11T11:30:31.683Z',
version: 'WzE2MDYsMV0=',
attributes: {
actionTypeId: '.slack',
name: 'slack',
isMissingSecrets: true,
config: {},
secrets: {},
},
references: [],
migrationVersion: { action: '8.3.0' },
coreMigrationVersion: '8.7.0',
});
}
if (eventCount === 1) {
eventCount += 1;
return this.push({
exportedCount: 1,
missingRefCount: 0,
missingReferences: [],
excludedObjectsCount: 0,
excludedObjects: [],
});
}
return this.push(null);
},
});
const objects = [{ rule_id: 'rule-1' }];
const exporterMockWithConnector = {
exportByObjects: () => jest.fn().mockReturnValueOnce(readable),

exportByTypes: jest.fn(),
};
const exports = await getExportByObjectIds(
rulesClient,
exceptionsClient,
clients.savedObjectsClient,
objects,
logger,
exporterMockWithConnector as never,
requestMock
);
const rulesJson = JSON.parse(exports.rulesNdjson);
const detailsJson = JSON.parse(exports.exportDetails);
const actionConnectorsJSON = JSON.parse(exports.actionConnectors);
expect(rulesJson).toEqual({
author: ['Elastic'],
actions: [
{
group: 'default',
id: '123',
params: {
message: 'Rule {{context.rule.name}} generated {{state.signals_count}} alerts',
},
action_type_id: '.slack',
},
],
building_block_type: 'default',
created_at: '2019-12-13T16:40:33.400Z',
updated_at: '2019-12-13T16:40:33.400Z',
created_by: 'elastic',
description: 'Detecting root and admin users',
enabled: true,
false_positives: [],
filters: [{ query: { match_phrase: { 'host.name': 'some-host' } } }],
from: 'now-6m',
id: '04128c15-0d1b-4716-a4c5-46997ac7f3bd',
immutable: false,
index: ['auditbeat-*', 'filebeat-*', 'packetbeat-*', 'winlogbeat-*'],
interval: '5m',
rule_id: 'rule-1',
language: 'kuery',
license: 'Elastic License',
output_index: '.siem-signals',
max_signals: 10000,
risk_score: 50,
risk_score_mapping: [],
name: 'Detect Root/Admin Users',
query: 'user.name: root or user.name: admin',
references: ['http://example.com', 'https://example.com'],
related_integrations: [],
required_fields: [],
setup: '',
timeline_id: 'some-timeline-id',
timeline_title: 'some-timeline-title',
meta: { someMeta: 'someField' },
severity: 'high',
severity_mapping: [],
updated_by: 'elastic',
tags: [],
to: 'now',
type: 'query',
threat: getThreatMock(),
throttle: 'rule',
note: '# Investigative notes',
version: 1,
exceptions_list: getListArrayMock(),
});
expect(detailsJson).toEqual({
exported_exception_list_count: 0,
exported_exception_list_item_count: 0,
exported_count: 2,
exported_rules_count: 1,
missing_exception_list_item_count: 0,
missing_exception_list_items: [],
missing_exception_lists: [],
missing_exception_lists_count: 0,
missing_rules: [],
missing_rules_count: 0,
excluded_action_connection_count: 0,
excluded_action_connections: [],
exported_action_connector_count: 1,
missing_action_connection_count: 0,
missing_action_connections: [],
});
expect(actionConnectorsJSON).toEqual({
attributes: {
actionTypeId: '.slack',
config: {},
isMissingSecrets: true,
name: 'slack',
secrets: {},
},
coreMigrationVersion: '8.7.0',
created_at: '2023-01-11T11:30:31.683Z',
id: 'cabc78e0-9031-11ed-b076-53cc4d57aaf1',
migrationVersion: {
action: '8.3.0',
},
references: [],
type: 'action',
updated_at: '2023-01-11T11:30:31.683Z',
version: 'WzE2MDYsMV0=',
});
});
});

describe('getRulesFromObjects', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ export const getExportDetailsNdjson = (
actionConnectorDetails?: DefaultActionConnectorDetails
): string => {
let exportedCount = rules.length;

// TODO check what will be the default
if (actionConnectorDetails != null)
exportedCount += actionConnectorDetails.exported_action_connector_count;
if (exceptionDetails != null)
Expand Down

0 comments on commit 7b2c13e

Please sign in to comment.