|
18 | 18 |
|
19 | 19 | import Utils from "@support/utils"; |
20 | 20 | import 'cypress-file-upload'; |
| 21 | +import testUsers from "../fixtures/testUsers.json"; |
21 | 22 |
|
22 | 23 | import AddNewRoleEnterDetailsPage from "./pages/carbon/AddNewRoleEnterDetailsPage"; |
23 | 24 | import AddNewRoleSelectPermissionPage from "./pages/carbon/AddNewRoleSelectPermissionPage"; |
@@ -64,11 +65,79 @@ Cypress.Commands.add('portalLogin', (username, password, portal, tenant = 'carbo |
64 | 65 | .click({ force: true }); |
65 | 66 | } |
66 | 67 | cy.url().should('contains', `/authenticationendpoint/login.do`); |
67 | | - cy.get('[data-testid=login-page-username-input]').click(); |
68 | | - cy.get('[data-testid=login-page-username-input]').type(username); |
69 | | - cy.get('[data-testid=login-page-password-input]').type(password); |
70 | | - cy.get('#loginForm').submit(); |
71 | | - cy.url().should('contains', `/${portal}`); |
| 68 | + const rawFallbacks = [ |
| 69 | + { username, password }, |
| 70 | + { |
| 71 | + username: Cypress.env(`${portal.toUpperCase()}_USERNAME`), |
| 72 | + password: Cypress.env(`${portal.toUpperCase()}_PASSWORD`), |
| 73 | + }, |
| 74 | + { |
| 75 | + username: Cypress.env('PORTAL_USERNAME'), |
| 76 | + password: Cypress.env('PORTAL_PASSWORD'), |
| 77 | + }, |
| 78 | + { |
| 79 | + username: testUsers?.publisherAdmin?.username, |
| 80 | + password: testUsers?.publisherAdmin?.password, |
| 81 | + }, |
| 82 | + { |
| 83 | + username: testUsers?.carbonAdmin?.username, |
| 84 | + password: testUsers?.carbonAdmin?.password, |
| 85 | + }, |
| 86 | + { username: 'admin', password: 'admin123' }, |
| 87 | + { username: 'admin', password: 'admin' }, |
| 88 | + ]; |
| 89 | + |
| 90 | + const credentialsToTry = rawFallbacks.filter((cred, index, allCreds) => { |
| 91 | + if (!cred.username || !cred.password) { |
| 92 | + return false; |
| 93 | + } |
| 94 | + return allCreds.findIndex((item) => item.username === cred.username && item.password === cred.password) === index; |
| 95 | + }); |
| 96 | + |
| 97 | + const tryLogin = (attemptIndex = 0) => { |
| 98 | + const cred = credentialsToTry[attemptIndex]; |
| 99 | + |
| 100 | + cy.get('[data-testid=login-page-username-input]', { timeout: Cypress.config().largeTimeout }) |
| 101 | + .should('be.visible') |
| 102 | + .clear() |
| 103 | + .type(cred.username); |
| 104 | + |
| 105 | + cy.get('[data-testid=login-page-password-input]') |
| 106 | + .should('be.visible') |
| 107 | + .clear() |
| 108 | + .type(cred.password); |
| 109 | + |
| 110 | + cy.get('#loginForm').submit(); |
| 111 | + |
| 112 | + const waitForAuthOutcome = (remainingPolls = 15) => { |
| 113 | + cy.location('href', { timeout: Cypress.config().largeTimeout }).then((href) => { |
| 114 | + if (href.includes(`/${portal}`)) { |
| 115 | + cy.url({ timeout: Cypress.config().largeTimeout }).should('contains', `/${portal}`); |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + if (href.includes('authFailure=true')) { |
| 120 | + if (attemptIndex >= credentialsToTry.length - 1) { |
| 121 | + throw new Error(`portalLogin failed for ${portal}. Last URL: ${href}`); |
| 122 | + } |
| 123 | + cy.log(`portalLogin auth failure for ${portal}; retrying with fallback credential ${attemptIndex + 2}/${credentialsToTry.length}`); |
| 124 | + tryLogin(attemptIndex + 1); |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + if (remainingPolls <= 0) { |
| 129 | + throw new Error(`portalLogin did not reach ${portal} or authFailure page. Last URL: ${href}`); |
| 130 | + } |
| 131 | + |
| 132 | + cy.wait(1000); |
| 133 | + waitForAuthOutcome(remainingPolls - 1); |
| 134 | + }); |
| 135 | + }; |
| 136 | + |
| 137 | + waitForAuthOutcome(); |
| 138 | + }; |
| 139 | + |
| 140 | + tryLogin(); |
72 | 141 | }) |
73 | 142 |
|
74 | 143 | Cypress.Commands.add('loginToPublisher', (username, password) => { |
|
0 commit comments