File size: 1,553 Bytes
ee446e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""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