Skip to content

Commit 9d4e83f

Browse files
committed
address PR comments
1 parent 23505eb commit 9d4e83f

File tree

1 file changed

+49
-77
lines changed

1 file changed

+49
-77
lines changed

tests/test_app_check.py

Lines changed: 49 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,21 @@ def setup_class(cls):
5959
def teardown_class(cls):
6060
testutils.cleanup_apps()
6161

62+
@pytest.fixture
63+
def app_check_mock(mocker):
64+
"""Fixture to mock JWT functions and provide a fresh app."""
65+
mocker.patch("jwt.decode", return_value=JWT_PAYLOAD_SAMPLE)
66+
mocker.patch("jwt.PyJWKClient.get_signing_key_from_jwt", return_value=PyJWK(signing_key))
67+
mocker.patch("jwt.get_unverified_header", return_value=JWT_PAYLOAD_SAMPLE.get("headers"))
68+
mock_http_client = mocker.patch("firebase_admin._http_client.JsonHttpClient")
69+
70+
cred = testutils.MockCredential()
71+
app = firebase_admin.initialize_app(cred, {'projectId': PROJECT_ID}, name='test_consume_app')
72+
73+
yield mock_http_client, app
74+
75+
firebase_admin.delete_app(app)
76+
6277
class TestVerifyToken(TestBatch):
6378

6479
def test_no_project_id(self):
@@ -233,96 +248,53 @@ def test_verify_token(self, mocker):
233248
expected['app_id'] = APP_ID
234249
assert payload == expected
235250

236-
def test_verify_token_with_consume(self, mocker):
251+
def test_verify_token_with_consume(self, app_check_mock):
237252
"""Test verify_token with consume=True."""
238-
mocker.patch("jwt.decode", return_value=JWT_PAYLOAD_SAMPLE)
239-
mocker.patch("jwt.PyJWKClient.get_signing_key_from_jwt", return_value=PyJWK(signing_key))
240-
mocker.patch("jwt.get_unverified_header", return_value=JWT_PAYLOAD_SAMPLE.get("headers"))
241-
mock_http_client = mocker.patch("firebase_admin._http_client.JsonHttpClient")
253+
mock_http_client, app = app_check_mock
242254
mock_http_client.return_value.body.return_value = {'alreadyConsumed': True}
243255

244-
# Use a fresh app to ensure _AppCheckService is re-initialized with the mock
245-
cred = testutils.MockCredential()
246-
app = firebase_admin.initialize_app(cred, {'projectId': PROJECT_ID}, name='test_consume')
247-
248-
try:
249-
payload = app_check.verify_token("encoded", app, consume=True)
250-
expected = JWT_PAYLOAD_SAMPLE.copy()
251-
expected['app_id'] = APP_ID
252-
expected['already_consumed'] = True
253-
assert payload == expected
254-
mock_http_client.return_value.body.assert_called_once_with(
255-
'post',
256-
f'{SCOPED_PROJECT_ID}:verifyAppCheckToken',
257-
json={'app_check_token': 'encoded'})
258-
finally:
259-
firebase_admin.delete_app(app)
260-
261-
def test_verify_token_with_consume_network_error(self, mocker):
256+
payload = app_check.verify_token("encoded", app, consume=True)
257+
expected = JWT_PAYLOAD_SAMPLE.copy()
258+
expected['app_id'] = APP_ID
259+
expected['already_consumed'] = True
260+
assert payload == expected
261+
mock_http_client.return_value.body.assert_called_once_with(
262+
'post',
263+
f'{SCOPED_PROJECT_ID}:verifyAppCheckToken',
264+
json={'app_check_token': 'encoded'})
265+
266+
def test_verify_token_with_consume_network_error(self, app_check_mock):
262267
"""Test verify_token with consume=True handles network errors."""
263-
mocker.patch("jwt.decode", return_value=JWT_PAYLOAD_SAMPLE)
264-
mocker.patch("jwt.PyJWKClient.get_signing_key_from_jwt", return_value=PyJWK(signing_key))
265-
mocker.patch("jwt.get_unverified_header", return_value=JWT_PAYLOAD_SAMPLE.get("headers"))
266-
mock_http_client = mocker.patch("firebase_admin._http_client.JsonHttpClient")
268+
mock_http_client, app = app_check_mock
267269
mock_http_client.return_value.body.side_effect = requests.exceptions.RequestException(
268270
"Network error")
269271

