Flask OpenID单元测试
我正在尝试为我的烧瓶应用程序编写OpenID的单元测试,但是在调用
I'm trying to write a unit test for my flask app for OpenID but upon calling
oid.try_login(<oid provider>, <params>)
我得到一个错误:
RuntimeError:
<class 'flask.testing.FlaskClient'>
不支持重定向到外部目标
RuntimeError:
<class 'flask.testing.FlaskClient'>
does not support redirect to external targets
因此,像每个优秀的SO用户一样,我环顾四周寻求一些解决方案:
So, like every good SO user, I looked around for some solutions:
- 使用NoExtRef烧瓶扩展名伪装oid提供程序.我不确定在应用程序级别是否可行,因为我认为flask-openid与oid url混为一谈(当我尝试使用它时,它只是将我重定向到了原始页面).但这似乎很丑陋,因为我严格针对单元测试进行代码更改.
- 创建自己的oid服务器,但这仍然可能是外部重定向(一旦我感到绝望,我将在稍后尝试).
我想另一种选择是忽略编写用于登录的单元测试,而只是使用很棒的Flask测试框架在Flask.g中设置用户.但我宁愿保留登录单元测试.
I guess another alternative is to ignore writing unit tests for login and just set the user in Flask.g using the awesome Flask test framework. But I'd prefer to keep the login unit tests.
还有另一种方法-猴子修补open-id扩展的try_login
方法:
There is an alternative - monkey-patch the open-id extension's try_login
method:
class LoginTestMonkeyPatch(object):
def __init__(self, oid=None, default_response=None):
self.response = default_response
if oid is not None:
self.init(oid)
def init(self, oid):
oid.try_login = self.try_login
def try_login(self, *args, **kwargs):
# Do whatever you want to do here