From 12b99ae0c4b9215e1291464739f0cab51ed9f176 Mon Sep 17 00:00:00 2001 From: Jon-Pierre Gentil Date: Wed, 1 May 2024 18:00:28 -0500 Subject: [PATCH 1/3] feature: added management command for simple use of create_public_tenant --- tenant_users/tenants/management/__init__.py | 0 .../tenants/management/commands/__init__.py | 0 .../commands/create_public_tenant.py | 29 +++++++++++++ tests/test_tenants/test_commands.py | 41 +++++++++++++++++++ 4 files changed, 70 insertions(+) create mode 100644 tenant_users/tenants/management/__init__.py create mode 100644 tenant_users/tenants/management/commands/__init__.py create mode 100644 tenant_users/tenants/management/commands/create_public_tenant.py create mode 100644 tests/test_tenants/test_commands.py diff --git a/tenant_users/tenants/management/__init__.py b/tenant_users/tenants/management/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tenant_users/tenants/management/commands/__init__.py b/tenant_users/tenants/management/commands/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tenant_users/tenants/management/commands/create_public_tenant.py b/tenant_users/tenants/management/commands/create_public_tenant.py new file mode 100644 index 00000000..4da4de1a --- /dev/null +++ b/tenant_users/tenants/management/commands/create_public_tenant.py @@ -0,0 +1,29 @@ +from django.core.management.base import BaseCommand + +from tenant_users.tenants.models import ExistsError, SchemaError +from tenant_users.tenants.utils import create_public_tenant + + +class Command(BaseCommand): + help = "Creates the initial public tenant" + + def add_arguments(self, parser): + parser.add_argument("--domain_url", nargs=1, required=True, type=str, + help="The URL for the public tenant's domain.") + parser.add_argument("--owner_email", nargs=1, required=True, type=str, + help="Email address of the owner user.") + + def handle(self, domain_url: str, owner_email: str, **kwargs): # noqa: ARG002, kwargs must be here. + try: + create_public_tenant(domain_url=domain_url, owner_email=owner_email) + self.stdout.write( + self.style.SUCCESS( + f"Successfully created public tenant with {domain_url=} {owner_email=}" + ) + ) + except (ExistsError, SchemaError) as e: + self.stdout.write( + self.style.ERROR( + f"Error creating public tenant: {e}" + ) + ) diff --git a/tests/test_tenants/test_commands.py b/tests/test_tenants/test_commands.py new file mode 100644 index 00000000..78c16bf2 --- /dev/null +++ b/tests/test_tenants/test_commands.py @@ -0,0 +1,41 @@ +from __future__ import annotations + +from io import StringIO +from unittest.mock import patch + +from django.core.management import call_command + +from tenant_users.tenants.models import ExistsError + +DOMAIN_URL = "example.net" +OWNER_EMAIL = "example@example.net" +ERROR_MSG = "Schema Exists" + + +def test_create_public_tenant_command_success(): + out = StringIO() + + with patch("tenant_users.tenants.management.commands.create_public_tenant.create_public_tenant" + ) as mocked_cpt: + call_command("create_public_tenant", stdout=out, domain_url=DOMAIN_URL, + owner_email=OWNER_EMAIL) + + mocked_cpt.assert_called_once_with(domain_url=DOMAIN_URL, owner_email=OWNER_EMAIL) + out_value = out.getvalue() + assert DOMAIN_URL in out_value + assert OWNER_EMAIL in out_value + assert "Successfully created public tenant" in out_value + + +def test_create_public_tenant_command_failure(): + out = StringIO() + + with patch("tenant_users.tenants.management.commands.create_public_tenant.create_public_tenant", + side_effect=ExistsError(ERROR_MSG)) as mocked_cpt: + call_command("create_public_tenant", stdout=out, domain_url=DOMAIN_URL, + owner_email=OWNER_EMAIL) + + mocked_cpt.assert_called_once_with(domain_url=DOMAIN_URL, owner_email=OWNER_EMAIL) + out_value = out.getvalue() + assert ERROR_MSG in out_value + assert "Error creating public tenant" in out_value From 651548e734aa073b6916a3812bd88d7d8ee2a02f Mon Sep 17 00:00:00 2001 From: Jon-Pierre Gentil Date: Wed, 1 May 2024 18:07:59 -0500 Subject: [PATCH 2/3] updated documentation and changelog --- CHANGELOG.md | 6 ++++++ docs/pages/installation.rst | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ebc2039b..2dd62748 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ We follow [Semantic Versions](https://semver.org/) starting at the `0.4.0` release. +## 1.3.1 (2024-05-01) + +### Features + +- Added management command for calling create_public_tenant during initial setup + ## 1.3.0 (2023-11-14) ### Features diff --git a/docs/pages/installation.rst b/docs/pages/installation.rst index 2a6b7baa..52e54073 100644 --- a/docs/pages/installation.rst +++ b/docs/pages/installation.rst @@ -160,6 +160,13 @@ which takes care of this for you. create_public_tenant(domain_url="public.domain.com", owner_email="admin@domain.com") +Or, alternatively, use the management command: + +.. code-block:: bash + + manage.py create_public_tenant --domain_url public.domain.com --owner_email admin@domain.com + + Fin! ---- From faa67694119547fd8adbb4df4ba05578719bbc7d Mon Sep 17 00:00:00 2001 From: Jon-Pierre Gentil Date: Mon, 6 May 2024 11:32:33 -0500 Subject: [PATCH 3/3] fixed f-string to be Python 3.7 compatible --- .../tenants/management/commands/create_public_tenant.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tenant_users/tenants/management/commands/create_public_tenant.py b/tenant_users/tenants/management/commands/create_public_tenant.py index 4da4de1a..3937f8cf 100644 --- a/tenant_users/tenants/management/commands/create_public_tenant.py +++ b/tenant_users/tenants/management/commands/create_public_tenant.py @@ -18,7 +18,7 @@ def handle(self, domain_url: str, owner_email: str, **kwargs): # noqa: ARG002, create_public_tenant(domain_url=domain_url, owner_email=owner_email) self.stdout.write( self.style.SUCCESS( - f"Successfully created public tenant with {domain_url=} {owner_email=}" + f"Successfully created public tenant with Domain URL ({domain_url}) and Owner ({owner_email})" ) ) except (ExistsError, SchemaError) as e: