Skip to content

chore: Use mock time for consistent token generation and verification tests #881

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 27, 2025

Conversation

jonathanedey
Copy link
Contributor

Patches time.time and google.auth.jwt._helpers.utcnow to use a fixed timestamp (MOCK_CURRENT_TIME) in tests/test_token_gen.py and tests/test_tenant_mgt.py.

This addresses test flakiness and inconsistencies by ensuring that:

  • Tokens and cookies are generated with predictable iat and exp claims based on MOCK_CURRENT_TIME.
  • The verification logic within the Firebase Admin SDK and the underlying google-auth library also uses MOCK_CURRENT_TIME.

google-labs-jules bot and others added 3 commits May 26, 2025 20:20
…ation tests

Patches time.time and google.auth.jwt._helpers.utcnow to use a fixed
timestamp (MOCK_CURRENT_TIME) throughout tests/test_token_gen.py.

This addresses test flakiness and inconsistencies by ensuring that:
1. Tokens and cookies are generated with predictable `iat` and `exp` claims
   based on MOCK_CURRENT_TIME.
2. The verification logic within the Firebase Admin SDK and the underlying
   google-auth library also uses MOCK_CURRENT_TIME.

Helper functions _get_id_token and _get_session_cookie were updated to
default to using MOCK_CURRENT_TIME for their internal time calculations,
simplifying test code.

Relevant fixtures and token definitions were updated to rely on these
new defaults and the fixed timestamp.

The setup_method in TestVerifyIdToken, TestVerifySessionCookie,
TestCertificateCaching, and TestCertificateFetchTimeout now mock
time.time and google.auth.jwt._helpers.utcnow to ensure that all
time-sensitive operations during testing use the MOCK_CURRENT_TIME.
Extends the time mocking strategy (using a fixed MOCK_CURRENT_TIME)
to tests in `tests/test_tenant_mgt.py` to ensure consistency with
changes previously made in `tests/test_token_gen.py`.

Specifically:
- Imported `MOCK_CURRENT_TIME` from `tests.test_token_gen`.
- Added `setup_method` (and `teardown_method`) to the
  `TestVerifyIdToken` and `TestCreateCustomToken` classes.
- These setup methods patch `time.time` and
  `google.auth.jwt._helpers.utcnow` to return `MOCK_CURRENT_TIME`
  (or its datetime equivalent).

This ensures that token generation (for custom tokens) and token
verification within `test_tenant_mgt.py` align with the mocked
timeline, preventing potential flakiness or failures due to
time inconsistencies. All tests in `test_tenant_mgt.py` pass
with these changes.
@jonathanedey jonathanedey merged commit 2d9b18c into master May 27, 2025
13 checks passed
@jonathanedey jonathanedey deleted the je-fix-mock-time-token-tests branch May 27, 2025 13:07
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.

2 participants