python - 谷歌应用引擎 : Webtest simulating logged in user and administrator

标签 python google-app-engine

我从 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/

相关文章:

python - 如何在 map 方法中预处理和标记 TensorFlow CsvDataset?

python-3.x - 在 gcloud 上将基于 python 的命令行脚本作为 cronjob 运行

google-app-engine - Google App Engine 帐户可以使用多少 Memcache 内存?

python - 模块未找到错误 : No module named 'google.appengine'

PYTHON-数据存储中 TextProperty 的属性 "{field_name}"的值超过 1048487 个字节

java - 使用 HTTP 方法从 blobstore 检索 blob key

python - 为什么 python re 模块不能与 @ 一起工作?

python - 使用 for 循环创建和分配不同的变量

python - 重写类的 __contains__ 方法

python - 文件字段在 Django 中不起作用