- Set up automated tests for different authentication products
- Generate Stytch session tokens or JWTs
- Avoid Stytch rate limits when testing
Automated tests for passwordless authentication flows
You may need to complete a passwordless authentication flow during some of your automated tests. The following table outlines our recommended testing strategy based on your test requirements:| Tests that use… | Recommended testing strategy |
|---|---|
| Backend Stytch endpoints in a Test environment | Leveraging our sandbox values to trigger predetermined Stytch API responses |
| Frontend Stytch SDKs, any Stytch endpoints or methods in the Live environment, or behavior unique to specific Users or Sessions | Using a platform like Mailosaur to set up a programmatically accessible email or SMS inbox that you can use in your tests |
You can find the repository that accompanies the above talk here.
OAuth in particular is generally not compatible with E2E testing, given that OAuth providers (like Google) do not allow you to log in via browser automation. If you offer OAuth in your application, we recommend using an alternative authentication product in your E2E tests.
Stytch validates Origin headers from SDK requests. When using Cypress, you’ll either need to ensure that
chromeWebSecurity: false, or if it must be true in the context of your tests, inject Origin
headers into SDK requests.Bypassing Email Magic Links bot detection
We have a bot detection mechanism built into our Email Magic Links flow that prevents email security bots from following and invalidating your users’ Magic Link tokens. For additional information, see our Magic Links guide. You may run into this bot protection when setting up automated Email Magic Links or Password reset by email tests. Below is a strategy that will allow your tests to proceed by bypassing the redirect to the Stytch-hosted page where bot detection runs:Extract the Magic Link query parameters
Instead of following the Magic Link URL from Email Magic Links emails directly, extract only the query parameters from the URL. For example, the Magic Link included in the email will look something like this:
https://test.stytch.com/v1/magic_links/redirect?public_token=YOUR_PUBLIC_TOKEN&stytch_token_type=magic_links&token=TOKEN_VALUE.You’d extract the query parameters only: public_token=YOUR_PUBLIC_TOKEN&stytch_token_type=magic_links&token=TOKEN_VALUE.Add the query parameters to your own login URL
Add those query parameters to your own login or signup redirect URL, and then redirect directly to that URL instead. For example, If your login URL were
https://yourdomain.com/yourloginpath, you’d redirect to https://yourdomain.com/yourloginpath?public_token=YOUR_PUBLIC_TOKEN&stytch_token_type=magic_links&token=TOKEN_VALUE during your automated tests.Here’s example code that demonstrates the above strategy:Generating a Stytch session token or session JWT
Sometimes, you’ll need to generate a Stytch session token or JWT for use in E2E tests that aren’t intended to test your login flow itself. For this use case, a common strategy is to leverage passwords for testing purposes, even if you don’t use our Passwords product as part of your production login flow:- Create a test User with test
emailandpasswordvalues. - At the beginning of each test that requires a Stytch session token or JWT, call the Authenticate password endpoint with the test
emailandpasswordvalues from the previous step. Be sure to specify thesession_duration_minutesparameter so that a Stytch Session is started. - Retrieve the
session_tokenorsession_jwtvalue from the Authenticate password response for use in the rest of your test.