diff --git a/workbench/.cypress/support/commands.js b/workbench/.cypress/support/commands.js index e4d90a3918..ab472854d4 100644 --- a/workbench/.cypress/support/commands.js +++ b/workbench/.cypress/support/commands.js @@ -49,3 +49,44 @@ // // -- This will overwrite an existing command -- // Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... }) + +import { ADMIN_AUTH } from '../utils/constants'; + +Cypress.Commands.overwrite('visit', (originalFn, url, options) => { + // Add the basic auth header when security enabled in the OpenSearch 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 OpenSearch Dashboards 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 OpenSearch 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)); +}); diff --git a/workbench/.cypress/support/constants.js b/workbench/.cypress/support/constants.js new file mode 100644 index 0000000000..48ee4ff9ff --- /dev/null +++ b/workbench/.cypress/support/constants.js @@ -0,0 +1,15 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + * + * Modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +export const ADMIN_AUTH = { + username: 'admin', + password: 'admin', +}; diff --git a/workbench/.cypress/support/index.js b/workbench/.cypress/support/index.js index a7af4e66b9..a3ce8c563c 100644 --- a/workbench/.cypress/support/index.js +++ b/workbench/.cypress/support/index.js @@ -44,3 +44,8 @@ import './commands'; // Alternatively you can use CommonJS syntax: // require('./commands') + +// Switch the base URL of OpenSearch when security enabled in the cluster +if (Cypress.env('security_enabled')) { + Cypress.env('opensearch', 'https://localhost:9200'); +} diff --git a/workbench/cypress.json b/workbench/cypress.json index e5441904ad..53c4ba96d8 100644 --- a/workbench/cypress.json +++ b/workbench/cypress.json @@ -9,5 +9,10 @@ "videosFolder": ".cypress/videos", "requestTimeout": 60000, "responseTimeout": 60000, - "defaultCommandTimeout": 60000 + "defaultCommandTimeout": 60000, + "env": { + "opensearch": "localhost:9200", + "opensearchDashboards": "localhost:5601", + "security_enabled": true + } }