Saturday, October 12, 2013

Re: How to add CSRF to context when using test client???

I'm getting a 401 (Which is technically an Authorization error) after doing this.

c = Client(enforce_csrf_checks=True)

response = c.get(a_url_that_requires_login)

csrfmiddlewaretoken =  '%s' %response.context['csrf_token']

response = c.post(url, data=json.dumps(body), content_type='application/json', X_CSRFToken = csrfmiddlewaretoken)


I also tried with/without updating the session with the token

c.session.update({'csrftoken': csrfmiddlewaretoken})


Any idea why there might be an authorization error?

My permissions are loaded fine from the fixtures.

Thanks,

Pratik

On Thursday, August 25, 2011 9:24:38 AM UTC+5:30, Matteius wrote:
Thanks for both suggestions, I recently tried this approach and
reached success when I ran my tests.  So I think this is the correct
approach and I'll include this CSRF style testing with my test suite
now.

-Matteius

On Jul 7, 6:02 am, Craig Blaszczyk <masterja...@gmail.com> wrote:
> csrf_token is a proxy object not a string.
>
> Try doing:
> csrf_token = '%s' % response.context['csrf_token']

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e7d47ed4-fbe1-4206-b4ae-dfd9500650f7%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate