Skip to content

Commit

Permalink
[Security Solution][Exceptions] Fix export toast text (#123307) (#12…
Browse files Browse the repository at this point in the history
…3326)

### Summary

Fix bug on rule export where toaster reports export total as being rule objects + exceptions objects, but should just be rule objects total. Adds cypress test.

(cherry picked from commit b2c9f10)

Co-authored-by: Yara Tercero <[email protected]>
  • Loading branch information
kibanamachine and yctercero authored Jan 19, 2022
1 parent d551f02 commit 3d11c35
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
waitForAlertsIndexToBeCreated,
waitForAlertsPanelToBeLoaded,
} from '../../tasks/alerts';
import { exportFirstRule } from '../../tasks/alerts_detection_rules';
import { exportFirstRule, getRulesImportExportToast } from '../../tasks/alerts_detection_rules';
import { createCustomRule } from '../../tasks/api_calls/rules';
import { cleanKibana } from '../../tasks/common';
import { loginAndWaitForPageWithoutDateRange } from '../../tasks/login';
Expand All @@ -36,6 +36,9 @@ describe('Export rules', () => {
exportFirstRule();
cy.wait('@export').then(({ response }) => {
cy.wrap(response?.body).should('eql', expectedExportedRule(this.ruleResponse));
getRulesImportExportToast([
'Successfully exported 1 of 1 rule. Prebuilt rules were excluded from the resulting file.',
]);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
waitForAlertsPanelToBeLoaded,
} from '../../tasks/alerts';
import {
getRulesImportToast,
getRulesImportExportToast,
importRules,
importRulesWithOverwriteAll,
} from '../../tasks/alerts_detection_rules';
Expand All @@ -35,7 +35,10 @@ describe('Import rules', () => {

cy.wait('@import').then(({ response }) => {
cy.wrap(response?.statusCode).should('eql', 200);
getRulesImportToast(['Successfully imported 1 rule', 'Successfully imported 2 exceptions.']);
getRulesImportExportToast([
'Successfully imported 1 rule',
'Successfully imported 2 exceptions.',
]);
});
});

Expand All @@ -51,7 +54,7 @@ describe('Import rules', () => {

cy.wait('@import').then(({ response }) => {
cy.wrap(response?.statusCode).should('eql', 200);
getRulesImportToast(['Failed to import 1 rule', 'Failed to import 2 exceptions']);
getRulesImportExportToast(['Failed to import 1 rule', 'Failed to import 2 exceptions']);
});
});

Expand All @@ -67,7 +70,10 @@ describe('Import rules', () => {

cy.wait('@import').then(({ response }) => {
cy.wrap(response?.statusCode).should('eql', 200);
getRulesImportToast(['Successfully imported 1 rule', 'Successfully imported 2 exceptions.']);
getRulesImportExportToast([
'Successfully imported 1 rule',
'Successfully imported 2 exceptions.',
]);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ export const importRules = (rulesFile: string) => {
cy.get(INPUT_FILE).should('not.exist');
};

export const getRulesImportToast = (headers: string[]) => {
export const getRulesImportExportToast = (headers: string[]) => {
cy.get(TOASTER)
.should('exist')
.then(($els) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { Query } from '@elastic/eui';
import { ExportRulesDetails } from '../../../../../../common/detection_engine/schemas/response/export_rules_details_schema';
import {
BulkRuleResponse,
RuleResponseBuckets,
Expand Down Expand Up @@ -71,16 +72,29 @@ export const getSearchFilters = ({

/**
* This function helps to parse NDJSON with exported rules
* and retrieve the number of successfully exported rules.
* and retrieve the metadata of exported rules.
*
* @param blob a Blob received from an _export endpoint
* @returns Number of exported rules
* @returns Export details
*/
export const getExportedRulesCount = async (blob: Blob): Promise<number> => {
export const getExportedRulesDetails = async (blob: Blob): Promise<ExportRulesDetails> => {
const blobContent = await blob.text();
// The Blob content is an NDJSON file, the last line of which contains export details.
const exportDetailsJson = blobContent.split('\n').filter(Boolean).slice(-1)[0];
const exportDetails = JSON.parse(exportDetailsJson);

return exportDetails.exported_count;
return exportDetails;
};

/**
* This function helps to parse NDJSON with exported rules
* and retrieve the number of successfully exported rules.
*
* @param blob a Blob received from an _export endpoint
* @returns Number of exported rules
*/
export const getExportedRulesCount = async (blob: Blob): Promise<number> => {
const details = await getExportedRulesDetails(blob);

return details.exported_rules_count;
};

0 comments on commit 3d11c35

Please sign in to comment.