Skip to content

Commit 57a4336

Browse files
Cypress with security (#165)
* upgrade yarn to ^6.0.0 * with security
1 parent d4ba276 commit 57a4336

File tree

7 files changed

+295
-273
lines changed

7 files changed

+295
-273
lines changed

cypress.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"defaultCommandTimeout": 10000,
55
"env": {
66
"elasticsearch": "localhost:9200",
7-
"kibana": "localhost:5601"
7+
"kibana": "localhost:5601",
8+
"security_enabled": false
89
}
910
}

cypress/integration/rollups_spec.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,11 @@ describe("Rollups", () => {
2323
// Set welcome screen tracking to true
2424
localStorage.setItem("home:welcome:show", "true");
2525

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

3429
// Click on "Sample data" tab
3530
cy.contains("Sample data").click({ force: true });
36-
3731
// Load sample eCommerce data
3832
cy.get(`button[data-test-subj="addSampleDataSetecommerce"]`).click({ force: true });
3933

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

226+
cy.contains(`${ROLLUP_ID}`);
227+
232228
// Click Disable button
233229
cy.get(`[data-test-subj="disableButton"]`).click({ force: true });
234230

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

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

241237
// Confirm we get toaster saying rollup job is enabled

cypress/support/commands.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* permissions and limitations under the License.
1414
*/
1515

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

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

44+
Cypress.Commands.overwrite("visit", (originalFn, url, options) => {
45+
// Add the basic auth header when security enabled in the Elasticsearch cluster
46+
// https://github.com/cypress-io/cypress/issues/1288
47+
if (Cypress.env("security_enabled")) {
48+
if (options) {
49+
options.auth = ADMIN_AUTH;
50+
} else {
51+
options = { auth: ADMIN_AUTH };
52+
}
53+
// Add query parameters - select the default Kibana tenant
54+
options.qs = { security_tenant: "private" };
55+
return originalFn(url, options);
56+
} else {
57+
return originalFn(url, options);
58+
}
59+
});
60+
61+
// Be able to add default options to cy.request(), https://github.com/cypress-io/cypress/issues/726
62+
Cypress.Commands.overwrite("request", (originalFn, ...args) => {
63+
let defaults = {};
64+
// Add the basic authentication header when security enabled in the Elasticsearch cluster
65+
if (Cypress.env("security_enabled")) {
66+
defaults.auth = ADMIN_AUTH;
67+
}
68+
69+
let options = {};
70+
if (typeof args[0] === "object" && args[0] !== null) {
71+
options = Object.assign({}, args[0]);
72+
} else if (args.length === 1) {
73+
[options.url] = args;
74+
} else if (args.length === 2) {
75+
[options.method, options.url] = args;
76+
} else if (args.length === 3) {
77+
[options.method, options.url, options.body] = args;
78+
}
79+
80+
return originalFn(Object.assign({}, defaults, options));
81+
});
82+
4483
Cypress.Commands.add("deleteAllIndices", () => {
45-
cy.request("DELETE", `${Cypress.env("elasticsearch")}/*`);
84+
cy.request("DELETE", `${Cypress.env("elasticsearch")}/index*,sample*,kibana*`);
4685
cy.request("DELETE", `${Cypress.env("elasticsearch")}/.opendistro-ism*?expand_wildcards=all`);
4786
});
4887

cypress/support/constants.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,8 @@ export const API = {
3131
};
3232

3333
export const PLUGIN_NAME = "opendistro_index_management_kibana";
34+
35+
export const ADMIN_AUTH = {
36+
username: "admin",
37+
password: "admin",
38+
};

cypress/support/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,8 @@ import "./commands";
3333

3434
// Alternatively you can use CommonJS syntax:
3535
// require('./commands')
36+
37+
// Switch the base URL of Elasticsearch when security enabled in the cluster
38+
if (Cypress.env("security_enabled")) {
39+
Cypress.env("elasticsearch", "https://localhost:9200");
40+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"@elastic/eslint-import-resolver-kibana": "link:../../packages/kbn-eslint-import-resolver-kibana",
4242
"@types/react-dom": "^16.9.8",
4343
"@types/react-router-dom": "^5.1.5",
44-
"cypress": "4.3.0",
44+
"cypress": "^6.0.0",
4545
"eslint-plugin-no-unsanitized": "^3.0.2",
4646
"eslint-plugin-prefer-object-spread": "^1.2.1",
4747
"husky": "^3.0.0",

0 commit comments

Comments
 (0)