diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:54:28 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:54:28 +0000 |
commit | e6918187568dbd01842d8d1d2c808ce16a894239 (patch) | |
tree | 64f88b554b444a49f656b6c656111a145cbbaa28 /src/pybind/mgr/dashboard/frontend/cypress/support/commands.ts | |
parent | Initial commit. (diff) | |
download | ceph-e6918187568dbd01842d8d1d2c808ce16a894239.tar.xz ceph-e6918187568dbd01842d8d1d2c808ce16a894239.zip |
Adding upstream version 18.2.2.upstream/18.2.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/pybind/mgr/dashboard/frontend/cypress/support/commands.ts')
-rw-r--r-- | src/pybind/mgr/dashboard/frontend/cypress/support/commands.ts | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/src/pybind/mgr/dashboard/frontend/cypress/support/commands.ts b/src/pybind/mgr/dashboard/frontend/cypress/support/commands.ts new file mode 100644 index 000000000..09a2788eb --- /dev/null +++ b/src/pybind/mgr/dashboard/frontend/cypress/support/commands.ts @@ -0,0 +1,130 @@ +declare global { + namespace Cypress { + interface Chainable<Subject> { + login(username?: string, password?: string): void; + logToConsole(message: string, optional?: any): void; + text(): Chainable<string>; + ceph2Login(username?: string, password?: string): Chainable<any>; + checkAccessibility(subject: any, axeOptions?: any, skip?: boolean): void; + } + } +} +// Disabling tslint rule since cypress-cucumber has +// issues with absolute import paths. +// This can be removed when +// https://github.com/cypress-io/cypress-browserify-preprocessor/issues/53 +// is fixed. +/* tslint:disable*/ +import { CdHelperClass } from '../../src/app/shared/classes/cd-helper.class'; +import { Permissions } from '../../src/app/shared/models/permissions'; +import { table } from 'table'; +/* tslint:enable*/ +let auth: any; + +const fillAuth = () => { + window.localStorage.setItem('dashboard_username', auth.username); + window.localStorage.setItem('dashboard_permissions', auth.permissions); + window.localStorage.setItem('user_pwd_expiration_date', auth.pwdExpirationDate); + window.localStorage.setItem('user_pwd_update_required', auth.pwdUpdateRequired); + window.localStorage.setItem('sso', auth.sso); +}; + +Cypress.Commands.add('login', (username, password) => { + cy.session([username, password], () => { + requestAuth(username, password).then((resp) => { + auth = resp.body; + auth.permissions = JSON.stringify(new Permissions(auth.permissions)); + auth.pwdExpirationDate = String(auth.pwdExpirationDate); + auth.pwdUpdateRequired = String(auth.pwdUpdateRequired); + auth.sso = String(auth.sso); + fillAuth(); + }); + }); +}); + +Cypress.Commands.add('ceph2Login', (username, password) => { + const url: string = Cypress.env('CEPH2_URL'); + cy.session([username, password, url], () => { + requestAuth(username, password, url).then((resp) => { + auth = resp.body; + auth.permissions = JSON.stringify(new Permissions(auth.permissions)); + auth.pwdExpirationDate = String(auth.pwdExpirationDate); + auth.pwdUpdateRequired = String(auth.pwdUpdateRequired); + auth.sso = String(auth.sso); + const args = { + username: auth.username, + permissions: auth.permissions, + pwdExpirationDate: auth.pwdExpirationDate, + pwdUpdateRequired: auth.pwdUpdateRequired, + sso: auth.sso + }; + // @ts-ignore + cy.origin( + url, + { args }, + ({ uname, permissions, pwdExpirationDate, pwdUpdateRequired, sso }: any) => { + window.localStorage.setItem('dashboard_username', uname); + window.localStorage.setItem('dashboard_permissions', permissions); + window.localStorage.setItem('user_pwd_expiration_date', pwdExpirationDate); + window.localStorage.setItem('user_pwd_update_required', pwdUpdateRequired); + window.localStorage.setItem('sso', sso); + } + ); + }); + }); +}); + +function requestAuth(username: string, password: string, url = '') { + username = username ? username : Cypress.env('LOGIN_USER'); + password = password ? password : Cypress.env('LOGIN_PWD'); + return cy.request({ + method: 'POST', + url: !url ? 'api/auth' : `${url}api/auth`, + headers: { Accept: CdHelperClass.cdVersionHeader('1', '0') }, + body: { username: username, password: password } + }); +} + +// @ts-ignore +Cypress.Commands.add('text', { prevSubject: true }, ($element: JQuery<HTMLElement>) => { + cy.wrap($element).scrollIntoView(); + return cy + .wrap($element) + .invoke('text') + .then((text: string) => { + return text.toString(); + }); +}); + +Cypress.Commands.add('logToConsole', (message: string, optional?: any) => { + cy.task('log', { message: `(${new Date().toISOString()}) ${message}`, optional }); +}); + +// Print cypress-axe violations to the terminal +function a11yErrorLogger(violations: any) { + const violationData = violations.flatMap(({ id, impact, description, nodes }: any) => { + return nodes.flatMap(({ html }: any) => { + return [ + ['Test', Cypress.currentTest.title], + ['Error', id], + ['Impact', impact], + ['Description', description], + ['Element', html], + ['', ''] + ]; + }); + }); + + cy.task('log', { + message: table(violationData, { + header: { + alignment: 'left', + content: Cypress.spec.relative + } + }) + }); +} + +Cypress.Commands.add('checkAccessibility', (subject: any, axeOptions?: any, skip?: boolean) => { + cy.checkA11y(subject, axeOptions, a11yErrorLogger, skip); +}); |