Skip to content

Commit

Permalink
test(cypress): add custom login command
Browse files Browse the repository at this point in the history
This is needed because Cypress version is still v6.9.1 and we cannot
upgrade cli-utils-cypress until we upgrade Cypress to 12+.
From 41 the login implementation has changed and uses a different
endpoint.
  • Loading branch information
edoardo committed Jul 11, 2024
1 parent f24ca67 commit ade516a
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

0 comments on commit ade516a

Please sign in to comment.