-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mocking fails when using Authlib HTTPX integration OAuth2 client #46
Comments
Interesting, and thanks for catching this @bobh66. I haven't used the authlib integration client myself, but since it is using httpx Quickly looked the authlib/httpx code around this, and the Don't think we need authlib to test and implement this. Just need a custom |
We might need to change to patching |
client.send_handling_redirects() calls client.send_handling_auth() which calls the auth_flow() method that clobbers the URL. client.send_single_request() might be the place to patch to ensure that all of the auth changes have happened and there is nothing left that will change the url response. |
For future @lundberg this is something that may/will change if/when Middleware becomes a thing: encode/httpx#800 |
I hacked something that works by mocking send_single_request but since not all clients use send_single_request it's not a good solution, the unit tests fail miserably with that code in. |
Awesome that you're adding this to the context @StephenBrown2 👍🏻 Just for background info, I've implemented the response patching as deep as possible to minimize httpx internal api changes, and also handle testing custom implementations. This is true for the async path, but unfortunately not for sync, since it is using urlib3. |
Just to chime in quickly with a workaround based on #46 (comment) from authlib.integrations.httpx_client.oauth2_client import OAuth2Auth, OAuth2ClientAuth
@pytest.fixture(name="sso")
def fixture_sso(monkeypatch):
def auth_flow_spy(auth_class):
original_auth_flow = auth_class.auth_flow
@functools.wraps(original_auth_flow)
def f(auth, request):
respx_url = request.url
authlib_request = next(original_auth_flow(auth, request))
authlib_request.url = respx_url
yield authlib_request
return f
monkeypatch.setattr(OAuth2Auth, "auth_flow", auth_flow_spy(OAuth2Auth))
monkeypatch.setattr(OAuth2ClientAuth, "auth_flow", auth_flow_spy(OAuth2ClientAuth)) It solved our particular needs, and might be helpful to others until a proper fix is in place |
@bobh66, a new release is out where I patch |
@firesock, would be awesome if you also could try the latest release to see if it solves this issue. |
This seems to be solved. |
Sorry, only got around to updating our pin recently. I can confirm with authlib 0.15.1, httpx 0.16.1 and respx 0.14.0 the workaround is unnecessary. |
I'm trying to mock the responses from Keycloak using the Authlib HTTPX integration client for OAuth2.
I'm getting the following exception because the auth_flow() hook for the OAuth2 client is rewriting the request.url attribute so it is no longer a URLResponse object, it's a plain URL (again).
By this time the response is gone so I don't know what the mocked method could return. It would have to go back and re-match on the request (somehow).
Any ideas to work around or fix this? I'm happy to push a PR if someone can point me at a possible fix.
Thanks
The text was updated successfully, but these errors were encountered: