OAuth 2.0 Testing: How to Test an API That Needs a Token, Without Code

OAuth 2.0 testing: how to test an API that needs a token, which flow to use, the test cases to copy (401, 403, expired), and how to keep secrets out of tests.

By Ait Nohaila, Business Development Representative 14 min read

To test an API with OAuth 2.0, sign in as an app made for your tests, then send each request with the token you get. Test the refusals too: no token, a bad token, a missing right.

You may know the other way. Someone gets a token by hand and pastes it into the test. Everything passes until the token ends. The next morning every API test answers 401, and the person who knows how to get a new one is on holiday.

What is OAuth 2.0 testing?

Definition

OAuth 2.0 testing checks that an API opens its door to the right callers, and only to them.

  • OAuth 2.0 is the common way for an app to ask for access: it signs in at one service, gets a token, and shows that token to the API.
  • A token lasts for a limited time, and its scope says what it may do.

So a test covers two things: the sign-in gives a token, and the API respects what the token says.

This is API authentication testing, a part of functional testing. It checks the promises your API makes to its callers. It is not a security audit, which looks for ways to break in.

Types of authentication in API testing

OAuth 2.0 is one of several ways. Your API's documentation says which one it takes, and that decides what your tests send.

TypeWhat the caller sendsWhat to test
No authenticationNothingThat public data is all it gives
User name and passwordBoth, with every requestA wrong password, an unknown user, nothing at all
API keyA key, in a header or in the addressA wrong key, a key that was withdrawn, no key
Bearer tokenA token it already hasA valid, a malformed and an expired token, and no token
OAuth 2.0A sign-in first, then the token it gotThe sign-in itself, every case of a bearer token, and the scope

The token OAuth 2.0 gives is usually a bearer token.RFC 6750 explains the word: any party that holds the token, the bearer, can use it.Treat a token like the key to a building, and keep it out of test files, screenshots and chats.

Client credentials vs authorization code: which one does a test use?

OAuth 2.0 describes several ways to get a token, called flows or grants. Two of them matter most to a tester.

QuestionClient credentialsAuthorization code
Who signs inThe app, as itselfA person, who lets the app act for them
What it needsA client ID and a client secretA web page, and a person in front of it
Where it is usedOne server calling another, scheduled jobs, API testsWeb and mobile apps that people use
What the token stands forThe appThat person, through the app
How to test itWith a Call API step, with no browserBy recording your own app's sign-in page

For an API test, client credentials is the natural choice.RFC 6749 puts it in one line: "The client can request an access token using only its client credentials".The other flow is in the same document.

Call API step

One step, two requests

1The sign-in service

Receives
Client ID, client secret, scope
Gives back
A token, and how long it lasts

2The API

Receives
The request, with the token
Gives back
The answer your checks read

The next steps that sign in the same way skip the first request: they use the same token until it ends.

The client credentials flow: the app signs in as itself, with no person and no browser.

One thing this token cannot show is a rule about people. It stands for an app, so it cannot prove that Sara may not open Omar's order. Test that rule through your app's interface, with two test accounts, or with the tokens your own test sign-in gives for test users.

Why a test should not sign in with a user's password

There is an older shortcut: send a user's name and password to the sign-in service, and get a token.It needs no web page, so some tests still use it.Today's security guidance, RFC 9700, Best Current Practice for OAuth 2.0 Security, rules it out.

"The resource owner password credentials grant [RFC6749] MUST NOT be used."
RFC 9700, section 2.4

Its reasons matter to a tester too.

The app, and here the test, sees the user's password, so the password can leak in more places.

It was not designed for a sign-in with a second step, such as a code on a phone.

A Call API step can sign in this way, with As a test user, because some older test systems still take nothing else.Our advice is plain, and in this order.

  • 1Use "As the app itself" whenever your sign-in service allows it.
  • 2Use "As a test user" only for an older server that offers nothing else.
  • 3When you do, use a throwaway account made for the tests, never a real person's.
  • 4Ask the team that runs that server when client credentials will be possible, and change the test that day.

How to test an API with OAuth 2.0, step by step

