Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Cypress with security #165

Merged
merged 5 commits into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/cypress-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ on:
push:
branches:
- main
- cypress
bowenlan-amzn marked this conversation as resolved.
Show resolved Hide resolved

jobs:
tests:
Expand Down
3 changes: 2 additions & 1 deletion cypress.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"defaultCommandTimeout": 10000,
"env": {
"elasticsearch": "localhost:9200",
"kibana": "localhost:5601"
"kibana": "localhost:5601",
"security_enabled": false
}
}
14 changes: 5 additions & 9 deletions cypress/integration/rollups_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,11 @@ describe("Rollups", () => {
// Set welcome screen tracking to true
localStorage.setItem("home:welcome:show", "true");

// Go to home page
cy.visit(`${Cypress.env("kibana")}/app/home`);

// Click to add sample data
cy.get(`button[class="euiButton euiButton--primary homWelcome__footerAction euiButton--fill"]`)
.contains("Add data")
.click({ force: true });
// Go to sample data page
cy.visit(`${Cypress.env("kibana")}/app/home#/tutorial_directory/sampleData`);

// Click on "Sample data" tab
cy.contains("Sample data").click({ force: true });

// Load sample eCommerce data
cy.get(`button[data-test-subj="addSampleDataSetecommerce"]`).click({ force: true });

Expand Down Expand Up @@ -229,13 +223,15 @@ describe("Rollups", () => {
// Click into rollup job details page
cy.get(`[data-test-subj="rollupLink_${ROLLUP_ID}"]`).click({ force: true });

cy.contains(`${ROLLUP_ID}`);

// Click Disable button
cy.get(`[data-test-subj="disableButton"]`).click({ force: true });

// Confirm we get toaster saying rollup job is disabled
cy.contains(`${ROLLUP_ID} is disabled`);

// Click Disable button
// Click Enable button
cy.get(`[data-test-subj="enableButton"]`).click({ force: true });

// Confirm we get toaster saying rollup job is enabled
Expand Down
43 changes: 41 additions & 2 deletions cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* permissions and limitations under the License.
*/

const { API, INDEX } = require("./constants");
const { API, INDEX, ADMIN_AUTH } = require("./constants");

// ***********************************************
// This example commands.js shows you how to
Expand Down Expand Up @@ -41,8 +41,47 @@ const { API, INDEX } = require("./constants");
// -- This will overwrite an existing command --
// Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... })

Cypress.Commands.overwrite("visit", (originalFn, url, options) => {
// Add the basic auth header when security enabled in the Elasticsearch cluster
// https://github.com/cypress-io/cypress/issues/1288
if (Cypress.env("security_enabled")) {
if (options) {
options.auth = ADMIN_AUTH;
} else {
options = { auth: ADMIN_AUTH };
}
// Add query parameters - select the default Kibana tenant
options.qs = { security_tenant: "private" };
return originalFn(url, options);
} else {
return 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 Elasticsearch cluster
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;
}

return originalFn(Object.assign({}, defaults, options));
});

Cypress.Commands.add("deleteAllIndices", () => {
cy.request("DELETE", `${Cypress.env("elasticsearch")}/*`);
cy.request("DELETE", `${Cypress.env("elasticsearch")}/index*,sample*,kibana*`);
cy.request("DELETE", `${Cypress.env("elasticsearch")}/.opendistro-ism*?expand_wildcards=all`);
});

Expand Down
5 changes: 5 additions & 0 deletions cypress/support/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,8 @@ export const API = {
};

export const PLUGIN_NAME = "opendistro_index_management_kibana";

export const ADMIN_AUTH = {
username: "admin",
password: "admin",
};
5 changes: 5 additions & 0 deletions cypress/support/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@ import "./commands";

// Alternatively you can use CommonJS syntax:
// require('./commands')

// Switch the base URL of Elasticsearch when security enabled in the cluster
if (Cypress.env("security_enabled")) {
Cypress.env("elasticsearch", "https://localhost:9200");
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"@elastic/eslint-import-resolver-kibana": "link:../../packages/kbn-eslint-import-resolver-kibana",
"@types/react-dom": "^16.9.8",
"@types/react-router-dom": "^5.1.5",
"cypress": "4.3.0",
"cypress": "^6.0.0",
"eslint-plugin-no-unsanitized": "^3.0.2",
"eslint-plugin-prefer-object-spread": "^1.2.1",
"husky": "^3.0.0",
Expand Down