Skip to content

Commit 966b313

Browse files
committed
chore: update tests
1 parent 839a497 commit 966b313

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

firebase_admin/fpnv.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@
2121

2222
import jwt
2323
from jwt import PyJWKClient, InvalidTokenError, DecodeError, InvalidSignatureError, \
24-
InvalidAudienceError, InvalidIssuerError, ExpiredSignatureError
24+
PyJWKClientError, InvalidAudienceError, InvalidIssuerError, ExpiredSignatureError
2525

2626
from firebase_admin import _utils
27-
from firebase_admin.exceptions import InvalidArgumentError
2827

2928
_FPNV_ATTRIBUTE = '_fpnv'
3029
_FPNV_JWKS_URL = 'https://fpnv.googleapis.com/v1beta/jwks'
@@ -155,7 +154,7 @@ def verify(self, token) -> Dict[str, Any]:
155154
self._validate_headers(jwt.get_unverified_header(token))
156155
signing_key = self._jwks_client.get_signing_key_from_jwt(token)
157156
claims = self._validate_payload(token, signing_key.key)
158-
except (InvalidTokenError, DecodeError) as exception:
157+
except (InvalidTokenError, DecodeError, PyJWKClientError) as exception:
159158
raise ValueError(
160159
f'Verifying FPNV token failed. Error: {exception}'
161160
) from exception
@@ -230,4 +229,4 @@ class _Validators:
230229
def check_string(cls, label: str, value: Any):
231230
"""Checks if the given value is a string."""
232231
if not isinstance(value, str) or not value:
233-
raise ValueError(f'{label} must be a non-empty string.')
232+
raise ValueError(f'{label} must be a non-empty string.')

tests/test_fpnv.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
"""Test cases for the firebase_admin.fpnv module."""
1616

17-
import base64
1817
from unittest import mock
1918

2019
import jwt
@@ -44,6 +43,7 @@
4443
"other": 'other'
4544
}
4645

46+
4747
@pytest.fixture
4848
def 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

Comments
 (0)