本文介绍了Google App Engine在使用测试平台时设置用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用GAE 服务,并在运行 users.get_current_user()我得到 None ie

 >>> import sys 
>>> sys.path.append(/ usr / local / google_appengine)#for Mac OS X
>>> from google.appengine.api导入用户
>>> from google.appengine.ext import testbed
>>> testbed = testbed.Testbed()
>>> testbed.activate()
>>> testbed.init_user_stub()
>>> users.get_current_user()== None
True

这是预期的结果。但是,我想在运行一些单元测试时登录一个假用户。



如果可能的话(我预计它会是这样),那么怎么能在命令行中运行 testbed 时使用户登录,以便 get_current_user()返回实际用户?



感谢您阅读。 解决方案

要模拟管理员用户的登录,你可以在测试函数中的任何地方调用:

  self.testbed.setup_env(
USER_EMAIL ='test @ example .com',
USER_ID ='123',
USER_IS_ADMIN ='1',
覆盖=真)

请注意,您需要使用 overwrite = True 参数!


I'm using the GAE testbed service and when I run users.get_current_user() I get None i.e.

>>> import sys
>>> sys.path.append("/usr/local/google_appengine") # for Mac OS X
>>> from google.appengine.api import users
>>> from google.appengine.ext import testbed
>>> testbed = testbed.Testbed()
>>> testbed.activate()
>>> testbed.init_user_stub()
>>> users.get_current_user() == None
True

This is the expected result. However, I'd like to log in a fake user when running some of my unit tests.

If it's possible (and I expect it would be), how can one log a user in when running testbed from the command line so that get_current_user() returns an actual user?

Thanks for reading.

解决方案

To simulate a sign in of an admin user, you can call anywhere in your test function:

self.testbed.setup_env(
    USER_EMAIL = '[email protected]',
    USER_ID = '123',
    USER_IS_ADMIN = '1',
    overwrite = True)

Note that you need to use the overwrite=True parameter!

这篇关于Google App Engine在使用测试平台时设置用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 06:05