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

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

问题描述

我使用Google App Engine testbed 框架来编写含有模拟对象的测试用例。这是记录在此处。我已经使用模拟数据库( Testbed.init_datastore_v3_stub )很好地处理了我的数据存储测试,这可以让我的测试用例运行在一个快速重新初始化的新数据库上为每个测试案例。现在我想测试依赖于当前用户的功能。



还有另一个名为 Testbed.init_user_stub ,我可以激活它以获取假用户服务。不幸的是,这个似乎没有任何文档。我正在激活并使用它:

  import unittest $ b $ from google.appengine.ext import testbed 
从google.appengine.api导入用户

class MyTest(unittest.TestCase):
def setUp(self):
self.testbed = testbed.Testbed()
self.testbed.activate()
self.testbed.init_user_stub()
$ b $ def testUser(self):
u = users.get_current_user()
self。 assertNotEqual(u,None)

问题在于我没有找到任何方法来告诉假用户服务来验证假用户。因此,运行该测试,我(可以预料)得到

  AssertionError:None == None 

这意味着假用户服务告诉我的应用程序当前用户没有登录。我如何告诉假用户服务假装用户已登录?理想情况下,我希望能够指定假用户的昵称,电子邮件,user_id以及他们是否是管理员。看起来这是很常见的事情(因为你需要测试应用程序在没有人登录时的行为),b)用户登录,和c)管理员登录),但是使用谷歌搜索init_user_stub几乎不返回任何内容。



注意:如果您想测试上述程序,您需要将其添加到顶端:

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

以及到底部:

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


解决方案

我不认为有这样的官方方式,但我一直在阅读源代码,并且发现了一种黑客方式做到目前为止效果很好。 (通常我会担心使用未记录的行为,但它是一个测试套件,所以它只在它在dev服务器上运行时才起作用。)



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


  • USER_EMAIL:用户的电子邮件地址用户的昵称。
  • USER_ID:用户唯一的Google ID(字符串)。

  • USER_IS_ADMIN:如果用户是非管理员,则为0 1,如果用户是管理员。



您可以使用 os.environ 像设置任何其他环境变量一样设置它们,并立即生效(显然这不适用于生产服务器)。但是你可以在测试平台的user_stub中使用它们,当你停用测试平台时(你应该对 tearDown 进行操作,这样你可以为每个测试用例获得全新的环境) 。

由于设置环境变量有点不方便,我写了一些包装函数来打包它们:

  import os 
$ b $ def def setCurrentUser(email,user_id,is_admin = False):
os.environ ['USER_EMAIL'] = email或''
os.environ ['USER_ID'] = user_id或''
os.environ ['USER_IS_ADMIN'] ='1'如果is_admin else'0'

def logoutCurrentUser():
setCurrentUser(None,None)


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.

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

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()

and this to the bottom:

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

解决方案

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: The user's email address, and the user's nickname.
  • USER_ID: The user's unique Google ID (string).
  • USER_IS_ADMIN: "0" if the user is non-admin, "1" if the user is an admin.

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天全站免登陆