1

I am trying to test the /auth endpoint in a Flask application that uses the Flask-JWT module.

import json
import pytest

@pytest.mark.usefixtures("testapp")
class TestURLs:

    def test_auth(self, testapp):
        rv = testapp.get('/auth',
            data=json.dumps({'username':'bob', 'password': 'pw'}),
            content_type='application/json')
        ... test assertion ...

The /auth endpoint functions perfectly when I am running a flask server and I can request JWT tokens.

But when the test is run, it receives this response from the server.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The requested URL was not found on the server.  If you entered the URL manually please check your spelling and try again.</p>

Is there a way to configure the test fixture to recognise the /auth endpoint?

ChrisGuest
  • 3,398
  • 4
  • 32
  • 53
  • 1
    Where do you initialise the JWT extension and link it with the app? Are you sure the app is initialised the same way in the test environment? Also, you mentioned `/auth` endpoint in your question but `/api/auth` in the actual test? – Adam Byrtek Apr 19 '17 at 22:19
  • I've edited the uri for consistency. I'll come back with answers to the rest of your questions. – ChrisGuest Apr 19 '17 at 23:40
  • Thanks. In the my `testapp()` method, I needed to initialise jwt, in the same way it is initialised in the flask `app.py`: `jwt = JWT(app, authenticate, identity)` – ChrisGuest Apr 19 '17 at 23:54
  • NOTE for newcomers: use instead, the Flask-JWT-Extended, which is found https://flask-jwt-extended.readthedocs.io/en/stable/, has been updated more recently and has better documentation. – Alexey Nikonov Mar 21 '21 at 14:26

0 Answers0