GUI Test Studio automates end-to-end tests of any app without code.Its Call API step sends a request and checks the answer, and it can make the OAuth 2.0 sign-in itself.

  1. 1

    Get a client made for your tests

    Ask the team that runs your sign-in service for a client ID and a client secret for your tests, on the test server, with only the rights the tests need.

  2. 2

    Keep the secret on your computer

    Open Settings, then Variables..., and add clientSecret under This computer.It is kept encrypted for your Windows account, and never enters the test file.

  3. 3

    Add a Call API step

    Drag Call API from the Test Flow group onto the timeline, then click Edit Request... in its Properties.Choose the method and type the address, such as {{baseUrl}}/orders.

  4. 4

    Choose OAuth 2.0

    On Authorization, choose OAuth 2.0.Give the Token address, the Client ID and, as the Client secret, {{clientSecret}}.Add a Scope when the API's documentation names one.Keep the sign-in on As the app itself.

  5. 5

    Send, and check the answer

    Press Send.The step signs in, then sends the request.Click a value of the answer, then Check This Value.

  6. 6

    Add the refusals

    Add one Call API step for each case that must be refused.Start with one that has no authorization: press Send, then click Expect 401.

The rest of the editor, from the first request to the clean-up, is in API testing without code.

One sign-in for the whole run

A test with ten calls should not sign in ten times. The sign-in is made once. The later steps of the run that sign in the same way use the same token, until it ends.

And when the sign-in is refused, you see why. The step fails with the reason the sign-in service gave, such as a wrong secret or a scope it does not know. You do not have to guess from a 401 three steps later.

OAuth 2.0 test cases to copy

The call that works is one test. The refusals are the others, and they protect your customers' data.

  • A valid token with the right to do this200, with the data
  • No token401
  • A token that makes no sense401
  • A token that has expired401
  • A valid token without the right to do this403
Five callers, one request, in an API that follows the rules of bearer tokens. The four refusals are tests that pass: the API did its job.

The statuses follow RFC 6750, section 3.1: 401 for a token that is expired, revoked or malformed, 403 for a token without enough rights.They are strong recommendations, and some APIs choose others.Check what yours promises.

Test caseHow to send itWhat to expect
Valid token, right scopeOAuth 2.0, with the scope the action needsA success, and the right data
No tokenNo authorization at all401
Malformed tokenBearer token, with a word such as not-a-token401
Expired tokenBearer token, with a saved token that has ended401
Withdrawn token, when your service can withdraw oneBearer token, with a saved token you then withdrew401
Valid token, wrong scopeOAuth 2.0 with the scope to read, on a request that creates403 in many APIs, and nothing is created
A token made for another APIBearer token, with a token saved from the other API's sign-inA refusal, a 401 in many APIs
Another client's tokenOAuth 2.0 with a second test client, asking for the first one's orderA refusal, a 403 or a 404 in many APIs, and never the order
ExampleThe wrong scope must change nothing

A step signs in with the scope orders:read and tries to create an order for a customer no other test uses.It expects 403.The next step asks for that customer's orders and checks that there are none.Without it, an API that answers 403 and still saves the order would pass.

The last case of the table has a name of its own, and many lists forget it.It is explained with the other cases in API test cases you can copy.A GraphQL API can also refuse a single field, as GraphQL testing shows.

How to test an OAuth token and the service that gives it

If your team runs the sign-in service, test it like any other API. Make the sign-in itself the request of a Call API step.

In the stepWhat to fill in
The requestPOST {{tokenUrl}}
The bodyForm fields: grant_type is client_credentials, and scope is the scope you want
The authorizationUser name and password: {{clientId}} and {{clientSecret}}
What to checkaccess_token exists, token_type is the kind your API expects, and expires_in is more than 0
What to saveaccess_token, as readToken.A name that says token is hidden in every result.

That is the request RFC 6749 describes for client credentials, and its list of errors gives you the refusals to expect.

What goes wrongStatusValue of error
A wrong client secret, sent in the header401invalid_client
A grant type is missing400invalid_request
A scope the service does not know or allow400invalid_scope
A client that may not use this flow400unauthorized_client

How to test an expired token

  • 1Ask for a test client whose tokens last a very short time, such as one minute.
  • 2Get a token as above and save it.
  • 3Add a Wait step a little longer than the token's life.
  • 4Send the API request with the saved token as its Bearer token, and expect 401.

It is one of the rare good uses of a fixed pause, because the thing you test is time itself.Everywhere else, wait for the answer instead of guessing a time.

Can a test use the sign-in page a person sees?

Yes, when the page is yours. A Call API step does not open a sign-in page, so it does not cover the authorization code flow. Your app's sign-in is a screen like any other: record it once, and the test replays it.

