Testing web applications

How to test 2FA, OAuth and social login

Signing in with a text message, authenticator app, or social account connects your application to another system. A single end-to-end test of the entire journey can be slow and depend on account state and a provider you do not control. A more reliable strategy assigns different questions to different test layers and reserves the complete journey for a small number of critical scenarios.

Why an ordinary login test becomes unreliable

Two-factor authentication (2FA) combines two different factors, such as a password and proof of device possession through a one-time code or approval. Automation needs a safe way to obtain that proof in a test environment. Waiting for a team member’s personal phone is not repeatable. A shared account may also change session state, exhaust allowed attempts, or become locked when tests run in parallel.

Social login adds a second boundary: the user leaves your domain and later returns to a callback URL. OAuth 2.0 provides a framework for delegated authorisation. OpenID Connect commonly adds the identity layer used for sign-in. Your test does not need to retest the provider’s own login page. It needs to show how your application starts the flow, handles the return, and creates or rejects a session.

Split coverage across three layers

1. Keep your application’s behaviour under your control. Test the available login choices, input validation, error messages, and resulting user state. Controlled provider responses can represent success, denied consent, missing profile data, or a provider error. These tests are fast, but they do not prove that the real connection works.

2. Check the integration in the provider’s test tenant or supported test mode. Use a separate client configuration, exact callback URLs, and accounts without production privileges. Cover a successful return, user cancellation, rejected or invalid responses, identity mapping, and the case where the person already has an account in your application. Provider capabilities differ, so verify the available mechanisms in the provider’s current documentation.

3. Send only a few end-to-end scenarios through the full journey. A representative check can confirm that the browser reaches the provider and returns to the correct account. Broader role combinations and error cases belong in lower layers. If third-party sign-in is a critical production journey, its availability can be observed after careful preparation, much like other third-party integrations.

Manage 2FA and sessions deliberately

Provide a mechanism intended for testing in a non-production environment: a dedicated inbox for one-time codes, a virtual authenticator, or a facility supported by the provider. Keep it separate from production and accessible only to the test process. A universal code or secret header that also works in production would create a security bypass, not testability.

Not every test needs to repeat login. Once the login flow is covered separately, other scenarios can start with a session created through a supported API or saved browser state. Playwright documents authentication state reuse and warns that the state file may contain sensitive cookies and headers. Do not commit it to Git, limit its lifetime, and assign separate accounts to parallel workers when they modify shared state.

Give the session itself explicit coverage. Test logout, expiry, revoked access, a changed role, and an attempt to open a protected page after the session ends. For 2FA, include an incorrect or expired code, reuse of a code, and the product’s expected response to excessive attempts.

Choose the test mechanism for the factor in use

SMS codes, time-based codes from an authenticator app and WebAuthn use different procedures. A virtual authenticator is not a universal replacement for all three. For example, Chrome documents authenticator emulation for WebAuthn, a specific user-authentication interface, rather than SMS delivery.

For a time-based code, known as TOTP, the test account needs its own protected secret and a prepared clock state. For a delivered code, distinguish the message for the current attempt from an older message. Select the code by test account and pending verification, rather than simply take the latest entry in a shared inbox.

Check that a used code is rejected even while it has not expired. NIST describes protection against OTP reuse. For TOTP, check the validity boundary according to product configuration and permitted clock drift. Do not set a universal wait that assumes every provider has the same time window.

The return must belong to the login that was started

In a controlled test, start two separate login sessions. Try handing the application a return belonging to the other session. The outcome must not be a login to the wrong account. Also check an invalid response and reuse of a return already consumed, according to the flow in use.

The specific protections depend on the protocol. OAuth uses mechanisms such as PKCE to bind an authorisation code to the attempt. Where the integration uses state or nonce, check their binding and validation. It would be incorrect to say all three values are mandatory in every flow. The chosen protection must work with a swapped or invalid response too.

For OpenID Connect, also test a response from the wrong issuer, a token intended for another client and an expired token. Run checks at the integration layer that validates the result, rather than only when a success page appears. Record the rejection reason without full tokens or secrets. These scenarios are specific integration checks, rather than a complete security assessment.

User identity is more than an email address

In OpenID Connect, stable identity comes from the combination of issuer iss and subject sub. Email can change and is not a guaranteed unique identifier across issuers. The application therefore needs an explicit rule for creating and linking accounts.

Prepare two illustrative external accounts, A and B. Check the resulting internal account and permissions, rather than only the name in the header. Then use controlled responses with matching email addresses and different external identities. The test should confirm the approved linking rule and prevent a matching field from granting access to another account without the necessary verification.

For an already linked account, check repeat sign-in and an email change without unintentionally creating a second user. Test linking or unlinking a provider separately if the product supports it. A missing profile field should lead to the agreed response, rather than arbitrary assignment to an existing account.

A saved session does not verify a new login

A page test using prepared cookies can check the signed-in user’s work. It does not establish that login, the second factor or access recovery can be completed today. Keep these journeys covered separately.

Check What the result should show
Valid login for account A The correct internal account and role after the required factors are completed.
Incorrect, used or expired code Rejection without establishing a full session for the protected area.
Return from another login session Rejection of the swapped response, without switching to another account.
Session expiry or revocation Protected operations are denied according to the agreed session policy.

Record which checks used controlled responses and which used a real test tenant. When the provider is unavailable, mark the complete journey as unverified. Successful tests using prepared sessions may remain valid for their own scope, but do not establish the missing integration result.

Important boundaries

A test facility must not weaken production security rules. The OAuth 2.0 Security Best Current Practice provides current guidance for designing OAuth flows. Functional automation is not a security audit, however, and cannot demonstrate resistance to every attack.

Treat test accounts as managed test data: record their owner, role, secret storage, and reset process. The guide to preparing a website for test automation covers the wider environment and data requirements.

What you gain

Next step

Draw the login journey from the first click to the established session and mark which system or provider owns each step. Decide which responses to simulate, which to verify in the provider’s test environment, and which single journey must run end to end. Add only stable checks to the appropriate stage of CI/CD.

Related topics

You might also be interested in

Catch bugs before they reach users

End-to-end tests of key scenarios can run automatically in CI/CD to catch bugs before they reach customers.