Skip to content

Commit

Permalink
[Security analytics] Update 2.7 tests (#1214)
Browse files Browse the repository at this point in the history
* refactored cypress tests for sap

Signed-off-by: Amardeepsingh Siglani <amardeep7194@gmail.com>

* updated tests

Signed-off-by: Amardeepsingh Siglani <amardeep7194@gmail.com>

* updated tests

Signed-off-by: Amardeepsingh Siglani <amardeep7194@gmail.com>

* updated tests

Signed-off-by: Amardeepsingh Siglani <amardeep7194@gmail.com>

* refactored intercepts

Signed-off-by: Amardeepsingh Siglani <amardeep7194@gmail.com>

* refactored tests

Signed-off-by: Amardeepsingh Siglani <amardeep7194@gmail.com>

* updated tests

Signed-off-by: Amardeepsingh Siglani <amardeep7194@gmail.com>

* fixed tests

Signed-off-by: Amardeepsingh Siglani <amardeep7194@gmail.com>

---------

Signed-off-by: Amardeepsingh Siglani <amardeep7194@gmail.com>
  • Loading branch information
amsiglan authored Apr 10, 2024
1 parent 4819ac4 commit a44ebdf
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
NODE_API,
OPENSEARCH_DASHBOARDS_URL,
} from '../../../utils/plugins/security-analytics-dashboards-plugin/constants';
import { setupIntercept } from '../../../utils/plugins/security-analytics-dashboards-plugin/helpers';

