Skip to content

Commit

Permalink
Address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
xcrzx committed Jun 13, 2023
1 parent fd23ecc commit 27a5ed3
Show file tree
Hide file tree
Showing 36 changed files with 452 additions and 914 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import {
testAllTagsBadges,
testTagsBadge,
testMultipleSelectedRulesLabel,
loadPrebuiltDetectionRulesFromHeaderBtn,
filterByElasticRules,
clickErrorToastBtn,
unselectRuleByName,
Expand Down Expand Up @@ -90,7 +89,10 @@ import {
} from '../../objects/rule';

import { esArchiverResetKibana } from '../../tasks/es_archiver';
import { getAvailablePrebuiltRulesCount } from '../../tasks/api_calls/prebuilt_rules';
import {
getAvailablePrebuiltRulesCount,
excessivelyInstallAllPrebuiltRules,
} from '../../tasks/api_calls/prebuilt_rules';
import { setRowsPerPageTo } from '../../tasks/table_pagination';

const RULE_NAME = 'Custom rule for bulk actions';
Expand Down Expand Up @@ -151,7 +153,7 @@ describe('Detection rules, bulk edit', () => {
it('Only prebuilt rules selected', () => {
const expectedNumberOfSelectedRules = 10;

loadPrebuiltDetectionRulesFromHeaderBtn();
excessivelyInstallAllPrebuiltRules();

// select Elastic(prebuilt) rules, check if we can't proceed further, as Elastic rules are not editable
filterByElasticRules();
Expand All @@ -167,7 +169,7 @@ describe('Detection rules, bulk edit', () => {
});

it('Prebuilt and custom rules selected: user proceeds with custom rules editing', () => {
loadPrebuiltDetectionRulesFromHeaderBtn();
excessivelyInstallAllPrebuiltRules();

// modal window should show how many rules can be edit, how many not
selectAllRules();
Expand All @@ -190,7 +192,7 @@ describe('Detection rules, bulk edit', () => {
});

it('Prebuilt and custom rules selected: user cancels action', () => {
loadPrebuiltDetectionRulesFromHeaderBtn();
excessivelyInstallAllPrebuiltRules();

// modal window should show how many rules can be edit, how many not
selectAllRules();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import {
import {
waitForRulesTableToBeLoaded,
selectNumberOfRules,
loadPrebuiltDetectionRulesFromHeaderBtn,
goToEditRuleActionsSettingsOf,
} from '../../tasks/alerts_detection_rules';
import {
Expand All @@ -58,6 +57,7 @@ import {
getMachineLearningRule,
getNewTermsRule,
} from '../../objects/rule';
import { excessivelyInstallAllPrebuiltRules } from '../../tasks/api_calls/prebuilt_rules';

const ruleNameToAssert = 'Custom rule name with actions';
const expectedNumberOfCustomRulesToBeEdited = 7;
Expand Down Expand Up @@ -136,7 +136,7 @@ describe.skip('Detection rules, bulk edit of rule actions', () => {
throttleUnit: 'd',
};

loadPrebuiltDetectionRulesFromHeaderBtn();
excessivelyInstallAllPrebuiltRules();

// select both custom and prebuilt rules
selectNumberOfRules(expectedNumberOfRulesToBeEdited);
Expand Down Expand Up @@ -164,7 +164,7 @@ describe.skip('Detection rules, bulk edit of rule actions', () => {
});

it('Overwrite rule actions in rules', () => {
loadPrebuiltDetectionRulesFromHeaderBtn();
excessivelyInstallAllPrebuiltRules();

// select both custom and prebuilt rules
selectNumberOfRules(expectedNumberOfRulesToBeEdited);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
TOASTER,
} from '../../screens/alerts_detection_rules';
import {
loadPrebuiltDetectionRulesFromHeaderBtn,
filterByElasticRules,
selectNumberOfRules,
bulkExportRules,
Expand All @@ -32,7 +31,10 @@ import { cleanKibana, resetRulesTableState, deleteAlertsAndRules } from '../../t
import { login, visitWithoutDateRange } from '../../tasks/login';

import { DETECTIONS_RULE_MANAGEMENT_URL } from '../../urls/navigation';
import { getAvailablePrebuiltRulesCount } from '../../tasks/api_calls/prebuilt_rules';
import {
excessivelyInstallAllPrebuiltRules,
getAvailablePrebuiltRulesCount,
} from '../../tasks/api_calls/prebuilt_rules';

const EXPORTED_RULES_FILENAME = 'rules_export.ndjson';
const exceptionList = getExceptionList();
Expand Down Expand Up @@ -83,7 +85,7 @@ describe('Export rules', () => {
it('shows a modal saying that no rules can be exported if all the selected rules are prebuilt', function () {
const expectedElasticRulesCount = 7;

loadPrebuiltDetectionRulesFromHeaderBtn();
excessivelyInstallAllPrebuiltRules();

filterByElasticRules();
selectNumberOfRules(expectedElasticRulesCount);
Expand All @@ -97,7 +99,7 @@ describe('Export rules', () => {
it('exports only custom rules', function () {
const expectedNumberCustomRulesToBeExported = 1;

loadPrebuiltDetectionRulesFromHeaderBtn();
excessivelyInstallAllPrebuiltRules();

selectAllRules();
bulkExportRules();
Expand Down Expand Up @@ -149,7 +151,7 @@ describe('Export rules', () => {
// one rule with exception, one without it
const expectedNumberCustomRulesToBeExported = 2;

loadPrebuiltDetectionRulesFromHeaderBtn();
excessivelyInstallAllPrebuiltRules();

selectAllRules();
bulkExportRules();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,23 @@ import {
RULES_MANAGEMENT_TABLE,
RULE_SWITCH,
SELECT_ALL_RULES_ON_PAGE_CHECKBOX,
INSTALL_ALL_RULES_BUTTON,
} from '../../screens/alerts_detection_rules';
import {
confirmRulesDelete,
deleteFirstRule,
deleteSelectedRules,
disableSelectedRules,
enableSelectedRules,
loadPrebuiltDetectionRules,
reloadDeletedRules,
selectAllRules,
selectNumberOfRules,
waitForPrebuiltDetectionRulesToBeLoaded,
waitForRuleToUpdate,
} from '../../tasks/alerts_detection_rules';
import { getAvailablePrebuiltRulesCount } from '../../tasks/api_calls/prebuilt_rules';
import {
excessivelyInstallAllPrebuiltRules,
getAvailablePrebuiltRulesCount,
} from '../../tasks/api_calls/prebuilt_rules';
import { cleanKibana, deleteAlertsAndRules } from '../../tasks/common';
import { login, visitWithoutDateRange } from '../../tasks/login';
import { DETECTIONS_RULE_MANAGEMENT_URL } from '../../urls/navigation';
Expand All @@ -43,7 +45,8 @@ describe('Prebuilt rules', () => {
login();
deleteAlertsAndRules();
visitWithoutDateRange(DETECTIONS_RULE_MANAGEMENT_URL);
loadPrebuiltDetectionRules();
excessivelyInstallAllPrebuiltRules();
cy.reload();
waitForPrebuiltDetectionRulesToBeLoaded();
});

Expand Down Expand Up @@ -111,15 +114,19 @@ describe('Prebuilt rules', () => {
'have.text',
`Elastic rules (${expectedNumberOfRulesAfterDeletion})`
);
cy.get(LOAD_PREBUILT_RULES_ON_PAGE_HEADER_BTN).should('exist');
cy.get(LOAD_PREBUILT_RULES_ON_PAGE_HEADER_BTN).should(
'have.text',
'Install 1 Elastic prebuilt rule '
);
cy.get(LOAD_PREBUILT_RULES_ON_PAGE_HEADER_BTN).should('have.text', `Add Elastic rules1`);

reloadDeletedRules();
// Navigate to the prebuilt rule installation page
cy.get(LOAD_PREBUILT_RULES_ON_PAGE_HEADER_BTN).click();

cy.get(LOAD_PREBUILT_RULES_ON_PAGE_HEADER_BTN).should('not.exist');
// Click the "Install all rules" button
cy.get(INSTALL_ALL_RULES_BUTTON).click();

// Wait for the rules to be installed
cy.get(INSTALL_ALL_RULES_BUTTON).should('be.disabled');

// Navigate back to the rules page
cy.go('back');

cy.get(ELASTIC_RULES_BTN).should(
'have.text',
Expand All @@ -137,20 +144,28 @@ describe('Prebuilt rules', () => {
selectNumberOfRules(numberOfRulesToBeSelected);
deleteSelectedRules();

cy.get(LOAD_PREBUILT_RULES_ON_PAGE_HEADER_BTN).should('exist');
cy.get(LOAD_PREBUILT_RULES_ON_PAGE_HEADER_BTN).should(
'have.text',
`Install ${numberOfRulesToBeSelected} Elastic prebuilt rules `
`Add Elastic rules${numberOfRulesToBeSelected}`
);
cy.get(ELASTIC_RULES_BTN).should(
'have.text',
`Elastic rules (${expectedNumberOfRulesAfterDeletion})`
);

reloadDeletedRules();
// Navigate to the prebuilt rule installation page
cy.get(LOAD_PREBUILT_RULES_ON_PAGE_HEADER_BTN).click();

// Click the "Install all rules" button
cy.get(INSTALL_ALL_RULES_BUTTON).click();

// Wait for the rules to be installed
cy.get(INSTALL_ALL_RULES_BUTTON).should('be.disabled');

cy.get(LOAD_PREBUILT_RULES_ON_PAGE_HEADER_BTN).should('not.exist');
// Navigate back to the rules page
cy.go('back');

// Check that the rules table contains all rules
cy.get(ELASTIC_RULES_BTN).should(
'have.text',
`Elastic rules (${expectedNumberOfRulesAfterRecovering})`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import {
SELECT_ALL_RULES_ON_PAGE_CHECKBOX,
} from '../../screens/alerts_detection_rules';
import {
loadPrebuiltDetectionRules,
selectNumberOfRules,
unselectNumberOfRules,
waitForPrebuiltDetectionRulesToBeLoaded,
} from '../../tasks/alerts_detection_rules';
import { getAvailablePrebuiltRulesCount } from '../../tasks/api_calls/prebuilt_rules';
import {
excessivelyInstallAllPrebuiltRules,
getAvailablePrebuiltRulesCount,
} from '../../tasks/api_calls/prebuilt_rules';
import { cleanKibana } from '../../tasks/common';
import { login, visitWithoutDateRange } from '../../tasks/login';
import { DETECTIONS_RULE_MANAGEMENT_URL } from '../../urls/navigation';
Expand All @@ -29,7 +31,7 @@ describe.skip('Rules selection', () => {
});

it('should correctly update the selection label when rules are individually selected and unselected', () => {
loadPrebuiltDetectionRules();
excessivelyInstallAllPrebuiltRules();
waitForPrebuiltDetectionRulesToBeLoaded();

selectNumberOfRules(2);
Expand All @@ -42,7 +44,7 @@ describe.skip('Rules selection', () => {
});

it('should correctly update the selection label when rules are bulk selected and then bulk un-selected', () => {
loadPrebuiltDetectionRules();
excessivelyInstallAllPrebuiltRules();
waitForPrebuiltDetectionRulesToBeLoaded();

cy.get(SELECT_ALL_RULES_BTN).click();
Expand All @@ -63,7 +65,7 @@ describe.skip('Rules selection', () => {
});

it('should correctly update the selection label when rules are bulk selected and then unselected via the table select all checkbox', () => {
loadPrebuiltDetectionRules();
excessivelyInstallAllPrebuiltRules();
waitForPrebuiltDetectionRulesToBeLoaded();

cy.get(SELECT_ALL_RULES_BTN).click();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ export const LOAD_PREBUILT_RULES_BTN = '[data-test-subj="load-prebuilt-rules"]';

export const LOAD_PREBUILT_RULES_ON_PAGE_HEADER_BTN = '[data-test-subj="loadPrebuiltRulesBtn"]';

export const INSTALL_ALL_RULES_BUTTON = '[data-test-subj="installAllRulesButton"]';

export const RULES_TABLE_INITIAL_LOADING_INDICATOR =
'[data-test-subj="initialLoadingPanelAllRulesTable"]';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
DELETE_RULE_BULK_BTN,
RULES_SELECTED_TAG,
LOAD_PREBUILT_RULES_BTN,
LOAD_PREBUILT_RULES_ON_PAGE_HEADER_BTN,
RULES_TABLE_INITIAL_LOADING_INDICATOR,
RULES_TABLE_AUTOREFRESH_INDICATOR,
RULE_CHECKBOX,
Expand Down Expand Up @@ -72,7 +71,6 @@ import { EUI_CHECKBOX } from '../screens/common/controls';
import { ALL_ACTIONS } from '../screens/rule_details';
import { EDIT_SUBMIT_BUTTON } from '../screens/edit_rule';
import { LOADING_INDICATOR } from '../screens/security_header';
import { waitTillPrebuiltRulesReadyToInstall } from './api_calls/prebuilt_rules';

import { goToRuleEditSettings } from './rule_details';
import { goToActionsStepTab } from './create_new_rule';
Expand Down Expand Up @@ -243,32 +241,10 @@ export const goToTheRuleDetailsOf = (ruleName: string) => {
cy.contains(RULE_NAME, ruleName).click();
};

export const loadPrebuiltDetectionRules = () => {
cy.log('load prebuilt detection rules');
waitTillPrebuiltRulesReadyToInstall();
cy.get(LOAD_PREBUILT_RULES_BTN, { timeout: 300000 }).should('be.enabled');
cy.get(LOAD_PREBUILT_RULES_BTN).click();
cy.get(LOAD_PREBUILT_RULES_BTN).should('be.disabled');
};

/**
* load prebuilt rules by clicking button on page header
*/
export const loadPrebuiltDetectionRulesFromHeaderBtn = () => {
cy.log('load prebuilt detection rules from header');
waitTillPrebuiltRulesReadyToInstall();
cy.get(LOAD_PREBUILT_RULES_ON_PAGE_HEADER_BTN, { timeout: 300000 }).click();
cy.get(LOAD_PREBUILT_RULES_ON_PAGE_HEADER_BTN, { timeout: 300000 }).should('not.exist');
};

export const openIntegrationsPopover = () => {
cy.get(INTEGRATIONS_POPOVER).click();
};

export const reloadDeletedRules = () => {
cy.get(LOAD_PREBUILT_RULES_ON_PAGE_HEADER_BTN).click();
};

/**
* Selects the number of rules. Since there can be missing click handlers
* when the page loads at first, we use a pipe and a trigger of click
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ export const getPrebuiltRulesStatus = () => {
});
};

export const installAllPrebuiltRulesRequest = () => {
return cy.request({
method: 'POST',
url: 'internal/detection_engine/prebuilt_rules/installation/_perform',
headers: { 'kbn-xsrf': 'cypress-creds' },
body: {
mode: 'ALL_RULES',
},
});
};

export const getAvailablePrebuiltRulesCount = () => {
cy.log('Get prebuilt rules count');
return getPrebuiltRulesStatus().then(({ body }) => {
Expand All @@ -34,3 +45,16 @@ export const waitTillPrebuiltRulesReadyToInstall = () => {
{ interval: 2000, timeout: 60000 }
);
};

/**
* Install all prebuilt rules.
*
* This is a heavy request and should be used with caution. Most likely you
* don't need all prebuilt rules to be installed, crating just a few prebuilt
* rules should be enough for most cases.
*/
export const excessivelyInstallAllPrebuiltRules = () => {
cy.log('Install prebuilt rules (heavy request)');
waitTillPrebuiltRulesReadyToInstall();
installAllPrebuiltRulesRequest();
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,32 @@ import { filterJobs, getStablePatternTitles, searchFilter } from './helpers';

describe('helpers', () => {
describe('filterJobs', () => {
test('returns all jobs when no filter is suplied', () => {
test('returns all jobs when no filter is supplied', () => {
const filteredJobs = filterJobs({
jobs: mockSecurityJobs,
selectedGroups: [],
showCustomJobs: false,
showElasticJobs: false,
filterQuery: '',
});
expect(filteredJobs.length).toEqual(3);
expect(filteredJobs.length).toEqual(mockSecurityJobs.length);
});
});

describe('searchFilter', () => {
test('returns all jobs when nullfilterQuery is provided', () => {
test('returns all jobs when null filterQuery is provided', () => {
const jobsToDisplay = searchFilter(mockSecurityJobs);
expect(jobsToDisplay.length).toEqual(mockSecurityJobs.length);
});

test('returns correct DisplayJobs when filterQuery matches job.id', () => {
const jobsToDisplay = searchFilter(mockSecurityJobs, 'rare_process');
expect(jobsToDisplay.length).toEqual(2);
expect(jobsToDisplay.length).toEqual(3);
});

test('returns correct DisplayJobs when filterQuery matches job.description', () => {
const jobsToDisplay = searchFilter(mockSecurityJobs, 'Detect unusually');
expect(jobsToDisplay.length).toEqual(2);
expect(jobsToDisplay.length).toEqual(3);
});
});

Expand Down
Loading

0 comments on commit 27a5ed3

Please sign in to comment.