测试Flask登录和身份验证? [英] Testing Flask login and authentication?

查看:146
本文介绍了测试Flask登录和身份验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Flask应用程序,并使用Flask-security进行用户身份验证(这反过来使用Flask-login)。

身份验证, / user 。我试图编写一个单元测试,测试,验证用户,这将返回适当的响应。

在我的单元测试中,我创建一个用户和日志记录像这样的用户:

  from unittest import TestCase 
from app import app,db
from models从flask_security.utils导入用户
导入login_user
$ b $ class UserTest(TestCase):
def setUp(self):
self.app = app
self .client = self.app.test_client()
self._ctx = self.app.test_request_context()
self._ctx.push()

db.create_all()

def tearDown(self):
如果self._ctx不是None:
self._ctx.pop()

db.session.remove )
db.drop_all()
$ b $ def test_user_authentication():
#(测试用例在测试请求上下文中)
user = User(active = True )
db.session.add(用户)
db.session.co mmit()
login_user(用户)

#current_user这里是用户
print(current_user)

#这个请求中的current_user是一个匿名用户
r = test_client.get('/ user')

在test current_user 返回正确的用户。但是,所请求的视图总是以 current_user 的形式返回 AnonymousUser

/ user 路由被定义为:

  class CurrentUser(资源):
def get(self):
return current_user#返回一个AnonymousUser

我很确定我只是不完全了解测试Flask请求上下文的工作方式。我已经阅读了这个Flask 请求上下文文档一堆,但我仍然不理解如何处理这个特殊的单元测试。

解决方案

问题在于 test_client.get()调用导致一个新的请求上下文被推送,所以你在你的测试用例的 setUp()方法中推送的那个不是那个 / user 处理程序看到。



我认为登录和退出测试添加消息部分的文档是测试登录的最佳方法。这个想法是通过应用程序发送登录请求,就像普通客户端一样。这将负责在用户会话中注册登录用户。


I'm developing a Flask application and using Flask-security for user authentication (which in turn uses Flask-login underneath).

I have a route which requires authentication, /user. I'm trying to write a unit test which tests that, for an authenticated user, this returns the appropriate response.

In my unittest I'm creating a user and logging as that user like so:

from unittest import TestCase
from app import app, db
from models import User
from flask_security.utils import login_user

class UserTest(TestCase):
   def setUp(self):
       self.app = app
       self.client = self.app.test_client()
       self._ctx = self.app.test_request_context()
       self._ctx.push()

       db.create_all()

   def tearDown(self):
       if self._ctx is not None:
           self._ctx.pop()

       db.session.remove()
       db.drop_all()

   def test_user_authentication():
       # (the test case is within a test request context)
       user = User(active=True)
       db.session.add(user)
       db.session.commit()
       login_user(user)

       # current_user here is the user
       print(current_user)

       # current_user within this request is an anonymous user
       r = test_client.get('/user')

Within the test current_user returns the correct user. However, the requested view always returns an AnonymousUser as the current_user.

The /user route is defined as:

class CurrentUser(Resource):
    def get(self):
        return current_user  # returns an AnonymousUser

I'm fairly certain I'm just not fully understanding how testing Flask request contexts work. I've read this Flask Request Context documentation a bunch but am still not understanding how to approach this particular unit test.

解决方案

The problem is that the test_client.get() call causes a new request context to be pushed, so the one you pushed in your the setUp() method of your test case is not the one that the /user handler sees.

I think the approach shown in the Logging In and Out and Test Adding Messages sections of the documentation is the best approach for testing logins. The idea is to send the login request through the application, like a regular client would. This will take care of registering the logged in user in the user session of the test client.

这篇关于测试Flask登录和身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