1. Mikhail Korobov
  2. django-webtest

Commits

Gregor Müllegger  committed ad79436

Disable CSRF protection while running webtest requests.

  • Participants
  • Parent commits d4ce9bf
  • Branches default

Comments (0)

Files changed (2)

File django_webtest/__init__.py

View file
                 environ['REMOTE_USER'] = user
         return environ
 
-    def do_request(self, *args, **kwargs):
+    def do_request(self, req, status, expect_errors):
         # Curry a data dictionary into an instance of the template renderer
         # callback function.
         data = {}
         on_template_render = curry(store_rendered_templates, data)
         template_rendered.connect(on_template_render)
 
-        response = super(DjangoTestApp, self).do_request(*args, **kwargs)
+        response = super(DjangoTestApp, self).do_request(req, status, expect_errors)
 
         # Add any rendered template detail to the response.
         # If there was only one template rendered (the most likely case),
         self._MIDDLEWARE_CLASSES = settings.MIDDLEWARE_CLASSES[:]
         self._AUTHENTICATION_BACKENDS = settings.AUTHENTICATION_BACKENDS[:]
 
+        disable_csrf_middleware = 'django_webtest.middleware.DisableCSRFCheckMiddleware'
+        if not disable_csrf_middleware in settings.MIDDLEWARE_CLASSES:
+            settings.MIDDLEWARE_CLASSES = (disable_csrf_middleware,) + settings.MIDDLEWARE_CLASSES
+
         remote_user_middleware = 'django.contrib.auth.middleware.RemoteUserMiddleware'
         if not remote_user_middleware in settings.MIDDLEWARE_CLASSES:
             settings.MIDDLEWARE_CLASSES += (remote_user_middleware,)

File django_webtest/middleware.py

View file
+# -*- coding: utf-8 -*-
+
+
+class DisableCSRFCheckMiddleware(object):
+    def process_request(self, request):
+        request._dont_enforce_csrf_checks = True