mirror of https://github.com/django/django.git
Fixed #25699 -- Allowed using the test client if 'django.contrib.sessions' isn't in INSTALLED_APPS.
This commit is contained in:
parent
5faf745999
commit
21dd98a386
|
@ -9,7 +9,6 @@ from copy import copy
|
||||||
from importlib import import_module
|
from importlib import import_module
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
|
||||||
from django.apps import apps
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.handlers.base import BaseHandler
|
from django.core.handlers.base import BaseHandler
|
||||||
from django.core.handlers.wsgi import ISO_8859_1, UTF_8, WSGIRequest
|
from django.core.handlers.wsgi import ISO_8859_1, UTF_8, WSGIRequest
|
||||||
|
@ -419,17 +418,15 @@ class Client(RequestFactory):
|
||||||
"""
|
"""
|
||||||
Obtains the current session variables.
|
Obtains the current session variables.
|
||||||
"""
|
"""
|
||||||
if apps.is_installed('django.contrib.sessions'):
|
|
||||||
engine = import_module(settings.SESSION_ENGINE)
|
engine = import_module(settings.SESSION_ENGINE)
|
||||||
cookie = self.cookies.get(settings.SESSION_COOKIE_NAME)
|
cookie = self.cookies.get(settings.SESSION_COOKIE_NAME)
|
||||||
if cookie:
|
if cookie:
|
||||||
return engine.SessionStore(cookie.value)
|
return engine.SessionStore(cookie.value)
|
||||||
else:
|
|
||||||
s = engine.SessionStore()
|
session = engine.SessionStore()
|
||||||
s.save()
|
session.save()
|
||||||
self.cookies[settings.SESSION_COOKIE_NAME] = s.session_key
|
self.cookies[settings.SESSION_COOKIE_NAME] = session.session_key
|
||||||
return s
|
return session
|
||||||
return {}
|
|
||||||
session = property(_session)
|
session = property(_session)
|
||||||
|
|
||||||
def request(self, **request):
|
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.
|
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
|
Returns True if login is possible; False if the provided credentials
|
||||||
are incorrect, or the user is inactive, or if the sessions framework is
|
are incorrect.
|
||||||
not available.
|
|
||||||
"""
|
"""
|
||||||
from django.contrib.auth import authenticate
|
from django.contrib.auth import authenticate
|
||||||
user = authenticate(**credentials)
|
user = authenticate(**credentials)
|
||||||
if user and apps.is_installed('django.contrib.sessions'):
|
if user:
|
||||||
self._login(user)
|
self._login(user)
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -417,6 +417,9 @@ Tests
|
||||||
* Added the :setting:`DATABASES['TEST']['MIGRATE'] <TEST_MIGRATE>` option to
|
* Added the :setting:`DATABASES['TEST']['MIGRATE'] <TEST_MIGRATE>` option to
|
||||||
allow disabling of migrations during test database creation.
|
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
|
URLs
|
||||||
~~~~
|
~~~~
|
||||||
|
|
||||||
|
|
|
@ -348,6 +348,13 @@ class ClientTest(TestCase):
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
self.assertEqual(response.context['user'].username, 'testclient')
|
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):
|
def test_view_with_force_login(self):
|
||||||
"Request a page that is protected with @login_required"
|
"Request a page that is protected with @login_required"
|
||||||
# Get the page without logging in. Should result in 302.
|
# Get the page without logging in. Should result in 302.
|
||||||
|
@ -590,6 +597,21 @@ class ClientTest(TestCase):
|
||||||
# Check that the session was modified
|
# Check that the session was modified
|
||||||
self.assertEqual(self.client.session['tobacconist'], 'hovercraft')
|
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):
|
def test_view_with_exception(self):
|
||||||
"Request a page that is known to throw an error"
|
"Request a page that is known to throw an error"
|
||||||
with self.assertRaises(KeyError):
|
with self.assertRaises(KeyError):
|
||||||
|
|
Loading…
Reference in New Issue