Last active
May 26, 2020 20:48
-
-
Save sir-dunxalot/f88ce6eb51611ec72ce27fa1002440f3 to your computer and use it in GitHub Desktop.
Revisions
-
sir-dunxalot revised this gist
May 26, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -18,7 +18,7 @@ let cachedUsername; https://docs.cypress.io/api/cypress-api/cookies.html#Defaults */ Cypress.Cookies.defaults({ whitelist: [sessionCookieName, stateCookieName], }); -
sir-dunxalot revised this gist
May 26, 2020 . 1 changed file with 0 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -54,11 +54,6 @@ Cypress.Commands.add('login', () => { const { accessToken, expiresIn, idToken, scope } = response; cy.getUserInfo(accessToken).then((user) => { const persistedSession = { user, idToken, -
sir-dunxalot revised this gist
May 26, 2020 . 1 changed file with 13 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -13,19 +13,28 @@ const stateCookieName = Cypress.env('stateCookieName'); let cachedUsername; /* 2. Keep auth cookies between tests https://docs.cypress.io/api/cypress-api/cookies.html#Defaults */ ypress.Cookies.defaults({ whitelist: [sessionCookieName, stateCookieName], }); Cypress.Commands.add('_login', /* ... */); Cypress.Commands.add('login', () => { /* 3. We see if a session cookie already exists */ cy.getCookie(sessionCookieName).then((cookieValue) => { /* 4. We see if the user is the same user previously auethenticated */ const cachedUserIsCurrentUser = cachedUsername && cachedUsername === username; /* 5. If the user is already authenticated, return true, which skips the rest of the helper code */ if (cookieValue && cachedUserIsCurrentUser) { return true; @@ -35,7 +44,7 @@ Cypress.Commands.add('login', () => { password: Cypress.env('auth0Password'), }; /* 6. Clear any cookie before authenticating in case the user was changed since last authentication */ cy.clearCookies(); -
sir-dunxalot revised this gist
May 26, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,7 +9,7 @@ const auth = new auth0.WebAuth({ const sessionCookieName = Cypress.env('sessionCookieName'); const stateCookieName = Cypress.env('stateCookieName'); /* 1. Let's plan to store the already-authenticated user's username */ let cachedUsername; -
sir-dunxalot created this gist
May 26, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,73 @@ import auth0 from 'auth0-js'; import Iron from '@hapi/iron'; const auth = new auth0.WebAuth({ domain: Cypress.env('auth0Domain'), clientID: Cypress.env('auth0ClientId'), }); const sessionCookieName = Cypress.env('sessionCookieName'); const stateCookieName = Cypress.env('stateCookieName'); /* 1. Let's plan to store the already-auehtnticated user's username */ let cachedUsername; Cypress.Commands.add('_login', /* ... */); Cypress.Commands.add('login', () => { /* 2. We see if a session cookie already exists */ cy.getCookie(sessionCookieName).then((cookieValue) => { /* 3. We see if the user is the same user previously auethenticated */ const cachedUserIsCurrentUser = cachedUsername && cachedUsername === username; /* 4. If the user is already authenticated, return true, which skips the rest of the helper code */ if (cookieValue && cachedUserIsCurrentUser) { return true; } else { const credentials = { username: Cypress.env('auth0Username'), password: Cypress.env('auth0Password'), }; /* 5. Clear any cookie before authenticating in case the user was changed since last authentication */ cy.clearCookies(); cy.setCookie(stateCookieName, 'some-random-state'); cy._login(credentials).then((response) => { const { accessToken, expiresIn, idToken, scope } = response; cy.getUserInfo(accessToken).then((user) => { /* https://github.com/auth0/nextjs-auth0/blob/master/src/handlers/callback.ts#L44 */ /* https://github.com/auth0/nextjs-auth0/blob/master/src/handlers/callback.ts#L47 */ /* https://github.com/auth0/nextjs-auth0/blob/master/src/session/cookie-store/index.ts#L57 */ const persistedSession = { user, idToken, accessToken, accessTokenScope: scope, accessTokenExpiresAt: Date.now() + expiresIn, createdAt: Date.now(), }; cy.seal(persistedSession).then((encryptedSession) => { cy.setCookie(sessionCookieName, encryptedSession); }); }); }); } }); }); Cypress.Commands.add('getUserInfo', /* ... */); Cypress.Commands.add('seal', /* ... */);