0

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.