diff --git a/.cypress/e2e/saml/saml_auth_test.spec.js b/.cypress/e2e/saml/saml_auth_test.spec.js index 7784abdf4..961178022 100644 --- a/.cypress/e2e/saml/saml_auth_test.spec.js +++ b/.cypress/e2e/saml/saml_auth_test.spec.js @@ -18,10 +18,21 @@ * SPDX-License-Identifier: Apache-2.0 */ +import { ALL_ACCESS_ROLE } from '../../support/constants'; + +import samlUserRoleMapping from '../../fixtures/saml/samlUserRoleMappiing.json' + +before(() => { + cy.intercept('https://localhost:9200'); + cy.createRoleMapping(ALL_ACCESS_ROLE, samlUserRoleMapping); + cy.clearCookies(); + cy.clearLocalStorage(); +}); + afterEach(() => { - cy.clearCookies(); - cy.clearLocalStorage(); - }); + cy.clearCookies(); + cy.clearLocalStorage(); +}); describe('Log in via SAML', () => { const samlLogin = () => { diff --git a/.cypress/fixtures/saml/samlUserRoleMappiing.json b/.cypress/fixtures/saml/samlUserRoleMappiing.json new file mode 100644 index 000000000..b1f015cfc --- /dev/null +++ b/.cypress/fixtures/saml/samlUserRoleMappiing.json @@ -0,0 +1,4 @@ +{ + "backend_roles" : [ "admin" ], + "users" : [ "saml.jackson@example.com" ] +} diff --git a/.cypress/utils/commands.js b/.cypress/support/commands.js similarity index 53% rename from .cypress/utils/commands.js rename to .cypress/support/commands.js index 7291d4060..dcd94d171 100644 --- a/.cypress/utils/commands.js +++ b/.cypress/support/commands.js @@ -18,7 +18,28 @@ * SPDX-License-Identifier: Apache-2.0 */ -import { SEC_API } from "./constants"; +import { SEC_API, ADMIN_AUTH } from "./constants"; + +/** + * Overwrite request command to support authentication similar to visit. + * The request function parameters can be url, or (method, url), or (method, url, body). + */ +Cypress.Commands.overwrite('request', (originalFn, ...args) => { + let defaults = {}; + 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('createRoleMapping', (roleID, rolemappingJson) => { cy.request( diff --git a/.cypress/utils/constants.js b/.cypress/support/constants.js similarity index 85% rename from .cypress/utils/constants.js rename to .cypress/support/constants.js index b2bef6afc..95cb4da04 100644 --- a/.cypress/utils/constants.js +++ b/.cypress/support/constants.js @@ -24,6 +24,14 @@ ***************************** */ +export const ALL_ACCESS_ROLE = 'all_access'; + +//Admin Credential +export const ADMIN_AUTH = { + username: Cypress.env('adminUserName'), + password: Cypress.env('adminPassword'), +}; + //Security API Constants export const SEC_API_PREFIX = '/_plugins/_security/api'; export const SEC_API = { diff --git a/.cypress/support/e2e.js b/.cypress/support/e2e.js index 53672f05d..433a1cab9 100644 --- a/.cypress/support/e2e.js +++ b/.cypress/support/e2e.js @@ -32,3 +32,5 @@ // You can read more here: // https://on.cypress.io/configuration // *********************************************************** + +import './commands'; diff --git a/.cypress/utils/index.d.ts b/.cypress/support/index.d.ts similarity index 73% rename from .cypress/utils/index.d.ts rename to .cypress/support/index.d.ts index 385ca69fd..3a6ba4f71 100644 --- a/.cypress/utils/index.d.ts +++ b/.cypress/support/index.d.ts @@ -8,9 +8,9 @@ declare namespace Cypress { * @example * cy.createRoleMapping('role_name', rolemappingJsonFixture ) */ - createRoleMapping( - roleID: string, - rolemappingJson: string - ): Chainable; + createRoleMapping( + roleID: string, + rolemappingJson: string + ): Chainable; } } \ No newline at end of file diff --git a/cypress.config.js b/cypress.config.js index 14de9819e..77941deca 100644 --- a/cypress.config.js +++ b/cypress.config.js @@ -29,5 +29,7 @@ module.exports = defineConfig({ }, env: { openSearchUrl: 'https://localhost:9200', + adminUserName: 'admin', + adminPassword: 'admin', }, });