Skip to content
·4 min read

Testing Authentication Flows Thoroughly and Correctly

The complete guide to unit, integration, and E2E tests for auth so you stop discovering security gaps in production

Share

Test authentication and authorization separately. Authentication establishes a user's session; authorization decides what that user may do. A passing login test does not prove that another account's records are protected.

Use a disposable copy of the app. Before creating accounts, check that it connects to a separate sign-in setup and test database. Check that the sign-in settings point back to the test address. If the app sends confirmation or recovery messages, verify that they stay in a local test inbox or go only to email addresses or phones you control. If you can’t verify those connections, keep the exercise on paper. Write down what you expect to happen for each case before you try it.

Define the session contract first

Write down how long sessions last, how refresh works, what sign-out revokes and what happens after password recovery. Some systems use short-lived access tokens that remain valid until expiry even after a refresh token is revoked. Test the documented and intended behavior rather than assuming every product instantly invalidates every token.

Use the authentication library's supported verification. Do not decode a token and cast its payload to a trusted TypeScript interface. Validate the signature and the applicable algorithm, issuer, audience, expiry and claim shape through the supported mechanism.

Cover the core matrix

CaseExpected evidence
Valid credentialsAuthenticated state and access to the user's intended resources
Invalid credentialsNo session, useful message and no sensitive detail disclosed
Signup awaiting confirmationCheck-email state; no false completed-signup claim
Invalid or expired confirmationStart signed out. Opening the link leaves the user signed out and keeps the private page closed.
Signed-out protected requestDenied without private data
Expired sessionSafe refresh or return to sign-in according to the contract
LogoutSubsequent access behaves according to the revocation policy
Recovery link replayExpired or consumed links cannot reset a password
Wrong-user record IDNo access and no change to the other user's data
Concurrent attemptsRate and state controls hold across simultaneous requests

Keep accounts and browser state isolated

Use separate browser contexts or profiles for account A, account B and signed-out requests. Shared authenticated state is suitable only when the tests do not interfere with each other's server-side data. Give mutating parallel tests independent accounts or fixtures.

Playwright's authentication guide documents reusable state and isolation. Saved auth state can impersonate the test account; keep it out of Git and restrict its storage. Reusing a state file is an optimization, not proof that login still works.

Check the underlying result

Create a private record as A. As B, request its exact identifier, attempt an update and attempt deletion. Inspect both the response and A's stored record afterward. An RLS-protected update may affect zero rows without returning the error you expected.

For storage, first confirm that the test copy uses a separate test storage connection. Use a dummy file owned by test account A. Skip these checks if you can’t verify the connection. Try the actual object request and the signed-URL creation path with each account’s normal session. A service-role request or privileged SQL-editor query can bypass the boundary you intend to exercise.

For the UI, inspect slow, failed, expired and successful paths. A form should keep useful input after a recoverable failure, stop its loading state and give a clear next action. Use the verification walkthrough for rendered evidence.

Retain meaningful regression checks

Keep pure logic checks for expiry and policy decisions, integration checks for the real auth and data services, and browser checks for redirects, cookies and visible states. Name the missing layer if you only ran one. A mock cannot establish that the deployed cookie domain or callback is correct.

Once a browser case works by hand, turn the journey into a repeatable check. Check that it fails when the app shows success without saving the expected result.

Before release, repeat the relevant browser checks on the test deployment with the isolated connections above. Keep raw tokens, passwords, email links and private customer records out of logs and analytics used to report the result.

Written forDevelopers
PJ
Pranay Joshi

20+ years building products at scale. VP of Product & Engineering, startup founder, and AI coach. Helping dreamers turn ideas into reality with vibe coding.