Skip to content

Commit

Permalink
Set up fixtures and commands correctly
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Liang <[email protected]>
  • Loading branch information
RyanL1997 committed Nov 22, 2023
1 parent 80192ed commit 82bb3a5
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 8 deletions.
17 changes: 14 additions & 3 deletions .cypress/e2e/saml/saml_auth_test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down
4 changes: 4 additions & 0 deletions .cypress/fixtures/saml/samlUserRoleMappiing.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"backend_roles" : [ "admin" ],
"users" : [ "[email protected]" ]
}
23 changes: 22 additions & 1 deletion .cypress/utils/commands.js → .cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 8 additions & 0 deletions .cypress/utils/constants.js → .cypress/support/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
2 changes: 2 additions & 0 deletions .cypress/support/e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************

import './commands';
8 changes: 4 additions & 4 deletions .cypress/utils/index.d.ts → .cypress/support/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ declare namespace Cypress {
* @example
* cy.createRoleMapping('role_name', rolemappingJsonFixture )
*/
createRoleMapping<S = any>(
roleID: string,
rolemappingJson: string
): Chainable<S>;
createRoleMapping<S = any>(
roleID: string,
rolemappingJson: string
): Chainable<S>;
}
}
2 changes: 2 additions & 0 deletions cypress.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,7 @@ module.exports = defineConfig({
},
env: {
openSearchUrl: 'https://localhost:9200',
adminUserName: 'admin',
adminPassword: 'admin',
},
});

0 comments on commit 82bb3a5

Please sign in to comment.