From 19c4588b2308217217c007242fdc44a0bb30aa16 Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Fri, 31 May 2019 11:55:20 +0200
Subject: [PATCH] hobo_deploy: code revue on import_template function from
 hobo_deploy.py (#33590)

---
 .../common/management/commands/hobo_deploy.py    | 10 ++++------
 tests/test_hobo_deploy.py                        | 16 +++++++---------
 tests_schemas/test_hobo_deploy.py                | 11 ++++++++++-
 3 files changed, 21 insertions(+), 16 deletions(-)

diff --git a/hobo/agent/common/management/commands/hobo_deploy.py b/hobo/agent/common/management/commands/hobo_deploy.py
index 8fa1e84..80f11d6 100644
--- a/hobo/agent/common/management/commands/hobo_deploy.py
+++ b/hobo/agent/common/management/commands/hobo_deploy.py
@@ -98,7 +98,7 @@ class Command(BaseCommand):
         self.generate_saml_keys(tenant, prefix='sp-')
         self.configure_service_provider(hobo_environment, tenant)
         self.configure_theme(hobo_environment, tenant)
-        self.configure_template(hobo_environment, tenant)
+        self.configure_template()
 
     def generate_saml_keys(self, tenant, prefix=''):
 
@@ -188,8 +188,6 @@ class Command(BaseCommand):
                         theme.get('overlay'), part)
                 atomic_symlink(target_dir, os.path.join(tenant_dir, part))
 
-    def configure_template(self, hobo_environment, tenant):
-        me = [x for x in hobo_environment.get('services') if x.get('this') is True][0]
-
-        if 'import_template' in get_commands() and me.get('template_name'):
-            call_command('import_template', me['template_name'])
+    def configure_template(self):
+        if self.me.get('template_name'):
+            call_command('import_template', self.me['template_name'])
diff --git a/tests/test_hobo_deploy.py b/tests/test_hobo_deploy.py
index f238760..78dce50 100644
--- a/tests/test_hobo_deploy.py
+++ b/tests/test_hobo_deploy.py
@@ -225,7 +225,7 @@ def test_deploy_specific():
     assert command.generate_saml_keys.mock_calls == [call('my_tenant', prefix='sp-')]
     assert command.configure_service_provider.mock_calls == [call('my_hobo_env', 'my_tenant')]
     assert command.configure_theme.mock_calls == [call('my_hobo_env', 'my_tenant')]
-    assert command.configure_template.mock_calls == [call('my_hobo_env', 'my_tenant')]
+    assert command.configure_template.mock_calls == [call()]
 
 
 def test_generate_saml_keys(tmpdir):
@@ -385,22 +385,20 @@ def test_configure_theme(fake_themes, tmpdir):
 @patch('hobo.agent.common.management.commands.hobo_deploy.call_command')
 def test_configure_template(mocked_call_command, mocked_get_commands):
     command = Command()
-    ENVIRONMENT = {'services': [{'service-id': 'combo',
-                                 'template_name': 'my_template',
-                                 'this': True}]}
+    command.me = {'service-id': 'combo',
+                  'template_name': 'my_template',
+                  'this': True}
 
-    # import_template.py located into hobo/agent/common: always belongs to command object
-    # TODO: dead condition
     mocked_get_commands.return_value = ['import_template', '...']
 
     # main case
-    command.configure_template(ENVIRONMENT, 'unused')  # TODO: dead tenant parameter
+    command.configure_template()
     assert mocked_call_command.mock_calls == [call('import_template', 'my_template')]
 
     # no template_name entry provided
     mocked_call_command.reset_mock()
-    del ENVIRONMENT['services'][0]['template_name']
-    command.configure_template(ENVIRONMENT, 'unused')
+    del command.me['template_name']
+    command.configure_template()
     assert mocked_call_command.mock_calls == []
 
 
diff --git a/tests_schemas/test_hobo_deploy.py b/tests_schemas/test_hobo_deploy.py
index 9f72327..50450c4 100644
--- a/tests_schemas/test_hobo_deploy.py
+++ b/tests_schemas/test_hobo_deploy.py
@@ -79,12 +79,21 @@ def test_deploy_specifics_on_hobo_agent(db):
 # here, because this code is not implemented here
 
 
-def test_get_theme_on_combo_agent(db):
+@mock.patch('hobo.agent.common.management.commands.hobo_deploy.call_command')
+def test_get_theme_on_combo_agent(mocked_call_command, db):
     """overloaded case for combo:
     $ combo-manage hobo-deploy env.json  # simulate this bash query
     """
     command = load_command_class('hobo.agent.combo', 'hobo_deploy')
     domain = 'combo.dev.publik.love'
+
+    def my_call_command(command, parameter):
+        if command == 'import_template':
+            my_call_command.import_template_was_called = True
+            return
+        call_command(command, parameter)
+
+    mocked_call_command.side_effect = my_call_command
     command.handle('https://%s/' % domain, 'tests_schemas/env.json')
     assert_deployed(domain)
     # lack an assert statement proving we do reach
-- 
2.20.1

