cypress-keycloak
cypress-keycloak copied to clipboard
Get user tokens for calling APIs from E2E tests
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
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!
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.
Any news on this? It's been a little while and it'd really come in handy to be able to access the token.
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);
});
});
});