fix(api): freeze the clock in the admin 2FA tests - #153
Merged
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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,
$nowpointed at the next step, so the "used" code was actually fresh and accepted.Fix:
Totp::verifyreads Laravel's clock (now()->getTimestamp()) instead oftime(), so a frozen test clock also freezes the window the server checks. Production behaviour is unchanged.AdminSecurityTesttravels 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=AdminSecurityTest10 times locally: 8/8 green each time.Project: Appwerk