Refs #26601 -- Added various middlewares tests for deprecation of passing None as get_response.

This commit is contained in:
Kevin Michel 2020-08-28 11:13:05 +02:00 committed by Mariusz Felisiak
parent abbdd3a622
commit 68d7cf4054
2 changed files with 50 additions and 23 deletions

View File

@ -2,14 +2,29 @@ import threading
from asgiref.sync import async_to_sync
from django.contrib.admindocs.middleware import XViewMiddleware
from django.contrib.auth.middleware import (
AuthenticationMiddleware, RemoteUserMiddleware,
)
from django.contrib.flatpages.middleware import FlatpageFallbackMiddleware
from django.contrib.messages.middleware import MessageMiddleware
from django.contrib.redirects.middleware import RedirectFallbackMiddleware
from django.contrib.sessions.middleware import SessionMiddleware
from django.contrib.sites.middleware import CurrentSiteMiddleware
from django.db import connection
from django.http.request import HttpRequest
from django.http.response import HttpResponse
from django.middleware.cache import (
CacheMiddleware, FetchFromCacheMiddleware, UpdateCacheMiddleware,
)
from django.middleware.common import CommonMiddleware
from django.middleware.clickjacking import XFrameOptionsMiddleware
from django.middleware.common import (
BrokenLinkEmailsMiddleware, CommonMiddleware,
)
from django.middleware.csrf import CsrfViewMiddleware
from django.middleware.gzip import GZipMiddleware
from django.middleware.http import ConditionalGetMiddleware
from django.middleware.locale import LocaleMiddleware
from django.middleware.security import SecurityMiddleware
from django.test import SimpleTestCase
from django.utils.deprecation import MiddlewareMixin, RemovedInDjango40Warning
@ -20,31 +35,40 @@ class MiddlewareMixinTests(SimpleTestCase):
Deprecation warning is raised when using get_response=None.
"""
msg = 'Passing None for the middleware get_response argument is deprecated.'
middlewares = [
AuthenticationMiddleware,
BrokenLinkEmailsMiddleware,
CacheMiddleware,
CommonMiddleware,
ConditionalGetMiddleware,
CsrfViewMiddleware,
CurrentSiteMiddleware,
FetchFromCacheMiddleware,
FlatpageFallbackMiddleware,
GZipMiddleware,
LocaleMiddleware,
MessageMiddleware,
RedirectFallbackMiddleware,
RemoteUserMiddleware,
SecurityMiddleware,
SessionMiddleware,
UpdateCacheMiddleware,
XFrameOptionsMiddleware,
XViewMiddleware,
]
def test_deprecation(self):
with self.assertRaisesMessage(RemovedInDjango40Warning, self.msg):
CommonMiddleware()
def test_passing_explicit_none(self):
with self.assertRaisesMessage(RemovedInDjango40Warning, self.msg):
CommonMiddleware(None)
def test_subclass_deprecation(self):
"""
Deprecation warning is raised in subclasses overriding __init__()
without calling super().
"""
for middleware in [
SessionMiddleware,
CacheMiddleware,
FetchFromCacheMiddleware,
UpdateCacheMiddleware,
SecurityMiddleware,
]:
for middleware in self.middlewares:
with self.subTest(middleware=middleware):
with self.assertRaisesMessage(RemovedInDjango40Warning, self.msg):
middleware()
def test_passing_explicit_none(self):
for middleware in self.middlewares:
with self.subTest(middleware=middleware):
with self.assertRaisesMessage(RemovedInDjango40Warning, self.msg):
middleware(None)
def test_sync_to_async_uses_base_thread_and_connection(self):
"""
The process_request() and process_response() hooks must be called with

View File

@ -90,8 +90,9 @@ ALWAYS_MIDDLEWARE = [
# avoid "RuntimeError: Model class X doesn't declare an explicit app_label
# and isn't in an application in INSTALLED_APPS."
CONTRIB_TESTS_TO_APPS = {
'flatpages_tests': 'django.contrib.flatpages',
'redirects_tests': 'django.contrib.redirects',
'deprecation': ['django.contrib.flatpages', 'django.contrib.redirects'],
'flatpages_tests': ['django.contrib.flatpages'],
'redirects_tests': ['django.contrib.redirects'],
}
@ -228,7 +229,9 @@ def setup(verbosity, test_labels, parallel, start_at, start_after):
)
if module_name in CONTRIB_TESTS_TO_APPS and module_found_in_labels:
settings.INSTALLED_APPS.append(CONTRIB_TESTS_TO_APPS[module_name])
for contrib_app in CONTRIB_TESTS_TO_APPS[module_name]:
if contrib_app not in settings.INSTALLED_APPS:
settings.INSTALLED_APPS.append(contrib_app)
if module_found_in_labels and module_label not in installed_app_names:
if verbosity >= 2: