Joe Crobak avatar Joe Crobak committed a8ce204

Add support for sessions to in WebTest

Comments (0)

Files changed (5)

 ``assertContains``, ``assertNotContains``, ``assertRedirects``) are
 also supported for WebTest responses.
+The session dictionary is available via ````, and has the
+content as django's native test client.
 Unlike django's native test client CSRF checks are not suppressed
 by default so missing CSRF tokens will cause test fails (and that's good).


 from django.test import TestCase
 from django.test.client import store_rendered_templates
 from django.utils.functional import curry
+from django.utils.importlib import import_module
 from webtest import TestApp
 from django_webtest.middleware import DjangoWsgiFix
         return super(DjangoTestApp, self).post(
                    url, params, headers, extra_environ, status,
                    upload_files, expect_errors, content_type)
+    @property
+    def session(self):
+        """
+        Obtains the current session variables.
+        """
+        if 'django.contrib.sessions' in settings.INSTALLED_APPS:
+            engine = import_module(settings.SESSION_ENGINE)
+            cookie = self.cookies.get(settings.SESSION_COOKIE_NAME, None)
+            if cookie:
+                return engine.SessionStore(cookie)
+        return {}
 class WebTest(TestCase):


         from django.conf import settings
         assert 'django_webtest.middleware.WebtestUserMiddleware' not in settings.MIDDLEWARE_CLASSES
         assert 'django_webtest.backends.WebtestUserBackend' not in settings.AUTHENTICATION_BACKENDS
+class TestSession(WebTest):
+    def test_session_not_set(self):
+        response ='/')
+        self.assertEqual(response.status_int, 200)
+        self.assertEquals({},
+    def test_sessions_disabled(self):
+        from django.conf import settings
+        apps = list(settings.INSTALLED_APPS)
+        apps.remove("django.contrib.sessions")
+        settings.INSTALLED_APPS= apps
+        response ='/')
+        self.assertEqual(response.status_int, 200)
+        self.assertEquals({},
+    def test_session_not_empty(self):
+        response ='set_session'))
+        self.assertEquals('foo',['test'])


         return HttpResponseRedirect('/')
     return direct_to_template(request, 'form.html', {'form': form})
+def set_session(request):
+    request.session['test'] = 'foo'
+    return HttpResponseRedirect('/')


     url(r'^template/(.*)$', simple_template_render, name='simple-template-test'),
     url(r'^check-password/$', 'testapp_tests.views.check_password', name='check_password'),
     url(r'^login/$', 'django.contrib.auth.views.login', name='auth_login'),
+    url(r'^set-session/$', 'testapp_tests.views.set_session', name='set_session')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.