Skip to content

Commit

Permalink
chore: add debug logging for login and session handling
Browse files Browse the repository at this point in the history
  • Loading branch information
adeldhis2 committed Oct 3, 2024
1 parent 746e960 commit adb9dac
Showing 1 changed file with 40 additions and 6 deletions.
46 changes: 40 additions & 6 deletions cypress/support/e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ before(() => {
const baseUrl = Cypress.env('dhis2BaseUrl')
const instanceVersion = Cypress.env('dhis2InstanceVersion')

cy.task(
'log',
`Attempting to log in with user: ${username} on base URL: ${baseUrl}`
)

cy.request({
url: `${baseUrl}/${LOGIN_ENDPOINT}`,
method: 'POST',
Expand All @@ -46,36 +51,65 @@ before(() => {
j_password: password,
'2fa_code': '',
},
}).should((response) => {
}).then((response) => {
cy.task('log', `Login request returned status: ${response.status}`)
expect(response.status).to.eq(200)
if (response.redirectedToUrl) {
cy.task('log', `Redirected to URL: ${response.redirectedToUrl}`)
}
})

cy.getAllCookies()
.should((cookies) => {
cy.task(
'log',
`Cookies after login attempt: ${JSON.stringify(cookies)}`
)
expect(cookies.length).to.be.at.least(1)
})
.then((cookies) => {
const sessionCookieForBaseUrl = findSessionCookieForBaseUrl(
baseUrl,
cookies
)
Cypress.env(
computeEnvVariableName(instanceVersion),
JSON.stringify(sessionCookieForBaseUrl)
)
if (sessionCookieForBaseUrl) {
cy.task(
'log',
`Found session cookie for base URL: ${JSON.stringify(
sessionCookieForBaseUrl
)}`
)
Cypress.env(
computeEnvVariableName(instanceVersion),
JSON.stringify(sessionCookieForBaseUrl)
)
} else {
cy.task(
'log',
`Session cookie not found for base URL: ${baseUrl}`
)
}
})
})

beforeEach(() => {
const baseUrl = Cypress.env('dhis2BaseUrl')
const instanceVersion = Cypress.env('dhis2InstanceVersion')
const envVariableName = computeEnvVariableName(instanceVersion)
const { name, value, ...options } = JSON.parse(Cypress.env(envVariableName))
const sessionCookie = Cypress.env(envVariableName)

cy.task(
'log',
`Setting session cookie for base URL: ${baseUrl} with cookie data: ${sessionCookie}`
)

const { name, value, ...options } = JSON.parse(sessionCookie)

localStorage.setItem(LOCAL_STORAGE_KEY, baseUrl)
cy.setCookie(name, value, options)

cy.getAllCookies().should((cookies) => {
cy.task('log', `Cookies in beforeEach: ${JSON.stringify(cookies)}`)
expect(findSessionCookieForBaseUrl(baseUrl, cookies)).to.exist
expect(localStorage.getItem(LOCAL_STORAGE_KEY)).to.equal(baseUrl)
})
Expand Down

0 comments on commit adb9dac

Please sign in to comment.