Test the password reset token rules you can check in a unit test
Test password reset token lifecycle rules you can check in code. SQLite fixtures cover consistent replies, hashed storage, single use, expiry and session end.
The forgot-password page looks like the simplest form on a site. It is also a place where small choices leak information or leave a door open. The OWASP Forgot Password Cheat Sheet opens by calling the feature a common source of vulnerabilities, including user enumeration, and gives a short list of rules. Return a consistent message for existing and non-existing accounts. Keep response time uniform. Send the reset method through a side channel. Generate tokens with a cryptographically safe algorithm, make them long enough to resist brute force, store them securely, make them single use, and let them expire. Do not change the account until a valid token is presented.
Some of those rules can be tested in a few lines of code. Others, such as timing, rate limiting and email delivery, cannot be tested this way. This guide tests the first group and says clearly where the second group starts. It was prepared with AI assistance and reviewed on 18 November 2026. The fixture is a SQLite and Python sketch, not an authentication system.
What the fixture builds
Two tables: users, with a password value and a session count, and resets, with a token hash, a user ID, an expiry time and a used flag. A fake clock replaces real time. Tokens come from secrets.token_urlsafe(32).
def request_reset(db, clock, email, outbox):
user = db.execute('SELECT id FROM users WHERE email=?', (email,)).fetchone()
token = secrets.token_urlsafe(32)
if user:
with db:
db.execute('INSERT INTO resets(token_hash,user_id,expires) VALUES (?,?,?)',
(h(token), user[0], clock.now + TTL))
outbox.append((email, token))
return MSG
def reset_password(db, clock, token, new_pw):
with db:
row = db.execute('SELECT user_id,expires,used FROM resets WHERE token_hash=?',
(h(token),)).fetchone()
if not row or row[2] or clock.now > row[1]:
return False
db.execute('UPDATE resets SET used=1 WHERE user_id=?', (row[0],))
db.execute('UPDATE users SET pw=?, sessions=0 WHERE id=?', (new_pw, row[0]))
return True
The request function returns the same message whether the email exists or not, and it generates a token on both paths so the code does not take an early exit for unknown addresses. The cheat sheet suggests following the same logic rather than a quick exit. Whether the real response time is uniform depends on the real database and mail system, and this fixture does not measure it.
Checks that passed
- Same answer. A known and an unknown email get identical text. The outbox holds one message, for the known account only.
- Hashes only. The raw token sent by email is not in the table. Stored values are 64-character SHA-256 hashes. The cheat sheet says tokens should be stored securely and points to its Password Storage Cheat Sheet for how, which I did not read for this article. Hashing a high-entropy random token is my design choice here, not a quote from OWASP.
- No change on request. Asking for a reset leaves the password and session count as they were. The sheet says not to change the account, such as locking it, until a valid token is presented.
- Wrong token. A guessed value is refused.
- Valid token. A correct token changes that user's password and ends their sessions. The cheat sheet asks whether to invalidate existing sessions or do it automatically, so this is one of the allowed choices. The other user's password and session count are untouched.
- Single use. The same token fails the second time and the password stays the new one.
- Expiry. A token used after the lifetime plus one second is refused and the password does not change. A token used exactly at the expiry time still works in this fixture, which is a boundary you should choose on purpose.
- Sibling tokens. Two outstanding tokens exist for one user. Using one makes the other fail. The sheet recommends invalidating outstanding reset links after a successful recovery in the compromise case. Applying it to every reset is a stricter choice that I made here.
All pass on Python 3.10.12 with SQLite 3.37.2. The 900-second lifetime is an example and not a recommendation.
What this does not test
Everything here sits behind one function call. The fixture does not test whether response times are really uniform, rate limits per account, or CAPTCHA. It does not check that the reset link uses HTTPS, that the base URL ignores a forged Host header, or that the reset page sends a no-referrer policy, all of which the sheet lists for URL tokens. It does not send email, check that the new password meets a policy, ask the user to type it twice, or notify the user after the change. It does not cover recovery after a suspected account compromise or reset of a second factor, which the sheet treats separately.
The password values and session counts are test placeholders, not secure password storage or real session revocation. This fixture does not test concurrent token use. Production code needs secure password hashing and transaction/concurrency checks as well as endpoint tests.
The checks that matter most are the ones outside a unit test. Treat this fixture as a guard on token lifecycle logic and add endpoint and integration tests for the rest.
Sources
- OWASP Forgot Password Cheat Sheet: https://cheatsheetseries.owasp.org/cheatsheets/Forgot_Password_Cheat_Sheet.html