20
20
import os
21
21
import time
22
22
import unittest .mock
23
- import datetime
24
23
25
24
from google .auth import crypt
26
25
from google .auth import jwt
27
26
import google .auth .exceptions
28
27
import google .oauth2 .id_token
29
- from google .auth .jwt import _helpers as jwt_helpers
30
28
import pytest
31
29
from pytest_localserver import plugin
32
30
40
38
41
39
42
40
MOCK_CURRENT_TIME = 1500000000
41
+ MOCK_CURRENT_TIME_UTC = datetime .datetime .fromtimestamp (
42
+ MOCK_CURRENT_TIME , tz = datetime .timezone .utc )
43
43
MOCK_UID = 'user1'
44
44
MOCK_CREDENTIAL = credentials .Certificate (
45
45
testutils .resource_filename ('service_account.json' ))
@@ -132,7 +132,8 @@ def _get_id_token(payload_overrides=None, header_overrides=None, current_time=MO
132
132
payload = _merge_jwt_claims (payload , payload_overrides )
133
133
return jwt .encode (signer , payload , header = headers )
134
134
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 ):
136
137
payload_overrides = payload_overrides or {}
137
138
if 'iss' not in payload_overrides :
138
139
payload_overrides ['iss' ] = 'https://session.firebase.google.com/{0}' .format (
@@ -425,14 +426,14 @@ def test_unexpected_response(self, user_mgt_app):
425
426
426
427
class TestVerifyIdToken :
427
428
428
- def setup_method (self , method ):
429
+ def setup_method (self ):
429
430
self .time_patch = unittest .mock .patch ('time.time' , return_value = MOCK_CURRENT_TIME )
430
431
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 )
433
434
self .utcnow_patch .start ()
434
435
435
- def teardown_method (self , method ):
436
+ def teardown_method (self ):
436
437
self .time_patch .stop ()
437
438
self .utcnow_patch .stop ()
438
439
@@ -452,8 +453,14 @@ def teardown_method(self, method):
452
453
'IntSubject' : _get_id_token ({'sub' : 10 }),
453
454
'LongStrSubject' : _get_id_token ({'sub' : 'a' * 129 }),
454
455
'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
+ }),
457
464
'BadFormatToken' : 'foobar'
458
465
}
459
466
@@ -628,14 +635,14 @@ def test_certificate_request_failure(self, user_mgt_app):
628
635
629
636
class TestVerifySessionCookie :
630
637
631
- def setup_method (self , method ):
638
+ def setup_method (self ):
632
639
self .time_patch = unittest .mock .patch ('time.time' , return_value = MOCK_CURRENT_TIME )
633
640
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 )
636
643
self .utcnow_patch .start ()
637
644
638
- def teardown_method (self , method ):
645
+ def teardown_method (self ):
639
646
self .time_patch .stop ()
640
647
self .utcnow_patch .stop ()
641
648
@@ -655,8 +662,14 @@ def teardown_method(self, method):
655
662
'IntSubject' : _get_session_cookie ({'sub' : 10 }),
656
663
'LongStrSubject' : _get_session_cookie ({'sub' : 'a' * 129 }),
657
664
'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
+ }),
660
673
'BadFormatCookie' : 'foobar' ,
661
674
'IDToken' : TEST_ID_TOKEN ,
662
675
}
@@ -807,14 +820,14 @@ def test_certificate_request_failure(self, user_mgt_app):
807
820
808
821
class TestCertificateCaching :
809
822
810
- def setup_method (self , method ):
823
+ def setup_method (self ):
811
824
self .time_patch = unittest .mock .patch ('time.time' , return_value = MOCK_CURRENT_TIME )
812
825
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 )
815
828
self .utcnow_patch .start ()
816
829
817
- def teardown_method (self , method ):
830
+ def teardown_method (self ):
818
831
self .time_patch .stop ()
819
832
self .utcnow_patch .stop ()
820
833
@@ -836,16 +849,17 @@ def test_certificate_caching(self, user_mgt_app, httpserver):
836
849
837
850
class TestCertificateFetchTimeout :
838
851
839
- def setup_method (self , method ):
852
+ def setup_method (self ):
840
853
self .time_patch = unittest .mock .patch ('time.time' , return_value = MOCK_CURRENT_TIME )
841
854
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 )
844
857
self .utcnow_patch .start ()
845
858
846
- def teardown_method (self , method ):
859
+ def teardown_method (self ):
847
860
self .time_patch .stop ()
848
861
self .utcnow_patch .stop ()
862
+ testutils .cleanup_apps ()
849
863
850
864
timeout_configs = [
851
865
({'httpTimeout' : 4 }, 4 ),
@@ -889,6 +903,3 @@ def _instrument_session(self, app):
889
903
recorder = []
890
904
request .session .mount ('https://' , testutils .MockAdapter (MOCK_PUBLIC_CERTS , 200 , recorder ))
891
905
return recorder
892
-
893
- def teardown_method (self ):
894
- testutils .cleanup_apps ()
0 commit comments