Ananya runs the API regression suite for a logistics platform in Kochi. It has 600 tests and takes about 40 minutes. Every night it starts green. Every night, somewhere around the 15-minute mark, tests begin failing with 401 Unauthorized, and by the end a third of the suite is red. In the morning she reruns the failures alone, they pass, and everyone shrugs and calls it flaky.
It is not flaky. It is deterministic, and the clue is the 15 minutes. Let us use her suite to walk through how OAuth 2.0 tokens behave and what automation has to do about them.
What the suite is actually holding
When her suite authenticates, it calls the authorisation server’s token endpoint and receives an access token. That token is a short-lived pass. In her system the response says expires_in is 900, meaning 900 seconds, which is 15 minutes. The suite fetched it once in a setup step and reused it for everything. After 15 minutes the API gateway correctly refuses it.
Short lifetimes are deliberate. If an access token leaks, it is only useful for a short while. So a test that assumes a token lasts forever is testing an insecure system, in effect.
Which flow does a test suite use?
OAuth 2.0 has several grant types, and the right one for automation depends on what is being tested.
Her suite calls the platform’s own APIs as a backend service with no human in the loop. For that, the client credentials grant fits: the suite sends a client id and secret and gets a token representing the application. Simple, and no browser needed.
But some of her tests need to act as a particular user, say a warehouse supervisor, to check that role-based screens return the right data. The authorisation code flow is the proper one for users, and it involves a browser redirect and a login page. Automating that for every test is slow. Teams commonly do the interactive login once, or use a test-only shortcut the identity provider offers for non-production, and then reuse the resulting tokens. Some older systems still use the resource owner password grant, where the suite sends a username and password directly. It is discouraged in current OAuth guidance, but you may meet it in a QA environment. Know which one you are looking at before you write a helper around it.
Fixing the 401s
Ananya has three honest options.
One: fetch a fresh token before each test. It is simple and safe, but 600 token calls hammer the identity provider, and some rate-limit it. Fine for small suites.
Two: wrap the token in a small provider object that remembers when it expires. Before returning the token, it checks the clock. If less than, say, 60 seconds remain, it fetches a new one. Tests ask the provider for a token every time and never think about expiry. This is what I would choose. The safety margin matters, because a token that is valid when you read it can expire before the request arrives.
Three: use the refresh token, if the flow issued one. A refresh token lets the client obtain a new access token without logging in again. Useful when the login step is expensive, as in user flows. Many providers rotate refresh tokens, meaning each use invalidates the old one, so two parallel workers sharing a single refresh token can break each other. If tests run in parallel, give each worker its own token, or serialise the refresh behind a lock.
The tests nobody wrote yet
Once her helper handles the happy path, Ananya can test the awkward parts on purpose. These are the cases where authentication goes wrong, and they deserve tests of their own:
- An expired token is refused with 401, not silently accepted.
- A token with the wrong scope, for example read-only, gets 403 on a write, because the caller is known but not permitted.
- A tampered or malformed token is refused.
- No Authorization header at all returns 401 with a sensible message.
- A token issued to one client is not honoured for another tenant’s data.
- After logout or revocation, the token stops working, if the system supports revocation.
The 401 versus 403 distinction trips people up. A 401 says “I do not know who you are”, usually missing or invalid credentials. A 403 says “I know who you are and the answer is no”. Checking that the API picks the right one is a quick way to find sloppy error handling.
Keeping secrets out of the repository
One more habit. The client secret in her config file was committed to the repository. Anyone who can read the repository can mint tokens. Secrets belong in the CI system’s secret store or a vault, injected as environment variables at run time, and test accounts should have only the permissions tests need. Never reuse a production client id for tests. Tokens also should not appear in logs or in the saved request and response artefacts that reporting tools attach, so mask the Authorization header there.
With the token provider in place, her next nightly run stays green to the end. The failures were never about flakiness. The suite had been outliving its own credentials, and nobody had asked how long a token lives.
Figures and names in the examples are illustrative.
Leave a Reply