IdentityModel / oidc-client-js

OpenID Connect (OIDC) and OAuth2 protocol support for browser-based JavaScript applications
Apache License 2.0
2.43k stars 841 forks source link

Suggested pattern for integrating with Cypress test #1209

Closed joe-mills closed 3 years ago

joe-mills commented 4 years ago

Hi,

Question - I'm hoping someone can suggest an approach for making a cypress login command, similar to the one in the article below, work with oidc-client-js

https://auth0.com/blog/end-to-end-testing-with-cypress-and-auth0/

Normally oidc-client-js saves some state to localstorage with the key oidc: before it redirects to IdentityServer with the sate id in the query params.

It then checks the saved state against the logged in details coming back from the IdentityServer when you call UserManager().signinRedirectCallback() on the callback.html page.

Because my cypress test just posts directly to /connect/token, oidc-client-js doesn't set the initial state and then fails the check when it compares the state returned from IdentityServer in callback.html.

I've tried populating the state manually but I can seem to get the guid that oidc-client-js generates or pass one of my own in.

Any hints appreciated

brockallen commented 4 years ago

Sorry, no idea.

larzon83 commented 4 years ago

@joe-mills I use vuex-oidc for Vue and have managed to trick my app to think it's logged-in. Maybe this can help you: https://github.com/perarnborg/vuex-oidc/issues/70#issuecomment-679187624

dgreene1 commented 3 years ago

So, to make this question more precise, @brockallen, would the oidc-client-js library behave exactly as expected if we set the storage as follows?

localStorage.setItem(
  `oidc.user:http://localhost:8080/auth/realms/${REALM_NAME}:${CLIENT_NAME},
  JSON.stringify(token)
);

If the library would work exactly as expected, then a Cypress test would be capable of operating as if the user had actually logged in.

larzon83 commented 3 years ago

@dgreene1 yes it should. See my post above

sadek-hussein commented 3 years ago

@joe-mills your Cypress issue was solved? I have the same one! can you please help me