flask unit test: how to test request from logged in user

后端 未结 1 437
我在风中等你
我在风中等你 2020-12-09 05:41

I\'m writing some unit tests for my Flask web application and I\'m trying to test the differences in the response between a request made by an anonymous user and a logged in

相关标签:
1条回答
  • 2020-12-09 05:57

    Flask-Login looks for user_id in the session, you can set this in the tests using session_transaction:

    with app.test_client() as c:
        with c.session_transaction() as sess:
            sess['user_id'] = 'myuserid'
            sess['_fresh'] = True # https://flask-login.readthedocs.org/en/latest/#fresh-logins
        resp = c.get('/someurl')
    

    Where myuserid is the id of your user object.

    0 讨论(0)
提交回复
热议问题