const testMappings = {
properties: {
Expand Down Expand Up @@ -42,12 +43,7 @@ const createDetector = (detectorName, dataSource, expectFailure) => {
.focus()
.realType(dataSource);

cy.intercept({
pathname: NODE_API.RULES_SEARCH,
query: {
prePackaged: 'true',
},
}).as('getSigmaRules');
setupIntercept(cy, NODE_API.RULES_SEARCH, 'getSigmaRules');

// Select threat detector type (Windows logs)
cy.get(`input[id="dns"]`).click({ force: true });
Expand Down Expand Up @@ -187,7 +183,7 @@ describe('Detectors', () => {
});

beforeEach(() => {
cy.intercept(NODE_API.SEARCH_DETECTORS).as('detectorsSearch');
setupIntercept(cy, NODE_API.SEARCH_DETECTORS, 'detectorsSearch');
// Visit Detectors page
cy.visit(`${OPENSEARCH_DASHBOARDS_URL}/detectors`);
cy.wait('@detectorsSearch').should('have.property', 'state', 'Complete');
Expand Down Expand Up @@ -220,6 +216,10 @@ describe('Detectors', () => {
.focus()
.realType(cypressIndexWindows)
.realPress('Enter');
cy.get(`[data-test-subj="define-detector-select-data-source"]`)
.find('input')
.blur();
cy.wait(1000);

cy.get('.euiCallOut')
.should('be.visible')
Expand Down Expand Up @@ -269,6 +269,17 @@ describe('Detectors', () => {
.realType(cypressIndexWindows)
.realPress('Enter');

// Select appropriate names to map fields to
for (let field_name in testMappings.properties) {
const mappedTo = testMappings.properties[field_name].path;

cy.contains('tr', field_name).within(() => {
cy.get(`[data-test-subj="detector-field-mappings-select"]`)
.click()
.type(mappedTo);
});
}

// Change detector scheduling
cy.get(`[data-test-subj="detector-schedule-number-select"]`)
.ospClear()
Expand Down Expand Up @@ -389,7 +400,7 @@ describe('Detectors', () => {

cy.get('.reviewFieldMappings').should('not.exist');

cy.intercept(NODE_API.MAPPINGS_VIEW).as('getMappingsView');
setupIntercept(cy, NODE_API.MAPPINGS_VIEW, 'getMappingsView', 'GET');

cy.get('table th').within(() => {
cy.get('button').first().click({ force: true });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
NODE_API,
OPENSEARCH_DASHBOARDS_URL,
} from '../../../utils/plugins/security-analytics-dashboards-plugin/constants';
import { setupIntercept } from '../../../utils/plugins/security-analytics-dashboards-plugin/helpers';

const uniqueId = Cypress._.random(0, 1e6);
const SAMPLE_RULE = {
Expand Down Expand Up @@ -159,18 +160,16 @@ const checkRulesFlyout = () => {
describe('Rules', () => {
before(() => cy.cleanUpTests());
beforeEach(() => {
cy.intercept({
pathname: NODE_API.RULES_SEARCH,
query: {
prePackaged: 'true',
},
}).as('rulesSearch');
setupIntercept(cy, NODE_API.RULES_SEARCH, 'rulesSearch');

// Visit Rules page
cy.visit(`${OPENSEARCH_DASHBOARDS_URL}/rules`);
cy.wait('@rulesSearch').should('have.property', 'state', 'Complete');

// Check that correct page is showing
cy.contains('Rules');
cy.url({ timeout: 60000 }).then(() => {
cy.contains('Rules').should('be.visible');
});
});

it('...can be created', () => {
Expand Down Expand Up @@ -236,9 +235,7 @@ describe('Rules', () => {
cy.get('[data-test-subj="rule_yaml_editor"]').contains(line)
);

cy.intercept({
url: NODE_API.RULES_BASE,
}).as('getRules');
setupIntercept(cy, NODE_API.RULES_BASE, 'getRules');

// Click "create" button
cy.get('[data-test-subj="submit_rule_form_button"]').click({
Expand Down Expand Up @@ -313,14 +310,6 @@ describe('Rules', () => {
});

it('...can be deleted', () => {
cy.intercept(`${NODE_API.RULES_SEARCH}?prePackaged=true`, {
delay: 5000,
}).as('getPrePackagedRules');

cy.intercept(`${NODE_API.RULES_SEARCH}?prePackaged=false`, {
delay: 5000,
}).as('getCustomRules');

cy.get(`input[placeholder="Search rules"]`).ospSearch(SAMPLE_RULE.name);

// Click the rule link to open the details flyout
Expand All @@ -345,13 +334,16 @@ describe('Rules', () => {
.click({ force: true })
);

cy.wait('@getCustomRules');
cy.wait('@getPrePackagedRules');
cy.wait(2000);

setupIntercept(cy, NODE_API.RULES_SEARCH, 'getRules');
cy.reload();

// Search for sample_detector, presumably deleted
// Search for the custom rule, presumably deleted
cy.wait('@getRules');
cy.wait(3000);
cy.get(`input[placeholder="Search rules"]`).ospSearch(SAMPLE_RULE.name);
// Click the rule link to open the details flyout
// The rule link should not exist
cy.get('tbody').contains(SAMPLE_RULE.name).should('not.exist');
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@

import {
DETECTOR_TRIGGER_TIMEOUT,
NODE_API,
OPENSEARCH_DASHBOARDS_URL,
NODE_API,
} from '../../../utils/plugins/security-analytics-dashboards-plugin/constants';
import sample_index_settings from '../../../fixtures/plugins/security-analytics-dashboards-plugin/sample_index_settings.json';
import sample_alias_mappings from '../../../fixtures/plugins/security-analytics-dashboards-plugin/sample_alias_mappings.json';
import sample_detector from '../../../fixtures/plugins/security-analytics-dashboards-plugin/sample_detector.json';
import sample_document from '../../../fixtures/plugins/security-analytics-dashboards-plugin/sample_document.json';
import { setupIntercept } from '../../../utils/plugins/security-analytics-dashboards-plugin/helpers';

const testIndex = 'sample_alerts_spec_cypress_test_index';
const testDetectorName = 'alerts_spec_cypress_test_detector';
Expand Down Expand Up @@ -79,7 +80,10 @@ describe('Alerts', () => {
cy.contains('Threat detectors');

// Filter table to only show the test detector
cy.get(`input[type="search"]`).type(`${testDetector.name}{enter}`);
cy.get(`input[type="search"]`)
.focus()
.type(`${testDetector.name}`)
.type(`{enter}`);

// Confirm detector was created
cy.get('tbody > tr').should(($tr) => {
Expand All @@ -98,16 +102,19 @@ describe('Alerts', () => {

beforeEach(() => {
// Visit Alerts table page
cy.intercept(NODE_API.SEARCH_DETECTORS).as('detectorsSearch');
setupIntercept(cy, NODE_API.SEARCH_DETECTORS, 'detectorsSearch');

// Visit Detectors page
cy.visit(`${OPENSEARCH_DASHBOARDS_URL}/alerts`);
cy.wait('@detectorsSearch').should('have.property', 'state', 'Complete');

// Wait for page to load
cy.contains('Security alerts');
cy.url({ timeout: 60000 }).then(() => {
cy.contains('Security alerts').should('be.visible');
});

// Filter table to only show alerts for the test detector
cy.get(`input[type="search"]`).type(`${testDetector.name}{enter}`);
cy.get(`input[type="search"]`).focus().type(`${testDetector.name}{enter}`);

// Adjust the date range picker to display alerts from today
cy.get(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import {
DETECTOR_TRIGGER_TIMEOUT,
NODE_API,
OPENSEARCH_DASHBOARDS_URL,
} from '../../../utils/plugins/security-analytics-dashboards-plugin/constants';
import sample_document from '../../../fixtures/plugins/security-analytics-dashboards-plugin/sample_document.json';
Expand Down Expand Up @@ -40,7 +39,9 @@ describe('Findings', () => {
cy.visit(`${OPENSEARCH_DASHBOARDS_URL}/findings`);

// Wait for page to load
cy.contains('Findings');
cy.url({ timeout: 60000 }).then(() => {
cy.contains('Findings').should('be.visible');
});
});

it('displays findings based on recently ingested data', () => {
Expand Down Expand Up @@ -148,50 +149,5 @@ describe('Findings', () => {
});
});

it('...can delete detector', () => {
// Visit Detectors page
cy.visit(`${OPENSEARCH_DASHBOARDS_URL}/detectors`);
cy.contains('Threat detectors');

// filter table to show only sample_detector findings
cy.get(`input[placeholder="Search threat detectors"]`).ospSearch(
'sample_detector'
);

// intercept detectors and rules requests
cy.intercept(NODE_API.SEARCH_DETECTORS).as('getDetector');
cy.intercept(`${NODE_API.RULES_SEARCH}?prePackaged=true`).as(
'getPrePackagedRules'
);
cy.intercept(`${NODE_API.RULES_SEARCH}?prePackaged=false`).as('getRules');

// Click on detector to be removed
cy.contains('sample_detector').click({ force: true });
cy.contains('Detector details');
cy.contains(sample_detector.name);

// wait for detector details to load before continuing
cy.wait(['@getDetector', '@getPrePackagedRules', '@getRules']).then(() => {
// Click "Actions" button, the click "Delete"
cy.get('button.euiButton')
.contains('Actions')
.click({ force: true })
.then(() => {
// Confirm arrival at detectors page
cy.get('[data-test-subj="editButton"]')
.contains('Delete')
.click({ force: true });

// Search for sample_detector, presumably deleted
cy.get(`input[placeholder="Search threat detectors"]`).ospSearch(
'sample_detector'
);

// Confirm sample_detector no longer exists
cy.contains('There are no existing detectors.');
});
});
});

after(() => cy.cleanUpTests());
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

export function setupIntercept(cy, url, interceptName, method = 'POST') {
const urlRegex = new RegExp(`.*${url}.*`);
cy.intercept(method, urlRegex).as(interceptName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

const { BASE_PATH } = require('../../base_constants');
const { BASE_PATH, BACKEND_BASE_PATH } = require('../../base_constants');
const { FEATURE_SYSTEM_INDICES, NODE_API } = require('./constants');

Cypress.Commands.add('createRule', (ruleJSON) => {
Expand Down Expand Up @@ -55,7 +55,7 @@ Cypress.Commands.add('deleteRule', (ruleName) => {
});

Cypress.Commands.add('deleteAllCustomRules', () => {
const url = `${BASE_PATH}/${FEATURE_SYSTEM_INDICES.CUSTOM_RULES_INDEX}`;
const url = `${BACKEND_BASE_PATH}/${FEATURE_SYSTEM_INDICES.CUSTOM_RULES_INDEX}`;
cy.request({
method: 'DELETE',
url: url,
Expand Down

0 comments on commit a44ebdf

Please sign in to comment.