270-
# Use a fresh app to ensure _AppCheckService is re-initialized with the mock
271-
cred = testutils.MockCredential()
272-
app = firebase_admin.initialize_app(
273-
cred, {'projectId': PROJECT_ID}, name='test_consume_error')
274-
275-
try:
276-
with pytest.raises(exceptions.UnknownError) as excinfo:
277-
app_check.verify_token("encoded", app, consume=True)
278-
assert str(excinfo.value) == (
279-
"Unknown error while making a remote service call: Network error")
280-
finally:
281-
firebase_admin.delete_app(app)
282-
283-
def test_verify_token_with_consume_non_dict_response(self, mocker):
272+
with pytest.raises(exceptions.UnknownError) as excinfo:
273+
app_check.verify_token("encoded", app, consume=True)
274+
assert str(excinfo.value) == (
275+
"Unknown error while making a remote service call: Network error")
276+
277+
def test_verify_token_with_consume_non_dict_response(self, app_check_mock):
284278
"""Test verify_token with consume=True handles non-dict response."""
285-
mocker.patch("jwt.decode", return_value=JWT_PAYLOAD_SAMPLE)
286-
mocker.patch("jwt.PyJWKClient.get_signing_key_from_jwt", return_value=PyJWK(signing_key))
287-
mocker.patch("jwt.get_unverified_header", return_value=JWT_PAYLOAD_SAMPLE.get("headers"))
288-
mock_http_client = mocker.patch("firebase_admin._http_client.JsonHttpClient")
279+
mock_http_client, app = app_check_mock
289280
mock_http_client.return_value.body.return_value = ["not", "a", "dict"]
290281

291-
# Use a fresh app to ensure _AppCheckService is re-initialized with the mock
292-
cred = testutils.MockCredential()
293-
app = firebase_admin.initialize_app(
294-
cred, {'projectId': PROJECT_ID}, name='test_consume_non_dict')
295-
296-
try:
297-
with pytest.raises(exceptions.UnknownError) as excinfo:
298-
app_check.verify_token("encoded", app, consume=True)
299-
assert str(excinfo.value) == (
300-
'Unexpected response from App Check service. '
301-
'Expected a JSON object, but got list.')
302-
finally:
303-
firebase_admin.delete_app(app)
304-
305-
def test_verify_token_with_consume_malformed_json(self, mocker):
282+
with pytest.raises(exceptions.UnknownError) as excinfo:
283+
app_check.verify_token("encoded", app, consume=True)
284+
assert str(excinfo.value) == (
285+
'Unexpected response from App Check service. '
286+
'Expected a JSON object, but got list.')
287+
288+
def test_verify_token_with_consume_malformed_json(self, app_check_mock):
306289
"""Test verify_token with consume=True handles malformed JSON response."""
307-
mocker.patch("jwt.decode", return_value=JWT_PAYLOAD_SAMPLE)
308-
mocker.patch("jwt.PyJWKClient.get_signing_key_from_jwt", return_value=PyJWK(signing_key))
309-
mocker.patch("jwt.get_unverified_header", return_value=JWT_PAYLOAD_SAMPLE.get("headers"))
310-
mock_http_client = mocker.patch("firebase_admin._http_client.JsonHttpClient")
290+
mock_http_client, app = app_check_mock
311291
mock_http_client.return_value.body.side_effect = ValueError("Malformed JSON")
312292

313-
# Use a fresh app to ensure _AppCheckService is re-initialized with the mock
314-
cred = testutils.MockCredential()
315-
app = firebase_admin.initialize_app(
316-
cred, {'projectId': PROJECT_ID}, name='test_consume_malformed_json')
317-
318-
try:
319-
with pytest.raises(exceptions.UnknownError) as excinfo:
320-
app_check.verify_token("encoded", app, consume=True)
321-
assert str(excinfo.value) == (
322-
'Unexpected response from App Check service. '
323-
'Error: Malformed JSON')
324-
finally:
325-
firebase_admin.delete_app(app)
293+
with pytest.raises(exceptions.UnknownError) as excinfo:
294+
app_check.verify_token("encoded", app, consume=True)
295+
assert str(excinfo.value) == (
296+
'Unexpected response from App Check service. '
297+
'Error: Malformed JSON')
326298

327299
def test_verify_token_with_non_list_audience_raises_error(self, mocker):
328300
jwt_with_non_list_audience = JWT_PAYLOAD_SAMPLE.copy()

0 commit comments

Comments
 (0)