Skip to content

Commit

Permalink
fix: recollect session data after validation is successful (#25112)
Browse files Browse the repository at this point in the history
  • Loading branch information
emilyrohrbough authored Dec 12, 2022
1 parent 7154fc8 commit 2062670
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 17 deletions.
56 changes: 43 additions & 13 deletions packages/driver/cypress/e2e/commands/sessions/sessions.cy.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

5 comments on commit 2062670

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 2062670 Dec 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux arm64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.1.0/linux-arm64/develop-2062670f5c51011de09f679352eb34c7bf1406a9/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 2062670 Dec 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.1.0/linux-x64/develop-2062670f5c51011de09f679352eb34c7bf1406a9/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 2062670 Dec 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.1.0/darwin-x64/develop-2062670f5c51011de09f679352eb34c7bf1406a9/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 2062670 Dec 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin arm64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.1.0/darwin-arm64/develop-2062670f5c51011de09f679352eb34c7bf1406a9/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 2062670 Dec 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the win32 x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.1.0/win32-x64/develop-2062670f5c51011de09f679352eb34c7bf1406a9/cypress.tgz

Please sign in to comment.