]> git.proxmox.com Git - ceph.git/blame - ceph/src/pybind/mgr/dashboard/frontend/cypress/support/commands.ts
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / cypress / support / commands.ts
CommitLineData
e306af50
TL
1declare global {
2 namespace Cypress {
3 interface Chainable<Subject> {
1e59de90 4 login(username?: string, password?: string): void;
2a845540 5 logToConsole(message: string, optional?: any): void;
e306af50 6 text(): Chainable<string>;
1e59de90 7 ceph2Login(username?: string, password?: string): Chainable<any>;
39ae355f 8 checkAccessibility(subject: any, axeOptions?: any, skip?: boolean): void;
e306af50
TL
9 }
10 }
11}
2a845540
TL
12// Disabling tslint rule since cypress-cucumber has
13// issues with absolute import paths.
14// This can be removed when
15// https://github.com/cypress-io/cypress-browserify-preprocessor/issues/53
16// is fixed.
17/* tslint:disable*/
18import { CdHelperClass } from '../../src/app/shared/classes/cd-helper.class';
19import { Permissions } from '../../src/app/shared/models/permissions';
39ae355f 20import { table } from 'table';
2a845540 21/* tslint:enable*/
e306af50
TL
22let auth: any;
23
24const fillAuth = () => {
25 window.localStorage.setItem('dashboard_username', auth.username);
e306af50
TL
26 window.localStorage.setItem('dashboard_permissions', auth.permissions);
27 window.localStorage.setItem('user_pwd_expiration_date', auth.pwdExpirationDate);
28 window.localStorage.setItem('user_pwd_update_required', auth.pwdUpdateRequired);
29 window.localStorage.setItem('sso', auth.sso);
30};
31
1e59de90
TL
32Cypress.Commands.add('login', (username, password) => {
33 cy.session([username, password], () => {
34 requestAuth(username, password).then((resp) => {
e306af50
TL
35 auth = resp.body;
36 auth.permissions = JSON.stringify(new Permissions(auth.permissions));
37 auth.pwdExpirationDate = String(auth.pwdExpirationDate);
38 auth.pwdUpdateRequired = String(auth.pwdUpdateRequired);
39 auth.sso = String(auth.sso);
40 fillAuth();
41 });
1e59de90
TL
42 });
43});
44
45Cypress.Commands.add('ceph2Login', (username, password) => {
46 const url: string = Cypress.env('CEPH2_URL');
47 cy.session([username, password, url], () => {
48 requestAuth(username, password, url).then((resp) => {
49 auth = resp.body;
50 auth.permissions = JSON.stringify(new Permissions(auth.permissions));
51 auth.pwdExpirationDate = String(auth.pwdExpirationDate);
52 auth.pwdUpdateRequired = String(auth.pwdUpdateRequired);
53 auth.sso = String(auth.sso);
54 const args = {
55 username: auth.username,
56 permissions: auth.permissions,
57 pwdExpirationDate: auth.pwdExpirationDate,
58 pwdUpdateRequired: auth.pwdUpdateRequired,
59 sso: auth.sso
60 };
61 // @ts-ignore
62 cy.origin(
63 url,
64 { args },
65 ({ uname, permissions, pwdExpirationDate, pwdUpdateRequired, sso }: any) => {
66 window.localStorage.setItem('dashboard_username', uname);
67 window.localStorage.setItem('dashboard_permissions', permissions);
68 window.localStorage.setItem('user_pwd_expiration_date', pwdExpirationDate);
69 window.localStorage.setItem('user_pwd_update_required', pwdUpdateRequired);
70 window.localStorage.setItem('sso', sso);
71 }
72 );
73 });
74 });
e306af50
TL
75});
76
1e59de90
TL
77function requestAuth(username: string, password: string, url = '') {
78 username = username ? username : Cypress.env('LOGIN_USER');
79 password = password ? password : Cypress.env('LOGIN_PWD');
80 return cy.request({
81 method: 'POST',
82 url: !url ? 'api/auth' : `${url}api/auth`,
83 headers: { Accept: CdHelperClass.cdVersionHeader('1', '0') },
84 body: { username: username, password: password }
85 });
86}
87
20effc67 88// @ts-ignore
2a845540 89Cypress.Commands.add('text', { prevSubject: true }, (subject: any) => {
e306af50
TL
90 return subject.text();
91});
2a845540
TL
92
93Cypress.Commands.add('logToConsole', (message: string, optional?: any) => {
94 cy.task('log', { message: `(${new Date().toISOString()}) ${message}`, optional });
95});
39ae355f
TL
96
97// Print cypress-axe violations to the terminal
98function a11yErrorLogger(violations: any) {
99 const violationData = violations.flatMap(({ id, impact, description, nodes }: any) => {
100 return nodes.flatMap(({ html }: any) => {
101 return [
102 ['Test', Cypress.currentTest.title],
103 ['Error', id],
104 ['Impact', impact],
105 ['Description', description],
106 ['Element', html],
107 ['', '']
108 ];
109 });
110 });
111
112 cy.task('log', {
113 message: table(violationData, {
114 header: {
115 alignment: 'left',
116 content: Cypress.spec.relative
117 }
118 })
119 });
120}
121
122Cypress.Commands.add('checkAccessibility', (subject: any, axeOptions?: any, skip?: boolean) => {
123 cy.checkA11y(subject, axeOptions, a11yErrorLogger, skip);
124});