When you secure an API, you enforce two separate rules that fail in separate ways: authentication verifies that the token or session identifies a valid caller, and authorization verifies that this caller has permission for the requested action on the requested object. An API that accepts a valid token is not automatically authorized. Assuming the two are the same causes most access-control bugs in production.
If your tests bundle both checks into one assertion, a failure tells you nothing about which layer broke. A user who logs in successfully and then reads another user’s invoice is an authorization defect on a valid authentication path. Separate the two into independent suites and each failure names its own layer.
The authentication suite checks the token and session: signature, expiry, audience, and logout invalidation. The authorization suite verifies role checks, object ownership, writable fields, and what the response leaks. You run both against every endpoint, because an endpoint that checks authentication but skips authorization is where the real bugs sit.
You will know how to build both suites and assemble them into a staging checklist before a release.
Why the two checks fail differently
Authentication usually runs once, as middleware or a filter that checks the credentials before the request reaches your route handlers. Authorization runs per operation, sometimes per object, and developers often forget it. An endpoint can pass the first check and still let a caller read another tenant’s data because no object-level check exists.
The token format matters for what you test. JWT access tokens are bearer tokens: anyone who possesses the token can use it. So the authentication suite must show that the server rejects tokens that are tampered with, expired, or aimed at a different service, and that a logged-out or expired session stops working. None of that tells you whether the caller may perform the action; that is the authorization suite’s job.
Token and session tests
For each protected operation, send no credentials, a valid token, and a token that lacks the needed role, and record the status code, the response body, and whether the operation changed data. That three-request set is the backbone of both suites. You run the same requests for authentication and authorization, but you read different signals from each. A valid token that gets a 401 points at the authentication layer. A valid token that gets a successful response on an operation the role should not allow points at authorization.
The authentication cases add tampering: alter a claim inside the token and expect rejection, strip the signature and expect rejection, and send an expired token and a token issued for the wrong audience. The audience is the intended recipient: a token minted for one service must not be accepted by another.
Add cases for session expiration and token invalidation on logout. In an end-to-end test you can also switch users mid-session and verify the server clears the previous role and applies the new one. That catches the bug where a reused session keeps the original user’s permissions. If you write this with Playwright, mind the timing issues covered in Replacing Fixed Sleeps in Playwright Tests.
Object-level tests need two real accounts
Object-level authorization is where the access-control bugs concentrate. Broken Object Level Authorization (BOLA) happens when the API fails to check that the logged-in user has access to the requested object ID. A caller can then modify a parameter and read or change another user’s data. It is the classic pattern of an insecure direct object reference.
The test is mechanical. Use two accounts and swap identifiers across read, update, and delete. Run positive controls first: confirm each intended owner can perform the allowed action, so a denied cross-account request actually means something. You need at least two normal users in different tenants plus one admin. Test each action independently, because read, update, delete, and export can pass through different code and policy checks.
Automated scanners cannot determine object ownership, so these tests must be scripted with real test accounts rather than left to a scanner. Unpredictable identifiers like UUIDs reduce enumeration risk, but they do not remove the need for the access check. A UUID makes guessing harder; it does not grant permission.
Function and property level tests
Role checks are the function-level part of authorization. Call an owner-only or admin-only operation with a low-privilege token and expect denial. Add method manipulation: send unexpected HTTP methods and verify the response, because an endpoint that only checks authentication can accept a DELETE against a route documented as GET. And do not treat URL paths as access control; a caller can change the path to reach another group, so the backend must re-check permissions on every request.
Property-level authorization is a separate concern from record access. Permission to see a record does not mean permission to see every property. So test response fields by checking that sensitive properties like IP address, last login time, or social security number are not exposed even when the caller is authorized to see the record. Test writable properties separately. An unauthorized property change should be rejected or ignored, and you verify the stored record afterward. Mass assignment gets the same treatment, so submit extra fields such as is_admin:true and verify the server rejects or ignores them instead of binding them to the database model.
Test list endpoints separately, because a forbidden record’s metadata, like the title, owner name, or total amount, can leak even when the full body is hidden. And authorization is a lifecycle: test granting access, verifying it, changing the permission, and checking access again. Review caches and background jobs too. A cached response must not be served to a different identity, and a queued export needs a permission check at execution time, not just at scheduling. If you control response caching, keep identity out of it; see Setting Cache-Control Headers for Pages and APIs.
Two principles keep the tests honest. Deny by default: the API blocks access unless a rule explicitly allows it. Least privilege: each user gets only the minimum data and permissions needed. For payment, health, and identity APIs, treat this as a baseline and add regulator-specific controls from a dedicated API security audit checklist.
Apply rate limiting on login, token, and recovery endpoints. per-IP limits can be bypassed, and an authenticated session should not switch the limits off. Error responses should not leak stack traces or internal hostnames. And add old API versions and undocumented hosts to the endpoint inventory, because they stay reachable and skip current checks. In OpenAPI, an operation-level security declaration replaces the root-level one, so trust nothing and test each operation individually.
A staging checklist
Before you start, define the environment, the endpoint list, and the test identities. Then specify each identity’s permitted actions, the request limits, the stopping conditions, and who resolves unexpected results. Then run this sequence.
- Authentication matrix: no credentials, valid token, token lacking the needed role; record status code, body, and whether the operation changed data.
- Token tampering: altered claims, stripped signature, expired token, wrong audience.
- Sessions: expiration, logout invalidation, mid-session role switch.
- Object access: two accounts swap identifiers across read, update, and delete; run positive controls first.
- Function access: low-privilege token against owner-only and admin operations.
- Properties: sensitive fields not exposed; writable fields reject unauthorized changes; extra fields ignored.
- Lifecycle: grant, verify, change permission, re-check.
- Infrastructure: rate limits, error bodies, old API versions, caches.
Run positive controls before negative ones so a denied cross-account request is meaningful. If you fix a defect by denying everyone, keep both the allowed and denied cases as regression checks so you do not break the product.