Skip to content

Commit

Permalink
Make the test more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitaindik committed Jul 6, 2023
1 parent ec35816 commit 6cd5f2b
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,18 @@
import { cleanKibana, resetRulesTableState, deleteAlertsAndRules } from '../../tasks/common';
import { login, visitWithoutDateRange } from '../../tasks/login';
import { esArchiverResetKibana } from '../../tasks/es_archiver';
import { findRuleRowInTable } from '../../tasks/rule_snoozing';
import { expectRulesWithExecutionStatus, filterByExecutionStatus } from '../../tasks/rule_filters';
import {
expectRulesWithExecutionStatus,
filterByExecutionStatus,
expectNumberOfRulesShownOnPage,
} from '../../tasks/rule_filters';

import { SECURITY_DETECTIONS_RULES_URL } from '../../urls/navigation';

import {
RULES_MANAGEMENT_TABLE,
RULE_EXECUTION_STATUS,
} from '../../screens/alerts_detection_rules';

import { waitForRulesTableToBeLoaded } from '../../tasks/alerts_detection_rules';

import { createRule, waitForRulesToFinishExecution } from '../../tasks/api_calls/rules';
import { deleteIndex, createIndex, indexDocument } from '../../tasks/api_calls/elasticsearch';
import { deleteIndex, createIndex, createDocument } from '../../tasks/api_calls/elasticsearch';

import { getNewRule } from '../../objects/rule';

Expand All @@ -36,63 +34,72 @@ describe('Rule management filters', () => {
resetRulesTableState();
deleteAlertsAndRules();
esArchiverResetKibana();

deleteIndex('test_index');

createIndex('test_index', {
'@timestamp': {
type: 'date',
},
});

indexDocument('test_index', {});

createRule(
getNewRule({
name: 'Successful rule',
rule_id: 'successful_rule',
index: ['test_index'],
})
);

createRule(
getNewRule({
name: 'Warning rule',
rule_id: 'warning_rule',
index: ['non_existent_index'],
})
);

createRule(
getNewRule({
name: 'Failed rule',
rule_id: 'failed_rule',
index: ['test_index'],
// Setting a crazy large "Additional look-back time" to force a failure
from: 'now-9007199254746990s',
})
);

waitForRulesToFinishExecution(['successful_rule', 'warning_rule', 'failed_rule']);

visitWithoutDateRange(SECURITY_DETECTIONS_RULES_URL);

waitForRulesTableToBeLoaded();
});

