我从 webtest 文档中了解到:
The best way to simulate authentication is if your application looks in environ['REMOTE_USER'] to see if someone is authenticated. Then you can simply set that value, like:
app.get('/secret', extra_environ=dict(REMOTE_USER='bob'))
我正在尝试做同样的事情,但在 Google App 引擎环境中。我想模拟一个登录用户和一个管理员用户。
如果可能,我必须在 extra_environ 中设置哪些字典值才能完成此操作?
最佳答案
设置用户:
os.environ['USER_EMAIL'] = 'info@example.com'
设置管理员:
os.environ['USER_IS_ADMIN'] = '1'
这就是我的整个测试的样子。我的示例使用 webtest、nose、nosegae 和 gaetestbed。
class TestingRoutes(WebTestCase, unittest.TestCase):
APPLICATION = application()
def tearDown(self):
os.environ['USER_EMAIL'] = ''
os.environ['USER_IS_ADMIN'] = ''
#AdminIndex .....
def test_adminindex_no_user(self):
#No user: redirect to login form
response = app.get( url_map['adminindex'] )
self.assertRedirects(response)
def test_adminindex_user(self):
os.environ['USER_EMAIL'] = 'info@example.com'
response = app.get( url_map['adminindex'] )
self.assertForbidden(response)
def test_adminindex_admin(self):
os.environ['USER_EMAIL'] = 'info@example.com'
os.environ['USER_IS_ADMIN'] = '1'
response = app.get( url_map['adminindex'] )
self.assertOK(response)
希望对您有所帮助。
关于python - 谷歌应用引擎 : Webtest simulating logged in user and administrator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3587022/