From bf99ab4cd768bb5c70d36c676254870c36adc598 Mon Sep 17 00:00:00 2001
From: Emmanuel Cazenave <ecazenave@entrouvert.com>
Date: Tue, 25 Jan 2022 17:47:28 +0100
Subject: [PATCH] agent/authentic: store portal-user url on correponding OU
 (#61029)

---
 .../management/commands/hobo_deploy.py        |  3 +++
 tests_authentic/test_hobo_deploy.py           | 25 +++++++++++++++----
 2 files changed, 23 insertions(+), 5 deletions(-)

diff --git a/hobo/agent/authentic2/management/commands/hobo_deploy.py b/hobo/agent/authentic2/management/commands/hobo_deploy.py
index 2175e80..859e54f 100644
--- a/hobo/agent/authentic2/management/commands/hobo_deploy.py
+++ b/hobo/agent/authentic2/management/commands/hobo_deploy.py
@@ -192,6 +192,9 @@ class Command(hobo_deploy.Command):
                     if service_created or not provider.ou:
                         provider.ou = ou
                         provision_target_ous[provider.ou.id] = provider.ou
+                    if service.get('template_name') == 'portal-user':
+                        provider.ou.base_url = service['base_url']
+                        provider.ou.save()
                     provider.save()
                     if service_created:
                         service_provider = LibertyServiceProvider(
diff --git a/tests_authentic/test_hobo_deploy.py b/tests_authentic/test_hobo_deploy.py
index 5055ac0..03610ef 100644
--- a/tests_authentic/test_hobo_deploy.py
+++ b/tests_authentic/test_hobo_deploy.py
@@ -120,9 +120,10 @@ def test_hobo_deploy(monkeypatch, tenant_base, mocker, skeleton_dir, tmp_path):
 </EntityDescriptor>'''
     meta2 = meta1.replace('eservices', 'passerelle')
     meta3 = meta1.replace('eservices', 'clapiers')
-    metadatas = [meta1, meta2, meta3]
+    meta4 = meta1.replace('eservices', 'portal')
+    metadatas = [meta1, meta2, meta3, meta4]
     monkeypatch.setattr(HoboDeployCommand, 'backoff_factor', 0.0001)
-    side_effect_iter = iter([meta1, meta2, RequestException(), meta3])
+    side_effect_iter = iter([meta1, meta2, RequestException(), meta4, meta3])
 
     def side_effect(*args, **kwargs):
         for v in side_effect_iter:
@@ -335,6 +336,14 @@ def test_hobo_deploy(monkeypatch, tenant_base, mocker, skeleton_dir, tmp_path):
                     'ou-slug': 'ou-slug',
                 },
             },
+            {
+                'service-id': 'combo',
+                'template_name': 'portal-user',
+                'slug': 'portal',
+                'title': u'Portail Montpellier-Métropole',
+                'base_url': 'http://portal.example.net',
+                'saml-sp-metadata-url': 'http://portal.example.net/saml/metadata',
+            },
         ],
     }
 
@@ -358,7 +367,11 @@ def test_hobo_deploy(monkeypatch, tenant_base, mocker, skeleton_dir, tmp_path):
     audiences = sorted([arg[0][0]['audience'] for arg in mock_notify.call_args_list])
     assert audiences == [
         ['http://clapiers.example.net/saml/metadata'],
-        ['http://eservices.example.net/saml/metadata', 'http://passerelle.example.net/saml/metadata'],
+        [
+            'http://eservices.example.net/saml/metadata',
+            'http://passerelle.example.net/saml/metadata',
+            'http://portal.example.net/saml/metadata',
+        ],
     ]
     assert [arg[0][0]['@type'] for arg in mock_notify.call_args_list] == ['provision', 'provision']
     assert [arg[0][0]['objects']['@type'] for arg in mock_notify.call_args_list] == ['role', 'role']
@@ -440,7 +453,7 @@ def test_hobo_deploy(monkeypatch, tenant_base, mocker, skeleton_dir, tmp_path):
                 ).count()
                 == 1
             )
-        assert LibertyProvider.objects.count() == 3
+        assert LibertyProvider.objects.count() == 4
         services = tenant.get_hobo_json()['services']
         from authentic2.a2_rbac.models import Role
         from authentic2.a2_rbac.utils import get_default_ou
@@ -464,6 +477,8 @@ def test_hobo_deploy(monkeypatch, tenant_base, mocker, skeleton_dir, tmp_path):
                     assert provider.name == service['title']
                 assert provider.federation_source == 'hobo'
                 assert provider.entity_id == service['saml-sp-metadata-url']
+                if service.get('template_name') == 'portal-user':
+                    assert provider.ou.base_url == service['base_url']
                 assert LibertyServiceProvider.objects.filter(liberty_provider=provider).count() == 1
 
                 service_provider = LibertyServiceProvider.objects.get(liberty_provider=provider)
@@ -525,7 +540,7 @@ def test_hobo_deploy(monkeypatch, tenant_base, mocker, skeleton_dir, tmp_path):
     field = env['profile']['fields'][9]
     assert field['name'] == 'mobile'
     field['kind'] = 'phone_number'
-    side_effect_iter = iter([meta1, meta2, RequestException(), meta3])
+    side_effect_iter = iter([meta1, meta2, RequestException(), meta4, meta3])
     with mock.patch('hobo.agent.authentic2.provisionning.notify_agents') as mock_notify, mock.patch(
         'hobo.agent.authentic2.management.commands.hobo_deploy.sleep', wraps=time.sleep
     ) as sleep_mock:
-- 
2.30.2

