Open adriantorrie opened 3 years ago
Sorry for the slow response, I was traveling. The issue is AsyncMock
not handling async generators properly. It is making the asend()
and aclose()
methods synchronous for some reason:
In [1]: from httpx_gssapi import HTTPSPNEGOAuth
In [2]: from unittest.mock import AsyncMock
In [3]: auth = AsyncMock(spec=HTTPSPNEGOAuth)
In [4]: auth_flow = auth.async_auth_flow(...)
In [5]: auth_flow
Out[5]: <MagicMock name='mock.async_auth_flow()' id='140057769280368'>
In [6]: auth_flow.__anext__() # Correctly a coroutine
Out[6]: <coroutine object AsyncMockMixin._execute_mock_call at 0x7f61bd9731c0>
In [7]: auth_flow.asend
Out[7]: <MagicMock name='mock.async_auth_flow().asend' id='140057769692224'>
In [8]: auth_flow.asend() # Not a coroutine
Out[8]: <MagicMock name='mock.async_auth_flow().asend()' id='140057769324208'>
In [9]: await auth_flow.asend()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-9-4d8d419a061f> in <module>
----> 1 await auth_flow.asend()
TypeError: object MagicMock can't be used in 'await' expression
To be honest, I'm not sure how to handle this simply, without mocking out some of the internal HTTPX auth flow details themselves. A better approach may be to use K5TEST like we do in the end_to_end tests and setup a fake Kerberos environment instead of mocking the authentication.
Using pytest, I've tried to create a mock fixture for an app written with FastAPI/HTTPX (FastAPI is an interface microservice receiving requests, and forwarding onto a backend service using an HTTPX Async client).
The fixture and unit test
MyCllient
has theauth
set as a member, using the following prod defaults, which I thought the mock replacing in the fixture would be enough for things to "just work".Within the function being tested, a get request is being made using a
HTTPSPNEGOAuth
auth object, with anhttpx.AsyncClient
instanceI receive the error:
I treid swapping the
Mock
for anAsyncMock
in the fixture:Which gives a similar error still:
I'm not sure where to take it from here to get the auth object mocked out correctly for use with pytest.