22
22
from google .auth .aio .credentials import AnonymousCredentials
23
23
from google .auth .aio .transport import (
24
24
_DEFAULT_TIMEOUT_SECONDS ,
25
- DEFAULT_MAX_REFRESH_ATTEMPTS ,
25
+ DEFAULT_MAX_RETRY_ATTEMPTS ,
26
26
DEFAULT_RETRYABLE_STATUS_CODES ,
27
27
Request ,
28
28
Response ,
@@ -146,7 +146,7 @@ async def test_timeout_with_async_task_timing_out_before_context(
146
146
)
147
147
148
148
149
- class TestAuthorizedSession (object ):
149
+ class TestAsyncAuthorizedSession (object ):
150
150
TEST_URL = "http://example.com/"
151
151
credentials = AnonymousCredentials ()
152
152
@@ -159,14 +159,14 @@ async def mocked_content(self):
159
159
@pytest .mark .asyncio
160
160
async def test_constructor_with_default_auth_request (self ):
161
161
with patch ("google.auth.aio.transport.sessions.AIOHTTP_INSTALLED" , True ):
162
- authed_session = sessions .AuthorizedSession (self .credentials )
162
+ authed_session = sessions .AsyncAuthorizedSession (self .credentials )
163
163
assert authed_session ._credentials == self .credentials
164
164
await authed_session .close ()
165
165
166
166
@pytest .mark .asyncio
167
167
async def test_constructor_with_provided_auth_request (self ):
168
168
auth_request = MockRequest ()
169
- authed_session = sessions .AuthorizedSession (
169
+ authed_session = sessions .AsyncAuthorizedSession (
170
170
self .credentials , auth_request = auth_request
171
171
)
172
172
@@ -177,7 +177,7 @@ async def test_constructor_with_provided_auth_request(self):
177
177
async def test_constructor_raises_no_auth_request_error (self ):
178
178
with patch ("google.auth.aio.transport.sessions.AIOHTTP_INSTALLED" , False ):
179
179
with pytest .raises (TransportError ) as exc :
180
- sessions .AuthorizedSession (self .credentials )
180
+ sessions .AsyncAuthorizedSession (self .credentials )
181
181
182
182
exc .match (
183
183
"`auth_request` must either be configured or the external package `aiohttp` must be installed to use the default value."
@@ -187,7 +187,7 @@ async def test_constructor_raises_no_auth_request_error(self):
187
187
async def test_constructor_raises_incorrect_credentials_error (self ):
188
188
credentials = Mock ()
189
189
with pytest .raises (InvalidType ) as exc :
190
- sessions .AuthorizedSession (credentials )
190
+ sessions .AsyncAuthorizedSession (credentials )
191
191
192
192
exc .match (
193
193
f"The configured credentials of type { type (credentials )} are invalid and must be of type `google.auth.aio.credentials.Credentials`"
@@ -199,7 +199,7 @@ async def test_request_default_auth_request_success(self):
199
199
mocked_chunks = [b"Cavefish " , b"have " , b"no " , b"sight." ]
200
200
mocked_response = b"" .join (mocked_chunks )
201
201
m .get (self .TEST_URL , status = 200 , body = mocked_response )
202
- authed_session = sessions .AuthorizedSession (self .credentials )
202
+ authed_session = sessions .AsyncAuthorizedSession (self .credentials )
203
203
response = await authed_session .request ("GET" , self .TEST_URL )
204
204
assert response .status_code == 200
205
205
assert response .headers == {"Content-Type" : "application/json" }
@@ -216,7 +216,7 @@ async def test_request_provided_auth_request_success(self, mocked_content):
216
216
content = mocked_content ,
217
217
)
218
218
auth_request = MockRequest (mocked_response )
219
- authed_session = sessions .AuthorizedSession (self .credentials , auth_request )
219
+ authed_session = sessions .AsyncAuthorizedSession (self .credentials , auth_request )
220
220
response = await authed_session .request ("GET" , self .TEST_URL )
221
221
assert response .status_code == 200
222
222
assert response .headers == {"Content-Type" : "application/json" }
@@ -229,21 +229,21 @@ async def test_request_provided_auth_request_success(self, mocked_content):
229
229
@pytest .mark .asyncio
230
230
async def test_request_raises_timeout_error (self ):
231
231
auth_request = MockRequest (side_effect = asyncio .TimeoutError )
232
- authed_session = sessions .AuthorizedSession (self .credentials , auth_request )
232
+ authed_session = sessions .AsyncAuthorizedSession (self .credentials , auth_request )
233
233
with pytest .raises (TimeoutError ):
234
234
await authed_session .request ("GET" , self .TEST_URL )
235
235
236
236
@pytest .mark .asyncio
237
237
async def test_request_raises_transport_error (self ):
238
238
auth_request = MockRequest (side_effect = TransportError )
239
- authed_session = sessions .AuthorizedSession (self .credentials , auth_request )
239
+ authed_session = sessions .AsyncAuthorizedSession (self .credentials , auth_request )
240
240
with pytest .raises (TransportError ):
241
241
await authed_session .request ("GET" , self .TEST_URL )
242
242
243
243
@pytest .mark .asyncio
244
244
async def test_request_max_allowed_time_exceeded_error (self ):
245
245
auth_request = MockRequest (side_effect = TransportError )
246
- authed_session = sessions .AuthorizedSession (self .credentials , auth_request )
246
+ authed_session = sessions .AsyncAuthorizedSession (self .credentials , auth_request )
247
247
with patch ("time.monotonic" , side_effect = [0 , 1 , 1 ]):
248
248
with pytest .raises (TimeoutError ):
249
249
await authed_session .request ("GET" , self .TEST_URL , max_allowed_time = 1 )
@@ -254,14 +254,16 @@ async def test_request_max_retries(self, retry_status):
254
254
mocked_response = MockResponse (status_code = retry_status )
255
255
auth_request = MockRequest (mocked_response )
256
256
with patch ("asyncio.sleep" , return_value = None ):
257
- authed_session = sessions .AuthorizedSession (self .credentials , auth_request )
257
+ authed_session = sessions .AsyncAuthorizedSession (
258
+ self .credentials , auth_request
259
+ )
258
260
await authed_session .request ("GET" , self .TEST_URL )
259
- assert auth_request .call_count == DEFAULT_MAX_REFRESH_ATTEMPTS
261
+ assert auth_request .call_count == DEFAULT_MAX_RETRY_ATTEMPTS
260
262
261
263
@pytest .mark .asyncio
262
264
async def test_http_get_method_success (self ):
263
265
expected_payload = b"content is retrieved."
264
- authed_session = sessions .AuthorizedSession (self .credentials )
266
+ authed_session = sessions .AsyncAuthorizedSession (self .credentials )
265
267
with aioresponses () as m :
266
268
m .get (self .TEST_URL , status = 200 , body = expected_payload )
267
269
response = await authed_session .get (self .TEST_URL )
@@ -271,7 +273,7 @@ async def test_http_get_method_success(self):
271
273
@pytest .mark .asyncio
272
274
async def test_http_post_method_success (self ):
273
275
expected_payload = b"content is posted."
274
- authed_session = sessions .AuthorizedSession (self .credentials )
276
+ authed_session = sessions .AsyncAuthorizedSession (self .credentials )
275
277
with aioresponses () as m :
276
278
m .post (self .TEST_URL , status = 200 , body = expected_payload )
277
279
response = await authed_session .post (self .TEST_URL )
@@ -281,7 +283,7 @@ async def test_http_post_method_success(self):
281
283
@pytest .mark .asyncio
282
284
async def test_http_put_method_success (self ):
283
285
expected_payload = b"content is retrieved."
284
- authed_session = sessions .AuthorizedSession (self .credentials )
286
+ authed_session = sessions .AsyncAuthorizedSession (self .credentials )
285
287
with aioresponses () as m :
286
288
m .put (self .TEST_URL , status = 200 , body = expected_payload )
287
289
response = await authed_session .put (self .TEST_URL )
@@ -291,7 +293,7 @@ async def test_http_put_method_success(self):
291
293
@pytest .mark .asyncio
292
294
async def test_http_patch_method_success (self ):
293
295
expected_payload = b"content is retrieved."
294
- authed_session = sessions .AuthorizedSession (self .credentials )
296
+ authed_session = sessions .AsyncAuthorizedSession (self .credentials )
295
297
with aioresponses () as m :
296
298
m .patch (self .TEST_URL , status = 200 , body = expected_payload )
297
299
response = await authed_session .patch (self .TEST_URL )
@@ -301,7 +303,7 @@ async def test_http_patch_method_success(self):
301
303
@pytest .mark .asyncio
302
304
async def test_http_delete_method_success (self ):
303
305
expected_payload = b"content is deleted."
304
- authed_session = sessions .AuthorizedSession (self .credentials )
306
+ authed_session = sessions .AsyncAuthorizedSession (self .credentials )
305
307
with aioresponses () as m :
306
308
m .delete (self .TEST_URL , status = 200 , body = expected_payload )
307
309
response = await authed_session .delete (self .TEST_URL )
0 commit comments