khteh
khteh

Reputation: 3976

python pytest quart post request with csrf token

How to test POST request, especially to an endpoint which checks for csrf token?

@pytest.mark.asyncio
async def test_fibonacci_pass(client):
    response = await client.post('/fibonacci', data=b"n=10", follow_redirects=True)
    assert response.headers['content-type'] == "text/html; charset=utf-8"
    assert response != ""
    assert response.status_code == HTTPStatus.OK, 'FibonacciController failed'

Snippet of controller endpoint code:

    if request.method == "POST":
        data = await request.get_data()
        params = parse_qs(data.decode('utf-8'))

Test console output:

E       AssertionError: FibonacciController failed
E       assert 400 == <HTTPStatus.OK: 200>
E        +  where 400 = <Response [400 BAD REQUEST]>.status_code
E        +  and   <HTTPStatus.OK: 200> = HTTPStatus.OK

test/fibonaccicontroller_test.py:16: AssertionError
------------------------------------------------------------------------------------- Captured log call --------------------------------------------------------------------------------------
INFO     flask_wtf.csrf:csrf.py:263 The CSRF token is missing.

I am not suer if the AssertionError is due to missing csrf_token as it shows up as INFO instead of being at least a warning.

Upvotes: 0

Views: 22

Answers (0)

Related Questions