The sign-in pageWhat to do
Your own app'sRecord it with a test account, in a test environment. Check what the person sees once signed in.
Your own service's, shared by several appsRecord it in the same way, and give the tests accounts of their own.
Another company'sIt often refuses automation, and that protection is there for a reason. Leave it out, and use your own service's test sign-in.

Record a test shows how, and end-to-end testing explains why the path a person takes deserves a test of its own.

Keep the client secret out of the test

The OWASP cheat sheet on secrets says of API keys, database credentials and the like: "Many organizations have them hardcoded within the source code in plaintext".A test file is no better place.It is copied, shared and reviewed.So the test should hold the secret's name, never its value.

On your computer

The secret is here

clientSecret

Kept under Variables, This computer, encrypted for your Windows account.

In the pipeline

The secret is here

GTS_VAR_clientSecret

A secret parameter of your pipeline, given to the run as an environment variable.

In the test file

Never the secret

{{clientSecret}}

Only the name. The file can be shared and reviewed.

In the results

Never the secret

(hidden)

The report, the console, the files and the bug report never show it.

One name, four places. The test works on every computer that has a value for that name.
staging.env: nothing secret in it
baseUrl = https://staging.example.com/api
tokenUrl = https://login.staging.example.com/oauth/token
clientId = shop-tests
The run, with GTS_VAR_clientSecret set by the pipeline
gtr-cli run tests --variables staging.env --junit results\junit.xml

Where each value comes from, and which one wins, is the subject of environment variables in API testing.What else a test can reveal, such as a screenshot of real customers, is in data privacy in test automation.

The tests, the recordings and the reports are files on your team's computers: see on-premise test automation.An AI assistant can add the steps when you ask.It can be Claude, ChatGPT with Codex, or a local AI model, one we provide or one you bring.A saved test needs no AI model to run.

What to know before you start

Good to knowWhat it means
No sign-in that opens a pageA Call API step signs in as an app, or with a test user's name and password. A page for a person is recorded instead.
No client certificatesAn API that only lets in computers holding its certificate is not covered.
Statuses differ401 and 403 are the rule of bearer tokens. Some APIs answer 404 to hide that something exists. Expect what yours promises.
Functional tests onlyThese cases check your API's promises. They do not replace a security review.

Key takeaways

  • 1Sign in as an app: client credentials is the flow made for a test, with no person and no browser.
  • 2Leave users' passwords out of it: RFC 9700 rules that sign-in out.Keep it for an older system that takes nothing else, with a throwaway account.
  • 3Test the refusals: no token, a malformed one, an expired one, a wrong scope, another client's token.
  • 4Use a Bearer token to test a bad one: with OAuth 2.0 the step always brings a good one.
  • 5Keep the name, not the secret: the value stays on your computer or in your pipeline.

Questions

How do I test an API that uses OAuth 2.0?

Sign in as an app made for your tests, then send the request with the token you get. In GUI Test Studio, a Call API step does both: choose OAuth 2.0, give the token address, the client ID and the client secret, and press Send.

What is the difference between client credentials and authorization code?

With client credentials, an app signs in as itself, with its own ID and secret, and no person is involved. With authorization code, a person signs in on a web page and lets the app act for them. An API test fits the first one.

What is a bearer token?

A bearer token is a pass: whoever holds it can use it. The request carries it, and the API checks it instead of a password. That is why a token must never stand in a test file or in a report.

What are the main OAuth 2.0 test cases?

A valid token is accepted. No token, a malformed token and an expired token are refused, with a 401 in most APIs. A valid token that does not give the right to do the action is refused too, with a 403 in many APIs.

Should an expired token return 401 or 403?

401, in an API that follows the rules of bearer tokens. An expired token is no longer valid. 403 is for a valid token that does not give the right to do this action. Check what your own API promises.

Can I use a user's name and password to get a token in a test?

Avoid it. Today's security guidance, RFC 9700, says that this way of signing in must not be used. Sign in as the app itself. Keep the user sign-in for an older test system that takes nothing else, with a throwaway test account.

Can I automate an OAuth sign-in page?

Your own one, yes: record it like any other part of your app, with a test account in a test environment. Another company's sign-in page often refuses automation, so use your own service's test sign-in for your tests.

Where should I keep a client secret for API tests?

Outside the test file. On your computer, keep it under Variables, This computer, where it is encrypted for your Windows account. In a pipeline, keep it as a secret parameter. The test holds only the secret's name.

All articles