Skip to content

fix(api): freeze the clock in the admin 2FA tests - #153

Merged
codemenschendev merged 1 commit into
mainfrom
fix/totp-test-clock
Sep 26, 2026
Merged

codemenschendev merged 1 commit into
mainfrom
fix/totp-test-clock

Conversation

@codemenschendev

Copy link
Copy Markdown
Owner

Fixes the flaky AdminSecurityTest > a new sign in asks for a fresh code and refuses a used one (failed in CI run 36232930244 with 200 instead of 422, passed on rerun).

Cause: the test built TOTP codes from the real clock. If a run crossed a 30 second step boundary after enrolling, $now pointed at the next step, so the "used" code was actually fresh and accepted.

Fix:

  • Totp::verify reads Laravel's clock (now()->getTimestamp()) instead of time(), so a frozen test clock also freezes the window the server checks. Production behaviour is unchanged.
  • AdminSecurityTest travels to a fixed moment 15 s into a TOTP step and derives every code from that step.

Assertions are unchanged: a used step is refused (422), the next step is accepted.

Ran php artisan test --filter=AdminSecurityTest 10 times locally: 8/8 green each time.

Project: Appwerk

The test "a new sign in asks for a fresh code and refuses a used one"
built its TOTP codes from the real clock. When a run crossed a 30 second
step boundary between enrolling and reading time() again, the "used"
code belonged to the next step and was accepted (200 instead of 422,
CI run 36232930244).

Totp::verify now reads Laravel's clock (now()) instead of PHP's time(),
so a frozen test clock also freezes the window the server checks. The
test class travels to a fixed moment 15 seconds into a step and derives
every code from that step. The assertions are unchanged: a used step is
refused, the next one is accepted.

Project: Appwerk
@codemenschendev
codemenschendev merged commit 3bb26c5 into main Sep 26, 2026
2 checks passed
@codemenschendev
codemenschendev deleted the fix/totp-test-clock branch September 26, 2026 09:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant