WebTest:使用装饰器进行测试 +数据存储调用

发布于 2024-09-16 07:57:04 字数 1211 浏览 4 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坏尐絯 2024-09-23 07:57:04

您应该在测试期间创建一个配置文件,以供装饰器使用:

def user_ok(self):
    key_name = '[email protected]'
    new_user = Profile(key_name=key_name)
    new_user.put()

    os.environ['USER_EMAIL'] = key_name
    os.environ['USER_ID'] = key_name
    os.environ['USER_IS_ADMIN'] = ''
    response = self.get( '/appindex' )
    self.assertOK(response)

    # Now let's reset it to check that the user will be redirected.
    new_user.delete()
    response = self.get( '/appindex' )
    self.assertEqual(response.headers['Location'], 'http://localhost/newuser')

You should create a profile during the test, to be used by the decorator:

def user_ok(self):
    key_name = '[email protected]'
    new_user = Profile(key_name=key_name)
    new_user.put()

    os.environ['USER_EMAIL'] = key_name
    os.environ['USER_ID'] = key_name
    os.environ['USER_IS_ADMIN'] = ''
    response = self.get( '/appindex' )
    self.assertOK(response)

    # Now let's reset it to check that the user will be redirected.
    new_user.delete()
    response = self.get( '/appindex' )
    self.assertEqual(response.headers['Location'], 'http://localhost/newuser')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文