-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added management command for simple use of create_public_tenant (…
- Loading branch information
Showing
6 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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! | ||
---- | ||
|
||
|
Empty file.
Empty file.
29 changes: 29 additions & 0 deletions
29
tenant_users/tenants/management/commands/create_public_tenant.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |