From 69127428ae9c7f3ba5bd2bfe1e2a541ee3b7d8c5 Mon Sep 17 00:00:00 2001
From: Christophe Siraut <csiraut@entrouvert.com>
Date: Thu, 10 Jan 2019 21:34:51 +0100
Subject: [PATCH] agent: add generic import_template management command
 (#14630)

---
 .../management/commands/import_template.py    |  40 ++
 tests/utils.py                                |  10 +
 .../data_authentic_export_site.json           | 480 ++++++++++++++++++
 tests_authentic/test_hobo_deploy.py           |  16 +
 .../data_passerelle_export_site.json          |  24 +
 tests_passerelle/test_deploy.py               |  15 +
 6 files changed, 585 insertions(+)
 create mode 100644 hobo/agent/common/management/commands/import_template.py
 create mode 100644 tests/utils.py
 create mode 100644 tests_authentic/data_authentic_export_site.json
 create mode 100644 tests_passerelle/data_passerelle_export_site.json

diff --git a/hobo/agent/common/management/commands/import_template.py b/hobo/agent/common/management/commands/import_template.py
new file mode 100644
index 0000000..efd36c5
--- /dev/null
+++ b/hobo/agent/common/management/commands/import_template.py
@@ -0,0 +1,40 @@
+# hobo - portal to configure and deploy applications
+# Copyright (C) 2015  Entr'ouvert
+#
+# This program is free software: you can redistribute it and/or modify it
+# under the terms of the GNU Affero General Public License as published
+# by the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+import os
+
+from django.conf import settings
+from django.core.management.base import BaseCommand, CommandError
+from django.core.management import call_command, get_commands
+
+
+class Command(BaseCommand):
+
+    def add_arguments(self, parser):
+        parser.add_argument('template_name', type=str)
+        parser.add_argument('--basepath', type=str)
+
+    def handle(self, *args, **kwargs):
+        app_name = settings.PROJECT_NAME
+        basepath = kwargs.get('basepath', '/var/lib/%s/skeletons')
+        template = '%s/%s.json' % (basepath, kwargs.get('template_name'))
+
+        if 'import_site' in get_commands():
+
+            if not os.path.isfile(template):
+                raise(CommandError('Template not found'))
+
+            call_command('import_site', template)
diff --git a/tests/utils.py b/tests/utils.py
new file mode 100644
index 0000000..559f2d2
--- /dev/null
+++ b/tests/utils.py
@@ -0,0 +1,10 @@
+def byteify(input):
+    if isinstance(input, dict):
+        return {byteify(key): byteify(value)
+                for key, value in input.iteritems()}
+    elif isinstance(input, list):
+        return [byteify(element) for element in input]
+    elif isinstance(input, unicode):
+        return input.encode('utf-8')
+    else:
+        return input
diff --git a/tests_authentic/data_authentic_export_site.json b/tests_authentic/data_authentic_export_site.json
new file mode 100644
index 0000000..d55988b
--- /dev/null
+++ b/tests_authentic/data_authentic_export_site.json
@@ -0,0 +1,480 @@
+{
+    "ous": [
+        {
+            "username_is_unique": false,
+            "validate_emails": false,
+            "uuid": "69b0a02cf58a4c71b1ae548f1375baff",
+            "default": true,
+            "description": "",
+            "email_is_unique": false,
+            "slug": "default",
+            "name": "Collectivit\u00e9 par d\u00e9faut"
+        }
+    ],
+    "roles": [
+        {
+            "description": "",
+            "service": {
+                "ou": {
+                    "slug": "default",
+                    "uuid": "69b0a02cf58a4c71b1ae548f1375baff",
+                    "name": "Collectivit\u00e9 par d\u00e9faut"
+                },
+                "slug": "hobo"
+            },
+            "name": "Administrateur de Hobo",
+            "attributes": [
+                {
+                    "kind": "string",
+                    "name": "is_superuser",
+                    "value": "true"
+                }
+            ],
+            "ou": {
+                "slug": "default",
+                "uuid": "69b0a02cf58a4c71b1ae548f1375baff",
+                "name": "Collectivit\u00e9 par d\u00e9faut"
+            },
+            "external_id": "",
+            "slug": "_a2-hobo-superuser",
+            "uuid": "25f33158b7e2449b9a5b00dbc57bf416"
+        },
+        {
+            "description": "",
+            "service": {
+                "ou": {
+                    "slug": "default",
+                    "uuid": "69b0a02cf58a4c71b1ae548f1375baff",
+                    "name": "Collectivit\u00e9 par d\u00e9faut"
+                },
+                "slug": "portal"
+            },
+            "name": "Administrateur de Compte citoyen",
+            "attributes": [
+                {
+                    "kind": "string",
+                    "name": "is_superuser",
+                    "value": "true"
+                }
+            ],
+            "ou": {
+                "slug": "default",
+                "uuid": "69b0a02cf58a4c71b1ae548f1375baff",
+                "name": "Collectivit\u00e9 par d\u00e9faut"
+            },
+            "external_id": "",
+            "slug": "_a2-hobo-superuser",
+            "uuid": "84b3b1ba76e44bcdb4fd4437c448a981"
+        },
+        {
+            "description": "",
+            "service": {
+                "ou": {
+                    "slug": "default",
+                    "uuid": "69b0a02cf58a4c71b1ae548f1375baff",
+                    "name": "Collectivit\u00e9 par d\u00e9faut"
+                },
+                "slug": "eservices"
+            },
+            "name": "Administrateur de D\u00e9marches",
+            "attributes": [
+                {
+                    "kind": "string",
+                    "name": "is_superuser",
+                    "value": "true"
+                }
+            ],
+            "ou": {
+                "slug": "default",
+                "uuid": "69b0a02cf58a4c71b1ae548f1375baff",
+                "name": "Collectivit\u00e9 par d\u00e9faut"
+            },
+            "external_id": "",
+            "slug": "_a2-hobo-superuser",
+            "uuid": "9054a61ccf684396b38189f1ca1ec087"
+        },
+        {
+            "description": "",
+            "service": {
+                "ou": {
+                    "slug": "default",
+                    "uuid": "69b0a02cf58a4c71b1ae548f1375baff",
+                    "name": "Collectivit\u00e9 par d\u00e9faut"
+                },
+                "slug": "portal-agent"
+            },
+            "name": "Administrateur de Portail agent",
+            "attributes": [
+                {
+                    "kind": "string",
+                    "name": "is_superuser",
+                    "value": "true"
+                }
+            ],
+            "ou": {
+                "slug": "default",
+                "uuid": "69b0a02cf58a4c71b1ae548f1375baff",
+                "name": "Collectivit\u00e9 par d\u00e9faut"
+            },
+            "external_id": "",
+            "slug": "_a2-hobo-superuser",
+            "uuid": "e6e22e5c0ca04ac0bf3b50d88eafe6d5"
+        },
+        {
+            "description": "",
+            "service": {
+                "ou": {
+                    "slug": "default",
+                    "uuid": "69b0a02cf58a4c71b1ae548f1375baff",
+                    "name": "Collectivit\u00e9 par d\u00e9faut"
+                },
+                "slug": "passerelle"
+            },
+            "name": "Administrateur de Passerelle",
+            "attributes": [
+                {
+                    "kind": "string",
+                    "name": "is_superuser",
+                    "value": "true"
+                }
+            ],
+            "ou": {
+                "slug": "default",
+                "uuid": "69b0a02cf58a4c71b1ae548f1375baff",
+                "name": "Collectivit\u00e9 par d\u00e9faut"
+            },
+            "external_id": "",
+            "slug": "_a2-hobo-superuser",
+            "uuid": "243f58712aa248e9b27aae669341c156"
+        },
+        {
+            "description": "",
+            "service": null,
+            "name": "Administrateur du r\u00f4le \u00ab\u00a0Debug eo\u00a0\u00bb",
+            "ou": {
+                "slug": "default",
+                "uuid": "69b0a02cf58a4c71b1ae548f1375baff",
+                "name": "Collectivit\u00e9 par d\u00e9faut"
+            },
+            "permissions": [
+                {
+                    "operation": {
+                        "slug": "view"
+                    },
+                    "ou": null,
+                    "target_ct": {
+                        "model": "contenttype",
+                        "app_label": "contenttypes"
+                    },
+                    "target": {
+                        "model": "user",
+                        "app_label": "custom_user"
+                    }
+                },
+                {
+                    "operation": {
+                        "slug": "admin"
+                    },
+                    "ou": {
+                        "slug": "default",
+                        "uuid": "69b0a02cf58a4c71b1ae548f1375baff",
+                        "name": "Collectivit\u00e9 par d\u00e9faut"
+                    },
+                    "target_ct": {
+                        "model": "role",
+                        "app_label": "a2_rbac"
+                    },
+                    "target": {
+                        "name": "Debug eo",
+                        "ou": {
+                            "slug": "default",
+                            "uuid": "69b0a02cf58a4c71b1ae548f1375baff",
+                            "name": "Collectivit\u00e9 par d\u00e9faut"
+                        },
+                        "uuid": "18e7bf78dc9a432396a99f32060052ec",
+                        "service": null,
+                        "slug": "debug-eo"
+                    }
+                },
+                {
+                    "operation": {
+                        "slug": "change"
+                    },
+                    "ou": null,
+                    "target_ct": {
+                        "model": "role",
+                        "app_label": "a2_rbac"
+                    },
+                    "target": {
+                        "name": "Administrateur du r\u00f4le \u00ab\u00a0Debug eo\u00a0\u00bb",
+                        "ou": {
+                            "slug": "default",
+                            "uuid": "69b0a02cf58a4c71b1ae548f1375baff",
+                            "name": "Collectivit\u00e9 par d\u00e9faut"
+                        },
+                        "uuid": "3049444b35874b3b9a8377ad2f10b8b6",
+                        "service": null,
+                        "slug": "_a2-managers-of-role-debug-eo"
+                    }
+                }
+            ],
+            "external_id": "",
+            "slug": "_a2-managers-of-role-debug-eo",
+            "uuid": "3049444b35874b3b9a8377ad2f10b8b6"
+        },
+        {
+            "description": "",
+            "service": null,
+            "name": "Debug eo",
+            "attributes": [
+                {
+                    "kind": "json",
+                    "name": "emails_to_members",
+                    "value": "false"
+                },
+                {
+                    "kind": "json",
+                    "name": "details",
+                    "value": "\"\""
+                },
+                {
+                    "kind": "json",
+                    "name": "emails",
+                    "value": "[]"
+                }
+            ],
+            "ou": {
+                "slug": "default",
+                "uuid": "69b0a02cf58a4c71b1ae548f1375baff",
+                "name": "Collectivit\u00e9 par d\u00e9faut"
+            },
+            "external_id": "",
+            "slug": "debug-eo",
+            "uuid": "18e7bf78dc9a432396a99f32060052ec"
+        },
+        {
+            "description": "",
+            "service": null,
+            "name": "Administrateur",
+            "parents": [
+                {
+                    "name": "Administrateur des entit\u00e9s",
+                    "ou": null,
+                    "uuid": "a1ff1b3da88f47cea91e344998dfdfbf",
+                    "service": null,
+                    "slug": "_a2-administrateur-des-entites"
+                },
+                {
+                    "name": "Administrateur des r\u00f4les",
+                    "ou": null,
+                    "uuid": "8dd625b74cff40aa8531d7d72616550e",
+                    "service": null,
+                    "slug": "_a2-administrateur-des-roles"
+                },
+                {
+                    "name": "Administrateur des utilisateurs",
+                    "ou": null,
+                    "uuid": "4ab5effedc404fb1bcba4d21ee89b719",
+                    "service": null,
+                    "slug": "_a2-administrateur-des-utilisateurs"
+                }
+            ],
+            "ou": null,
+            "permissions": [
+                {
+                    "operation": {
+                        "slug": "change"
+                    },
+                    "ou": null,
+                    "target_ct": {
+                        "model": "role",
+                        "app_label": "a2_rbac"
+                    },
+                    "target": {
+                        "name": "Administrateur",
+                        "ou": null,
+                        "uuid": "81a8708382bb4e8ea12ed0e172aa48b9",
+                        "service": null,
+                        "slug": "_a2-manager"
+                    }
+                }
+            ],
+            "external_id": "",
+            "slug": "_a2-manager",
+            "uuid": "81a8708382bb4e8ea12ed0e172aa48b9"
+        },
+        {
+            "description": "",
+            "service": null,
+            "name": "Administrateur des entit\u00e9s",
+            "ou": null,
+            "permissions": [
+                {
+                    "operation": {
+                        "slug": "view"
+                    },
+                    "ou": null,
+                    "target_ct": {
+                        "model": "contenttype",
+                        "app_label": "contenttypes"
+                    },
+                    "target": {
+                        "model": "organizationalunit",
+                        "app_label": "a2_rbac"
+                    }
+                },
+                {
+                    "operation": {
+                        "slug": "admin"
+                    },
+                    "ou": null,
+                    "target_ct": {
+                        "model": "contenttype",
+                        "app_label": "contenttypes"
+                    },
+                    "target": {
+                        "model": "organizationalunit",
+                        "app_label": "a2_rbac"
+                    }
+                },
+                {
+                    "operation": {
+                        "slug": "search"
+                    },
+                    "ou": null,
+                    "target_ct": {
+                        "model": "contenttype",
+                        "app_label": "contenttypes"
+                    },
+                    "target": {
+                        "model": "organizationalunit",
+                        "app_label": "a2_rbac"
+                    }
+                }
+            ],
+            "external_id": "",
+            "slug": "_a2-administrateur-des-entites",
+            "uuid": "a1ff1b3da88f47cea91e344998dfdfbf"
+        },
+        {
+            "description": "",
+            "service": null,
+            "name": "Administrateur des r\u00f4les",
+            "ou": null,
+            "permissions": [
+                {
+                    "operation": {
+                        "slug": "view"
+                    },
+                    "ou": null,
+                    "target_ct": {
+                        "model": "contenttype",
+                        "app_label": "contenttypes"
+                    },
+                    "target": {
+                        "model": "user",
+                        "app_label": "custom_user"
+                    }
+                },
+                {
+                    "operation": {
+                        "slug": "view"
+                    },
+                    "ou": null,
+                    "target_ct": {
+                        "model": "contenttype",
+                        "app_label": "contenttypes"
+                    },
+                    "target": {
+                        "model": "organizationalunit",
+                        "app_label": "a2_rbac"
+                    }
+                },
+                {
+                    "operation": {
+                        "slug": "admin"
+                    },
+                    "ou": null,
+                    "target_ct": {
+                        "model": "contenttype",
+                        "app_label": "contenttypes"
+                    },
+                    "target": {
+                        "model": "role",
+                        "app_label": "a2_rbac"
+                    }
+                },
+                {
+                    "operation": {
+                        "slug": "search"
+                    },
+                    "ou": null,
+                    "target_ct": {
+                        "model": "contenttype",
+                        "app_label": "contenttypes"
+                    },
+                    "target": {
+                        "model": "organizationalunit",
+                        "app_label": "a2_rbac"
+                    }
+                }
+            ],
+            "external_id": "",
+            "slug": "_a2-administrateur-des-roles",
+            "uuid": "8dd625b74cff40aa8531d7d72616550e"
+        },
+        {
+            "description": "",
+            "service": null,
+            "name": "Administrateur des utilisateurs",
+            "ou": null,
+            "permissions": [
+                {
+                    "operation": {
+                        "slug": "view"
+                    },
+                    "ou": null,
+                    "target_ct": {
+                        "model": "contenttype",
+                        "app_label": "contenttypes"
+                    },
+                    "target": {
+                        "model": "organizationalunit",
+                        "app_label": "a2_rbac"
+                    }
+                },
+                {
+                    "operation": {
+                        "slug": "admin"
+                    },
+                    "ou": null,
+                    "target_ct": {
+                        "model": "contenttype",
+                        "app_label": "contenttypes"
+                    },
+                    "target": {
+                        "model": "user",
+                        "app_label": "custom_user"
+                    }
+                },
+                {
+                    "operation": {
+                        "slug": "search"
+                    },
+                    "ou": null,
+                    "target_ct": {
+                        "model": "contenttype",
+                        "app_label": "contenttypes"
+                    },
+                    "target": {
+                        "model": "organizationalunit",
+                        "app_label": "a2_rbac"
+                    }
+                }
+            ],
+            "external_id": "",
+            "slug": "_a2-administrateur-des-utilisateurs",
+            "uuid": "4ab5effedc404fb1bcba4d21ee89b719"
+        }
+    ]
+}
diff --git a/tests_authentic/test_hobo_deploy.py b/tests_authentic/test_hobo_deploy.py
index 47bb535..e9a53e6 100644
--- a/tests_authentic/test_hobo_deploy.py
+++ b/tests_authentic/test_hobo_deploy.py
@@ -6,6 +6,13 @@ import shutil
 import json
 import mock
 
+from django.core.management import call_command
+from django.db import connection
+from hobo.multitenant.middleware import TenantMiddleware
+from authentic2.data_transfer import export_site
+os.sys.path.append('%s/tests' % os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
+from utils import byteify
+
 pytestmark = pytest.mark.django_db
 
 
@@ -436,3 +443,12 @@ def test_hobo_deploy(tenant_base, mocker, skeleton_dir):
                     assert Role.objects.filter(
                         ou=provider.ou, service__isnull=True,
                         name=u'Service état-civil').count() == 1
+
+
+def test_import_template(db, tenant_base):
+    call_command('create_tenant', 'authentic.example.net')
+    tenant = TenantMiddleware.get_tenant_by_hostname('authentic.example.net')
+    connection.set_tenant(tenant)
+    call_command('import_template', '--basepath=%s' % os.path.dirname(__file__), 'data_authentic_export_site')
+    content = open('%s/data_authentic_export_site.json' % os.path.dirname(__file__)).read()
+    assert byteify(export_site()) == byteify(json.loads(content))
diff --git a/tests_passerelle/data_passerelle_export_site.json b/tests_passerelle/data_passerelle_export_site.json
new file mode 100644
index 0000000..56d9e29
--- /dev/null
+++ b/tests_passerelle/data_passerelle_export_site.json
@@ -0,0 +1,24 @@
+{
+        "apiusers": [],
+        "resources": [
+        {
+            "log_level": "INFO",
+            "description": "",
+            "title": "Foo",
+            "access_rights": [],
+            "@type": "passerelle-resource",
+            "text_value_template": "",
+            "slug": "foo",
+            "resource_type": "jsondatastore.jsondatastore"
+        },
+        {
+            "log_level": "INFO",
+            "description": ".",
+            "title": "Bar",
+            "access_rights": [],
+            "@type": "passerelle-resource",
+            "text_value_template": "{{nom|safe}}",
+            "slug": "bar",
+            "resource_type": "jsondatastore.jsondatastore"
+        }]
+    }
diff --git a/tests_passerelle/test_deploy.py b/tests_passerelle/test_deploy.py
index e8a03ec..f11dd52 100644
--- a/tests_passerelle/test_deploy.py
+++ b/tests_passerelle/test_deploy.py
@@ -1,10 +1,16 @@
 import json
+import os
 import sys
 
 from hobo.multitenant.middleware import TenantMiddleware
 from django.core.management import call_command
+from django.db import connection
 import StringIO
 
+from passerelle.utils import export_site
+os.sys.path.append('%s/tests' % os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
+from utils import byteify
+
 
 def test_deploy_specifics(db, tenant_base):
     hobo_json = {
@@ -42,3 +48,12 @@ def test_deploy_specifics(db, tenant_base):
         sys.stdin = old_stdin
 
     assert len(list(TenantMiddleware.get_tenants())) == 1
+
+
+def test_import_template(db, tenant_base):
+    call_command('create_tenant', 'passerelle.example.net')
+    tenant = TenantMiddleware.get_tenant_by_hostname('passerelle.example.net')
+    connection.set_tenant(tenant)
+    call_command('import_template', '--basepath=%s' % os.path.dirname(__file__), 'data_passerelle_export_site')
+    content = open('%s/data_passerelle_export_site.json' % os.path.dirname(__file__)).read()
+    assert export_site() == byteify(json.loads(content))
-- 
2.20.1

