Test that logout really logs out

A user presses Log out. The app forgets the token. But if the token still works when someone else sends it, from a stolen laptop, a log file or a browser extension, logout was only cosmetic. It's one of the most common findings under "broken authentication" in the OWASP API Top 10, and it's easy to test.

The folder

Make a Logout folder with four requests, run in order:

  1. Log in: saves accessToken and refreshToken.
  2. Get profile: with {{accessToken}}, expect 200.
  3. Log out: expect 200 or 204.
  4. Get profile after logout: the same request, the same {{accessToken}}, expect 401.
JavaScript
// Tests on "Log in"
const body = pm.response.json();
pm.environment.set('accessToken', body.accessToken);
pm.environment.set('refreshToken', body.refreshToken);
JavaScript
// Tests on "Get profile after logout"
pm.test('the old access token is refused after logout', () => {
  pm.response.to.have.status(401);
});

Variables set by a script carry to the later requests of the run, so request 4 sends exactly the token from request 1.

Refresh tokens

The refresh token matters more than the access token: it lives for days or weeks. Add:

  1. Refresh after logout: POST /auth/refresh with {{refreshToken}}, expect 400 or 401.
JavaScript
pm.test('the refresh token is revoked by logout', () => {
  pm.expect(pm.response.code).to.be.oneOf([400, 401]);
});

Refresh token reuse

If your API rotates refresh tokens (each refresh returns a new one), using an old one again is a sign of theft. Good servers then revoke the whole session. Test it:

  1. Log in, save refreshToken as firstRefresh.
  2. Refresh once, save the new token.
  3. Refresh again with {{firstRefresh}}: expect 401.
  4. Refresh with the newest token: expect 401 too, if your API revokes the family on reuse.

Decide which behaviour you want, write it in the request's docs, and test that.

Password change and "log out everywhere"

Log in twice (two sessions, tokenA and tokenB). Change the password with tokenA. Then call Get profile with tokenB: expect 401. Same for a "log out of all devices" endpoint, if you have one.

When tokens can't be revoked

Plain JWTs are checked by signature, so the server can't "forget" one without a deny list. If that's your design, logout can't stop an access token, and the test in step 4 will fail. That's a decision, not a bug, but then:

  • test that access tokens are short-lived: decode the token and check exp - iat is minutes, not days (see JWT handling);
  • test that the refresh token is revoked, because that's where revocation has to happen.
JavaScript
const CryptoJS = require('crypto-js');
const part = pm.environment.get('accessToken').split('.')[1].replace(/-/g, '+').replace(/_/g, '/');
const padded = part + '='.repeat((4 - (part.length % 4)) % 4);
const claims = JSON.parse(CryptoJS.enc.Base64.parse(padded).toString(CryptoJS.enc.Utf8));
pm.test('access tokens live 15 minutes or less', () => {
  pm.expect(claims.exp - claims.iat).to.be.at.most(900);
});