Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: recollect session data after validation is successful #25112

Merged
merged 3 commits into from Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 43 additions & 13 deletions packages/driver/cypress/e2e/commands/sessions/sessions.cy.js
Expand Up @@ -256,7 +256,8 @@ describe('cy.session', { retries: 0 }, () => {
const sessionStorageData = consoleProps.groups[0].groups[0]

expect(sessionStorageData.name).to.contain('Session Storage - (1)')
expect(sessionStorageData.items).to.deep.eq({ cypressAuthToken: '{"body":{"username":"tester"}}' })
expect(sessionStorageData.items).to.have.property('cypressAuthToken')
expect(sessionStorageData.items.cypressAuthToken).to.contains('"username":"tester"')
})
})

Expand Down Expand Up @@ -839,29 +840,39 @@ describe('cy.session', { retries: 0 }, () => {
let clearPageCount = 0
let sessionGroupId
let setup
let slowSetup
let validate

const handleSetup = () => {
const handleSetup = (slowLogin = false) => {
// create session clears page before running
cy.contains('Default blank page').should('not.exist')

cy.visit('/fixtures/auth/index.html')
cy.contains('You are not logged in')
cy.window().then((win) => {
win.sessionStorage.setItem('cypressAuthToken', JSON.stringify({ body: { username: 'tester' } }))
})
cy.get('[data-cy=login-same-origin]').click()
cy.get('input').type('tester')
if (slowLogin) {
cy.get('[data-cy=slow-login]').click()
} else {
cy.get('[data-cy=login]').click()
}
}

const handleSlowSetup = () => handleSetup(true)

const handleValidate = () => {
// both create & restore session clears page after running
// both create & restore session clears page after running
cy.contains('Default blank page').should('not.exist')

cy.visit('/fixtures/auth/index.html')
cy.contains('Welcome tester')
cy.window()
.its('sessionStorage')
.its('cypressAuthToken', { timeout: 5000 })
.should('contain', '"username":"tester"')
}

before(() => {
setup = cy.stub().callsFake(handleSetup).as('setupSession')
slowSetup = cy.stub().callsFake(handleSlowSetup).as('setupSlowSession')
validate = cy.stub().callsFake(handleValidate).as('validateSession')
})

Expand All @@ -879,7 +890,7 @@ describe('cy.session', { retries: 0 }, () => {
resetMocks()
clearAllSavedSessions()
cy.on('log:added', (attrs, log) => {
if (attrs.name === 'session' || attrs.name === 'sessions_manager' || attrs.name === 'page load' || attrs.alias?.includes('setupSession') || attrs.alias?.includes('validateSession')) {
if (attrs.name === 'session' || attrs.name === 'sessions_manager' || attrs.alias?.includes('setupSession') || attrs.alias?.includes('setupSlowSession') || attrs.alias?.includes('validateSession')) {
logs.push(log)
if (!sessionGroupId) {
sessionGroupId = attrs.id
Expand Down Expand Up @@ -974,7 +985,8 @@ describe('cy.session', { retries: 0 }, () => {
const sessionStorageData = consoleProps.groups[0].groups[0]

expect(sessionStorageData.name).to.contain('Session Storage - (1)')
expect(sessionStorageData.items).to.deep.eq({ cypressAuthToken: '{"body":{"username":"tester"}}' })
expect(sessionStorageData.items).to.have.property('cypressAuthToken')
expect(sessionStorageData.items.cypressAuthToken).to.contains('"username":"tester"')
})
})

Expand All @@ -985,15 +997,15 @@ describe('cy.session', { retries: 0 }, () => {
setupTestContext()
cy.log('Creating new session with validation to test against')
sessionId = `session-${Cypress.state('test').id}`
cy.session(sessionId, setup, { validate })
cy.session(sessionId, slowSetup, { validate })
})

it('does not clear the page after command', () => {
cy.url().should('contain', '/fixtures/auth/index.html')
})

it('successfully creates new session and validates it', () => {
expect(setup).to.be.calledOnce
expect(slowSetup).to.be.calledOnce
expect(validate).to.be.calledOnce
expect(clearPageCount, 'total times session cleared the page').to.eq(0)
})
Expand Down Expand Up @@ -1024,7 +1036,7 @@ describe('cy.session', { retries: 0 }, () => {
})

expect(logs[3].get()).to.deep.contain({
alias: ['setupSession'],
alias: ['setupSlowSession'],
group: createNewSessionGroup.id,
})

Expand All @@ -1040,6 +1052,24 @@ describe('cy.session', { retries: 0 }, () => {
group: validateSessionGroup.id,
})
})

it('has session details in the consoleProps', () => {
const consoleProps = logs[0].get('consoleProps')()

expect(consoleProps.Command).to.eq('session')
expect(consoleProps.id).to.eq(sessionId)
expect(consoleProps.Domains).to.eq('This session captured data from localhost.')

expect(consoleProps.groups).to.have.length(1)
expect(consoleProps.groups[0].name).to.eq('localhost data:')
expect(consoleProps.groups[0].groups).to.have.length(1)

const sessionStorageData = consoleProps.groups[0].groups[0]

expect(sessionStorageData.name).to.contain('Session Storage - (1)')
expect(sessionStorageData.items).to.have.property('cypressAuthToken')
expect(sessionStorageData.items.cypressAuthToken).to.contains('"username":"tester"')
})
})

describe('create session with failed validation flow', () => {
Expand Down
18 changes: 14 additions & 4 deletions packages/driver/cypress/fixtures/auth/index.html
Expand Up @@ -13,7 +13,7 @@


if (newToken) {
sessionStorage.setItem('cypressAuthToken', decodeURIComponent(newToken));
sessionStorage.setItem('cypressAuthToken', decodeURIComponent(newToken))
urlParams.delete('token')
const newSearchParams = urlParams.toString()

Expand Down Expand Up @@ -43,13 +43,23 @@

// Add Login button that redirects to the idp
const loginBtn = document.createElement("button");
loginBtn.innerHTML = "Login IDP"
loginBtn.dataset.cy = "login-idp"
loginBtn.innerHTML = "Login Same Origin"
loginBtn.dataset.cy = "login-same-origin"
loginBtn.onclick = function () {
window.location.href = `http://www.idp.com:3500/fixtures/auth/idp.html?redirect=${encodeURIComponent(window.location.href)}`
window.location.href = `http://localhost:3500/fixtures/auth/idp.html?redirect=${encodeURIComponent(window.location.href)}`
};
document.body.appendChild(loginBtn);


// Add Login button that redirects to the idp
const loginIDPBtn = document.createElement("button");
loginIDPBtn.innerHTML = "Login IDP"
loginIDPBtn.dataset.cy = "login-idp"
loginIDPBtn.onclick = function () {
window.location.href = `http://www.idp.com:3500/fixtures/auth/idp.html?redirect=${encodeURIComponent(window.location.href)}`
};
document.body.appendChild(loginIDPBtn);

// Add Login button that redirects to the idp
const loginFoobarBtn = document.createElement("button");
loginFoobarBtn.innerHTML = "Login Foobar"
Expand Down
16 changes: 16 additions & 0 deletions packages/driver/src/cy/commands/sessions/index.ts
Expand Up @@ -185,6 +185,8 @@ export default function (Commands, Cypress, cy) {
consoleProps: () => {
return {
Step: statusMap.stepName(step),
Message: 'The following is the collected session data after the session was successfully setup:',

...getConsoleProps(existingSession),
}
},
Expand Down Expand Up @@ -396,6 +398,20 @@ export default function (Commands, Cypress, cy) {
}
}

// collect all session data again that may have been updated during the validation check
const data = await sessions.getCurrentSessionData()

_.extend(existingSession, data)
validateLog.set({
consoleProps: () => {
return {
Step: 'Validate Session',
Message: 'The following is the collected session data after the session was successfully validated:',
...getConsoleProps(existingSession),
}
},
})

return isValidSession
})

Expand Down