"""The two OAuth-callback routes read their body with a raw `await request.json()` and then call `.get()` on it. A malformed body made `request.json()` raise `json.JSONDecodeError` and a non-object body (a JSON array, which is valid JSON) made the `.get()` raise `AttributeError`. Both escaped the handler's `except Exception` -- that only wraps the token exchange below them -- so the client got a 500 for what is plainly a bad request. Every other route in these modules turns a bad body into a 400. """ import pytest @pytest.mark.parametrize("path", ["/api/schwab/auth/callback", "/api/trades/auth/callback"]) def test_malformed_json_body_is_a_400(raw_app_client, path): resp = raw_app_client.post( path, content="{not json", headers={"Content-Type": "application/json"}, ) assert resp.status_code == 400, resp.text @pytest.mark.parametrize("path", ["/api/schwab/auth/callback", "/api/trades/auth/callback"]) def test_non_object_json_body_is_a_400(raw_app_client, path): resp = raw_app_client.post( path, content='["redirect_url"]', headers={"Content-Type": "application/json"}, ) assert resp.status_code == 400, resp.text @pytest.mark.parametrize("path", ["/api/schwab/auth/callback", "/api/trades/auth/callback"]) def test_non_string_redirect_url_is_a_400(raw_app_client, path): """A JSON number is a well-formed object but still not a usable URL.""" resp = raw_app_client.post(path, json={"redirect_url": 12345}) assert resp.status_code == 400, resp.text