Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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.
  • Loading branch information
google-labs-jules[bot] committed May 26, 2025
commit cd346f4941b80284e15f388f48d8c365a333abd6
30 changes: 30 additions & 0 deletions tests/test_tenant_mgt.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
"""Test cases for the firebase_admin.tenant_mgt module."""

import json
import time
import datetime
import unittest.mock
from urllib import parse

import pytest
Expand All @@ -29,6 +32,9 @@
from firebase_admin import _utils
from tests import testutils
from tests import test_token_gen
from tests.test_token_gen import MOCK_CURRENT_TIME
# jwt_helpers will be used in mocker.patch.object, if not, the string path is fine.
from google.auth.jwt import _helpers as jwt_helpers


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

class TestVerifyIdToken:

def setup_method(self, method):
self.time_patch = unittest.mock.patch('time.time', return_value=MOCK_CURRENT_TIME)
self.mock_time = self.time_patch.start()
self.utcnow_patch = unittest.mock.patch.object(
jwt_helpers, 'utcnow', return_value=datetime.datetime.fromtimestamp(
MOCK_CURRENT_TIME, tz=datetime.timezone.utc))
self.mock_utcnow = self.utcnow_patch.start()

def teardown_method(self, method):
self.time_patch.stop()
self.utcnow_patch.stop()

def test_valid_token(self, tenant_mgt_app):
client = tenant_mgt.auth_for_tenant('test-tenant', app=tenant_mgt_app)
client._token_verifier.request = test_token_gen.MOCK_REQUEST
Expand Down Expand Up @@ -997,6 +1015,18 @@ def tenant_aware_custom_token_app():

class TestCreateCustomToken:

def setup_method(self, method):
self.time_patch = unittest.mock.patch('time.time', return_value=MOCK_CURRENT_TIME)
self.mock_time = self.time_patch.start()
self.utcnow_patch = unittest.mock.patch.object(
jwt_helpers, 'utcnow', return_value=datetime.datetime.fromtimestamp(
MOCK_CURRENT_TIME, tz=datetime.timezone.utc))
self.mock_utcnow = self.utcnow_patch.start()

def teardown_method(self, method):
self.time_patch.stop()
self.utcnow_patch.stop()

def test_custom_token(self, tenant_aware_custom_token_app):
client = tenant_mgt.auth_for_tenant('test-tenant', app=tenant_aware_custom_token_app)

Expand Down