OOmnistu

Blog / Web & Ecommerce

Web & Ecommerce

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.

OOmnistu
Reviewed 2026-11-18 · 4 min read
Separate reset checks: first valid use and reuse of token A, expiry of an unused token, and use of one of two distinct sibling tokens.
Original Omnistu diagram of the article fixture. Scroll horizontally on a small screen. These are synthetic test outcomes, not a live-system certification.

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

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