Skip to content

Commit 2062670

Browse files
fix: recollect session data after validation is successful (#25112)
1 parent 7154fc8 commit 2062670

File tree

3 files changed

+73
-17
lines changed

3 files changed

+73
-17
lines changed

packages/driver/cypress/e2e/commands/sessions/sessions.cy.js

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,8 @@ describe('cy.session', { retries: 0 }, () => {
256256
const sessionStorageData = consoleProps.groups[0].groups[0]
257257

258258
expect(sessionStorageData.name).to.contain('Session Storage - (1)')
259-
expect(sessionStorageData.items).to.deep.eq({ cypressAuthToken: '{"body":{"username":"tester"}}' })
259+
expect(sessionStorageData.items).to.have.property('cypressAuthToken')
260+
expect(sessionStorageData.items.cypressAuthToken).to.contains('"username":"tester"')
260261
})
261262
})
262263

@@ -839,29 +840,39 @@ describe('cy.session', { retries: 0 }, () => {
839840
let clearPageCount = 0
840841
let sessionGroupId
841842
let setup
843+
let slowSetup
842844
let validate
843845

844-
const handleSetup = () => {
846+
const handleSetup = (slowLogin = false) => {
845847
// create session clears page before running
846848
cy.contains('Default blank page').should('not.exist')
847849

848850
cy.visit('/fixtures/auth/index.html')
849851
cy.contains('You are not logged in')
850-
cy.window().then((win) => {
851-
win.sessionStorage.setItem('cypressAuthToken', JSON.stringify({ body: { username: 'tester' } }))
852-
})
852+
cy.get('[data-cy=login-same-origin]').click()
853+
cy.get('input').type('tester')
854+
if (slowLogin) {
855+
cy.get('[data-cy=slow-login]').click()
856+
} else {
857+
cy.get('[data-cy=login]').click()
858+
}
853859
}
854860

861+
const handleSlowSetup = () => handleSetup(true)
862+
855863
const handleValidate = () => {
856-
// both create & restore session clears page after running
864+
// both create & restore session clears page after running
857865
cy.contains('Default blank page').should('not.exist')
858866

859-
cy.visit('/fixtures/auth/index.html')
860-
cy.contains('Welcome tester')
867+
cy.window()
868+
.its('sessionStorage')
869+
.its('cypressAuthToken', { timeout: 5000 })
870+
.should('contain', '"username":"tester"')
861871
}
862872

863873
before(() => {
864874
setup = cy.stub().callsFake(handleSetup).as('setupSession')
875+
slowSetup = cy.stub().callsFake(handleSlowSetup).as('setupSlowSession')
865876
validate = cy.stub().callsFake(handleValidate).as('validateSession')
866877
})
867878

@@ -879,7 +890,7 @@ describe('cy.session', { retries: 0 }, () => {
879890
resetMocks()
880891
clearAllSavedSessions()
881892
cy.on('log:added', (attrs, log) => {
882-
if (attrs.name === 'session' || attrs.name === 'sessions_manager' || attrs.name === 'page load' || attrs.alias?.includes('setupSession') || attrs.alias?.includes('validateSession')) {
893+
if (attrs.name === 'session' || attrs.name === 'sessions_manager' || attrs.alias?.includes('setupSession') || attrs.alias?.includes('setupSlowSession') || attrs.alias?.includes('validateSession')) {
883894
logs.push(log)
884895
if (!sessionGroupId) {
885896
sessionGroupId = attrs.id
@@ -974,7 +985,8 @@ describe('cy.session', { retries: 0 }, () => {
974985
const sessionStorageData = consoleProps.groups[0].groups[0]
975986

976987
expect(sessionStorageData.name).to.contain('Session Storage - (1)')
977-
expect(sessionStorageData.items).to.deep.eq({ cypressAuthToken: '{"body":{"username":"tester"}}' })
988+
expect(sessionStorageData.items).to.have.property('cypressAuthToken')
989+
expect(sessionStorageData.items.cypressAuthToken).to.contains('"username":"tester"')
978990
})
979991
})
980992

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

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

9951007
it('successfully creates new session and validates it', () => {
996-
expect(setup).to.be.calledOnce
1008+
expect(slowSetup).to.be.calledOnce
9971009
expect(validate).to.be.calledOnce
9981010
expect(clearPageCount, 'total times session cleared the page').to.eq(0)
9991011
})
@@ -1024,7 +1036,7 @@ describe('cy.session', { retries: 0 }, () => {
10241036
})
10251037

10261038
expect(logs[3].get()).to.deep.contain({
1027-
alias: ['setupSession'],
1039+
alias: ['setupSlowSession'],
10281040
group: createNewSessionGroup.id,
10291041
})
10301042

@@ -1040,6 +1052,24 @@ describe('cy.session', { retries: 0 }, () => {
10401052
group: validateSessionGroup.id,
10411053
})
10421054
})
1055+
1056+
it('has session details in the consoleProps', () => {
1057+
const consoleProps = logs[0].get('consoleProps')()
1058+
1059+
expect(consoleProps.Command).to.eq('session')
1060+
expect(consoleProps.id).to.eq(sessionId)
1061+
expect(consoleProps.Domains).to.eq('This session captured data from localhost.')
1062+
1063+
expect(consoleProps.groups).to.have.length(1)
1064+
expect(consoleProps.groups[0].name).to.eq('localhost data:')
1065+
expect(consoleProps.groups[0].groups).to.have.length(1)
1066+
1067+
const sessionStorageData = consoleProps.groups[0].groups[0]
1068+
1069+
expect(sessionStorageData.name).to.contain('Session Storage - (1)')
1070+
expect(sessionStorageData.items).to.have.property('cypressAuthToken')
1071+
expect(sessionStorageData.items.cypressAuthToken).to.contains('"username":"tester"')
1072+
})
10431073
})
10441074

10451075
describe('create session with failed validation flow', () => {

packages/driver/cypress/fixtures/auth/index.html

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414

1515
if (newToken) {
16-
sessionStorage.setItem('cypressAuthToken', decodeURIComponent(newToken));
16+
sessionStorage.setItem('cypressAuthToken', decodeURIComponent(newToken))
1717
urlParams.delete('token')
1818
const newSearchParams = urlParams.toString()
1919

@@ -43,13 +43,23 @@
4343

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

53+
54+
// Add Login button that redirects to the idp
55+
const loginIDPBtn = document.createElement("button");
56+
loginIDPBtn.innerHTML = "Login IDP"
57+
loginIDPBtn.dataset.cy = "login-idp"
58+
loginIDPBtn.onclick = function () {
59+
window.location.href = `http://www.idp.com:3500/fixtures/auth/idp.html?redirect=${encodeURIComponent(window.location.href)}`
60+
};
61+
document.body.appendChild(loginIDPBtn);
62+
5363
// Add Login button that redirects to the idp
5464
const loginFoobarBtn = document.createElement("button");
5565
loginFoobarBtn.innerHTML = "Login Foobar"

packages/driver/src/cy/commands/sessions/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ export default function (Commands, Cypress, cy) {
185185
consoleProps: () => {
186186
return {
187187
Step: statusMap.stepName(step),
188+
Message: 'The following is the collected session data after the session was successfully setup:',
189+
188190
...getConsoleProps(existingSession),
189191
}
190192
},
@@ -396,6 +398,20 @@ export default function (Commands, Cypress, cy) {
396398
}
397399
}
398400

401+
// collect all session data again that may have been updated during the validation check
402+
const data = await sessions.getCurrentSessionData()
403+
404+
_.extend(existingSession, data)
405+
validateLog.set({
406+
consoleProps: () => {
407+
return {
408+
Step: 'Validate Session',
409+
Message: 'The following is the collected session data after the session was successfully validated:',
410+
...getConsoleProps(existingSession),
411+
}
412+
},
413+
})
414+
399415
return isValidSession
400416
})
401417

0 commit comments

Comments
 (0)