describe('Last response filter', () => {
it('Filters rules by last response', () => {
findRuleRowInTable(RULES_MANAGEMENT_TABLE, 'Successful rule').should('exist');

cy.get(RULE_EXECUTION_STATUS).should('have.length', 3);

it('Filters rules by last response', function () {
deleteIndex('test_index');

createIndex('test_index', {
'@timestamp': {
type: 'date',
},
});

createDocument('test_index', {});

createRule(
getNewRule({
name: 'Successful rule',
rule_id: 'successful_rule',
index: ['test_index'],
})
);

createRule(
getNewRule({
name: 'Warning rule',
rule_id: 'warning_rule',
index: ['non_existent_index'],
})
);

createRule(
getNewRule({
name: 'Failed rule',
rule_id: 'failed_rule',
index: ['test_index'],
// Setting a crazy large "Additional look-back time" to force a failure
from: 'now-9007199254746990s',
})
);

waitForRulesToFinishExecution(['successful_rule', 'warning_rule', 'failed_rule'], new Date());

visitWithoutDateRange(SECURITY_DETECTIONS_RULES_URL);

waitForRulesTableToBeLoaded();

// Initial table state - before filtering by status
expectNumberOfRulesShownOnPage(3);
expectRulesWithExecutionStatus(1, 'Succeeded');
expectRulesWithExecutionStatus(1, 'Warning');
expectRulesWithExecutionStatus(1, 'Failed');

// Table state after filtering by Succeeded status
filterByExecutionStatus('Succeeded');
expectNumberOfRulesShownOnPage(1);
expectRulesWithExecutionStatus(1, 'Succeeded');

// Table state after filtering by Warning status
filterByExecutionStatus('Warning');
expectNumberOfRulesShownOnPage(1);
expectRulesWithExecutionStatus(1, 'Warning');

// Table state after filtering by Failed status
filterByExecutionStatus('Failed');
expectNumberOfRulesShownOnPage(1);
expectRulesWithExecutionStatus(1, 'Failed');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export const REFRESH_SETTINGS_SELECTION_NOTE = '[data-test-subj="refreshSettings

export const REFRESH_RULES_STATUS = '[data-test-subj="refreshRulesStatus"]';

export const RULE_EXECUTION_STATUS = '[data-test-subj="ruleExecutionStatus"]';
export const RULE_EXECUTION_STATUS_BADGE = '[data-test-subj="ruleExecutionStatus"]';

export const EXECUTION_STATUS_FILTER_BUTTON = '[data-test-subj="executionStatusFilterButton"]';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,20 @@ export const deleteIndex = (index: string) => {
};

export const createIndex = (indexName: string, properties: Record<string, unknown>) =>
cy.request({
rootRequest({
method: 'PUT',
url: `${Cypress.env('ELASTICSEARCH_URL')}/${indexName}`,
headers: { 'kbn-xsrf': 'cypress-creds' },
body: {
mappings: {
properties,
},
},
});

export const indexDocument = (indexName: string, document: Record<string, unknown>) =>
cy.request({
export const createDocument = (indexName: string, document: Record<string, unknown>) =>
rootRequest({
method: 'POST',
url: `${Cypress.env('ELASTICSEARCH_URL')}/${indexName}/_doc`,
headers: { 'kbn-xsrf': 'cypress-creds' },
body: document,
});

Expand Down
16 changes: 12 additions & 4 deletions x-pack/plugins/security_solution/cypress/tasks/api_calls/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,25 @@ export const importRule = (ndjsonPath: string) => {
});
};

export const waitForRulesToFinishExecution = (ruleIds: string[]) =>
export const waitForRulesToFinishExecution = (ruleIds: string[], afterDate?: Date) =>
cy.waitUntil(
() =>
rootRequest<FetchRulesResponse>({
method: 'GET',
url: DETECTION_ENGINE_RULES_URL_FIND,
}).then((response) => {
const areAllRulesFinished = ruleIds.every((ruleId) =>
response.body.data.some(
(rule) => rule.rule_id === ruleId && typeof rule.execution_summary !== 'undefined'
)
response.body.data.some((rule) => {
const ruleExecutionDate = rule.execution_summary?.last_execution?.date;

const isDateOk = afterDate
? !!(ruleExecutionDate && new Date(ruleExecutionDate) > afterDate)
: true;

return (
rule.rule_id === ruleId && typeof rule.execution_summary !== 'undefined' && isDateOk
);
})
);
return areAllRulesFinished;
}),
Expand Down
13 changes: 8 additions & 5 deletions x-pack/plugins/security_solution/cypress/tasks/rule_filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@
*/

import {
RULE_EXECUTION_STATUS,
RULE_NAME,
RULE_EXECUTION_STATUS_BADGE,
EXECUTION_STATUS_FILTER_BUTTON,
EXECUTION_STATUS_FILTER_OPTION,
} from '../screens/alerts_detection_rules';

export const expectRulesWithExecutionStatus = (expectedCount: number, status: string) => {
cy.get(`${RULE_EXECUTION_STATUS}:contains("${status}")`).should('have.length', expectedCount);
cy.get(`${RULE_EXECUTION_STATUS_BADGE}:contains("${status}")`).should(
'have.length',
expectedCount
);
};

export const expectNumberOfRulesShownOnPage = (expectedCount: number) =>
cy.get(RULE_EXECUTION_STATUS_BADGE).should('have.length', expectedCount);

export const filterByExecutionStatus = (status: string) => {
cy.get(EXECUTION_STATUS_FILTER_BUTTON).click();
cy.get(`${EXECUTION_STATUS_FILTER_OPTION}:contains("${status}")`).click();
cy.get(RULE_NAME).should('have.length', 1);
expectRulesWithExecutionStatus(1, status);
};

0 comments on commit 6cd5f2b

Please sign in to comment.