diff --git a/django/test/client.py b/django/test/client.py index 42b325bd14..8349a902fa 100644 --- a/django/test/client.py +++ b/django/test/client.py @@ -9,7 +9,6 @@ from copy import copy from importlib import import_module from io import BytesIO -from django.apps import apps from django.conf import settings from django.core.handlers.base import BaseHandler from django.core.handlers.wsgi import ISO_8859_1, UTF_8, WSGIRequest @@ -419,17 +418,15 @@ class Client(RequestFactory): """ Obtains the current session variables. """ - if apps.is_installed('django.contrib.sessions'): - engine = import_module(settings.SESSION_ENGINE) - cookie = self.cookies.get(settings.SESSION_COOKIE_NAME) - if cookie: - return engine.SessionStore(cookie.value) - else: - s = engine.SessionStore() - s.save() - self.cookies[settings.SESSION_COOKIE_NAME] = s.session_key - return s - return {} + engine = import_module(settings.SESSION_ENGINE) + cookie = self.cookies.get(settings.SESSION_COOKIE_NAME) + if cookie: + return engine.SessionStore(cookie.value) + + session = engine.SessionStore() + session.save() + self.cookies[settings.SESSION_COOKIE_NAME] = session.session_key + return session session = property(_session) def request(self, **request): @@ -594,12 +591,11 @@ class Client(RequestFactory): Sets the Factory to appear as if it has successfully logged into a site. Returns True if login is possible; False if the provided credentials - are incorrect, or the user is inactive, or if the sessions framework is - not available. + are incorrect. """ from django.contrib.auth import authenticate user = authenticate(**credentials) - if user and apps.is_installed('django.contrib.sessions'): + if user: self._login(user) return True else: diff --git a/docs/releases/1.10.txt b/docs/releases/1.10.txt index 1ad08f10c2..fc155735dc 100644 --- a/docs/releases/1.10.txt +++ b/docs/releases/1.10.txt @@ -417,6 +417,9 @@ Tests * Added the :setting:`DATABASES['TEST']['MIGRATE'] ` option to allow disabling of migrations during test database creation. +* You can now login and use sessions with the test client even if + :mod:`django.contrib.sessions` is not in :setting:`INSTALLED_APPS`. + URLs ~~~~ diff --git a/tests/test_client/tests.py b/tests/test_client/tests.py index 43f6d84a65..5c3735601f 100644 --- a/tests/test_client/tests.py +++ b/tests/test_client/tests.py @@ -348,6 +348,13 @@ class ClientTest(TestCase): self.assertEqual(response.status_code, 200) self.assertEqual(response.context['user'].username, 'testclient') + @override_settings( + INSTALLED_APPS=['django.contrib.auth'], + SESSION_ENGINE='django.contrib.sessions.backends.file', + ) + def test_view_with_login_when_sessions_app_is_not_installed(self): + self.test_view_with_login() + def test_view_with_force_login(self): "Request a page that is protected with @login_required" # Get the page without logging in. Should result in 302. @@ -590,6 +597,21 @@ class ClientTest(TestCase): # Check that the session was modified self.assertEqual(self.client.session['tobacconist'], 'hovercraft') + @override_settings( + INSTALLED_APPS=[], + SESSION_ENGINE='django.contrib.sessions.backends.file', + ) + def test_sessions_app_is_not_installed(self): + self.test_session_modifying_view() + + @override_settings( + INSTALLED_APPS=[], + SESSION_ENGINE='django.contrib.sessions.backends.nonexistent', + ) + def test_session_engine_is_invalid(self): + with self.assertRaisesMessage(ImportError, 'nonexistent'): + self.test_session_modifying_view() + def test_view_with_exception(self): "Request a page that is known to throw an error" with self.assertRaises(KeyError):