4

Pyramid アプリケーションでセキュリティ設定をテストする推奨される方法はありますか? より具体的には、ルートとカスタム ルート ファクトリを使用しています。きめ細かい ACL を使用すると、セキュリティ設定がさまざまな場所に分割されます。構成設定、ファクトリ、@view_config で設定されたアクセス許可、およびビュー内のアクセス許可のイベントの明示的なチェックです。

単体テストと機能テストに関するページ ( http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/testing.html ) は、ユーザー A がデータの表示と変更しかできないかどうかをテストする方法を示していないようです。彼は許可されています。

4

2 に答える 2

5

これは機能テストです。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

アプリの特定の部分だけをテストする限り、認証ポリシーをカスタムでオーバーライドできます (またはカスタム グループファインダーを使用するだけです)。

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 []
于 2013-08-27T16:49:16.673 に答える