Use:
from django.test import Client
csrf_client = Client(enforce_csrf_checks=True)
https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#testing
10.06.2011, 08:48, "Matteius" <matt...@gmail.com>:
> Greetings,
>
> I am writing unit tests for my application, and I want to leave CSRF
> checks enabled in testing. О©╫This means on my POST I am getting 403
> because I have not figured out how to add csrf to my context when
> using the test client. О©╫Please advise on how to most easily do this:
>
> О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫# Issue a POST request.
> О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫response = self.client.post('/student/open_enrollments/',
> {'course': 3})
> О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫# Check that the response is 200 OK.
> О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫self.assertEqual(response.status_code, 200)
> О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫# Verify template is the expected one.
> О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫self.assertTemplateUsed(response, 'student_portal/
> open_enrollments.html')
> О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫# Check that the rendered context contains now 0 Courses.
> О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫self.assertEqual(len(response.context['courses']), 0)
>
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.