Skip to main content

Authentication — Testing

Test strategy

Authentication tests cover server API endpoints and mobile client token management. The primary concern is correctness of security invariants: lockout triggers correctly, blacklisted tokens are rejected, rate limits enforce.


Server — integration tests

Registration (POST /auth/)

ScenarioExpected result
Valid username, name, password201, user created, Argon2 hash stored
Duplicate username409 Conflict
Duplicate email409 Conflict
Username < 2 characters422 Unprocessable Entity
Phone number wrong format (+63 not followed by 9 digits)422 Unprocessable Entity
> 3 requests/minute from same client429 Too Many Requests

Login (POST /auth/token)

ScenarioExpected result
Valid email + correct password200, access_token + refresh_token returned
Wrong password401, attempt counter incremented
Non-existent email401, phantom lockout budget consumed
Account locked (locked_until in future)429 with lockout message
> 5 requests/minute429 Too Many Requests

Logout (POST /auth/logout)

ScenarioExpected result
Valid access token200, jti written to blacklistedtoken
Reuse blacklisted token401 Unauthorized
No Authorization header401 Unauthorized

Token refresh (POST /auth/refresh)

ScenarioExpected result
Valid refresh token200, new access_token returned
Expired refresh token401 Unauthorized
Blacklisted refresh token401 Unauthorized
Access token used instead of refresh401 (wrong token type)

Password change (POST /auth/change-password)

ScenarioExpected result
Correct current password200, password updated
Wrong current password401 Unauthorized
> 3 requests/minute429 Too Many Requests

Mobile — token management tests

ScenarioExpected result
Login stores tokens in expo-secure-storeTokens retrievable after login
Logout clears tokens from expo-secure-storeNo tokens present after logout
Access token expiry triggers refreshNew access token obtained transparently
Refresh token expiry forces re-loginUser redirected to login screen
App restart with valid stored tokensSession restored without re-login

Coverage targets

  • All POST /auth/* endpoints: 100% branch coverage on happy path and error paths.
  • Lockout: at least one test exhausting the attempt budget and verifying locked_until is set.
  • Token blacklist: at least one test verifying a token cannot be reused after logout.
  • Rate limiting: at least one test per rate-limited endpoint verifying 429 after limit exceeded.

Test data conventions

  • Synthetic emails: user@example.com, admin@example.com.
  • Placeholder password: Passw0rd!.
  • Never use real credentials in test fixtures.
  • Reset login_attempt and blacklistedtoken tables between test runs.