Google App Engine set up a user when using testbed

与世无争的帅哥 提交于 2019-11-29 05:57:42

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

self.testbed.setup_env(
    USER_EMAIL = 'test@example.com',
    USER_ID = '123',
    USER_IS_ADMIN = '1',
    overwrite = True)

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

After some experimentation the following worked for me:

>>> os.environ['USER_EMAIL'] = 'a@b.c'
>>> os.environ['USER_ID'] = '123'
>>> users.get_current_user()
users.User(email='a@b.c',_user_id='123')

I hope this is a good solution, and this answer helps the next person to run into this issue.

EDIT: Related variables

To avoid /google/appengine/api/users.py:115 - AssertionError: assert _auth_domain

>>> os.environ['AUTH_DOMAIN'] = 'testbed'

Per Elliot de Vries comment, for an administrative user:

>>> os.environ['USER_IS_ADMIN'] = '1'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!