[英]Flask-Login package is broken by importing Google App Engine's testbed
When using Google App Engine 1.6.4 testbed with Flask 0.8, and in particular the Flask-Login 0.1 package ( source ), I encounter a strange problem with command-line unit testing.当使用带有Flask 0.8 的 Google App Engine 1.6.4 测试台,特别是Flask-Login 0.1 package ( source ) 时,我遇到了一个奇怪的命令行单元测试问题。
Below is an example that demonstrates the problem.下面是一个演示问题的示例。 Note the commented line (
from google.appengine.ext import testbed
).请注意注释行 (
from google.appengine.ext import testbed
)。 When this line is commented, the tests work as expected.评论此行时,测试将按预期工作。
When this line is uncommented, the @login_required
decorator for Flask-Login
stops recognizing the logged-in user ie current_user.is_authenticated()
returns False.当取消注释该行时,
Flask-Login
的@login_required
装饰器将停止识别登录用户,即current_user.is_authenticated()
返回 False。 It would seem the culprit is importing testbed
.罪魁祸首似乎是进口
testbed
。
#!/usr/bin/env python2.7
import unittest
import sys
from flask import Flask, current_app, url_for
from flaskext import login
sys.path.append('/usr/local/google_appengine')
# Go ahead and uncomment this:
# from google.appengine.ext import testbed
app = Flask('test')
app.secret_key = 'abc'
login_manager = login.LoginManager()
login_manager.setup_app(app)
login_manager.login_view = 'index'
class User(login.UserMixin):
def get_id(self):
return "1"
@login_manager.user_loader
def load_user(user_id):
return User()
@app.route('/')
@login.login_required
def index():
pass
@login_manager.unauthorized_handler
def unauthorized():
raise Exception("Unauthorized.")
class MyTest(unittest.TestCase):
def setUp(self):
self.app = app
self.client = app.test_client()
def test_user(self):
with self.app.test_request_context():
logged_in = login.login_user(User())
r = self.client.get('/')
if __name__ == '__main__':
unittest.main()
The specific exception is:具体异常是:
ERROR:test:Exception on / [GET]
Traceback (most recent call last):
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Flask-0.8-py2.7.egg/flask/app.py", line 1504, in wsgi_app
response = self.full_dispatch_request()
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Flask-0.8-py2.7.egg/flask/app.py", line 1264, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Flask-0.8-py2.7.egg/flask/app.py", line 1262, in full_dispatch_request
rv = self.dispatch_request()
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Flask-0.8-py2.7.egg/flask/app.py", line 1248, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Flask_Login-0.1-py2.7.egg/flaskext/login.py", line 479, in decorated_view
return current_app.login_manager.unauthorized()
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Flask_Login-0.1-py2.7.egg/flaskext/login.py", line 250, in unauthorized
return self.unauthorized_callback()
File "./test.py", line 34, in unauthorized
raise Exception("Unauthorized.")
Exception: Unauthorized.
The behaviour I would expect is that importing (and using) testbed
would have no effect on the Flask context stack and, by extension, Flask-Login would continue to work in the unit testing environment even though testbed
has been imported.我期望的行为是导入(和使用)
testbed
对 Flask 上下文堆栈没有影响,并且通过扩展,Flask-Login 将继续在单元测试环境中工作,即使已导入testbed
。
I have stared at this for a bit, to no avail, and would be grateful for any insight and suggestions for potential solutions to this problem.我已经盯着这个看了一会儿,但无济于事,如果您对此问题的潜在解决方案有任何见解和建议,我将不胜感激。
Thank you for reading.感谢您的阅读。
The testbed uses separate stubs for many of the GAE services, including the datastore and user service.测试平台为许多 GAE 服务使用单独的存根,包括数据存储和用户服务。
I'm not familiar with flask, but if the login requires a user to exist in the database, it'll fail since the testbed uses a separate database.我不熟悉 flask,但如果登录要求用户存在于数据库中,它将失败,因为测试台使用单独的数据库。 You'll have to load user data into the testbed database first.
您必须先将用户数据加载到测试台数据库中。
Also if you include the testbed, there's some initialization calls you have to make to set up the stubs before you can use it.此外,如果您包括测试台,则在使用它之前必须进行一些初始化调用以设置存根。 https://developers.google.com/appengine/docs/python/tools/localunittesting
https://developers.google.com/appengine/docs/python/tools/localunittesting
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.