I am unit testing my flask app which uses the flask-login extension.
I am setting up all my tests like this using webtest:
class TestCase(unittest.TestCase):
def setUp(self):
app.config['TESTING'] = True
self.client = webtest.TestApp(app)
But when I try to visit urls through self.client.get() which are decorated with @login_required, I get a 401 error with a message that I am not authorized to access the url.
According to the documentation https://flask-login.readthedocs.org/en/latest/#protecting-views and this discussion, setting the config value of 'TESTING' to True should disable the login requirements, but that doesn't seem to be working for me.
Any suggestions?
This because
Flask-Login
cachingTESTING
orLOGIN_DISABLED
oninit_app
(https://github.com/maxcountryman/flask-login/blob/master/flask_login.py#L164).So if you create application and then set something in config then you config changes will ignored.
Better way use application factory with separated configs for production and tests, it also decrease probability get errors with not cleaned application state in tests.
The easiest way reinit
login_manager
:I'm not sure if this will help, but:
in my old flaskr project file, I had the configurations in my "flaskr.py" file, and they looked like this:
So maybe you would have
?
from flask login documentation it's said that :
if you are using application factory design pattern add this to your testing config :
or you can add it while creating the app like this :
Hope this will help the others who will have the same problem