您如何模拟 App Engine 中的用户服务? [英] How do you mock the User service in App Engine?

查看:17
本文介绍了您如何模拟 App Engine 中的用户服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Google App Engine testbed 框架来编写带有模拟对象的测试用例.这在此处进行了记录.我使用模拟数据库 (Testbed.init_datastore_v3_stub) 使我的数据存储测试运行良好,这让我的测试用例可以在快速、全新的数据库上运行,该数据库为每个测试用例重新初始化.现在我想测试依赖于当前用户的功能.

I am using the Google App Engine testbed framework to write test cases with mock objects. This is documented here. I've got my datastore tests working nicely using the mock database (Testbed.init_datastore_v3_stub), and this lets my test cases run over a fast, fresh database which is re-initialised for each test case. Now I want to test functionality that depends on the current user.

还有另一个名为 Testbed.init_user_stub 的测试平台服务,我可以激活它以获得假"用户服务.不幸的是,这似乎没有任何文档.我正在像这样激活和使用它:

There is another testbed service called Testbed.init_user_stub, which I can activate to get the "fake" user service. Unfortunately, there doesn't seem to be any documentation for this one. I am activating and using it like this:

import unittest
from google.appengine.ext import testbed
from google.appengine.api import users

class MyTest(unittest.TestCase):
    def setUp(self):
        self.testbed = testbed.Testbed()
        self.testbed.activate()
        self.testbed.init_user_stub()

    def testUser(self):
        u = users.get_current_user()
        self.assertNotEqual(u, None)

问题是我还没有找到任何方法来告诉假"用户服务对假"用户进行身份验证.所以运行那个测试,我(可以预见)得到

The problem is that I haven't found any way to tell the "fake" user service to authenticate a "fake" user. So running that test, I (predictably) get

AssertionError: None == None

意思是假用户服务告诉我的应用当前用户没有登录.我怎样才能告诉假用户服务假装用户已经登录?理想情况下,我希望能够指定假用户的昵称、电子邮件、user_id 以及他们是否是管理员.这似乎是一件很常见的事情(因为您需要测试应用程序在以下情况下的行为:a)没有人登录,b)用户已登录,c)管理员已登录),但是谷歌搜索init_user_stub"几乎什么都不返回.

meaning the fake user service is telling my app that the current user is not logged in. How can I tell the fake user service to pretend that a user is logged in? Ideally, I'd like to be able to specify the fake user's nickname, email, user_id and whether or not they are an admin. It seems like this would be quite a common thing to want (since you need to test how the app behaves when a) nobody is logged in, b) a user is logged in, and c) an admin is logged in), but googling "init_user_stub" returns almost nothing.

注意:如果要测试上面的程序,需要在最上面加上这个:

Note: If you want to test the above program, you need to add this to the top:

import sys
sys.path.append('/PATH/TO/APPENGINE/SDK')
import dev_appserver
dev_appserver.fix_sys_path()

然后到底部:

if __name__ == '__main__':
    unittest.main()

推荐答案

好吧,我认为没有官方的方法可以做到,但是我一直在阅读源代码,并且找到了一种hack"的方法到目前为止运行良好.(通常我会担心使用未记录的行为,但它是一个测试套件,因此只有在开发服务器上运行时才重要.)

Well I don't think there is an official way to do it, but I have been reading the source code and I found a "hack" way to do it that is working well so far. (Normally I'd be worried about using undocumented behaviour, but it's a test suite so it only matters if it works on the dev server.)

开发服务器根据三个环境变量计算出当前登录的用户:

The dev server figures out the currently logged-in user based on three environment variables:

  • USER_EMAIL:用户的电子邮件地址,用户的昵称.
  • USER_ID:用户的唯一 Google ID(字符串).
  • USER_IS_ADMIN:0"如果用户是非管理员,1"如果用户是管理员.

您可以像设置任何其他环境变量一样使用 os.environ 来设置它们,它们会立即生效(显然这不适用于生产服务器).但是您可以将它们与测试床的 user_stub 一起使用,并且在您停用测试床时它们将被重置(您应该在 tearDown 上执行此操作,以便为每个测试用例获得一个全新的环境).

You can use os.environ to set these as you would any other environment variable, and they take immediate effect (obviously this won't work on the production server). But you can use them with testbed's user_stub and they will be reset when you deactivate the testbed (which you should do on tearDown, so you get a fresh environment for each test case).

由于设置环境变量有点笨拙,我写了一些包装函数来打包:

Since setting environment variables is a bit unwieldy, I wrote some wrapper functions to package them up:

import os

def setCurrentUser(email, user_id, is_admin=False):
    os.environ['USER_EMAIL'] = email or ''
    os.environ['USER_ID'] = user_id or ''
    os.environ['USER_IS_ADMIN'] = '1' if is_admin else '0'

def logoutCurrentUser():
    setCurrentUser(None, None)

这篇关于您如何模拟 App Engine 中的用户服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