
How to Test That Your OTP Codes Actually Expire
Need a disposable inbox right now?
Spin up a temporary email address in seconds — no signup, no spam, no trace.
Most OTP tests stop at "did the code arrive." Few teams check what happens to the code after it's used, or after it expires. That gap is where account-takeover bugs live: a six-digit code that still verifies ten minutes after it was issued, or one that verifies twice. Here's how to actually test for both, using disposable inboxes you can throw away after each run.
Why "it arrived" isn't the same as "it's secure"
A signup flow QA pass usually looks like: request code, read inbox, paste code, confirm account activates. That proves delivery and parsing work. It proves nothing about the two properties that matter for OTP security: expiry and single-use.
An OTP that's valid for 30 minutes "just in case" is a 30-minute window for anyone who gets to the code — through a shared inbox, a forwarding rule, a screenshot in a support ticket. An OTP that still verifies after the first successful login is worse: it means the backend never invalidated the code, so anyone who captured it earlier can replay it.
Neither bug shows up in a happy-path test. Both show up the first time you deliberately misuse the code instead of using it correctly once.
The three tests that actually catch this
1. Replay after success
Request a code, verify successfully, then submit the exact same code again. A correctly built flow rejects the second attempt — the code should be single-use regardless of whether it's still inside its time window. If the second submission also succeeds, the backend is checking expiry but not consumption, and that's the bug worth filing.
2. Expiry after the stated window
Request a code, note the timestamp, and hold it past whatever window the product claims (5 minutes, 10 minutes, whatever's documented). Submit it after that window closes. It should fail with an expired-code error, not a generic "incorrect code" message — the distinction matters for support tickets and for anyone probing the flow for weaknesses.
3. Reissue invalidates the old code
Request a code, then request a second one before using the first (the "didn't receive it, resend" path every user hits eventually). Try to verify with the first code. If it still works, your flow has two live codes for one session at once — which quietly doubles the attack surface without anyone noticing, since the UI only ever shows the newest code.
Why you need a disposable inbox for this, not your own
Running these tests against your real email works exactly once before your inbox is a mess of stale codes and timestamp-sensitive test data you have to track manually. A fresh throwaway address per test run means every scenario starts clean — no leftover codes from a previous test run to confuse the assertion, no risk of a slow test polluting a shared inbox other engineers are also testing against.
Open cybertemp.xyz, generate an inbox, and run the signup flow against it manually the first time to confirm the timestamps and error messages line up with what you expect. Something like [email protected] for the replay case and [email protected] for the reissue case keeps the three scenarios from colliding in one inbox. No account, no install — paste the address into the signup form, watch the code land, and you're already set up to try the misuse case.
Scripting it so the test actually runs in CI
Once you've confirmed the behavior by hand, scripting it is straightforward. CyberTemp's API has one endpoint that matters here: GET /getMail?email=<address>, authenticated with an X-API-Key header. The same call both creates the inbox on first read and returns new mail on every call after, so there's no separate setup step.
import httpx, re, time
API_KEY = "tk_..."
HEADERS = {"X-API-Key": API_KEY}
BASE = "https://api.cybertemp.xyz"
def get_latest_otp(address, timeout=30):
deadline = time.time() + timeout
while time.time() < deadline:
r = httpx.get(f"{BASE}/getMail", params={"email": address}, headers=HEADERS)
r.raise_for_status()
messages = r.json()
if messages:
match = re.search(r"\b\d{6}\b", messages[0]["body"])
if match:
return match.group(0)
time.sleep(2)
raise TimeoutError("no OTP arrived in time")
def request_code(address):
httpx.post("https://yourapp.example.com/api/request-otp", json={"email": address})
def verify_code(address, code):
resp = httpx.post("https://yourapp.example.com/api/verify-otp", json={"email": address, "code": code})
return resp.status_code, resp.json()
# Replay test
addr = "[email protected]"
request_code(addr)
code = get_latest_otp(addr)
first = verify_code(addr, code)
second = verify_code(addr, code)
assert first[0] == 200
assert second[0] != 200, "replayed OTP was accepted — single-use check is missing"
The expiry test is the same shape with a time.sleep() past your stated window instead of an immediate second call, and the reissue test requests twice before verifying the first code. Wire all three into whatever CI job already runs your signup tests — they're cheap to run on every PR once the inbox plumbing exists, and they catch a class of bug that code review almost never does.
If you're scripting against signups at any real scale — these OTP checks, broader CI test suites, multi-account workflows — the paid tiers raise the per-key request limits past what FREE allows and add the OTP-extraction helpers in the Go and Rust SDKs, so you're not hand-rolling regex against every message body. ECO and CORE cover most CI pipelines; ELITE is for teams running these tests across many services at once. For a one-off manual check like the walkthrough above, FREE and the browser tool at cybertemp.xyz are all you need.