Skip to content

Commit 997c5a3

Browse files
committed
fix lint and refactor
1 parent cd346f4 commit 997c5a3

File tree

2 files changed

+46
-41
lines changed

2 files changed

+46
-41
lines changed

tests/test_tenant_mgt.py

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

1717
import json
18-
import time
19-
import datetime
2018
import unittest.mock
2119
from urllib import parse
2220

@@ -32,9 +30,7 @@
3230
from firebase_admin import _utils
3331
from tests import testutils
3432
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
33+
from tests.test_token_gen import MOCK_CURRENT_TIME, MOCK_CURRENT_TIME_UTC
3834

3935

4036
GET_TENANT_RESPONSE = """{
@@ -970,15 +966,14 @@ def _assert_saml_provider_config(self, provider_config, want_id='saml.provider')
970966

971967
class TestVerifyIdToken:
972968

973-
def setup_method(self, method):
969+
def setup_method(self):
974970
self.time_patch = unittest.mock.patch('time.time', return_value=MOCK_CURRENT_TIME)
975971
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))
972+
self.utcnow_patch = unittest.mock.patch(
973+
'google.auth.jwt._helpers.utcnow', return_value=MOCK_CURRENT_TIME_UTC)
979974
self.mock_utcnow = self.utcnow_patch.start()
980975

981-
def teardown_method(self, method):
976+
def teardown_method(self):
982977
self.time_patch.stop()
983978
self.utcnow_patch.stop()
984979

@@ -1015,15 +1010,14 @@ def tenant_aware_custom_token_app():
10151010

10161011
class TestCreateCustomToken:
10171012

1018-
def setup_method(self, method):
1013+
def setup_method(self):
10191014
self.time_patch = unittest.mock.patch('time.time', return_value=MOCK_CURRENT_TIME)
10201015
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))
1016+
self.utcnow_patch = unittest.mock.patch(
1017+
'google.auth.jwt._helpers.utcnow', return_value=MOCK_CURRENT_TIME_UTC)
10241018
self.mock_utcnow = self.utcnow_patch.start()
10251019

1026-
def teardown_method(self, method):
1020+
def teardown_method(self):
10271021
self.time_patch.stop()
10281022
self.utcnow_patch.stop()
10291023

tests/test_token_gen.py

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@
2020
import os
2121
import time
2222
import unittest.mock
23-
import datetime
2423

2524
from google.auth import crypt
2625
from google.auth import jwt
2726
import google.auth.exceptions
2827
import google.oauth2.id_token
29-
from google.auth.jwt import _helpers as jwt_helpers
3028
import pytest
3129
from pytest_localserver import plugin
3230

@@ -40,6 +38,8 @@
4038

4139

4240
MOCK_CURRENT_TIME = 1500000000
41+
MOCK_CURRENT_TIME_UTC = datetime.datetime.fromtimestamp(
42+
MOCK_CURRENT_TIME, tz=datetime.timezone.utc)
4343
MOCK_UID = 'user1'
4444
MOCK_CREDENTIAL = credentials.Certificate(
4545
testutils.resource_filename('service_account.json'))
@@ -132,7 +132,8 @@ def _get_id_token(payload_overrides=None, header_overrides=None, current_time=MO
132132
payload = _merge_jwt_claims(payload, payload_overrides)
133133
return jwt.encode(signer, payload, header=headers)
134134

135-
def _get_session_cookie(payload_overrides=None, header_overrides=None, current_time=MOCK_CURRENT_TIME):
135+
def _get_session_cookie(
136+
payload_overrides=None, header_overrides=None, current_time=MOCK_CURRENT_TIME):
136137
payload_overrides = payload_overrides or {}
137138
if 'iss' not in payload_overrides:
138139
payload_overrides['iss'] = 'https://session.firebase.google.com/{0}'.format(
@@ -425,14 +426,14 @@ def test_unexpected_response(self, user_mgt_app):
425426

426427
class TestVerifyIdToken:
427428

428-
def setup_method(self, method):
429+
def setup_method(self):
429430
self.time_patch = unittest.mock.patch('time.time', return_value=MOCK_CURRENT_TIME)
430431
self.time_patch.start()
431-
self.utcnow_patch = unittest.mock.patch.object(
432-
jwt_helpers, 'utcnow', return_value=datetime.datetime.utcfromtimestamp(MOCK_CURRENT_TIME))
432+
self.utcnow_patch = unittest.mock.patch(
433+
'google.auth.jwt._helpers.utcnow', return_value=MOCK_CURRENT_TIME_UTC)
433434
self.utcnow_patch.start()
434435

435-
def teardown_method(self, method):
436+
def teardown_method(self):
436437
self.time_patch.stop()
437438
self.utcnow_patch.stop()
438439

@@ -452,8 +453,14 @@ def teardown_method(self, method):
452453
'IntSubject': _get_id_token({'sub': 10}),
453454
'LongStrSubject': _get_id_token({'sub': 'a' * 129}),
454455
'FutureToken': _get_id_token({'iat': MOCK_CURRENT_TIME + 1000}),
455-
'ExpiredToken': _get_id_token({'iat': MOCK_CURRENT_TIME - 10000, 'exp': MOCK_CURRENT_TIME - 3600}),
456-
'ExpiredTokenShort': _get_id_token({'iat': MOCK_CURRENT_TIME - 10000, 'exp': MOCK_CURRENT_TIME - 30}),
456+
'ExpiredToken': _get_id_token({
457+
'iat': MOCK_CURRENT_TIME - 10000,
458+
'exp': MOCK_CURRENT_TIME - 3600
459+
}),
460+
'ExpiredTokenShort': _get_id_token({
461+
'iat': MOCK_CURRENT_TIME - 10000,
462+
'exp': MOCK_CURRENT_TIME - 30
463+
}),
457464
'BadFormatToken': 'foobar'
458465
}
459466

@@ -628,14 +635,14 @@ def test_certificate_request_failure(self, user_mgt_app):
628635

629636
class TestVerifySessionCookie:
630637

631-
def setup_method(self, method):
638+
def setup_method(self):
632639
self.time_patch = unittest.mock.patch('time.time', return_value=MOCK_CURRENT_TIME)
633640
self.time_patch.start()
634-
self.utcnow_patch = unittest.mock.patch.object(
635-
jwt_helpers, 'utcnow', return_value=datetime.datetime.utcfromtimestamp(MOCK_CURRENT_TIME))
641+
self.utcnow_patch = unittest.mock.patch(
642+
'google.auth.jwt._helpers.utcnow', return_value=MOCK_CURRENT_TIME_UTC)
636643
self.utcnow_patch.start()
637644

638-
def teardown_method(self, method):
645+
def teardown_method(self):
639646
self.time_patch.stop()
640647
self.utcnow_patch.stop()
641648

@@ -655,8 +662,14 @@ def teardown_method(self, method):
655662
'IntSubject': _get_session_cookie({'sub': 10}),
656663
'LongStrSubject': _get_session_cookie({'sub': 'a' * 129}),
657664
'FutureCookie': _get_session_cookie({'iat': MOCK_CURRENT_TIME + 1000}),
658-
'ExpiredCookie': _get_session_cookie({'iat': MOCK_CURRENT_TIME - 10000, 'exp': MOCK_CURRENT_TIME - 3600}),
659-
'ExpiredCookieShort': _get_session_cookie({'iat': MOCK_CURRENT_TIME - 10000, 'exp': MOCK_CURRENT_TIME - 30}),
665+
'ExpiredCookie': _get_session_cookie({
666+
'iat': MOCK_CURRENT_TIME - 10000,
667+
'exp': MOCK_CURRENT_TIME - 3600
668+
}),
669+
'ExpiredCookieShort': _get_session_cookie({
670+
'iat': MOCK_CURRENT_TIME - 10000,
671+
'exp': MOCK_CURRENT_TIME - 30
672+
}),
660673
'BadFormatCookie': 'foobar',
661674
'IDToken': TEST_ID_TOKEN,
662675
}
@@ -807,14 +820,14 @@ def test_certificate_request_failure(self, user_mgt_app):
807820

808821
class TestCertificateCaching:
809822

810-
def setup_method(self, method):
823+
def setup_method(self):
811824
self.time_patch = unittest.mock.patch('time.time', return_value=MOCK_CURRENT_TIME)
812825
self.time_patch.start()
813-
self.utcnow_patch = unittest.mock.patch.object(
814-
jwt_helpers, 'utcnow', return_value=datetime.datetime.utcfromtimestamp(MOCK_CURRENT_TIME))
826+
self.utcnow_patch = unittest.mock.patch(
827+
'google.auth.jwt._helpers.utcnow', return_value=MOCK_CURRENT_TIME_UTC)
815828
self.utcnow_patch.start()
816829

817-
def teardown_method(self, method):
830+
def teardown_method(self):
818831
self.time_patch.stop()
819832
self.utcnow_patch.stop()
820833

@@ -836,16 +849,17 @@ def test_certificate_caching(self, user_mgt_app, httpserver):
836849

837850
class TestCertificateFetchTimeout:
838851

839-
def setup_method(self, method):
852+
def setup_method(self):
840853
self.time_patch = unittest.mock.patch('time.time', return_value=MOCK_CURRENT_TIME)
841854
self.time_patch.start()
842-
self.utcnow_patch = unittest.mock.patch.object(
843-
jwt_helpers, 'utcnow', return_value=datetime.datetime.utcfromtimestamp(MOCK_CURRENT_TIME))
855+
self.utcnow_patch = unittest.mock.patch(
856+
'google.auth.jwt._helpers.utcnow', return_value=MOCK_CURRENT_TIME_UTC)
844857
self.utcnow_patch.start()
845858

846-
def teardown_method(self, method):
859+
def teardown_method(self):
847860
self.time_patch.stop()
848861
self.utcnow_patch.stop()
862+
testutils.cleanup_apps()
849863

850864
timeout_configs = [
851865
({'httpTimeout': 4}, 4),
@@ -889,6 +903,3 @@ def _instrument_session(self, app):
889903
recorder = []
890904
request.session.mount('https://', testutils.MockAdapter(MOCK_PUBLIC_CERTS, 200, recorder))
891905
return recorder
892-
893-
def teardown_method(self):
894-
testutils.cleanup_apps()

0 commit comments

Comments
 (0)