Skip to content

Commit

Permalink
Updated cypress commands to avoid duplication (#835)
Browse files Browse the repository at this point in the history
* updated commands to avoid duplication

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

* updated command header for create rule; changed url for searching rules

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

* fix linter issues

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

---------

Signed-off-by: Amardeepsingh Siglani <amardeep7194@gmail.com>
  • Loading branch information
amsiglan authored Sep 8, 2023
1 parent 8fd59a8 commit 997ff94
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -378,22 +346,14 @@ 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',
200
);
});

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);
});
Expand All @@ -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*`,
Expand All @@ -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,
},
});
});

Expand Down Expand Up @@ -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;
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -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(() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,11 @@ declare namespace Cypress {
ospType(text: string): Chainable<any>;

/**
* 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<any>;
sa_createIndex(index: string, settings?: object): Chainable<any>;

/**
* Creates an index template.
Expand All @@ -222,9 +222,9 @@ declare namespace Cypress {
/**
* Deletes all indices in cluster
* @example
* cy.deleteAllIndices()
* cy.sa_deleteAllIndices()
*/
deleteAllIndices(): Chainable<any>;
sa_deleteAllIndices(): Chainable<any>;

/**
* Deletes all custom rules in cluster
Expand Down

0 comments on commit 997ff94

Please sign in to comment.