Skip to content
This repository has been archived by the owner on Aug 5, 2024. It is now read-only.

Get user tokens for calling APIs from E2E tests #39

Open
dthisner opened this issue Sep 8, 2022 · 4 comments
Open

Get user tokens for calling APIs from E2E tests #39

dthisner opened this issue Sep 8, 2022 · 4 comments

Comments

@dthisner
Copy link

dthisner commented Sep 8, 2022

Is Cypress-Keycloak supporting the ability just to grab the token?

This old Cypress-Keycloak-Commands repo is supporting following:

describe("Keycloak Login", () => {
  beforeEach(() => {
    cy.kcLogout();
    cy.kcLogin("user").as("tokens");
    cy.visit("/");
  });

  it("should call an API with the token", () => {
    cy.get("@tokens").then(tokens => {
      cy.request({
        url: "/my_api"
        auth: {
          bearer: tokens.access_token
        }
      });
    });
  });
});

https://github.com/Fredx87/cypress-keycloak-commands#get-user-tokens-for-calling-apis-from-e2e-tests

@Waterstraal
Copy link

Waterstraal commented Oct 6, 2022

I also want to migrate away from cypress-keycloak-commands to cypress-keycloak, but the lack of this feature is preventing me from doing so. I assume there are many more people that want to do the same :).

@babangsund, is there any intention to add this functionality? Thanks!

@miwurster
Copy link

miwurster commented Jan 4, 2023

Hi, yeah we have the same requirement and we upgraded to this lib due to the lack of Keycloak >18 support of cypress-keycloak-commands (the kcLogout command cannot handle the logout confirmation atm).

As a workaround we now use both libs, cypress-keycloak-commands to log in and cypress-keycloak just to handle log outs. Seems to work for us at the moment.

@paulchartres
Copy link

Any news on this? It's been a little while and it'd really come in handy to be able to access the token.

@Alex-G
Copy link

Alex-G commented Mar 28, 2023

I do this to get the token:

Cypress.Commands.overwrite('login', (originalFn) => {
  originalFn({
    root: 'test',
    realm: 'test',
    username: 'test',
    password: 'test',
    client_id: 'test',
    redirect_uri: 'test',
  }).then((response) => {
    // get authentifaction code from response
    const code = '......';
    cy.request({
      method: 'POST',
      url: `${host}/auth/realms/${id}/protocol/openid-connect/token`,
      headers: {
        'content-type': contentTypeApplication,
      },
      body: {
        code: code,
        redirect_uri: baseUrl,
        grant_type: 'authorization_code',
        client_id: 'client',
      },
    })
      .its('body')
      .as('tokens')
      .then((tokens) => {
        localStorage.setItem('token', tokens.access_token);
        localStorage.setItem('refreshToken', tokens.refresh_token);
        localStorage.setItem('id_token', tokens.id_token);
      });
  });
});

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

No branches or pull requests

5 participants