代码之家  ›  专栏  ›  技术社区  ›  Pickels

googleappengine:Webtest模拟登录用户和管理员

  •  4
  • Pickels  · 技术社区  · 15 年前

    从webtest文档中,我了解到:

    最好的模拟方法 在environ['REMOTE\u USER']中查看 可以简单地设置该值,例如:

    app.get('/secret', extra_environ=dict(REMOTE_USER='bob'))
    

    我正在尝试做同样的事情,但在谷歌应用引擎环境。我想模拟一个登录用户和一个管理员用户。

    如果可能的话,我需要在额外的环境中设置哪些字典值来完成这个任务?

    1 回复  |  直到 15 年前
        1
  •  6
  •   Pickels    15 年前

    设置用户:

    os.environ['USER_EMAIL'] = 'info@example.com'
    

    设置管理员:

    os.environ['USER_IS_ADMIN'] = '1'
    

    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)