chore: Use mock time for consistent token generation and verification tests by jonathanedey · Pull Request #881 · firebase/firebase-admin-python

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.

lahirumaramba