Skip to content

Commit cd346f4

Browse files
Fix(tests): Apply time mocking to test_tenant_mgt.py
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.
1 parent 1d053dc commit cd346f4

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

tests/test_tenant_mgt.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
"""Test cases for the firebase_admin.tenant_mgt module."""
1616

1717
import json
18+
import time
19+
import datetime
20+
import unittest.mock
1821
from urllib import parse
1922

2023
import pytest
@@ -29,6 +32,9 @@
2932
from firebase_admin import _utils
3033
from tests import testutils
3134
from tests import test_token_gen
35+
from tests.test_token_gen import MOCK_CURRENT_TIME
36+
# jwt_helpers will be used in mocker.patch.object, if not, the string path is fine.
37+
from google.auth.jwt import _helpers as jwt_helpers
3238

3339

3440
GET_TENANT_RESPONSE = """{
@@ -964,6 +970,18 @@ def _assert_saml_provider_config(self, provider_config, want_id='saml.provider')
964970

965971
class TestVerifyIdToken:
966972

973+
def setup_method(self, method):
974+
self.time_patch = unittest.mock.patch('time.time', return_value=MOCK_CURRENT_TIME)
975+
self.mock_time = self.time_patch.start()
976+
self.utcnow_patch = unittest.mock.patch.object(
977+
jwt_helpers, 'utcnow', return_value=datetime.datetime.fromtimestamp(
978+
MOCK_CURRENT_TIME, tz=datetime.timezone.utc))
979+
self.mock_utcnow = self.utcnow_patch.start()
980+
981+
def teardown_method(self, method):
982+
self.time_patch.stop()
983+
self.utcnow_patch.stop()
984+
967985
def test_valid_token(self, tenant_mgt_app):
968986
client = tenant_mgt.auth_for_tenant('test-tenant', app=tenant_mgt_app)
969987
client._token_verifier.request = test_token_gen.MOCK_REQUEST
@@ -997,6 +1015,18 @@ def tenant_aware_custom_token_app():
9971015

9981016
class TestCreateCustomToken:
9991017

1018+
def setup_method(self, method):
1019+
self.time_patch = unittest.mock.patch('time.time', return_value=MOCK_CURRENT_TIME)
1020+
self.mock_time = self.time_patch.start()
1021+
self.utcnow_patch = unittest.mock.patch.object(
1022+
jwt_helpers, 'utcnow', return_value=datetime.datetime.fromtimestamp(
1023+
MOCK_CURRENT_TIME, tz=datetime.timezone.utc))
1024+
self.mock_utcnow = self.utcnow_patch.start()
1025+
1026+
def teardown_method(self, method):
1027+
self.time_patch.stop()
1028+
self.utcnow_patch.stop()
1029+
10001030
def test_custom_token(self, tenant_aware_custom_token_app):
10011031
client = tenant_mgt.auth_for_tenant('test-tenant', app=tenant_aware_custom_token_app)
10021032

0 commit comments

Comments
 (0)