On Wednesday, August 20, 2014 8:21:05 AM UTC+2, Wichert Akkerman wrote:
>
>
> On 19 Aug 2014, at 23:13, pyramidX <[email protected] <javascript:>>
> wrote:
>
> I'm using DummyRequest in my tests to test authentication, this is what I
> return from the view when login succeeds
>
> headers = remember(request, email)
> return HTTPFound(location=next, headers=headers)
>
>
>
> This is my test
>
> request = testing.DummyRequest(post={'usr': '[email protected]
> <javascript:>', 'pwd': 'mypassword'})
> view = views.login(request)
> print(view.headers)
>
>
> The returned value there is a response object.
>
> print(request.headers)
>
>
> The exact response here will vary a bit depending on your authentication
> policy. For auth_tkt you should see a couple of Set-Cookie headers that set
> the authentication cookie. If you use a session authentication policy you
> might not see anything here, or only a Set-Cookie header to set the session
> cookie.
>
> print(unauthenticated_userid(request))
> print(authenticated_userid(request))
>
>
> Those function only work on an incoming request, not on a response object.
>
>
Am I using it incorrectly? I'm passing it an instance of a request
(DummyRequest).
> How can I test that authentication has worked?
>
>
> That depends a bit on your testing style. The most reliable method is to
> write an integration test and do a second request and check the response to
> see if authentication succeeded. The pure unit-test approach is to insert a
> mock for remember() and check if the headers returned by remember() show up
> in the response headers.
>
For unit testing, using DummyRequest is not sufficient? In the view, I do
"headers = remember(request, email)" where request is a DummyRequest.
Shouldn't I be able to inspect the request object then and see the headers?
Maybe the problem is I've set up an auth policy in the project itself, but
not in the unit tests. I've set up AuthTkt in my project's __init__.py,
something like
authn_policy = AuthTktAuthenticationPolicy('----------', hashalg=
'sha512')
config.set_authentication_policy(authn_policy)
But nothing changes in the following print output when I comment out those
lines. When I do print(view.headers) where view is the response, I get
"ResponseHeaders([('Content-Type', 'text/html; charset=UTF-8'),
('Content-Length', '0'), ('Location', <built-in function next>)])". No sign
of the AuthTkt cookies.
To get my unit tests to use the config object should I use the "with
testConfig() as config:" approach or should I do something like 'import
myapp' so that __init__.py runs, which is where I set up the config object?
Is the latter what you mean by integration test, and the former a unit test?
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/pylons-discuss.
For more options, visit https://groups.google.com/d/optout.