Skip to content

Commit

Permalink
feat: added management command for simple use of create_public_tenant (
Browse files Browse the repository at this point in the history
  • Loading branch information
jgentil authored May 6, 2024
1 parent e285adb commit 2194126
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions docs/pages/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@ which takes care of this for you.
create_public_tenant(domain_url="public.domain.com", owner_email="[email protected]")
Or, alternatively, use the management command:

.. code-block:: bash
manage.py create_public_tenant --domain_url public.domain.com --owner_email [email protected]
Fin!
----

Expand Down
Empty file.
Empty file.
29 changes: 29 additions & 0 deletions tenant_users/tenants/management/commands/create_public_tenant.py
Original file line number Diff line number Diff line change
@@ -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 ({domain_url}) and Owner ({owner_email})"
)
)
except (ExistsError, SchemaError) as e:
self.stdout.write(
self.style.ERROR(
f"Error creating public tenant: {e}"
)
)
41 changes: 41 additions & 0 deletions tests/test_tenants/test_commands.py
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]"
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

0 comments on commit 2194126

Please sign in to comment.