1414
1515"""Test cases for the firebase_admin.fpnv module."""
1616
17- import base64
1817from unittest import mock
1918
2019import jwt
4443 "other" : 'other'
4544}
4645
46+
4747@pytest .fixture
4848def client ():
4949 app = firebase_admin .get_app ()
@@ -56,7 +56,6 @@ def setup_class(cls):
5656 cred = testutils .MockCredential ()
5757 firebase_admin .initialize_app (cred , {'projectId' : _PROJECT_ID })
5858
59-
6059 @classmethod
6160 def teardown_class (cls ):
6261 testutils .cleanup_apps ()
@@ -155,17 +154,21 @@ def test_verify_token_wrong_alg(self, mock_header, client):
155154 with pytest .raises (ValueError , match = "incorrect alg" ):
156155 client .verify_token ('token' )
157156
158- @mock .patch ('jwt.PyJWKClient' )
159- @mock .patch ('jwt.get_unverified_header' )
160- def test_verify_token_jwk_error (self , mock_header , mock_jwks_cls , client ):
161- mock_header .return_value = {'kid' : 'k' , 'typ' : 'JWT' , 'alg' : 'ES256' }
162- mock_jwks_instance = mock_jwks_cls .return_value
163- # Simulate Key not found or other PyJWKClient error
164- mock_jwks_instance .get_signing_key_from_jwt .side_effect = jwt .PyJWKClientError (
165- "Key not found" )
157+ def test_verify_token_jwk_error (self , client ):
158+ # Access the ACTUAL client instance used by the verifier
159+ # (Assuming internal structure: client -> _verifier -> _jwks_client)
160+ jwks_client = client ._verifier ._jwks_client
166161
167- with pytest .raises (ValueError , match = "Verifying FPNV token failed" ):
168- client .verify_token ('token' )
162+ # Mock the method on the existing instance
163+ with mock .patch .object (jwks_client , 'get_signing_key_from_jwt' ) as mock_method :
164+ mock_method .side_effect = jwt .PyJWKClientError ("Key not found" )
165+
166+ # Mock header is still needed if _get_signing_key calls it before the client
167+ with mock .patch ('jwt.get_unverified_header' ) as mock_header :
168+ mock_header .return_value = {'kid' : 'k' , 'typ' : 'JWT' , 'alg' : 'ES256' }
169+
170+ with pytest .raises (ValueError , match = "Verifying FPNV token failed" ):
171+ client .verify_token ('token' )
169172
170173 @mock .patch ('jwt.PyJWKClient' )
171174 @mock .patch ('jwt.decode' )
@@ -176,7 +179,6 @@ def test_verify_token_expired(self, mock_header, mock_decode, mock_jwks_cls, cli
176179 mock_jwks_instance .get_signing_key_from_jwt .return_value .key = _PUBLIC_KEY
177180 client ._verifier ._jwks_client = mock_jwks_instance
178181
179-
180182 # Simulate ExpiredSignatureError
181183 mock_decode .side_effect = jwt .ExpiredSignatureError ("Expired" )
182184
@@ -211,4 +213,4 @@ def test_verify_token_invalid_issuer(self, mock_header, mock_decode, mock_jwks_c
211213 mock_decode .side_effect = jwt .InvalidIssuerError ("Wrong Iss" )
212214
213215 with pytest .raises (ValueError , match = "incorrect \" iss\" " ):
214- client .verify_token ('token' )
216+ client .verify_token ('token' )
0 commit comments