Google App Engine:Webtest模拟登录用户和管理员 [英] Google App Engine: Webtest simulating logged in user and administrator

查看:120
本文介绍了Google App Engine:Webtest模拟登录用户和管理员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从webtest文档中我学到:


模拟
身份验证的最佳方式是,如果您的应用程序
在environ ['REMOTE_USER']查看
,如果有人被认证。然后你
可以简单地设置该值,例如:



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

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



如果可能的话,我必须在extra_environ中设置哪些字典值来完成此操作?

解决方案 pre $ 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(响应)

def test_adminindex_user(self):
os.environ ['USER_EMAIL'] ='info@example.com'
response = app.get(url_map ['adminindex'])
self.assertForbidden(回复)

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)

希望它有帮助。


from the webtest documentation I learn that:

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'))

I am trying to do the same thing but in a Google App engine environment. I would like to simulate a logged in user and a user that's an administrator.

If possible which dictionary values do I have to set in extra_environ to accomplish this?

解决方案

Set User:

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

Set Admin:

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

This is how my whole test looks like. My example uses webtest, nose, nosegae and 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)

Hope it helps.

这篇关于Google App Engine:Webtest模拟登录用户和管理员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