From 84c71148e37235396436fa4b03de137766a15c02 Mon Sep 17 00:00:00 2001 From: Ramiro Morales Date: Sun, 12 Jun 2011 16:33:40 +0000 Subject: [PATCH] Fixed #7836 -- Modified strategy used by the test client to check for session functionality so it is useful in environments where the DB-based session backend isn't being used. Thanks trevor for the report. git-svn-id: http://code.djangoproject.com/svn/django/trunk@16386 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/test/client.py | 4 ++-- .../regressiontests/test_client_regress/models.py | 15 ++++++++++++++- .../test_client_regress/session.py | 4 ++-- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/django/test/client.py b/django/test/client.py index 5cbc0ca257..4335f21224 100644 --- a/django/test/client.py +++ b/django/test/client.py @@ -356,7 +356,7 @@ class Client(RequestFactory): """ Obtains the current session variables. """ - if 'django.contrib.sessions' in settings.INSTALLED_APPS: + if 'django.contrib.sessions.middleware.SessionMiddleware' in settings.MIDDLEWARE_CLASSES: engine = import_module(settings.SESSION_ENGINE) cookie = self.cookies.get(settings.SESSION_COOKIE_NAME, None) if cookie: @@ -504,7 +504,7 @@ class Client(RequestFactory): """ user = authenticate(**credentials) if user and user.is_active \ - and 'django.contrib.sessions' in settings.INSTALLED_APPS: + and 'django.contrib.sessions.middleware.SessionMiddleware' in settings.MIDDLEWARE_CLASSES: engine = import_module(settings.SESSION_ENGINE) # Create a fake request to store login details. diff --git a/tests/regressiontests/test_client_regress/models.py b/tests/regressiontests/test_client_regress/models.py index 8b47fe2e5b..57ed618ea3 100644 --- a/tests/regressiontests/test_client_regress/models.py +++ b/tests/regressiontests/test_client_regress/models.py @@ -13,7 +13,7 @@ from django.template import (TemplateDoesNotExist, TemplateSyntaxError, import django.template.context from django.test import Client, TestCase from django.test.client import encode_file, RequestFactory -from django.test.utils import ContextList +from django.test.utils import ContextList, override_settings class AssertContainsTests(TestCase): @@ -518,6 +518,19 @@ class SessionEngineTests(TestCase): self.assertEqual(response.status_code, 200) self.assertEqual(response.context['user'].username, 'testclient') + +# Remove the 'session' contrib app from INSTALLED_APPS +@override_settings(INSTALLED_APPS=tuple(filter(lambda a: a!='django.contrib.sessions', settings.INSTALLED_APPS))) +class NoSessionsAppInstalled(SessionEngineTests): + """#7836 - Test client can exercise sessions even when 'django.contrib.sessions' isn't installed.""" + + def test_session(self): + # This request sets a session variable. + response = self.client.get('/test_client_regress/set_session/') + self.assertEqual(response.status_code, 200) + self.assertEqual(self.client.session['session_var'], 'YES') + + class URLEscapingTests(TestCase): def test_simple_argument_get(self): "Get a view that has a simple string argument" diff --git a/tests/regressiontests/test_client_regress/session.py b/tests/regressiontests/test_client_regress/session.py index 74ae3b6835..070584d32d 100644 --- a/tests/regressiontests/test_client_regress/session.py +++ b/tests/regressiontests/test_client_regress/session.py @@ -2,7 +2,7 @@ from django.contrib.sessions.backends.base import SessionBase class SessionStore(SessionBase): """ - A simple cookie-based session storage implemenation. + A simple cookie-based session storage implementation. The session key is actually the session data, pickled and encoded. This means that saving the session will change the session key. @@ -27,4 +27,4 @@ class SessionStore(SessionBase): return self.decode(self.session_key) except: self.modified = True - return {} \ No newline at end of file + return {}