From 564685eff67748b47b37d0e2590b47e9137dfcdd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Tue, 1 Sep 2020 15:09:17 +0200
Subject: [PATCH] settings: remove old MIDDLEWARE_CLASSES support (#45671)

---
 debian/debian_config_common.py | 86 ++++++++++------------------------
 hobo/multitenant/README        |  4 +-
 hobo/settings.py               |  2 +-
 tests/settings.py              |  2 +-
 tests/test_version.py          |  4 +-
 tests_multitenant/settings.py  | 29 ++++--------
 tests_schemas/settings.py      |  2 +-
 7 files changed, 42 insertions(+), 87 deletions(-)

diff --git a/debian/debian_config_common.py b/debian/debian_config_common.py
index 87996fe..3f3cdbd 100644
--- a/debian/debian_config_common.py
+++ b/debian/debian_config_common.py
@@ -275,42 +275,24 @@ SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
 if 'MIDDLEWARE' not in globals():
     MIDDLEWARE = global_settings.MIDDLEWARE
 
-if 'MIDDLEWARE_CLASSES' in globals():
-    MIDDLEWARE_CLASSES = (
-        'hobo.middleware.RobotsTxtMiddleware',
-        'hobo.middleware.VersionMiddleware',  # /__version__
-        'hobo.middleware.cors.CORSMiddleware',
-    ) + MIDDLEWARE_CLASSES
-
-    if PROJECT_NAME != 'wcs' and 'authentic2' not in INSTALLED_APPS:
-        MIDDLEWARE_CLASSES = MIDDLEWARE_CLASSES + (
-            'mellon.middleware.PassiveAuthenticationMiddleware',
-            'hobo.provisionning.middleware.ProvisionningMiddleware',
-        )
-
-    if 'authentic2' in INSTALLED_APPS:
-        MIDDLEWARE_CLASSES = MIDDLEWARE_CLASSES + (
-            'hobo.agent.authentic2.middleware.ProvisionningMiddleware',
-        )
-else:
-    MIDDLEWARE = (
-        'hobo.middleware.VersionMiddleware',  # /__version__
-        'hobo.middleware.cors.CORSMiddleware',
-    ) + MIDDLEWARE
+MIDDLEWARE = (
+    'hobo.middleware.VersionMiddleware',  # /__version__
+    'hobo.middleware.cors.CORSMiddleware',
+) + MIDDLEWARE
 
-    if PROJECT_NAME != 'wcs':
-        MIDDLEWARE = ('hobo.middleware.RobotsTxtMiddleware',) + MIDDLEWARE
+if PROJECT_NAME != 'wcs':
+    MIDDLEWARE = ('hobo.middleware.RobotsTxtMiddleware',) + MIDDLEWARE
 
-    if PROJECT_NAME != 'wcs' and 'authentic2' not in INSTALLED_APPS:
-        MIDDLEWARE = MIDDLEWARE + (
-            'mellon.middleware.PassiveAuthenticationMiddleware',
-            'hobo.provisionning.middleware.ProvisionningMiddleware',
-        )
+if PROJECT_NAME != 'wcs' and 'authentic2' not in INSTALLED_APPS:
+    MIDDLEWARE = MIDDLEWARE + (
+        'mellon.middleware.PassiveAuthenticationMiddleware',
+        'hobo.provisionning.middleware.ProvisionningMiddleware',
+    )
 
-    if 'authentic2' in INSTALLED_APPS:
-        MIDDLEWARE = MIDDLEWARE + (
-            'hobo.agent.authentic2.middleware.ProvisionningMiddleware',
-        )
+if 'authentic2' in INSTALLED_APPS:
+    MIDDLEWARE = MIDDLEWARE + (
+        'hobo.agent.authentic2.middleware.ProvisionningMiddleware',
+    )
 
 
 if PROJECT_NAME != 'wcs':
@@ -319,18 +301,11 @@ if PROJECT_NAME != 'wcs':
     TENANT_BASE = os.path.join(VAR_DIR, 'tenants')
     TENANT_MODEL = 'multitenant.Tenant'
 
-    if 'MIDDLEWARE_CLASSES' in globals():
-        MIDDLEWARE_CLASSES = (
-            'hobo.multitenant.middleware.TenantMiddleware',
-            'hobo.middleware.CookiesSameSiteFixMiddleware',
-            'hobo.middleware.debug.InternalIPMiddleware',
-        ) + MIDDLEWARE_CLASSES
-    else:
-        MIDDLEWARE = (
-            'hobo.multitenant.middleware.TenantMiddleware',
-            'hobo.middleware.CookiesSameSiteFixMiddleware',
-            'hobo.middleware.debug.InternalIPMiddleware',
-        ) + MIDDLEWARE
+    MIDDLEWARE = (
+        'hobo.multitenant.middleware.TenantMiddleware',
+        'hobo.middleware.CookiesSameSiteFixMiddleware',
+        'hobo.middleware.debug.InternalIPMiddleware',
+    ) + MIDDLEWARE
 
     DATABASES = {
         'default': {
@@ -399,22 +374,13 @@ if 'authentic2' not in INSTALLED_APPS:
     MELLON_OPENED_SESSION_COOKIE_NAME = 'A2_OPENED_SESSION'
     MELLON_ADD_AUTHNREQUEST_NEXT_URL_EXTENSION = True
 
-if 'MIDDLEWARE_CLASSES' in globals():
-    MIDDLEWARE_CLASSES = (
-        'hobo.middleware.utils.StoreRequestMiddleware',
-        'hobo.middleware.xforwardedfor.XForwardedForMiddleware',
-    ) + MIDDLEWARE_CLASSES
-
-    MIDDLEWARE_CLASSES = MIDDLEWARE_CLASSES + (
-        'hobo.middleware.PrometheusStatsMiddleware',)
-else:
-    MIDDLEWARE = (
-        'hobo.middleware.utils.StoreRequestMiddleware',
-        'hobo.middleware.xforwardedfor.XForwardedForMiddleware',
-    ) + MIDDLEWARE
+MIDDLEWARE = (
+    'hobo.middleware.utils.StoreRequestMiddleware',
+    'hobo.middleware.xforwardedfor.XForwardedForMiddleware',
+) + MIDDLEWARE
 
-    MIDDLEWARE = MIDDLEWARE + (
-        'hobo.middleware.PrometheusStatsMiddleware',)
+MIDDLEWARE = MIDDLEWARE + (
+    'hobo.middleware.PrometheusStatsMiddleware',)
 
 
 HOBO_MANAGER_HOMEPAGE_URL_VAR = 'portal_agent_url'
diff --git a/hobo/multitenant/README b/hobo/multitenant/README
index 974fc53..52b0970 100644
--- a/hobo/multitenant/README
+++ b/hobo/multitenant/README
@@ -25,9 +25,9 @@ Where are tenants:
 
 Add the middlewares for multitenant, they must be first:
 
-    MIDDLEWARE_CLASSES = (
+    MIDDLEWARE = (
         'hobo.multitenant.middleware.TenantMiddleware',
-    ) + MIDDLEWARE_CLASSES
+    ) + MIDDLEWARE
     TENANT_SETTINGS_LOADERS = (
         'hobo.multitenant.settings_loaders.TemplateVars',
         'hobo.multitenant.settings_loaders.SettingsJSON',
diff --git a/hobo/settings.py b/hobo/settings.py
index f0363b8..37615e8 100644
--- a/hobo/settings.py
+++ b/hobo/settings.py
@@ -52,7 +52,7 @@ INSTALLED_APPS = (
     'hobo.deploy',
 )
 
-MIDDLEWARE_CLASSES = (
+MIDDLEWARE = (
     'hobo.middleware.xforwardedfor.XForwardedForMiddleware',
     'django.contrib.sessions.middleware.SessionMiddleware',
     'django.middleware.locale.LocaleMiddleware',
diff --git a/tests/settings.py b/tests/settings.py
index 52861e9..34dbf31 100644
--- a/tests/settings.py
+++ b/tests/settings.py
@@ -10,7 +10,7 @@ ALLOWED_HOSTS.append('localhost')
 
 TEMPLATES[0]['OPTIONS']['debug'] = True
 
-MIDDLEWARE_CLASSES = MIDDLEWARE_CLASSES + (
+MIDDLEWARE = MIDDLEWARE + (
         'hobo.middleware.RobotsTxtMiddleware',
         'hobo.provisionning.middleware.ProvisionningMiddleware')
 
diff --git a/tests/test_version.py b/tests/test_version.py
index 18b611e..6d33c0a 100644
--- a/tests/test_version.py
+++ b/tests/test_version.py
@@ -10,8 +10,8 @@ def test_version_middleware(settings, client):
     # disable apt_cache to get stable test results
     hobo.scrutiny.wsgi.middleware.apt_cache = None
 
-    settings.MIDDLEWARE_CLASSES = (
+    settings.MIDDLEWARE = (
         'hobo.middleware.version.VersionMiddleware',
-        ) + settings.MIDDLEWARE_CLASSES
+        ) + settings.MIDDLEWARE
     json_response = client.get('/__version__').json()
     assert set(json_response.keys()) == set(['pytest', 'pytest-django'])
diff --git a/tests_multitenant/settings.py b/tests_multitenant/settings.py
index 2cc60a7..156a8f2 100644
--- a/tests_multitenant/settings.py
+++ b/tests_multitenant/settings.py
@@ -12,26 +12,15 @@ INSTALLED_APPS = ('django.contrib.auth', 'django.contrib.sessions', 'django.cont
 
 PROJECT_NAME = 'fake-agent'
 
-if 'OLD_STYLE_MIDDLEWARE' in os.environ:
-    MIDDLEWARE_CLASSES = (
-        'django.contrib.sessions.middleware.SessionMiddleware',
-        'django.middleware.locale.LocaleMiddleware',
-        'django.middleware.common.CommonMiddleware',
-        'django.middleware.csrf.CsrfViewMiddleware',
-        'django.contrib.auth.middleware.AuthenticationMiddleware',
-        'django.contrib.messages.middleware.MessageMiddleware',
-        'django.middleware.clickjacking.XFrameOptionsMiddleware',
-    )
-else:
-    MIDDLEWARE = (
-        'django.contrib.sessions.middleware.SessionMiddleware',
-        'django.middleware.locale.LocaleMiddleware',
-        'django.middleware.common.CommonMiddleware',
-        'django.middleware.csrf.CsrfViewMiddleware',
-        'django.contrib.auth.middleware.AuthenticationMiddleware',
-        'django.contrib.messages.middleware.MessageMiddleware',
-        'django.middleware.clickjacking.XFrameOptionsMiddleware',
-    )
+MIDDLEWARE = (
+    'django.contrib.sessions.middleware.SessionMiddleware',
+    'django.middleware.locale.LocaleMiddleware',
+    'django.middleware.common.CommonMiddleware',
+    'django.middleware.csrf.CsrfViewMiddleware',
+    'django.contrib.auth.middleware.AuthenticationMiddleware',
+    'django.contrib.messages.middleware.MessageMiddleware',
+    'django.middleware.clickjacking.XFrameOptionsMiddleware',
+)
 
 TEMPLATES = [
     {
diff --git a/tests_schemas/settings.py b/tests_schemas/settings.py
index bc6eab2..3041b8d 100644
--- a/tests_schemas/settings.py
+++ b/tests_schemas/settings.py
@@ -1,7 +1,7 @@
 import tempfile
 TENANT_BASE = tempfile.mkdtemp('hobo-tenant-base')
 TENANT_MODEL = 'multitenant.Tenant'
-MIDDLEWARE_CLASSES = ('hobo.multitenant.middleware.TenantMiddleware',) + MIDDLEWARE_CLASSES
+MIDDLEWARE = ('hobo.multitenant.middleware.TenantMiddleware',) + MIDDLEWARE
 DATABASES = {
 	'default': {
 		'ENGINE': 'tenant_schemas.postgresql_backend',
-- 
2.28.0

