Added tests for middlewares' checks.

This commit is contained in:
Sergey Fedoseev 2019-10-23 08:04:14 +02:00 committed by Mariusz Felisiak
parent 2847d2c760
commit d4e767911f
3 changed files with 31 additions and 0 deletions

View File

@ -1,4 +1,5 @@
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.test import override_settings
from .tests import AdminDocsTestCase, TestDataMixin from .tests import AdminDocsTestCase, TestDataMixin
@ -45,3 +46,13 @@ class XViewMiddlewareTest(TestDataMixin, AdminDocsTestCase):
self.client.force_login(self.superuser) self.client.force_login(self.superuser)
response = self.client.head('/xview/callable_object/') response = self.client.head('/xview/callable_object/')
self.assertEqual(response['X-View'], 'admin_docs.views.XViewCallableObject') 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/')

View File

@ -27,3 +27,13 @@ class TestAuthenticationMiddleware(TestCase):
self.assertTrue(self.request.user.is_anonymous) self.assertTrue(self.request.user.is_anonymous)
# session should be flushed # session should be flushed
self.assertIsNone(self.request.session.session_key) 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())

View File

@ -1,6 +1,7 @@
from django.contrib.messages import constants from django.contrib.messages import constants
from django.contrib.messages.storage.base import Message from django.contrib.messages.storage.base import Message
from django.contrib.messages.storage.session import SessionStorage from django.contrib.messages.storage.session import SessionStorage
from django.http import HttpRequest
from django.test import TestCase from django.test import TestCase
from django.utils.safestring import SafeData, mark_safe from django.utils.safestring import SafeData, mark_safe
@ -34,6 +35,15 @@ class SessionTests(BaseTests, TestCase):
def stored_messages_count(self, storage, response): def stored_messages_count(self, storage, response):
return stored_session_messages_count(storage) 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): def test_get(self):
storage = self.storage_class(self.get_request()) storage = self.storage_class(self.get_request())
example_messages = ['test', 'me'] example_messages = ['test', 'me']