From 994b20916bcfdcafadb6c6e5861d8bacaa56b08b Mon Sep 17 00:00:00 2001
From: Emmanuel Cazenave <ecazenave@entrouvert.com>
Date: Wed, 20 Apr 2022 16:54:33 +0200
Subject: [PATCH] multitenant: add legacy urls settings loader (#64347)

---
 debian/debian_config_common.py       |  1 +
 hobo/multitenant/settings_loaders.py | 19 +++++++++++++++++++
 tests_multitenant/conftest.py        |  1 +
 tests_multitenant/settings.py        |  1 +
 tests_multitenant/test_settings.py   | 12 ++++++++++++
 5 files changed, 34 insertions(+)

diff --git a/debian/debian_config_common.py b/debian/debian_config_common.py
index 36fde1a..5a8e748 100644
--- a/debian/debian_config_common.py
+++ b/debian/debian_config_common.py
@@ -318,6 +318,7 @@ if PROJECT_NAME != 'wcs':
         'hobo.multitenant.settings_loaders.SettingsJSON',
         'hobo.multitenant.settings_loaders.SettingsVars',
         'hobo.multitenant.settings_loaders.BackofficeLoginHint',
+        'hobo.multitenant.settings_loaders.LegacyURLSSettings',
     )
 
     # templates in <tenant>/templates
diff --git a/hobo/multitenant/settings_loaders.py b/hobo/multitenant/settings_loaders.py
index d1bc2e9..d5cb81c 100644
--- a/hobo/multitenant/settings_loaders.py
+++ b/hobo/multitenant/settings_loaders.py
@@ -244,6 +244,25 @@ class CORSSettings(FileBaseSettingsLoader):
         tenant_settings.CORS_ORIGIN_WHITELIST = whitelist
 
 
+class LegacyURLSSettings(FileBaseSettingsLoader):
+    FILENAME = 'hobo.json'
+
+    def update_settings_from_path(self, tenant_settings, path):
+        domain_mapping = {}
+        with open(path) as f:
+            hobo_json = json.load(f)
+        for service in hobo_json.get('services', []):
+            base_url = service.get('base_url')
+            if not base_url:
+                continue
+            hostname = urllib.parse.urlparse(base_url).netloc
+            for legacy_url in service.get('legacy_urls', []):
+                old_base_url = urllib.parse.urlparse(legacy_url['base_url'])
+                domain_mapping[old_base_url.netloc] = hostname
+
+        tenant_settings.LEGACY_URLS_MAPPING = domain_mapping
+
+
 class ThemeSettings(SettingsDictUpdateMixin):
     def get_new_time(self, tenant):
         return 0
diff --git a/tests_multitenant/conftest.py b/tests_multitenant/conftest.py
index 724a6cc..53b5e8e 100644
--- a/tests_multitenant/conftest.py
+++ b/tests_multitenant/conftest.py
@@ -67,6 +67,7 @@ def make_tenant(tmp_path, transactional_db, settings, request):
                             'secret_key': 'abcde',
                             'service-id': 'authentic',
                             'base_url': 'http://other.example.net',
+                            'legacy_urls': [{'base_url': 'http://olda2.example.net'}],
                         },
                         {
                             'slug': 'another',
diff --git a/tests_multitenant/settings.py b/tests_multitenant/settings.py
index ad1c40a..c0e24de 100644
--- a/tests_multitenant/settings.py
+++ b/tests_multitenant/settings.py
@@ -90,6 +90,7 @@ TENANT_SETTINGS_LOADERS = (
     'hobo.multitenant.settings_loaders.CookieNames',
     'hobo.multitenant.settings_loaders.SettingsJSON',
     'hobo.multitenant.settings_loaders.SettingsVars',
+    'hobo.multitenant.settings_loaders.LegacyURLSSettings',
 )
 
 GLOBAL1 = 0
diff --git a/tests_multitenant/test_settings.py b/tests_multitenant/test_settings.py
index 351454a..cb33748 100644
--- a/tests_multitenant/test_settings.py
+++ b/tests_multitenant/test_settings.py
@@ -227,6 +227,18 @@ def test_known_services(tenants, settings):
             )
 
 
+def test_legacy_urls_mapping(tenants, settings):
+
+    settings.clear_tenants_settings()
+    settings.SETTINGS_MODULE = 'fake.settings'
+
+    for tenant in tenants:
+        with tenant_context(tenant):
+            hobo_json = tenant.get_hobo_json()
+            assert hasattr(settings, 'LEGACY_URLS_MAPPING')
+            assert settings.LEGACY_URLS_MAPPING['olda2.example.net'] == 'other.example.net'
+
+
 def test_unique_cookies(tenants, settings):
     settings.clear_tenants_settings()
 
-- 
2.30.2

