Skip to content

Commit

Permalink
[SIEM] Adds Cypress 'Exports custom rule' test (elastic#64099) (elast…
Browse files Browse the repository at this point in the history
…ic#65233)

* changes default browser download folder

* adds 'exportRuleAction' data-test-subj attribute

* implements 'Exports a custom rule' test

* changes headless browser to chrome

* updates path

* updates 'EXPECTED_RULE_FILE_PATH'

* gives time to the file to be downloaded

* adds downloads folder

* updates download directory

* updates paths

* captures API call to check the content of the file

* removes browser launch hook since is not needed anymore

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
MadameSheema and elasticmachine authored May 5, 2020
1 parent 02ddfda commit 9f02c06
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import {
goToManageSignalDetectionRules,
waitForSignalsIndexToBeCreated,
waitForSignalsPanelToBeLoaded,
} from '../tasks/detections';
import { esArchiverLoad, esArchiverUnload } from '../tasks/es_archiver';
import { loginAndWaitForPageWithoutDateRange } from '../tasks/login';
import { exportFirstRule } from '../tasks/signal_detection_rules';

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

const EXPECTED_EXPORTED_RULE_FILE_PATH = 'cypress/test_files/expected_rules_export.ndjson';

describe('Export rules', () => {
before(() => {
esArchiverLoad('custom_rules');
cy.server();
cy.route(
'POST',
'**api/detection_engine/rules/_export?exclude_export_details=false&file_name=rules_export.ndjson*'
).as('export');
});

after(() => {
esArchiverUnload('custom_rules');
});

it('Exports a custom rule', () => {
loginAndWaitForPageWithoutDateRange(DETECTIONS);
waitForSignalsPanelToBeLoaded();
waitForSignalsIndexToBeCreated();
goToManageSignalDetectionRules();
exportFirstRule();
cy.wait('@export').then(xhr => {
cy.readFile(EXPECTED_EXPORTED_RULE_FILE_PATH).then($expectedExportedJson => {
cy.wrap(xhr.responseBody).should('eql', $expectedExportedJson);
});
});
});
});
1 change: 1 addition & 0 deletions x-pack/plugins/siem/cypress/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

// eslint-disable-next-line import/no-extraneous-dependencies
const wp = require('@cypress/webpack-preprocessor');

module.exports = on => {
const options = {
webpackOptions: {
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/siem/cypress/screens/signal_detection_rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export const DELETE_RULE_BULK_BTN = '[data-test-subj="deleteRuleBulk"]';

export const ELASTIC_RULES_BTN = '[data-test-subj="show-elastic-rules-filter-button"]';

export const EXPORT_ACTION_BTN = '[data-test-subj="exportRuleAction"]';

export const FIFTH_RULE = 4;

export const FIRST_RULE = 0;
Expand Down
5 changes: 5 additions & 0 deletions x-pack/plugins/siem/cypress/support/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,10 @@ Cypress.on('uncaught:exception', err => {
}
});

Cypress.on('window:before:load', win => {
win.fetch = null;
win.Blob = null;
});

// Alternatively you can use CommonJS syntax:
// require('./commands')
9 changes: 9 additions & 0 deletions x-pack/plugins/siem/cypress/tasks/signal_detection_rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
RULES_TABLE,
SORT_RULES_BTN,
THREE_HUNDRED_ROWS,
EXPORT_ACTION_BTN,
} from '../screens/signal_detection_rules';

export const activateRule = (rulePosition: number) => {
Expand All @@ -48,6 +49,14 @@ export const deleteSelectedRules = () => {
cy.get(DELETE_RULE_BULK_BTN).click();
};

export const exportFirstRule = () => {
cy.get(COLLAPSED_ACTION_BTN)
.first()
.click({ force: true });
cy.get(EXPORT_ACTION_BTN).click();
cy.get(EXPORT_ACTION_BTN).should('not.exist');
};

export const filterByCustomRules = () => {
cy.get(CUSTOM_RULES_BTN).click({ force: true });
cy.get(LOADING_SPINNER).should('exist');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{"actions":[],"created_at":"2020-03-26T10:09:07.569Z","updated_at":"2020-03-26T10:09:08.021Z","created_by":"elastic","description":"Rule 1","enabled":true,"false_positives":[],"filters":[],"from":"now-360s","id":"49db5bd1-bdd5-4821-be26-bb70a815dedb","immutable":false,"index":["apm-*-transaction*","auditbeat-*","endgame-*","filebeat-*","packetbeat-*","winlogbeat-*"],"interval":"5m","rule_id":"0cea4194-03f2-4072-b281-d31b72221d9d","language":"kuery","output_index":".siem-signals-default","max_signals":100,"risk_score":50,"name":"Rule 1","query":"host.name:*","references":[],"meta":{"from":"1m","throttle":"no_actions"},"severity":"low","updated_by":"elastic","tags":["rule1"],"to":"now","type":"query","threat":[],"throttle":"no_actions","version":1}
{"exported_count":1,"missing_rules":[],"missing_rules_count":0}
2 changes: 1 addition & 1 deletion x-pack/plugins/siem/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"extract-mitre-attacks": "node scripts/extract_tactics_techniques_mitre.js && node ../../../scripts/eslint ./public/pages/detection_engine/mitre/mitre_tactics_techniques.ts --fix",
"build-graphql-types": "node scripts/generate_types_from_graphql.js",
"cypress:open": "cypress open --config-file ./cypress/cypress.json",
"cypress:run": "cypress run --spec ./cypress/integration/**/*.spec.ts --config-file ./cypress/cypress.json --reporter ../../node_modules/cypress-multi-reporters --reporter-options configFile=./cypress/reporter_config.json; status=$?; ../../node_modules/.bin/mochawesome-merge --reportDir ../../../target/kibana-siem/cypress/results > ../../../target/kibana-siem/cypress/results/output.json; ../../../node_modules/.bin/marge ../../../target/kibana-siem/cypress/results/output.json --reportDir ../../../target/kibana-siem/cypress/results; mkdir -p ../../../target/junit && cp ../../../target/kibana-siem/cypress/results/*.xml ../../../target/junit/ && exit $status;",
"cypress:run": "cypress run --browser chrome --headless --spec ./cypress/integration/**/*.spec.ts --config-file ./cypress/cypress.json --reporter ../../node_modules/cypress-multi-reporters --reporter-options configFile=./cypress/reporter_config.json; status=$?; ../../node_modules/.bin/mochawesome-merge --reportDir ../../../target/kibana-siem/cypress/results > ../../../target/kibana-siem/cypress/results/output.json; ../../../node_modules/.bin/marge ../../../target/kibana-siem/cypress/results/output.json --reportDir ../../../target/kibana-siem/cypress/results; mkdir -p ../../../target/junit && cp ../../../target/kibana-siem/cypress/results/*.xml ../../../target/junit/ && exit $status;",
"cypress:run-as-ci": "node ../../../scripts/functional_tests --config ../../test/siem_cypress/config.ts"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export const getActions = (
},
},
{
'data-test-subj': 'exportRuleAction',
description: i18n.EXPORT_RULE,
type: 'icon',
icon: 'exportAction',
Expand Down

0 comments on commit 9f02c06

Please sign in to comment.