1616import http .client as http_client
1717import json
1818from unittest import mock
19+ try :
20+ from unittest .mock import AsyncMock
21+ except ImportError :
22+ # Fallback for Python < 3.8
23+ from mock import AsyncMock
1924import urllib
2025
2126import pytest # type: ignore
2934
3035
3136def make_request (response_data , status = http_client .OK , text = False ):
32- response = mock . AsyncMock (spec = ["transport.Response" ])
37+ response = AsyncMock (spec = ["transport.Response" ])
3338 response .status = status
3439 data = response_data if text else json .dumps (response_data ).encode ("utf-8" )
35- response .data = mock . AsyncMock (spec = ["__call__" , "read" ])
36- response .data .read = mock . AsyncMock (spec = ["__call__" ], return_value = data )
37- response .content = mock . AsyncMock (spec = ["__call__" ], return_value = data )
38- request = mock . AsyncMock (spec = ["transport.Request" ])
40+ response .data = AsyncMock (spec = ["__call__" , "read" ])
41+ response .data .read = AsyncMock (spec = ["__call__" ], return_value = data )
42+ response .content = AsyncMock (spec = ["__call__" ], return_value = data )
43+ request = AsyncMock (spec = ["transport.Request" ])
3944 request .return_value = response
4045 return request
4146
@@ -142,21 +147,21 @@ async def test__token_endpoint_request_internal_failure_error():
142147
143148@pytest .mark .asyncio
144149async def test__token_endpoint_request_internal_failure_and_retry_failure_error ():
145- retryable_error = mock . AsyncMock (spec = ["transport.Response" ])
150+ retryable_error = AsyncMock (spec = ["transport.Response" ])
146151 retryable_error .status = http_client .BAD_REQUEST
147152 data = json .dumps ({"error_description" : "internal_failure" }).encode ("utf-8" )
148- retryable_error .data = mock . AsyncMock (spec = ["__call__" , "read" ])
149- retryable_error .data .read = mock . AsyncMock (spec = ["__call__" ], return_value = data )
150- retryable_error .content = mock . AsyncMock (spec = ["__call__" ], return_value = data )
153+ retryable_error .data = AsyncMock (spec = ["__call__" , "read" ])
154+ retryable_error .data .read = AsyncMock (spec = ["__call__" ], return_value = data )
155+ retryable_error .content = AsyncMock (spec = ["__call__" ], return_value = data )
151156
152- unretryable_error = mock . AsyncMock (spec = ["transport.Response" ])
157+ unretryable_error = AsyncMock (spec = ["transport.Response" ])
153158 unretryable_error .status = http_client .BAD_REQUEST
154159 data = json .dumps ({"error_description" : "invalid_scope" }).encode ("utf-8" )
155- unretryable_error .data = mock . AsyncMock (spec = ["__call__" , "read" ])
156- unretryable_error .data .read = mock . AsyncMock (spec = ["__call__" ], return_value = data )
157- unretryable_error .content = mock . AsyncMock (spec = ["__call__" ], return_value = data )
160+ unretryable_error .data = AsyncMock (spec = ["__call__" , "read" ])
161+ unretryable_error .data .read = AsyncMock (spec = ["__call__" ], return_value = data )
162+ unretryable_error .content = AsyncMock (spec = ["__call__" ], return_value = data )
158163
159- request = mock . AsyncMock (spec = ["transport.Request" ])
164+ request = AsyncMock (spec = ["transport.Request" ])
160165 request .side_effect = [retryable_error , retryable_error , unretryable_error ]
161166
162167 with pytest .raises (exceptions .RefreshError ):
@@ -170,21 +175,21 @@ async def test__token_endpoint_request_internal_failure_and_retry_failure_error(
170175
171176@pytest .mark .asyncio
172177async def test__token_endpoint_request_internal_failure_and_retry_succeeds ():
173- retryable_error = mock . AsyncMock (spec = ["transport.Response" ])
178+ retryable_error = AsyncMock (spec = ["transport.Response" ])
174179 retryable_error .status = http_client .BAD_REQUEST
175180 data = json .dumps ({"error_description" : "internal_failure" }).encode ("utf-8" )
176- retryable_error .data = mock . AsyncMock (spec = ["__call__" , "read" ])
177- retryable_error .data .read = mock . AsyncMock (spec = ["__call__" ], return_value = data )
178- retryable_error .content = mock . AsyncMock (spec = ["__call__" ], return_value = data )
181+ retryable_error .data = AsyncMock (spec = ["__call__" , "read" ])
182+ retryable_error .data .read = AsyncMock (spec = ["__call__" ], return_value = data )
183+ retryable_error .content = AsyncMock (spec = ["__call__" ], return_value = data )
179184
180- response = mock . AsyncMock (spec = ["transport.Response" ])
185+ response = AsyncMock (spec = ["transport.Response" ])
181186 response .status = http_client .OK
182187 data = json .dumps ({"hello" : "world" }).encode ("utf-8" )
183- response .data = mock . AsyncMock (spec = ["__call__" , "read" ])
184- response .data .read = mock . AsyncMock (spec = ["__call__" ], return_value = data )
185- response .content = mock . AsyncMock (spec = ["__call__" ], return_value = data )
188+ response .data = AsyncMock (spec = ["__call__" , "read" ])
189+ response .data .read = AsyncMock (spec = ["__call__" ], return_value = data )
190+ response .content = AsyncMock (spec = ["__call__" ], return_value = data )
186191
187- request = mock . AsyncMock (spec = ["transport.Request" ])
192+ request = AsyncMock (spec = ["transport.Request" ])
188193 request .side_effect = [retryable_error , response ]
189194
190195 _ = await _client ._token_endpoint_request (
@@ -399,7 +404,7 @@ async def test_jwt_grant_retry_with_retry(
399404 mock_token_endpoint_request , mock_expiry , can_retry
400405):
401406 _ = await _client .jwt_grant (
402- mock . AsyncMock (), mock .Mock (), mock .Mock (), can_retry = can_retry
407+ AsyncMock (), mock .Mock (), mock .Mock (), can_retry = can_retry
403408 )
404409 mock_token_endpoint_request .assert_called_with (
405410 mock .ANY , mock .ANY , mock .ANY , can_retry = can_retry
@@ -426,7 +431,7 @@ async def test_id_token_jwt_grant_retry_with_retry(
426431 mock_token_endpoint_request , mock_jwt_decode , can_retry
427432):
428433 _ = await _client .id_token_jwt_grant (
429- mock . AsyncMock (), mock . AsyncMock (), mock . AsyncMock (), can_retry = can_retry
434+ AsyncMock (), AsyncMock (), AsyncMock (), can_retry = can_retry
430435 )
431436 mock_token_endpoint_request .assert_called_with (
432437 mock .ANY , mock .ANY , mock .ANY , can_retry = can_retry
@@ -440,11 +445,11 @@ async def test_refresh_grant_retry_default(
440445 mock_token_endpoint_request , mock_parse_expiry
441446):
442447 _ = await _client .refresh_grant (
443- mock . AsyncMock (),
444- mock . AsyncMock (),
445- mock . AsyncMock (),
446- mock . AsyncMock (),
447- mock . AsyncMock (),
448+ AsyncMock (),
449+ AsyncMock (),
450+ AsyncMock (),
451+ AsyncMock (),
452+ AsyncMock (),
448453 )
449454 mock_token_endpoint_request .assert_called_with (
450455 mock .ANY , mock .ANY , mock .ANY , can_retry = True
@@ -459,11 +464,11 @@ async def test_refresh_grant_retry_with_retry(
459464 mock_token_endpoint_request , mock_parse_expiry , can_retry
460465):
461466 _ = await _client .refresh_grant (
462- mock . AsyncMock (),
463- mock . AsyncMock (),
464- mock . AsyncMock (),
465- mock . AsyncMock (),
466- mock . AsyncMock (),
467+ AsyncMock (),
468+ AsyncMock (),
469+ AsyncMock (),
470+ AsyncMock (),
471+ AsyncMock (),
467472 can_retry = can_retry ,
468473 )
469474 mock_token_endpoint_request .assert_called_with (
@@ -481,10 +486,10 @@ async def test__token_endpoint_request_no_throw_with_retry(can_retry):
481486
482487 _ = await _client ._token_endpoint_request_no_throw (
483488 mock_request ,
484- mock . AsyncMock (),
489+ AsyncMock (),
485490 "body" ,
486- mock . AsyncMock (),
487- mock . AsyncMock (),
491+ AsyncMock (),
492+ AsyncMock (),
488493 can_retry = can_retry ,
489494 )
490495
0 commit comments