【问题标题】:How to test Pyramid security setup?如何测试 Pyramid 安全设置?
【发布时间】:2013-08-26 19:40:49
【问题描述】:

是否有推荐的方法来测试 Pyramid 应用程序中的安全设置?更具体地说,我正在使用路线和自定义路线工厂。使用细粒度的 ACL,安全设置被拆分到不同的位置:配置设置、工厂、@view_config 中的权限集以及视图内权限的事件显式检查。

关于单元和功能测试的页面 (http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/testing.html) 似乎没有指明一种方法来测试用户 A 是否只能查看和修改他被允许的数据。

【问题讨论】:

    标签: security testing pyramid


    【解决方案1】:

    这是功能测试。 Webtest 可以保留会话 cookie,以便您可以使用它以用户身份登录和访问各种页面。

    myapp = pyramid.paster.get_app('testing.ini')
    app = TestApp(myapp)
    resp = app.post('/login', params={'login': 'foo', 'password': 'seekrit'})
    # this may be a redirect in which case you may want to follow it
    
    resp = app.get('/protected/resource')
    assert resp.status_code == 200
    

    就仅测试应用的某些部分而言,您可以使用自定义的东西(或仅使用自定义 groupfinder)覆盖身份验证策略。

    def make_test_groupfinder(principals=None):
        def _groupfinder(u, r):
            return principals
        return _groupfinder
    

    然后您可以使用此功能来模拟各种主体。但是,如果您的应用程序还依赖于 authenticated_userid(request) 任何地方,这不会处理用户 ID。为此,您必须将身份验证策略替换为虚拟策略。

    class DummyAuthenticationPolicy(object):
        def __init__(self, userid, extra_principals=()):
            self.userid = userid
            self.extra_principals = extra_principals
    
        def authenticated_userid(self, request):
            return self.userid
    
        def effective_principals(self, request):
            principals = [Everyone]
            if self.userid:
                principals += [Authenticated]
                principals += list(self.extra_principals)
            return principals
    
        def remember(self, request, userid, **kw):
            return []
    
        def forget(self, request):
            return []
    

    【讨论】:

    • 我正在使用 mozilla 角色进行登录过程。我是否需要仅为测试创建一个虚假的登录视图?
    • 您需要一种方法来以经过身份验证的用户身份与您的应用程序对话。获得经过身份验证的用户的最简单方法是登录。还有很多其他方法,但它们基本上都涉及您弄乱您的应用程序如何确定登录用户(testing.ini 可以帮助您设置)。
    【解决方案2】:

    我认为此时问题和答案都可能已经过时了:在当前版本的 Pyramid 中,有一个 testing_securitypolicy 方法 (docs here) 可以轻松访问设置,如authenticated_userid、effective_principals、remember 和忘记之类的。

    如果需要在请求中设置authenticated_userid,这是一个使用示例。

    from pyramid.testing import (setUp, tearDown, DummyRequest)
    
    def test_some_view():
        config = setUp()
        config.testing_securitypolicy(userid='mock_user')  # Sets authenticated_userid
    
        dummy_request = DummyRequest()
        print(dummy_request.authenticated_userid)  # Prints 'mock_user'
    
        # Now ready to test something that uses request.authenticated_userid
        from mypyramidapp.views.secure import some_auth_view
        result = some_auth_view(dummy_request)
        expected = 'Hello mock_user!'
        assert result == expected
    
        # Finally, to avoid security changes leaking to other tests, use tearDown
        tearDown()  # Undo the effects of pyramid.testing.setUp()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-14
      • 2014-06-13
      • 2021-08-20
      • 1970-01-01
      • 1970-01-01
      • 2012-08-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多