gpt4 book ai didi

python - (unittest) 测试 Flask-Security : Cannot get past login page

转载 作者:太空狗 更新时间:2023-10-30 01:15:15 25 4
gpt4 key购买 nike

我正在尝试向我的基本应用添加测试。访问所有内容都需要登录。

这是我的测试用例类:

class MyAppTestCase(FlaskTestCaseMixin):

def _create_app(self):
raise NotImplementedError

def _create_fixtures(self):
self.user = EmployeeFactory()

def setUp(self):
super(MyAppTestCase, self).setUp()
self.app = self._create_app()
self.client = self.app.test_client()
self.app_context = self.app.app_context()
self.app_context.push()
db.create_all()
self._create_fixtures()
self._create_csrf_token()

def tearDown(self):
super(MyAppTestCase, self).tearDown()
db.drop_all()
self.app_context.pop()

def _post(self, route, data=None, content_type=None, follow_redirects=True, headers=None):
content_type = content_type or 'application/x-www-form-urlencoded'
return self.client.post(route, data=data, follow_redirects=follow_redirects, content_type=content_type, headers=headers)

def _login(self, email=None, password=None):
email = email or self.user.email
password = password or 'password'
data = {
'email': email,
'password': password,
'remember': 'y'
}
return self._post('/login', data=data)


class MyFrontendTestCase(MyAppTestCase):

def _create_app(self):
return create_app(settings)

def setUp(self):
super(MyFrontendTestCase, self).setUp()
self._login()

我正在终端中使用 nosetests 运行我的测试,如下所示:source my_env/bin/activate && nosetests --exe

像这样的基本测试失败了:

class CoolTestCase(MyFrontendTestCase):

def test_logged_in(self):
r = self._login()
self.assertIn('MyAppName', r.data)

def test_authenticated_access(self):
r = self.get('/myroute/')
self.assertIn('MyAppName', r.data)

从输出中,我看到 r.data 只是登录页面的 HTML,没有错误(例如,错误的用户名或密码)或警告(“请登录以访问此页面").

我在 setUp 过程中登录,所以 test_authenticated_access 应该 让我访问 /myroute/ 或将我重定向到带有闪烁消息“请登录以访问此页面”的登录页面。但它没有。

我不知道哪里出了问题。我的测试基于我在 Flask 文档和 this app boilerplate 中找到的那些

最佳答案

经过一个星期的自杀,我终于想通了......

有几个问题:

  1. Flask-Security 和 Flask-SSLify 之间存在一些冲突。虽然自动 https 重定向在实际的 Web 服务器上运行良好,但在测试中它会阻止完全登录。我通过制作伪造的测试路线并尝试 POST 随机数据来解决这个问题。测试客户端中的 POST 没有工作。要解决此问题,我必须将测试配置 DEBUG 更改为 True。请注意,这不是一个很好的解决方法,因为不会编译 CSS 和 JS 等内容,并且其他应用行为可能会有所不同...

  2. Flask-Security 不适用于 factory_boy(或者我没有正确使用 factory_boy?)。我试图通过 factory_boy 创建用户和角色,因为我看到了一些示例应用教程用过它。我修复了上述问题后,它一直告诉我用户不存在。我最终从 Flask-Security 自己的测试中窃取了代码,用于创建具有不同角色的假用户。

有问题的代码:

session = db.create_scoped_session()

class RoleFactory(SQLAlchemyModelFactory):
FACTORY_FOR = Role
FACTORY_SESSION = session

id = Sequence(int)
name = 'admin'
description = 'Administrator'


class EmployeeFactory(SQLAlchemyModelFactory):
FACTORY_FOR = Employee
FACTORY_SESSION = session

id = Sequence(int)
email = Sequence(lambda n: 'user{0}@app.com'.format(n))
password = LazyAttribute(lambda a: encrypt_password('password'))
username = Sequence(lambda n: 'user{0}'.format(n))
#last_login_at = datetime.utcnow()
#current_login_at = datetime.utcnow()
last_login_ip = '127.0.0.1'
current_login_ip = '127.0.0.1'
login_count = 1
roles = LazyAttribute(lambda _: [RoleFactory()])
active = True

这是我的测试用例:

class MyAppTestCase(FlaskTestCaseMixin, MyTestCase):

def _create_app(self):
raise NotImplementedError

def _create_fixtures(self):
#self.user = EmployeeFactory()
populate_data(1)

def setUp(self):
super(MyAppTestCase, self).setUp()
self.app = self._create_app()
self.client = self.app.test_client()
self.app_context = self.app.app_context()
self.app_context.push()
db.create_all()
self._create_fixtures()
self._create_csrf_token()

def tearDown(self):
super(MyAppTestCase, self).tearDown()
db.drop_all()
self.app_context.pop()

def _post(self, route, data=None, content_type=None, follow_redirects=True, headers=None):
content_type = content_type or 'application/x-www-form-urlencoded'
return self.client.post(route, data=data, follow_redirects=follow_redirects, content_type=content_type, headers=headers)

def _login(self, email=None, password=None):
email = email or 'matt@lp.com' #self.user.email
password = password or 'password'
data = {
'email': email,
'password': password,
'remember': 'y'
}
return self._post('/login', data=data)

关于python - (unittest) 测试 Flask-Security : Cannot get past login page,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23117629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com