diff --git a/cypress/support/commands.js b/cypress/support/commands.js index bc732e6d7..34449dc4a 100644 --- a/cypress/support/commands.js +++ b/cypress/support/commands.js @@ -23,3 +23,46 @@ Cypress.Commands.add( ) ) ) + +// In this repo we are still using Cypress v6.9.1 so we cannot upgrade cli_utils_cypress to a version where the new login (>=41) is supported. +// The solution is to override the login command to accommodate for the new login in >=41, until we upgrade Cypress to 13. +Cypress.Commands.add('login', () => { + const username = Cypress.env('dhis2_username') + const password = Cypress.env('dhis2_password') + const baseUrl = Cypress.env('dhis2_base_url') + + // Login via API + cy.request({ + url: `${baseUrl}/api/loginConfig`, + method: 'GET', + }).then((response) => { + // Versions >= 41 + if (response.body['apiVersion']) { + cy.request({ + url: `${baseUrl}/api/auth/login`, + method: 'POST', + followRedirect: true, + body: { + username: username, + password: password, + }, + }) + } else { + // Versions <=40 + cy.request({ + url: `${baseUrl}/dhis-web-commons-security/login.action`, + method: 'POST', + form: true, + followRedirect: true, + body: { + j_username: username, + j_password: password, + '2fa_code': '', + }, + }) + } + }) + + // Set base url for the app platform + window.localStorage.setItem('DHIS2_BASE_URL', baseUrl) +})