Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: added management command for simple use of create_public_tenant #565

Merged
merged 4 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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