Skip to content

Instantly share code, notes, and snippets.

@sir-dunxalot
Last active May 26, 2020 20:48
Show Gist options
  • Select an option

  • Save sir-dunxalot/f88ce6eb51611ec72ce27fa1002440f3 to your computer and use it in GitHub Desktop.

Select an option

Save sir-dunxalot/f88ce6eb51611ec72ce27fa1002440f3 to your computer and use it in GitHub Desktop.

Revisions

  1. sir-dunxalot revised this gist May 26, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion commands.js
    Original 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
    */

    ypress.Cookies.defaults({
    Cypress.Cookies.defaults({
    whitelist: [sessionCookieName, stateCookieName],
    });

  2. sir-dunxalot revised this gist May 26, 2020. 1 changed file with 0 additions and 5 deletions.
    5 changes: 0 additions & 5 deletions commands.js
    Original 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) => {

    /* 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,
  3. sir-dunxalot revised this gist May 26, 2020. 1 changed file with 13 additions and 4 deletions.
    17 changes: 13 additions & 4 deletions commands.js
    Original 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', () => {

    /* 2. We see if a session cookie already exists */
    /* 3. 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 */
    /* 4. 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 */
    /* 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'),
    };

    /* 5. Clear any cookie before authenticating in case the user was changed since last authentication */
    /* 6. Clear any cookie before authenticating in case the user was changed since last authentication */

    cy.clearCookies();

  4. sir-dunxalot revised this gist May 26, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion commands.js
    Original 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-auehtnticated user's username */
    /* 1. Let's plan to store the already-authenticated user's username */

    let cachedUsername;

  5. sir-dunxalot created this gist May 26, 2020.
    73 changes: 73 additions & 0 deletions commands.js
    Original 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', /* ... */);