Skip to content
This repository was archived by the owner on Mar 6, 2026. It is now read-only.

Commit b4fb17b

Browse files
committed
fixed tests
1 parent 38dc583 commit b4fb17b

5 files changed

Lines changed: 53 additions & 51 deletions

File tree

tests_async/oauth2/test__client_async.py

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -391,19 +391,11 @@ async def test_refresh_grant_no_access_token():
391391
assert not excinfo.value.retryable
392392

393393

394-
def create_async_mock():
395-
m = mock.Mock()
396-
async def async_response(*args, **kwargs):
397-
return mock.Mock()
398-
m.side_effect = async_response
399-
return m
400-
401-
402394
@pytest.mark.asyncio
403395
@mock.patch("google.oauth2._client._parse_expiry", return_value=None)
404-
@mock.patch.object(_client, "_token_endpoint_request", autospec=True)
396+
@mock.patch.object(_client, "_token_endpoint_request", new_callable=AsyncMock)
405397
async def test_jwt_grant_retry_default(mock_token_endpoint_request, mock_expiry):
406-
_ = await _client.jwt_grant(create_async_mock(), mock.Mock(), mock.Mock())
398+
_ = await _client.jwt_grant(mock.Mock(), mock.Mock(), mock.Mock())
407399
mock_token_endpoint_request.assert_called_with(
408400
mock.ANY, mock.ANY, mock.ANY, can_retry=True
409401
)
@@ -412,12 +404,12 @@ async def test_jwt_grant_retry_default(mock_token_endpoint_request, mock_expiry)
412404
@pytest.mark.asyncio
413405
@pytest.mark.parametrize("can_retry", [True, False])
414406
@mock.patch("google.oauth2._client._parse_expiry", return_value=None)
415-
@mock.patch.object(_client, "_token_endpoint_request", autospec=True)
407+
@mock.patch.object(_client, "_token_endpoint_request", new_callable=AsyncMock)
416408
async def test_jwt_grant_retry_with_retry(
417409
mock_token_endpoint_request, mock_expiry, can_retry
418410
):
419411
_ = await _client.jwt_grant(
420-
create_async_mock(), mock.Mock(), mock.Mock(), can_retry=can_retry
412+
mock.Mock(), mock.Mock(), mock.Mock(), can_retry=can_retry
421413
)
422414
mock_token_endpoint_request.assert_called_with(
423415
mock.ANY, mock.ANY, mock.ANY, can_retry=can_retry
@@ -426,11 +418,11 @@ async def test_jwt_grant_retry_with_retry(
426418

427419
@pytest.mark.asyncio
428420
@mock.patch("google.auth.jwt.decode", return_value={"exp": 0})
429-
@mock.patch.object(_client, "_token_endpoint_request", autospec=True)
421+
@mock.patch.object(_client, "_token_endpoint_request", new_callable=AsyncMock)
430422
async def test_id_token_jwt_grant_retry_default(
431423
mock_token_endpoint_request, mock_jwt_decode
432424
):
433-
_ = await _client.id_token_jwt_grant(create_async_mock(), mock.Mock(), mock.Mock())
425+
_ = await _client.id_token_jwt_grant(mock.Mock(), mock.Mock(), mock.Mock())
434426
mock_token_endpoint_request.assert_called_with(
435427
mock.ANY, mock.ANY, mock.ANY, can_retry=True
436428
)
@@ -439,12 +431,12 @@ async def test_id_token_jwt_grant_retry_default(
439431
@pytest.mark.asyncio
440432
@pytest.mark.parametrize("can_retry", [True, False])
441433
@mock.patch("google.auth.jwt.decode", return_value={"exp": 0})
442-
@mock.patch.object(_client, "_token_endpoint_request", autospec=True)
434+
@mock.patch.object(_client, "_token_endpoint_request", new_callable=AsyncMock)
443435
async def test_id_token_jwt_grant_retry_with_retry(
444436
mock_token_endpoint_request, mock_jwt_decode, can_retry
445437
):
446438
_ = await _client.id_token_jwt_grant(
447-
create_async_mock(), AsyncMock(), AsyncMock(), can_retry=can_retry
439+
mock.Mock(), mock.Mock(), mock.Mock(), can_retry=can_retry
448440
)
449441
mock_token_endpoint_request.assert_called_with(
450442
mock.ANY, mock.ANY, mock.ANY, can_retry=can_retry
@@ -453,16 +445,16 @@ async def test_id_token_jwt_grant_retry_with_retry(
453445

454446
@pytest.mark.asyncio
455447
@mock.patch("google.oauth2._client._parse_expiry", return_value=None)
456-
@mock.patch.object(_client, "_token_endpoint_request", autospec=True)
448+
@mock.patch.object(_client, "_token_endpoint_request", new_callable=AsyncMock)
457449
async def test_refresh_grant_retry_default(
458450
mock_token_endpoint_request, mock_parse_expiry
459451
):
460452
_ = await _client.refresh_grant(
461-
AsyncMock(),
462-
AsyncMock(),
463-
AsyncMock(),
464-
AsyncMock(),
465-
AsyncMock(),
453+
mock.Mock(),
454+
mock.Mock(),
455+
mock.Mock(),
456+
mock.Mock(),
457+
mock.Mock(),
466458
)
467459
mock_token_endpoint_request.assert_called_with(
468460
mock.ANY, mock.ANY, mock.ANY, can_retry=True
@@ -472,16 +464,16 @@ async def test_refresh_grant_retry_default(
472464
@pytest.mark.asyncio
473465
@pytest.mark.parametrize("can_retry", [True, False])
474466
@mock.patch("google.oauth2._client._parse_expiry", return_value=None)
475-
@mock.patch.object(_client, "_token_endpoint_request", autospec=True)
467+
@mock.patch.object(_client, "_token_endpoint_request", new_callable=AsyncMock)
476468
async def test_refresh_grant_retry_with_retry(
477469
mock_token_endpoint_request, mock_parse_expiry, can_retry
478470
):
479471
_ = await _client.refresh_grant(
480-
AsyncMock(),
481-
AsyncMock(),
482-
AsyncMock(),
483-
AsyncMock(),
484-
AsyncMock(),
472+
mock.Mock(),
473+
mock.Mock(),
474+
mock.Mock(),
475+
mock.Mock(),
476+
mock.Mock(),
485477
can_retry=can_retry,
486478
)
487479
mock_token_endpoint_request.assert_called_with(

tests_async/oauth2/test_credentials_async.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def test_default_state(self):
6363
assert credentials.client_id == self.CLIENT_ID
6464
assert credentials.client_secret == self.CLIENT_SECRET
6565

66-
@mock.patch("google.oauth2._credentials_async.reauth.refresh_grant")
66+
@mock.patch("google.oauth2._credentials_async.reauth.refresh_grant", new_callable=AsyncMock)
6767
@mock.patch(
6868
"google.auth._helpers.utcnow",
6969
return_value=datetime.datetime.min + _helpers.REFRESH_THRESHOLD,
@@ -125,7 +125,7 @@ async def test_refresh_no_refresh_token(self):
125125

126126
request.assert_not_called()
127127

128-
@mock.patch("google.oauth2._credentials_async.reauth.refresh_grant")
128+
@mock.patch("google.oauth2._credentials_async.reauth.refresh_grant", new_callable=AsyncMock)
129129
@mock.patch(
130130
"google.auth._helpers.utcnow",
131131
return_value=datetime.datetime.min + _helpers.REFRESH_THRESHOLD,
@@ -189,7 +189,7 @@ async def test_credentials_with_scopes_requested_refresh_success(
189189
# expired.)
190190
assert creds.valid
191191

192-
@mock.patch("google.oauth2._credentials_async.reauth.refresh_grant")
192+
@mock.patch("google.oauth2._credentials_async.reauth.refresh_grant", new_callable=AsyncMock)
193193
@mock.patch(
194194
"google.auth._helpers.utcnow",
195195
return_value=datetime.datetime.min + _helpers.REFRESH_THRESHOLD,
@@ -252,7 +252,7 @@ async def test_credentials_with_scopes_returned_refresh_success(
252252
# expired.)
253253
assert creds.valid
254254

255-
@mock.patch("google.oauth2._credentials_async.reauth.refresh_grant")
255+
@mock.patch("google.oauth2._credentials_async.reauth.refresh_grant", new_callable=AsyncMock)
256256
@mock.patch(
257257
"google.auth._helpers.utcnow",
258258
return_value=datetime.datetime.min + _helpers.REFRESH_THRESHOLD,
@@ -471,7 +471,7 @@ def test_unpickle_old_credentials_pickle(self):
471471
assert credentials.quota_project_id is None
472472

473473
@mock.patch("google.oauth2._credentials_async.Credentials.apply", autospec=True)
474-
@mock.patch("google.oauth2._credentials_async.Credentials.refresh", autospec=True)
474+
@mock.patch("google.oauth2._credentials_async.Credentials.refresh", new_callable=AsyncMock)
475475
@pytest.mark.asyncio
476476
async def test_before_request(self, refresh, apply):
477477
cred = self.make_credentials()

tests_async/oauth2/test_id_token.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ async def test__fetch_certs_failure():
7272

7373

7474
@mock.patch("google.auth.jwt.decode", autospec=True)
75-
@mock.patch("google.oauth2._id_token_async._fetch_certs", autospec=True)
75+
@mock.patch("google.oauth2._id_token_async._fetch_certs", new_callable=AsyncMock)
7676
@pytest.mark.asyncio
7777
async def test_verify_token(_fetch_certs, decode):
7878
result = await id_token.verify_token(mock.sentinel.token, mock.sentinel.request)
@@ -90,7 +90,7 @@ async def test_verify_token(_fetch_certs, decode):
9090

9191

9292
@mock.patch("google.auth.jwt.decode", autospec=True)
93-
@mock.patch("google.oauth2._id_token_async._fetch_certs", autospec=True)
93+
@mock.patch("google.oauth2._id_token_async._fetch_certs", new_callable=AsyncMock)
9494
@pytest.mark.asyncio
9595
async def test_verify_token_clock_skew(_fetch_certs, decode):
9696
result = await id_token.verify_token(
@@ -110,7 +110,7 @@ async def test_verify_token_clock_skew(_fetch_certs, decode):
110110

111111

112112
@mock.patch("google.auth.jwt.decode", autospec=True)
113-
@mock.patch("google.oauth2._id_token_async._fetch_certs", autospec=True)
113+
@mock.patch("google.oauth2._id_token_async._fetch_certs", new_callable=AsyncMock)
114114
@pytest.mark.asyncio
115115
async def test_verify_token_args(_fetch_certs, decode):
116116
result = await id_token.verify_token(
@@ -130,7 +130,7 @@ async def test_verify_token_args(_fetch_certs, decode):
130130
)
131131

132132

133-
@mock.patch("google.oauth2._id_token_async.verify_token", autospec=True)
133+
@mock.patch("google.oauth2._id_token_async.verify_token", new_callable=AsyncMock)
134134
@pytest.mark.asyncio
135135
async def test_verify_oauth2_token(verify_token):
136136
verify_token.return_value = {"iss": "accounts.google.com"}
@@ -148,7 +148,7 @@ async def test_verify_oauth2_token(verify_token):
148148
)
149149

150150

151-
@mock.patch("google.oauth2._id_token_async.verify_token", autospec=True)
151+
@mock.patch("google.oauth2._id_token_async.verify_token", new_callable=AsyncMock)
152152
@pytest.mark.asyncio
153153
async def test_verify_oauth2_token_clock_skew(verify_token):
154154
verify_token.return_value = {"iss": "accounts.google.com"}
@@ -169,7 +169,7 @@ async def test_verify_oauth2_token_clock_skew(verify_token):
169169
)
170170

171171

172-
@mock.patch("google.oauth2._id_token_async.verify_token", autospec=True)
172+
@mock.patch("google.oauth2._id_token_async.verify_token", new_callable=AsyncMock)
173173
@pytest.mark.asyncio
174174
async def test_verify_oauth2_token_invalid_iss(verify_token):
175175
verify_token.return_value = {"iss": "invalid_issuer"}
@@ -180,7 +180,7 @@ async def test_verify_oauth2_token_invalid_iss(verify_token):
180180
)
181181

182182

183-
@mock.patch("google.oauth2._id_token_async.verify_token", autospec=True)
183+
@mock.patch("google.oauth2._id_token_async.verify_token", new_callable=AsyncMock)
184184
@pytest.mark.asyncio
185185
async def test_verify_firebase_token(verify_token):
186186
result = await id_token.verify_firebase_token(
@@ -197,7 +197,7 @@ async def test_verify_firebase_token(verify_token):
197197
)
198198

199199

200-
@mock.patch("google.oauth2._id_token_async.verify_token", autospec=True)
200+
@mock.patch("google.oauth2._id_token_async.verify_token", new_callable=AsyncMock)
201201
@pytest.mark.asyncio
202202
async def test_verify_firebase_token_clock_skew(verify_token):
203203
result = await id_token.verify_firebase_token(

tests_async/oauth2/test_reauth_async.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def obtain_challenge_input(self, metadata):
6060
@pytest.mark.asyncio
6161
async def test__get_challenges():
6262
with mock.patch(
63-
"google.oauth2._client_async._token_endpoint_request"
63+
"google.oauth2._client_async._token_endpoint_request", new_callable=AsyncMock
6464
) as mock_token_endpoint_request:
6565
await _reauth_async._get_challenges(MOCK_REQUEST, ["SAML"], "token")
6666
mock_token_endpoint_request.assert_called_with(
@@ -75,7 +75,7 @@ async def test__get_challenges():
7575
@pytest.mark.asyncio
7676
async def test__get_challenges_with_scopes():
7777
with mock.patch(
78-
"google.oauth2._client_async._token_endpoint_request"
78+
"google.oauth2._client_async._token_endpoint_request", new_callable=AsyncMock
7979
) as mock_token_endpoint_request:
8080
await _reauth_async._get_challenges(
8181
MOCK_REQUEST, ["SAML"], "token", requested_scopes=["scope"]
@@ -95,7 +95,7 @@ async def test__get_challenges_with_scopes():
9595
@pytest.mark.asyncio
9696
async def test__send_challenge_result():
9797
with mock.patch(
98-
"google.oauth2._client_async._token_endpoint_request"
98+
"google.oauth2._client_async._token_endpoint_request", new_callable=AsyncMock
9999
) as mock_token_endpoint_request:
100100
await _reauth_async._send_challenge_result(
101101
MOCK_REQUEST, "123", "1", {"credential": "password"}, "token"
@@ -171,7 +171,7 @@ async def test__run_next_challenge_success():
171171
"google.oauth2.challenges.AVAILABLE_CHALLENGES", {"PASSWORD": mock_challenge}
172172
):
173173
with mock.patch(
174-
"google.oauth2._reauth_async._send_challenge_result"
174+
"google.oauth2._reauth_async._send_challenge_result", new_callable=AsyncMock
175175
) as mock_send_challenge_result:
176176
await _reauth_async._run_next_challenge(
177177
CHALLENGES_RESPONSE_TEMPLATE, MOCK_REQUEST, "token"
@@ -185,6 +185,7 @@ async def test__run_next_challenge_success():
185185
async def test__obtain_rapt_authenticated():
186186
with mock.patch(
187187
"google.oauth2._reauth_async._get_challenges",
188+
new_callable=AsyncMock,
188189
return_value=CHALLENGES_RESPONSE_AUTHENTICATED,
189190
):
190191
new_rapt_token = await _reauth_async._obtain_rapt(MOCK_REQUEST, "token", None)
@@ -195,10 +196,12 @@ async def test__obtain_rapt_authenticated():
195196
async def test__obtain_rapt_authenticated_after_run_next_challenge():
196197
with mock.patch(
197198
"google.oauth2._reauth_async._get_challenges",
199+
new_callable=AsyncMock,
198200
return_value=CHALLENGES_RESPONSE_TEMPLATE,
199201
):
200202
with mock.patch(
201203
"google.oauth2._reauth_async._run_next_challenge",
204+
new_callable=AsyncMock,
202205
side_effect=[
203206
CHALLENGES_RESPONSE_TEMPLATE,
204207
CHALLENGES_RESPONSE_AUTHENTICATED,
@@ -216,7 +219,7 @@ async def test__obtain_rapt_unsupported_status():
216219
challenges_response = copy.deepcopy(CHALLENGES_RESPONSE_TEMPLATE)
217220
challenges_response["status"] = "STATUS_UNSPECIFIED"
218221
with mock.patch(
219-
"google.oauth2._reauth_async._get_challenges", return_value=challenges_response
222+
"google.oauth2._reauth_async._get_challenges", new_callable=AsyncMock, return_value=challenges_response
220223
):
221224
with pytest.raises(exceptions.ReauthFailError) as excinfo:
222225
await _reauth_async._obtain_rapt(MOCK_REQUEST, "token", None)
@@ -227,6 +230,7 @@ async def test__obtain_rapt_unsupported_status():
227230
async def test__obtain_rapt_not_interactive():
228231
with mock.patch(
229232
"google.oauth2._reauth_async._get_challenges",
233+
new_callable=AsyncMock,
230234
return_value=CHALLENGES_RESPONSE_TEMPLATE,
231235
):
232236
with mock.patch("google.oauth2.reauth.is_interactive", return_value=False):
@@ -239,6 +243,7 @@ async def test__obtain_rapt_not_interactive():
239243
async def test__obtain_rapt_not_authenticated():
240244
with mock.patch(
241245
"google.oauth2._reauth_async._get_challenges",
246+
new_callable=AsyncMock,
242247
return_value=CHALLENGES_RESPONSE_TEMPLATE,
243248
):
244249
with mock.patch("google.oauth2.reauth.RUN_CHALLENGE_RETRY_LIMIT", 0):
@@ -251,10 +256,11 @@ async def test__obtain_rapt_not_authenticated():
251256
async def test_get_rapt_token():
252257
with mock.patch(
253258
"google.oauth2._client_async.refresh_grant",
259+
new_callable=AsyncMock,
254260
return_value=("token", None, None, None),
255261
) as mock_refresh_grant:
256262
with mock.patch(
257-
"google.oauth2._reauth_async._obtain_rapt", return_value="new_rapt_token"
263+
"google.oauth2._reauth_async._obtain_rapt", new_callable=AsyncMock, return_value="new_rapt_token"
258264
) as mock_obtain_rapt:
259265
assert (
260266
await _reauth_async.get_rapt_token(
@@ -282,7 +288,7 @@ async def test_get_rapt_token():
282288
@pytest.mark.asyncio
283289
async def test_refresh_grant_failed():
284290
with mock.patch(
285-
"google.oauth2._client_async._token_endpoint_request_no_throw"
291+
"google.oauth2._client_async._token_endpoint_request_no_throw", new_callable=AsyncMock
286292
) as mock_token_request:
287293
mock_token_request.return_value = (False, {"error": "Bad request"}, True)
288294
with pytest.raises(exceptions.RefreshError) as excinfo:
@@ -314,14 +320,14 @@ async def test_refresh_grant_failed():
314320
@pytest.mark.asyncio
315321
async def test_refresh_grant_success():
316322
with mock.patch(
317-
"google.oauth2._client_async._token_endpoint_request_no_throw"
323+
"google.oauth2._client_async._token_endpoint_request_no_throw", new_callable=AsyncMock
318324
) as mock_token_request:
319325
mock_token_request.side_effect = [
320326
(False, {"error": "invalid_grant", "error_subtype": "rapt_required"}, True),
321327
(True, {"access_token": "access_token"}, None),
322328
]
323329
with mock.patch(
324-
"google.oauth2._reauth_async.get_rapt_token", return_value="new_rapt_token"
330+
"google.oauth2._reauth_async.get_rapt_token", new_callable=AsyncMock, return_value="new_rapt_token"
325331
):
326332
assert await _reauth_async.refresh_grant(
327333
MOCK_REQUEST,
@@ -342,7 +348,7 @@ async def test_refresh_grant_success():
342348
@pytest.mark.asyncio
343349
async def test_refresh_grant_reauth_refresh_disabled():
344350
with mock.patch(
345-
"google.oauth2._client_async._token_endpoint_request_no_throw"
351+
"google.oauth2._client_async._token_endpoint_request_no_throw", new_callable=AsyncMock
346352
) as mock_token_request:
347353
mock_token_request.side_effect = [
348354
(

tests_async/transport/test_aiohttp_requests.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
# limitations under the License.
1414

1515
from unittest import mock
16+
try:
17+
from unittest.mock import AsyncMock
18+
except ImportError:
19+
from mock import AsyncMock
1620

1721
import aiohttp # type: ignore
1822
from aioresponses import aioresponses, core # type: ignore

0 commit comments

Comments
 (0)