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