]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/cypress/support/commands.ts
6ff17f9d6978e11f35787a657e2c02ad6222ca59
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / cypress / support / commands.ts
1 declare global {
2 namespace Cypress {
3 interface Chainable<Subject> {
4 login(): void;
5 logToConsole(message: string, optional?: any): void;
6 text(): Chainable<string>;
7 }
8 }
9 }
10 // Disabling tslint rule since cypress-cucumber has
11 // issues with absolute import paths.
12 // This can be removed when
13 // https://github.com/cypress-io/cypress-browserify-preprocessor/issues/53
14 // is fixed.
15 /* tslint:disable*/
16 import { CdHelperClass } from '../../src/app/shared/classes/cd-helper.class';
17 import { Permissions } from '../../src/app/shared/models/permissions';
18 /* tslint:enable*/
19 let auth: any;
20
21 const fillAuth = () => {
22 window.localStorage.setItem('dashboard_username', auth.username);
23 window.localStorage.setItem('dashboard_permissions', auth.permissions);
24 window.localStorage.setItem('user_pwd_expiration_date', auth.pwdExpirationDate);
25 window.localStorage.setItem('user_pwd_update_required', auth.pwdUpdateRequired);
26 window.localStorage.setItem('sso', auth.sso);
27 };
28
29 Cypress.Commands.add('login', () => {
30 const username = Cypress.env('LOGIN_USER') || 'admin';
31 const password = Cypress.env('LOGIN_PWD') || 'admin';
32
33 if (auth === undefined) {
34 cy.request({
35 method: 'POST',
36 url: 'api/auth',
37 headers: { Accept: CdHelperClass.cdVersionHeader('1', '0') },
38 body: { username: username, password: password }
39 }).then((resp) => {
40 auth = resp.body;
41 auth.permissions = JSON.stringify(new Permissions(auth.permissions));
42 auth.pwdExpirationDate = String(auth.pwdExpirationDate);
43 auth.pwdUpdateRequired = String(auth.pwdUpdateRequired);
44 auth.sso = String(auth.sso);
45 fillAuth();
46 });
47 } else {
48 fillAuth();
49 }
50 });
51
52 // @ts-ignore
53 Cypress.Commands.add('text', { prevSubject: true }, (subject: any) => {
54 return subject.text();
55 });
56
57 Cypress.Commands.add('logToConsole', (message: string, optional?: any) => {
58 cy.task('log', { message: `(${new Date().toISOString()}) ${message}`, optional });
59 });