2020import os
2121import time
2222import unittest .mock
23- import datetime
2423
2524from google .auth import crypt
2625from google .auth import jwt
2726import google .auth .exceptions
2827import google .oauth2 .id_token
29- from google .auth .jwt import _helpers as jwt_helpers
3028import pytest
3129from pytest_localserver import plugin
3230
4038
4139
4240MOCK_CURRENT_TIME = 1500000000
41+ MOCK_CURRENT_TIME_UTC = datetime .datetime .fromtimestamp (
42+ MOCK_CURRENT_TIME , tz = datetime .timezone .utc )
4343MOCK_UID = 'user1'
4444MOCK_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
426427class 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
629636class 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
808821class 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
837850class 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