简体   繁体   English

使用flask-login对烧瓶进行单元测试时禁用@login_required

[英]Disabling @login_required while unit-testing flask with flask-login

I am unit testing my flask app which uses the flask-login extension. 我正在测试我的烧瓶应用程序,它使用flask-login扩展。

I am setting up all my tests like this using webtest: 我正在使用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. 但是当我尝试通过使用@login_required修饰的self.client.get()访问URL时,我收到401错误,并显示一条消息,表示我无权访问该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. 根据文档https://flask-login.readthedocs.org/en/latest/#protecting-views和本讨论 ,将'TESTING'的配置值设置为True应该禁用登录要求,但这似乎不是为我工作

Any suggestions? 有什么建议?

This because Flask-Login caching TESTING or LOGIN_DISABLED on init_app ( https://github.com/maxcountryman/flask-login/blob/master/flask_login.py#L164 ). 这是因为Flask-Login缓存TESTINGLOGIN_DISABLEDinit_apphttps://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 : login_manager最简单的方法:

class TestCase(unittest.TestCase):
    def setUp(self):
        app.config['TESTING'] = True
        app.login_manager.init_app(app)
        self.client = webtest.TestApp(app)

from flask login documentation it's said that : 来自flask登录文档,它说:

It can be convenient to globally turn off authentication when unit testing. 在单元测试时全局关闭认证可能很方便。 To enable this, if the application configuration variable LOGIN_DISABLED is set to True, this decorator will be ignored. 要启用此功能,如果应用程序配置变量LOGIN_DISABLED设置为True,则将忽略此装饰器。

if you are using application factory design pattern add this to your testing config : 如果您正在使用应用程序工厂设计模式,请将此添加到您的测试配置

LOGIN_DISABLED = True

or you can add it while creating the app like this : 或者您可以在创建应用程序时添加它,如下所示:

class TestCase(unittest.TestCase):
    def setUp(self):
        app.config['LOGIN_DISABLED'] = True
        self.client = webtest.TestApp(app)

Hope this will help the others who will have the same problem 希望这将有助于其他将遇到同样问题的人

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: 在我的旧flaskr项目文件中,我在“flaskr.py”文件中有配置,它们看起来像这样:

# configuration
DATABASE = 'flaskr.db'
DEBUG = True
SECRET_KEY = 'development key'
USERNAME = 'admin'
PASSWORD = 'default'

So maybe you would have 所以也许你会

TESTING = True

?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM