Skip to content

Commit

Permalink
feat(tenants): allow verbosity to be set when creating public tenant
Browse files Browse the repository at this point in the history
Add a new 'verbosity' parameter to the create_public_tenant function.
  • Loading branch information
Dresdn committed Sep 10, 2024
1 parent c03f250 commit 760e9bd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
5 changes: 4 additions & 1 deletion tenant_users/tenants/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def create_public_tenant(
is_superuser: bool = False,
is_staff: bool = False,
tenant_extra_data: Optional[dict] = None,
verbosity=1,
**owner_extra,
):
"""Creates a public tenant and assigns an owner user.
Expand Down Expand Up @@ -88,13 +89,15 @@ def create_public_tenant(
{get_multi_type_database_field_name(): public_schema_name}
)

public_tenant = TenantModel.objects.create(
public_tenant = TenantModel(
schema_name=public_schema_name,
name="Public Tenant",
owner=profile,
**tenant_extra_data,
)

public_tenant.save(verbosity=verbosity)

# Add one or more domains for the tenant
domain = get_tenant_domain_model().objects.create(
domain=domain_url,
Expand Down
35 changes: 30 additions & 5 deletions tests/test_tenants/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from unittest.mock import patch
from unittest.mock import Mock, patch

import pytest
from django.conf import settings
Expand Down Expand Up @@ -63,21 +63,25 @@ def test_create_public_tenant_with_specified_password():
assert user.check_password(secret)


@patch("django_test_app.companies.models.Company.objects.create")
@patch("tenant_users.tenants.utils.get_tenant_model")
@pytest.mark.usefixtures("_tenant_type_settings")
@pytest.mark.django_db()
@pytest.mark.no_db_setup()
def test_tenant_public_tenant_with_multitype(mock_create):
def test_tenant_public_tenant_with_multitype(mock_get_tenant_model):
"""Tests that multi-type information is used during the Public Tenant creation."""
mock_tenant_model = Mock()
mock_tenant_model.objects.filter.return_value.first.return_value = None
mock_get_tenant_model.return_value = mock_tenant_model

# Since we're mocking, we expect an exception to be thrown after Tenant.create()
with pytest.raises(ValueError, match='must be a "Company" instance'):
utils.create_public_tenant("domain.test", "[email protected]")

# Check the mock was called
assert mock_create.called
assert mock_tenant_model.called

# Get the arguments it was called with
_, kwargs = mock_create.call_args
_, kwargs = mock_tenant_model.call_args

# Ensure the multi-type database field was added during Tenant creation
assert kwargs.get(settings.MULTI_TYPE_DATABASE_FIELD) == get_public_schema_name()
Expand Down Expand Up @@ -116,3 +120,24 @@ def test_create_public_tenant_with_tenant_extras():
# Test deleting tenant
with pytest.raises(ValueError, match="Cannot delete public tenant schema"):
public_tenant.delete_tenant()


@patch("tenant_users.tenants.utils.get_tenant_model")
@pytest.mark.django_db()
@pytest.mark.no_db_setup()
def test_tenant_public_tenant_save_verbosity(mock_get_tenant_model):
"""Tests that the verbosity parameter is correctly passed to save()."""
mock_tenant_model = Mock()
mock_tenant_model.objects.filter.return_value.first.return_value = None
mock_get_tenant_model.return_value = mock_tenant_model

# Since we're mocking, we expect an exception to be thrown after Tenant.create()
with pytest.raises(ValueError, match='must be a "Company" instance'):
utils.create_public_tenant("domain.test", "[email protected]", verbosity=3)

# Check the mock was called
assert mock_tenant_model.called

# Check that save() was called with the correct verbosity
mock_tenant_instance = mock_tenant_model.return_value
mock_tenant_instance.save.assert_called_once_with(verbosity=3)

0 comments on commit 760e9bd

Please sign in to comment.