diff --git a/tests/admin_docs/test_middleware.py b/tests/admin_docs/test_middleware.py index ab53716481..7c89dce929 100644 --- a/tests/admin_docs/test_middleware.py +++ b/tests/admin_docs/test_middleware.py @@ -1,4 +1,5 @@ from django.contrib.auth.models import User +from django.test import override_settings from .tests import AdminDocsTestCase, TestDataMixin @@ -45,3 +46,13 @@ class XViewMiddlewareTest(TestDataMixin, AdminDocsTestCase): self.client.force_login(self.superuser) response = self.client.head('/xview/callable_object/') self.assertEqual(response['X-View'], 'admin_docs.views.XViewCallableObject') + + @override_settings(MIDDLEWARE=[]) + def test_no_auth_middleware(self): + msg = ( + "The XView middleware requires authentication middleware to be " + "installed. Edit your MIDDLEWARE setting to insert " + "'django.contrib.auth.middleware.AuthenticationMiddleware'." + ) + with self.assertRaisesMessage(AssertionError, msg): + self.client.head('/xview/func/') diff --git a/tests/auth_tests/test_middleware.py b/tests/auth_tests/test_middleware.py index 635c43beb0..fb50854066 100644 --- a/tests/auth_tests/test_middleware.py +++ b/tests/auth_tests/test_middleware.py @@ -27,3 +27,13 @@ class TestAuthenticationMiddleware(TestCase): self.assertTrue(self.request.user.is_anonymous) # session should be flushed self.assertIsNone(self.request.session.session_key) + + def test_no_session(self): + msg = ( + "The Django authentication middleware requires session middleware " + "to be installed. Edit your MIDDLEWARE setting to insert " + "'django.contrib.sessions.middleware.SessionMiddleware' before " + "'django.contrib.auth.middleware.AuthenticationMiddleware'." + ) + with self.assertRaisesMessage(AssertionError, msg): + self.middleware(HttpRequest()) diff --git a/tests/messages_tests/test_session.py b/tests/messages_tests/test_session.py index e06ba656fa..16f893f08b 100644 --- a/tests/messages_tests/test_session.py +++ b/tests/messages_tests/test_session.py @@ -1,6 +1,7 @@ from django.contrib.messages import constants from django.contrib.messages.storage.base import Message from django.contrib.messages.storage.session import SessionStorage +from django.http import HttpRequest from django.test import TestCase from django.utils.safestring import SafeData, mark_safe @@ -34,6 +35,15 @@ class SessionTests(BaseTests, TestCase): def stored_messages_count(self, storage, response): return stored_session_messages_count(storage) + def test_no_session(self): + msg = ( + 'The session-based temporary message storage requires session ' + 'middleware to be installed, and come before the message ' + 'middleware in the MIDDLEWARE list.' + ) + with self.assertRaisesMessage(AssertionError, msg): + self.storage_class(HttpRequest()) + def test_get(self): storage = self.storage_class(self.get_request()) example_messages = ['test', 'me']