From 31e2d207e0bad95338b1a43fbebae17a1c482028 Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Tue, 7 May 2019 19:33:24 +0200
Subject: [PATCH 2/2] cook: add tests using tenants for cook.py (#32886)

---
 tests_schemas/conftest.py         | 31 ++++++++++++++
 tests_schemas/ozwillo_recipe.json | 67 +++++++++++++++++++++++++++++++
 tests_schemas/test_cook.py        | 17 +++++++-
 3 files changed, 114 insertions(+), 1 deletion(-)
 create mode 100644 tests_schemas/ozwillo_recipe.json

diff --git a/tests_schemas/conftest.py b/tests_schemas/conftest.py
index cdc4f79..b77976f 100644
--- a/tests_schemas/conftest.py
+++ b/tests_schemas/conftest.py
@@ -1,3 +1,7 @@
+import os
+import shutil
+import tempfile
+
 import pytest
 
 from django.core.management import call_command
@@ -47,3 +51,30 @@ def fake_notify(monkeypatch):
     monkeypatch.setattr(Command, 'wait_operationals', fake_wait)
 
     return services
+
+@pytest.fixture
+def fake_themes(request, settings):
+    THEMES="""
+[
+  {
+    "id": "publik",
+    "label": "Publik",
+    "variables": {
+      "css_variant": "publik",
+      "no_extra_js": false,
+      "theme_color": "#E80E89"
+    }
+  }
+]
+"""
+    base_dir = tempfile.mkdtemp('themes')
+    settings.THEMES_DIRECTORY = base_dir
+
+    themes_dir = os.path.join(base_dir, 'publik-base')
+    os.mkdir(themes_dir)
+    with open(os.path.join(themes_dir, 'themes.json'), 'w') as handler:
+        handler.write(THEMES)
+
+    def fin():
+        shutil.rmtree(themes_dir)
+    request.addfinalizer(fin)
diff --git a/tests_schemas/ozwillo_recipe.json b/tests_schemas/ozwillo_recipe.json
new file mode 100644
index 0000000..4e61a5b
--- /dev/null
+++ b/tests_schemas/ozwillo_recipe.json
@@ -0,0 +1,67 @@
+{
+    "steps": [
+        {
+            "create-hobo": {
+                "url": "https://${hobo}/",
+                "primary": true
+            }
+        },
+        {
+            "create-authentic": {
+                "title": "Connexion",
+                "url": "https://${authentic}/"
+            }
+        },
+        {
+            "set-idp": {}
+        },
+        {
+            "create-combo": {
+                "template_name": "portal-user",
+                "title": "Compte citoyen",
+                "url": "https://${combo}/"
+            }
+        },
+        {
+            "create-combo": {
+                "slug": "portal-agent",
+                "template_name": "portal-agent",
+                "title": "Portail agent",
+                "url": "https://${combo_agent}/"
+            }
+        },
+        {
+            "create-wcs": {
+                "template_name": "export.zip",
+                "title": "D\u00e9marches",
+                "url": "https://${wcs}/"
+            }
+        },
+        {
+            "create-fargo": {
+                "title": "Porte documents",
+                "url": "https://${fargo}/"
+            }
+        },
+        {
+            "create-passerelle": {
+                "title": "Passerelle",
+                "url": "https://${passerelle}/"
+            }
+        },
+        {
+            "set-theme": {
+                "theme": "publik"
+            }
+        }
+    ],
+    "variables": {
+        "authentic": "connexion-instance-name.sictiam.dev.entrouvert.org",
+        "combo": "instance-name.sictiam.dev.entrouvert.org",
+        "combo_agent": "agents-instance-name.sictiam.dev.entrouvert.org",
+        "fargo": "porte-documents-instance-name.sictiam.dev.entrouvert.org",
+        "hobo": "hobo-instance-name.sictiam.dev.entrouvert.org",
+        "passerelle": "passerelle-instance-name.sictiam.dev.entrouvert.org",
+        "wcs": "demarches-instance-name.sictiam.dev.entrouvert.org"
+    }
+}
diff --git a/tests_schemas/test_cook.py b/tests_schemas/test_cook.py
index e0ab2f1..7c40c20 100644
--- a/tests_schemas/test_cook.py
+++ b/tests_schemas/test_cook.py
@@ -1,8 +1,10 @@
 import pytest
+import mock
 
-from django.core.management import call_command
+from django.core.management import call_command, load_command_class
 from django.core.management.base import CommandError
 
+from hobo.deploy.utils import get_hobo_json
 from hobo.environment.models import ServiceBase
 
 
@@ -18,3 +20,16 @@ def test_cook_unresolvable(db, fake_notify, monkeypatch):
     with pytest.raises(CommandError) as e_info:
         call_command('cook', 'tests_schemas/recipe.json')
     assert 'is not resolvable' in str(e_info.value)
+
+
+def test_cook_ozwillo(db, fake_notify, monkeypatch, fake_themes):
+    """hobo/cook (before rabbitmq) scenario having.
+    (cf hobo/contrib/ozwillo/examples)
+    the resulting JSON may be helpfull to test hobo/deploy (after rabbitmq)
+    """
+    monkeypatch.setattr(ServiceBase, 'is_resolvable', lambda x: True)
+    monkeypatch.setattr(ServiceBase, 'has_valid_certificate', lambda x: True)
+    call_command('cook', 'tests_schemas/ozwillo_recipe.json')
+    assert len(fake_notify) == 7
+    environment = get_hobo_json()
+    assert environment['services'][4]['template_name'] == 'portal-user'
-- 
2.20.1

