# Automation testing and PKCE

**URL:** <https://community.auth0.com/t/automation-testing-and-pkce/64071>\
**Category:** Get Help\
**Tags:** pkce, react, testing\
**Created:** [June 8, 2021, 7:18am UTC](https://community.auth0.com/t/automation-testing-and-pkce/64071 "2021-06-08T07:18:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sazzer](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/sazzer/32/27418_2.png) [@sazzer](https://community.auth0.com/u/sazzer)\
**Post date:** [June 8, 2021, 7:18am UTC](https://community.auth0.com/t/automation-testing-and-pkce/64071/1 "2021-06-08T07:18:24Z")

</div>

I’m currently using the `@auth0/auth0-react` library to manage login and logout in my app, and it works great. However, I’m now looking to add automation tests to it and I’m looking for a way to fake logging in.

I did find a blog post at [End-to-End Testing with Cypress and Auth0](https://auth0.com/blog/end-to-end-testing-with-cypress-and-auth0/) which attempts to describe how to do this, but it doesn’t work with this library. Doing the login in the background, and then redirecting to the page as if it was an OAuth2 Implicit Grant simply does nothing. No errors, no network requests, and certainly not logged in.

It turns out that `@auth0/auth0-react` uses PKCE, which is great. But this means that I also can’t just fake a redirect to `/?code=...&state=...`, because the `state` value needs to be present in the `a0.spajs.txs` session storage object, and that also needs to store a valid `code_verifier` value.

Is there a recommended way to automation test logging in when using `@auth0/auth0-react`? Or should I rework with one of the other client libraries?

Cheers

Edit: I should say that right now I’m driving the Auth0 login dialog, and have had to configure it to force login every time - with `prompt:"login"`. That’s not a great solution for a number of reasons, which is why I’m trying to find a better way to do this.

---

<div class="post-metadata">

**Author:** ![sazzer](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/sazzer/32/27418_2.png) [@sazzer](https://community.auth0.com/u/sazzer)\
**Post date:** [June 9, 2021, 5:49pm UTC](https://community.auth0.com/t/automation-testing-and-pkce/64071/2 "2021-06-09T17:49:26Z")

</div>

It _looks_ like I can fake this from some other stuff I’ve found. In particular, if you set `cacheLocation="localstorage"` on the `Auth0Provider` then the resolved token details are stored in Local Storage instead of JavaScript memory. This means that if you’re able to generate values for the local storage key and value that are valid then you can inject these and presto you’re logged in.

Now, the values here are non-trivial. The key is easy enough: `@@auth0spajs@@::[clientId]::[audience]::[scopes]`. But the value is a JSON document containing:

- Client ID
- Token response details (Access Token, ID Token, Scopes, Expires In, Token Type)
- Actual expiry time
- Decoded ID Token details

It _seems_ that if I can get all of these details and build a local storage with them correctly then I can set that, reload the page and auth0-react suddenly thinks I’m correctly logged in.

I’m _hoping_ there’s something within auth0-spa-js that I can leverage to do this work for me, so that I can just give it the correct values and all is good. Watch this space 🙂

---

<div class="post-metadata">

**Author:** ![sazzer](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/sazzer/32/27418_2.png) [@sazzer](https://community.auth0.com/u/sazzer)\
**Post date:** [June 9, 2021, 6:11pm UTC](https://community.auth0.com/t/automation-testing-and-pkce/64071/3 "2021-06-09T18:11:13Z")

</div>

Even better, it seems the absolute minimum I can get away with in the local storage value is:

```auto
{
	"body": {
		"access_token": "<someAccessToken>",
		"id_token": "<someIdToken>",
		"decodedToken": {
			"user": {
			    <decoded ID Token claims>
			}
		}
	},
	"expiresAt": 1623296339
}

```

That’s probably not _too_ hard to work with.

---

<div class="post-metadata">

**Author:** ![dan\_ap](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan_ap/32/31893_2.png) [@dan\_ap](https://community.auth0.com/u/dan_ap)\
**Post date:** [September 23, 2021, 11:58pm UTC](https://community.auth0.com/t/automation-testing-and-pkce/64071/4 "2021-09-23T23:58:07Z")

</div>

Not a lot of activity on this post so far, but I just wanted to say that I was able to adapt this to work for **Cypress tests in an Angular app** using the **`@auth0/auth0-angular`** SDK.

In case it helps others in this situation, some specifics:

- When initializing the application, include `cacheLocation: 'localstorage'` in the config passed to `AuthModule.forRoot()`
- Use password grant to fetch a valid token response from `/oauth/token`
- Before each test, push JSON similar to the example above into localstorage using the key format mentioned by @sazzer: `@@auth0spajs@@::[clientId]::[audience]::[scopes]`
- Voila, user is logged in for the purposes of the spec

🎉 Much appreciated, thanks for the assist!

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [September 24, 2021, 9:58am UTC](https://community.auth0.com/t/automation-testing-and-pkce/64071/5 "2021-09-24T09:58:12Z")

</div>

Thanks for sharing that with the rest of community!

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [September 30, 2021, 9:46am UTC](https://community.auth0.com/t/automation-testing-and-pkce/64071/6 "2021-09-30T09:46:40Z")

</div>


