WebTest:使用装饰器进行测试 +数据存储调用
我有一个 Google App Engine 应用程序,我的请求 hadler 有一个进行身份验证的装饰器。通过 WebTest,我昨天发现了您如何可以设置登录用户和管理员。
今天我的身份验证装饰器变得有点复杂了。它还会检查用户是否在数据库中拥有个人资料,如果没有,他将被重定向到“新用户”页面。
def authenticated(method):
@functools.wraps(method)
def wrapper(self, *args, **kwargs):
user = users.get_current_user()
if not user:
self.redirect(users.create_login_url(self.request.uri))
return
profile = Profile.get_by_key_name(str(user.user_id))
if not profile:
self.redirect( '/newuser' )
return method(self, *args, **kwargs)
return wrapper
现在添加配置文件部分会破坏我的单元测试,该测试检查用户是否登录并获取状态代码 200(assertOK)。
def user_ok(self):
os.environ['USER_EMAIL'] = '[email protected]'
os.environ['USER_IS_ADMIN'] = ''
response = self.get( '/appindex' )
self.assertOK(response)
所以现在我需要能够以某种方式将配置文件功能注入装饰器中,以便我可以在测试中设置它。有人知道如何做到这一点吗?我一直在想办法,但我一直陷入困境。
I have a Google App Engine application and my request hadnler has a decorator that does authentication. With WebTest I found out yesterday how you can set a logged in user and administrator.
Now today my authentication decorator got a little more complex. It's also checking if a user has a profile in the database and if he doesn't he'll get redirected to the 'new user' page.
def authenticated(method):
@functools.wraps(method)
def wrapper(self, *args, **kwargs):
user = users.get_current_user()
if not user:
self.redirect(users.create_login_url(self.request.uri))
return
profile = Profile.get_by_key_name(str(user.user_id))
if not profile:
self.redirect( '/newuser' )
return method(self, *args, **kwargs)
return wrapper
Now adding the profile part breaks my unit test that checks if a user is logged in and gets a status code 200(assertOK).
def user_ok(self):
os.environ['USER_EMAIL'] = '[email protected]'
os.environ['USER_IS_ADMIN'] = ''
response = self.get( '/appindex' )
self.assertOK(response)
So now I need to be able to somehow inject the profile functionality into the decorator so I can set it in my tests. Does anybody got an idea how to do this I've been trying to think of a way but I keep getting stuck.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该在测试期间创建一个配置文件,以供装饰器使用:
You should create a profile during the test, to be used by the decorator: