From 09e0389db72cc9d6c5dde34864cb54d717dc0b92 Mon Sep 17 00:00:00 2001 From: Slava Date: Thu, 3 Jun 2021 10:44:40 +0300 Subject: [PATCH] fix: session object was never used in aiohttp request (#700) (#701) * fix: session object was never used in aiohttp request (#700) * fixup! fix: session object was never used in aiohttp request (#700) * fixup! fixup! fix: session object was never used in aiohttp request (#700) Co-authored-by: arithmetic1728 <58957152+arithmetic1728@users.noreply.github.com> --- google/auth/transport/_aiohttp_requests.py | 7 ++++++- system_tests/system_tests_async/conftest.py | 14 ++++++++++---- tests_async/transport/test_aiohttp_requests.py | 15 ++++++++++++--- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/google/auth/transport/_aiohttp_requests.py b/google/auth/transport/_aiohttp_requests.py index 4293810dd..ab7dfef67 100644 --- a/google/auth/transport/_aiohttp_requests.py +++ b/google/auth/transport/_aiohttp_requests.py @@ -138,7 +138,12 @@ class Request(transport.Request): """ def __init__(self, session=None): - self.session = None + # TODO: Use auto_decompress property for aiohttp 3.7+ + if session is not None and session._auto_decompress: + raise ValueError( + "Client sessions with auto_decompress=True are not supported." + ) + self.session = session async def __call__( self, diff --git a/system_tests/system_tests_async/conftest.py b/system_tests/system_tests_async/conftest.py index 47a473e7f..966909924 100644 --- a/system_tests/system_tests_async/conftest.py +++ b/system_tests/system_tests_async/conftest.py @@ -26,9 +26,7 @@ from google.auth.transport import _aiohttp_requests as aiohttp_requests from system_tests.system_tests_sync import conftest as sync_conftest -ASYNC_REQUESTS_SESSION = aiohttp.ClientSession() -ASYNC_REQUESTS_SESSION.verify = False TOKEN_INFO_URL = "https://www.googleapis.com/oauth2/v3/tokeninfo" @@ -49,10 +47,18 @@ def authorized_user_file(): """The full path to a valid authorized user file.""" yield sync_conftest.AUTHORIZED_USER_FILE + +@pytest.fixture +async def aiohttp_session(): + async with aiohttp.ClientSession(auto_decompress=False) as session: + yield session + + @pytest.fixture(params=["aiohttp"]) -async def http_request(request): +async def http_request(request, aiohttp_session): """A transport.request object.""" - yield aiohttp_requests.Request(ASYNC_REQUESTS_SESSION) + yield aiohttp_requests.Request(aiohttp_session) + @pytest.fixture async def token_info(http_request): diff --git a/tests_async/transport/test_aiohttp_requests.py b/tests_async/transport/test_aiohttp_requests.py index 10c31db8f..a64a4eec9 100644 --- a/tests_async/transport/test_aiohttp_requests.py +++ b/tests_async/transport/test_aiohttp_requests.py @@ -112,11 +112,18 @@ def make_request(self): return aiohttp_requests.Request() def make_with_parameter_request(self): - http = mock.create_autospec(aiohttp.ClientSession, instance=True) + http = aiohttp.ClientSession(auto_decompress=False) return aiohttp_requests.Request(http) + def test_unsupported_session(self): + http = aiohttp.ClientSession(auto_decompress=True) + with pytest.raises(ValueError): + aiohttp_requests.Request(http) + def test_timeout(self): - http = mock.create_autospec(aiohttp.ClientSession, instance=True) + http = mock.create_autospec( + aiohttp.ClientSession, instance=True, _auto_decompress=False + ) request = aiohttp_requests.Request(http) request(url="http://example.com", method="GET", timeout=5) @@ -142,7 +149,9 @@ def test_constructor(self): assert authed_session.credentials == mock.sentinel.credentials def test_constructor_with_auth_request(self): - http = mock.create_autospec(aiohttp.ClientSession) + http = mock.create_autospec( + aiohttp.ClientSession, instance=True, _auto_decompress=False + ) auth_request = aiohttp_requests.Request(http) authed_session = aiohttp_requests.AuthorizedSession(