From 68d7cf4054e4d53d77270659113520540cc7cadc Mon Sep 17 00:00:00 2001 From: Kevin Michel Date: Fri, 28 Aug 2020 11:13:05 +0200 Subject: [PATCH] Refs #26601 -- Added various middlewares tests for deprecation of passing None as get_response. --- tests/deprecation/test_middleware_mixin.py | 64 +++++++++++++++------- tests/runtests.py | 9 ++- 2 files changed, 50 insertions(+), 23 deletions(-) diff --git a/tests/deprecation/test_middleware_mixin.py b/tests/deprecation/test_middleware_mixin.py index c90aeb8360..3e4260b422 100644 --- a/tests/deprecation/test_middleware_mixin.py +++ b/tests/deprecation/test_middleware_mixin.py @@ -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 diff --git a/tests/runtests.py b/tests/runtests.py index db32da13eb..db1fd65dd2 100755 --- a/tests/runtests.py +++ b/tests/runtests.py @@ -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: