I have a Flask app that I use,
Im working on authenticating all traffic to the server (except for /login
and one more endpoint)
I implemented this using the @app.before_request
decorator like so:
@app.before_request
def authenticate():
if any(request.path in s for s in NO_AUTH_ENDPOINTS):
return
# authentication process....
This works fine, but then when I got to the flask unittests that were failing because of the auth, I didnt manage to patch that function and all the tests keep getting unauthorized error.
I tried the following:
@patch('authentication.authenticate')
def post(self, body, mock_auth, id=None, token='test_token', **params):
mock_auth.return_value = None
return self.app.post(self._get_url(id, params),
data=json.dumps(body),
follow_redirects=True,
content_type='application/json',
headers={'Token': token})
and also:
@patch('autoai.server.app.before_request')
def post(self, body, mock_request, id=None, token='test_token', **params):
mock_request.return_value = None
return self.app.post(self._get_url(id, params),
data=json.dumps(body),
follow_redirects=True,
content_type='application/json',
headers={'Token': token})
I use the flast test_app so that might be related, but I still cant figure out how to patch that begfore_request properly (self.app = app.test_client
)