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

fakeLogin is not working with cypress 12 #57

Open
sureshbabudj opened this issue Feb 3, 2023 · 3 comments
Open

fakeLogin is not working with cypress 12 #57

sureshbabudj opened this issue Feb 3, 2023 · 3 comments

Comments

@sureshbabudj
Copy link

sureshbabudj commented Feb 3, 2023

cypress-keycloak-commands library is still using cy.sever() and cy.route() . These commands are deprecated in Cypress 12 version. the test cases fail with cypress 12.

the below code in file

    cy.server();

    cy.route(
      "post",
      `${authBaseUrl}/realms/${realm}/protocol/openid-connect/token`,
      token
    );

    cy.route(`${authBaseUrl}/realms/${realm}/account`, account);

should be replaced with the below in order to work:

    cy.intercept(
        {method: 'post', url: authBaseUrl + "/realms/" + realm + "/protocol/openid-connect/token"},
        token
    );
    cy.intercept({method: 'get', url: authBaseUrl + "/realms/" + realm + "/account"}, account);

Source: https://docs.cypress.io/guides/references/migration-guide

@jdtully
Copy link

jdtully commented Feb 3, 2023 via email

@sureshbabudj
Copy link
Author

@jdtully I can attach the file with an updated code

// https://github.com/Fredx87/cypress-keycloak-commands/blob/develop/src/kc-fake-login.ts
// the cypress-keycloak-commands library is still using cy.sever() and cy.route() 
// which commands are deprecated in cypress 12 version. it fails with cypress 12.
// this file hold the same logic with using cy.intercept() instead of using the above commands 
// This file should be deleted once the library starts supporting the latest version of cypress
// reported issue: https://github.com/Fredx87/cypress-keycloak-commands/issues/57

import { createUUID, decodeToken } from "cypress-keycloak-commands/dist/utils";

export default Cypress.Commands.add("kcFakeLogin", (user: string, visitUrl = "") => {
  Cypress.log({ name: "Discover Fake Login" });

  return cy.fixture(`users/${user}`).then((userData: UserData) => {
    if (!userData.fakeLogin) {
      throw new Error(
        "To use kcFakeLogin command you should define fakeLogin data in fixture"
      );
    }

    const authBaseUrl = Cypress.env("auth_base_url");
    const realm = Cypress.env("auth_realm");
    const {
      account,
      access_token,
      refresh_token,
      id_token
    } = userData.fakeLogin;

    const state = createUUID();
    const { nonce } = decodeToken(access_token);

    const token = {
      access_token,
      expires_in: 300,
      refresh_expires_in: 1800,
      refresh_token,
      token_type: "bearer",
      id_token,
      "not-before-policy": 0,
      session_state: createUUID(),
      scope: "openid"
    };

    const localStorageObj = {
      state,
      nonce,
      expires: Date() + 3600
    };

    const localStorageKey = `kc-callback-${state}`;

    window.localStorage.setItem(
      localStorageKey,
      JSON.stringify(localStorageObj)
    );

    // the below 2 lines are updated to work with cypress 12
    cy.intercept({method: 'post', url: authBaseUrl + "/realms/" + realm + "/protocol/openid-connect/token"}, token)
    cy.intercept({method: 'get', url: authBaseUrl + "/realms/" + realm + "/account"}, account);

    // in case visitUrl is an url with a hash, a second hash should not be added to the url
    const joiningCharacter = visitUrl.indexOf("#") === -1 ? "#" : "&";

    const url = `${
      Cypress.config().baseUrl
    }/${visitUrl}${joiningCharacter}state=${state}&session_state=${createUUID()}&code=${createUUID()}`;
    cy.visit(url);
  });
});

@jdtully
Copy link

jdtully commented Feb 6, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants