From 997ff9405a8d0509f9d6b0cb3daa8e0ffe2d98b9 Mon Sep 17 00:00:00 2001 From: Amardeepsingh Siglani Date: Fri, 8 Sep 2023 13:18:14 -0700 Subject: [PATCH] Updated cypress commands to avoid duplication (#835) * updated commands to avoid duplication Signed-off-by: Amardeepsingh Siglani * updated command header for create rule; changed url for searching rules Signed-off-by: Amardeepsingh Siglani * fix linter issues Signed-off-by: Amardeepsingh Siglani --------- Signed-off-by: Amardeepsingh Siglani --- .../1_detectors.spec.js | 7 +- .../commands.js | 80 ++----------------- .../constants.js | 2 +- .../index.d.ts | 10 +-- 4 files changed, 16 insertions(+), 83 deletions(-) diff --git a/cypress/integration/plugins/security-analytics-dashboards-plugin/1_detectors.spec.js b/cypress/integration/plugins/security-analytics-dashboards-plugin/1_detectors.spec.js index 997b56ab7..5f6b9306d 100644 --- a/cypress/integration/plugins/security-analytics-dashboards-plugin/1_detectors.spec.js +++ b/cypress/integration/plugins/security-analytics-dashboards-plugin/1_detectors.spec.js @@ -12,6 +12,7 @@ import sample_dns_index_settings from '../../../fixtures/plugins/security-analyt import dns_name_rule_data from '../../../fixtures/plugins/security-analytics-dashboards-plugin/integration_tests/rule/create_dns_rule_with_name_selection.json'; import dns_type_rule_data from '../../../fixtures/plugins/security-analytics-dashboards-plugin/integration_tests/rule/create_dns_rule_with_type_selection.json'; import _ from 'lodash'; +import { BACKEND_BASE_PATH } from '../../../utils/base_constants'; const cypressIndexDns = 'cypress-index-dns'; const cypressIndexWindows = 'cypress-index-windows'; @@ -258,14 +259,14 @@ describe('Detectors', () => { before(() => { cy.cleanUpTests(); - cy.createIndex(cypressIndexWindows, sample_windows_index_settings); + cy.sa_createIndex(cypressIndexWindows, sample_windows_index_settings); // Create test index - cy.createIndex(cypressIndexDns, sample_dns_index_settings).then(() => + cy.sa_createIndex(cypressIndexDns, sample_dns_index_settings).then(() => cy .request( 'POST', - '_plugins/_security_analytics/rules/_search?prePackaged=true', + `${BACKEND_BASE_PATH}${NODE_API.RULES_BASE}/_search?pre_packaged=true`, { from: 0, size: 5000, diff --git a/cypress/utils/plugins/security-analytics-dashboards-plugin/commands.js b/cypress/utils/plugins/security-analytics-dashboards-plugin/commands.js index d0de8fcfc..9e49f0e20 100644 --- a/cypress/utils/plugins/security-analytics-dashboards-plugin/commands.js +++ b/cypress/utils/plugins/security-analytics-dashboards-plugin/commands.js @@ -36,42 +36,10 @@ const { BACKEND_BASE_PATH } = require('../../base_constants'); // -- This will overwrite an existing command -- // Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... }) -// Be able to add default options to cy.request(), https://github.com/cypress-io/cypress/issues/726 -Cypress.Commands.overwrite('request', (originalFn, ...args) => { - let defaults = {}; - // Add the basic authentication header when security enabled in the Opensearch cluster - const ADMIN_AUTH = { - username: Cypress.env('username'), - password: Cypress.env('password'), - }; - if (Cypress.env('SECURITY_ENABLED')) { - defaults.auth = ADMIN_AUTH; - } - - let options = {}; - if (typeof args[0] === 'object' && args[0] !== null) { - options = Object.assign({}, args[0]); - } else if (args.length === 1) { - [options.url] = args; - } else if (args.length === 2) { - [options.method, options.url] = args; - } else if (args.length === 3) { - [options.method, options.url, options.body] = args; - } - - Object.assign(options, { - headers: { - 'osd-xsrf': '', - }, - }); - - return originalFn(Object.assign({}, defaults, options)); -}); - Cypress.Commands.add('cleanUpTests', () => { cy.deleteAllCustomRules(); cy.deleteAllDetectors(); - cy.deleteAllIndices(); + cy.sa_deleteAllIndices(); }); Cypress.Commands.add('getTableFirstRow', (selector) => { @@ -378,7 +346,7 @@ Cypress.Commands.add( } ); -Cypress.Commands.add('createIndex', (index, settings = {}) => { +Cypress.Commands.add('sa_createIndex', (index, settings = {}) => { cy.request('PUT', `${BACKEND_BASE_PATH}/${index}`, settings).should( 'have.property', 'status', @@ -386,14 +354,6 @@ Cypress.Commands.add('createIndex', (index, settings = {}) => { ); }); -Cypress.Commands.add('createIndexTemplate', (name, template) => { - cy.request( - 'PUT', - `${BACKEND_BASE_PATH}${NODE_API.INDEX_TEMPLATE_BASE}/${name}`, - template - ); -}); - Cypress.Commands.add('ingestDocument', (indexId, documentJSON) => { cy.request('POST', `${BACKEND_BASE_PATH}/${indexId}/_doc`, documentJSON); }); @@ -409,16 +369,7 @@ Cypress.Commands.add( } ); -Cypress.Commands.add('deleteIndex', (indexName, options = {}) => { - cy.request({ - method: 'DELETE', - url: `${BACKEND_BASE_PATH}/${indexName}`, - failOnStatusCode: false, - ...options, - }); -}); - -Cypress.Commands.add('deleteAllIndices', () => { +Cypress.Commands.add('sa_deleteAllIndices', () => { cy.request({ method: 'DELETE', url: `${BACKEND_BASE_PATH}/index*,sample*,opensearch_dashboards*,test*,cypress*`, @@ -435,6 +386,9 @@ Cypress.Commands.add('createRule', (ruleJSON) => { method: 'POST', url: `${OPENSEARCH_DASHBOARDS}${NODE_API.RULES_BASE}?category=${ruleJSON.category}`, body: JSON.stringify(ruleJSON), + headers: { + 'osd-xsrf': true, + }, }); }); @@ -526,25 +480,3 @@ Cypress.Commands.add( return cy.get(subject).wait(10).focus().realType(text); } ); - -Cypress.Commands.add( - 'pressEnterKey', - { - prevSubject: true, - }, - (subject) => { - Cypress.log({ - message: 'Enter key pressed', - }); - Cypress.automation('remote:debugger:protocol', { - command: 'Input.dispatchKeyEvent', - params: { - type: 'char', - unmodifiedText: '\r', - text: '\r', - }, - }); - - return subject; - } -); diff --git a/cypress/utils/plugins/security-analytics-dashboards-plugin/constants.js b/cypress/utils/plugins/security-analytics-dashboards-plugin/constants.js index a0f510043..3bdd9d238 100644 --- a/cypress/utils/plugins/security-analytics-dashboards-plugin/constants.js +++ b/cypress/utils/plugins/security-analytics-dashboards-plugin/constants.js @@ -82,7 +82,7 @@ export const createDetector = ( cy.cleanUpTests() // Create test index - .then(() => cy.createIndex(indexName, indexSettings)) + .then(() => cy.sa_createIndex(indexName, indexSettings)) // Create field mappings .then(() => diff --git a/cypress/utils/plugins/security-analytics-dashboards-plugin/index.d.ts b/cypress/utils/plugins/security-analytics-dashboards-plugin/index.d.ts index 1225c9c59..c4805c8cc 100644 --- a/cypress/utils/plugins/security-analytics-dashboards-plugin/index.d.ts +++ b/cypress/utils/plugins/security-analytics-dashboards-plugin/index.d.ts @@ -205,11 +205,11 @@ declare namespace Cypress { ospType(text: string): Chainable; /** - * Creates index with policy + * Creates index with optional settings * @example - * cy.createIndex("some_index", "some_policy") + * cy.sa_createIndex("some_index", settingObj) */ - createIndex(index: string, settings?: object): Chainable; + sa_createIndex(index: string, settings?: object): Chainable; /** * Creates an index template. @@ -222,9 +222,9 @@ declare namespace Cypress { /** * Deletes all indices in cluster * @example - * cy.deleteAllIndices() + * cy.sa_deleteAllIndices() */ - deleteAllIndices(): Chainable; + sa_deleteAllIndices(): Chainable; /** * Deletes all custom rules in cluster