From 9f075fcb9ab440f6069781eaa13dc26e345eff06 Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Wed, 12 Jun 2019 16:22:45 +0200
Subject: [PATCH] hobo_deploy: internally call import_template (using
 tenant_command)

---
 .../common/management/commands/hobo_deploy.py |  6 +++--
 tests/test_hobo_deploy.py                     | 19 +++++++++------
 tests_schemas/env.json                        |  4 ++--
 tests_schemas/test_hobo_deploy.py             | 23 +++++++------------
 4 files changed, 26 insertions(+), 26 deletions(-)

diff --git a/hobo/agent/common/management/commands/hobo_deploy.py b/hobo/agent/common/management/commands/hobo_deploy.py
index 8fa1e84..87dbb92 100644
--- a/hobo/agent/common/management/commands/hobo_deploy.py
+++ b/hobo/agent/common/management/commands/hobo_deploy.py
@@ -8,7 +8,7 @@ import urlparse
 
 from django.conf import settings
 from django.core.management.base import BaseCommand, CommandError
-from django.core.management import call_command, get_commands
+from django.core.management import call_command, execute_from_command_line, get_commands
 
 from hobo.multitenant.middleware import TenantMiddleware, TenantNotFound
 from hobo.theme.utils import get_theme
@@ -192,4 +192,6 @@ class Command(BaseCommand):
         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'])
+            execute_from_command_line([sys.argv[0], 'tenant_command',
+                                       'import_template', me['template_name'],
+                                       '--domain', tenant.domain_url])
diff --git a/tests/test_hobo_deploy.py b/tests/test_hobo_deploy.py
index f238760..54ac38f 100644
--- a/tests/test_hobo_deploy.py
+++ b/tests/test_hobo_deploy.py
@@ -381,10 +381,12 @@ def test_configure_theme(fake_themes, tmpdir):
     assert os.path.exists(theme_path) is False
 
 
+@patch('hobo.agent.common.management.commands.hobo_deploy.execute_from_command_line')
 @patch('hobo.agent.common.management.commands.hobo_deploy.get_commands')
-@patch('hobo.agent.common.management.commands.hobo_deploy.call_command')
-def test_configure_template(mocked_call_command, mocked_get_commands):
+def test_configure_template(mocked_get_commands, mocked_execute_from_command_line):
     command = Command()
+    tenant = Mock()
+    tenant.domain_url = 'combo.dev.publik.love'
     ENVIRONMENT = {'services': [{'service-id': 'combo',
                                  'template_name': 'my_template',
                                  'this': True}]}
@@ -394,14 +396,17 @@ def test_configure_template(mocked_call_command, mocked_get_commands):
     mocked_get_commands.return_value = ['import_template', '...']
 
     # main case
-    command.configure_template(ENVIRONMENT, 'unused')  # TODO: dead tenant parameter
-    assert mocked_call_command.mock_calls == [call('import_template', 'my_template')]
+    sys.argv[0] = '.../manage.py'
+    command.configure_template(ENVIRONMENT, tenant)
+    assert mocked_execute_from_command_line.mock_calls == [
+        call(['.../manage.py', 'tenant_command', 'import_template', 'my_template',
+              '--domain', 'combo.dev.publik.love'])]
 
     # no template_name entry provided
-    mocked_call_command.reset_mock()
+    mocked_execute_from_command_line.reset_mock()
     del ENVIRONMENT['services'][0]['template_name']
-    command.configure_template(ENVIRONMENT, 'unused')
-    assert mocked_call_command.mock_calls == []
+    command.configure_template(ENVIRONMENT, tenant)
+    assert mocked_execute_from_command_line.mock_calls == []
 
 
 def test_combo_get_theme(fake_themes):
diff --git a/tests_schemas/env.json b/tests_schemas/env.json
index e9ad2e7..8e73cdb 100644
--- a/tests_schemas/env.json
+++ b/tests_schemas/env.json
@@ -6,7 +6,7 @@
 	    "slug": "agendas",
 	    "title": "CHRONO",
 	    "secret_key": "123",
-	    "template_name": "import_me.txt"
+	    "template_name": "import_me"
 	},
 	{
 	    "service-id": "wcs",
@@ -27,7 +27,7 @@
 	    "slug": "portal",
 	    "title": "COMBO",
 	    "secret_key": "123",
-	    "template_name": "import_me.txt"
+	    "template_name": "import_me"
 	},
 	{
 	    "service-id": "authentic",
diff --git a/tests_schemas/test_hobo_deploy.py b/tests_schemas/test_hobo_deploy.py
index 9f72327..a17fc80 100644
--- a/tests_schemas/test_hobo_deploy.py
+++ b/tests_schemas/test_hobo_deploy.py
@@ -1,4 +1,5 @@
 import os
+import sys
 
 import mock
 from django.core.management import call_command, get_commands, load_command_class
@@ -14,33 +15,25 @@ def assert_deployed(domain):
     assert os.path.exists(tenant_hobo_json)
 
 
-@mock.patch('hobo.agent.common.management.commands.hobo_deploy.call_command')
+@mock.patch('hobo.agent.common.management.commands.hobo_deploy.execute_from_command_line')
 @mock.patch('hobo.agent.common.management.commands.hobo_deploy.get_commands')
-def test_import_template(mocked_get_commands, mocked_call_command, db):
+def test_import_template(mocked_get_commands, mocked_execute_from_command_line, db):
     """check 'hobo-deploy' result in '$ chrono-manage import_template' call
     $ chrono-manage hobo_deploy env.json  # simulate this bash query
-
-    warning:
-    this test must be the first one executed else 'get_commands' fails to be mocked
     """
     command = load_command_class('hobo.agent.common', 'hobo_deploy')
     domain = 'chrono.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_get_commands.return_value = ['import_template']
-    mocked_call_command.side_effect = my_call_command
-    my_call_command.import_template_was_called = False
+    sys.argv[0] = '.../manage.py'
 
     command.handle('https://%s/' % domain, 'tests_schemas/env.json')
     assert_deployed(domain)
 
     # assert the 'import_template' command was called
-    assert my_call_command.import_template_was_called is True
+    assert mocked_execute_from_command_line.mock_calls == [
+        mock.call(['.../manage.py', 'tenant_command',
+                   'import_template', 'import_me',
+                   '--domain', 'chrono.dev.publik.love'])]
 
 
 def test_deploy_on_common_agent(db):
-- 
2.20.1

