Giter Site home page Giter Site logo

Comments (3)

jdtully avatar jdtully commented on July 19, 2024

from cypress-keycloak-commands.

sureshbabudj avatar sureshbabudj commented on July 19, 2024

@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);
  });
});

from cypress-keycloak-commands.

jdtully avatar jdtully commented on July 19, 2024

from cypress-keycloak-commands.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.