From f4b872827e32fcd54e3d9f6192b08c395d92d22c Mon Sep 17 00:00:00 2001 From: Jed Giblin Date: Tue, 19 Nov 2024 15:48:44 -0500 Subject: [PATCH] added support for primary keys where the field id is missing --- django_test_app/users/models.py | 6 +++++- tenant_users/tenants/models.py | 2 +- tests/fixtures/tenant.py | 7 ++++++ tests/test_tenants/test_tenants_models.py | 26 +++++++++++++++++------ 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/django_test_app/users/models.py b/django_test_app/users/models.py index 63ee365d..be802fb5 100644 --- a/django_test_app/users/models.py +++ b/django_test_app/users/models.py @@ -1,10 +1,14 @@ +import uuid from django.db import models - from tenant_users.tenants.models import UserProfile _NameFieldLength = 64 +class GuidUser(UserProfile): + guid = models.UUIDField(default=uuid.uuid4, primary_key=True) + + class TenantUser(UserProfile): """Simple user model definition for testing.""" diff --git a/tenant_users/tenants/models.py b/tenant_users/tenants/models.py index 94427e42..34a22ad1 100644 --- a/tenant_users/tenants/models.py +++ b/tenant_users/tenants/models.py @@ -107,7 +107,7 @@ def add_user(self, user_obj, *, is_superuser: bool = False, is_staff: bool = Fal is_staff (bool): If True, assigns staff status to the user. Defaults to False. """ # User already is linked here.. - if self.user_set.filter(id=user_obj.pk).exists(): + if self.user_set.filter(pk=user_obj.pk).exists(): raise ExistsError( f"User already added to tenant: {user_obj}", ) diff --git a/tests/fixtures/tenant.py b/tests/fixtures/tenant.py index 42577966..f9342451 100644 --- a/tests/fixtures/tenant.py +++ b/tests/fixtures/tenant.py @@ -1,6 +1,7 @@ import pytest from django.contrib.auth import get_user_model from django_tenants.utils import get_tenant_model, schema_context +from django_test_app.users.models import GuidUser #: Constants TenantModel = get_tenant_model() @@ -8,6 +9,12 @@ _USER_PASS = "test1234" # noqa: S105 +@pytest.fixture() +def guid_tenant_user(db) -> GuidUser: + with schema_context("public"): + return TenantUser.objects.create_user(email="guid-user@test.com") + + @pytest.fixture() def public_tenant(db) -> TenantModel: # noqa: ARG001 """Returns Public Tenant instance.""" diff --git a/tests/test_tenants/test_tenants_models.py b/tests/test_tenants/test_tenants_models.py index da6b3b4d..c7340471 100644 --- a/tests/test_tenants/test_tenants_models.py +++ b/tests/test_tenants/test_tenants_models.py @@ -20,6 +20,21 @@ UserModel = get_user_model() +def test_add_guid_based_user_to_tenant(guid_tenant_user) -> None: + """Test tenant user management with guid based user""" + slug = "sample" + custom_schema_name = "guid_schema" + tenant, _ = provision_tenant( + tenant_name="GUID Tenant", + tenant_slug=slug, + owner=guid_tenant_user, + schema_name=custom_schema_name + ) + owner = tenant.owner + tenant.add_user(owner) + assert tenant.user_set.count() == 1 + + def test_provision_with_schema_name(tenant_user) -> None: """Test tenant provisioning with a custom schema name. @@ -37,7 +52,7 @@ def test_provision_with_schema_name(tenant_user) -> None: owner = tenant.owner with pytest.raises( - ExistsError, match="User already added to tenant: tenant-user@test.com" + ExistsError, match="User already added to tenant: tenant-user@test.com" ): tenant.add_user(owner) error_message = f"Cannot remove owner from tenant: {owner}" @@ -77,7 +92,6 @@ def test_deleting_a_provision_tenant(tenant_user) -> None: assert public_tenant.owner == tenant.owner assert tenant.user_set.count() == 1 with tenant_context(tenant): - # Verify that the public owner retains permission to the provisioned tenant after deletion public_owner_has_permission = UserTenantPermissions.objects.filter( profile=tenant.owner @@ -91,7 +105,7 @@ def test_deleting_a_provision_tenant(tenant_user) -> None: profile=another_user ).exists() assert ( - previous_owner_has_permission == False + previous_owner_has_permission == False ), "The previous owner should not retain permission to the provisioned tenant after deletion." @@ -134,11 +148,9 @@ def test_delete_provisioned_tenant_with_assigned_user_roles(tenant_user): def test_transfer_ownership_to_existing_user(test_tenants, public_tenant): - tenant = test_tenants.first() assert tenant.owner != public_tenant.owner, "Can't transfer ownership to same owner" with tenant_context(tenant): - tenant.add_user(public_tenant.owner) # Assign group or role to user through UserTenantPermissions tenant.transfer_ownership(public_tenant.owner) @@ -148,6 +160,6 @@ def test_transfer_ownership_to_existing_user(test_tenants, public_tenant): profile=public_tenant.owner ).exists() assert ( - UserTenantPermissions.objects.get(profile=public_tenant.owner).is_superuser - == True + UserTenantPermissions.objects.get(profile=public_tenant.owner).is_superuser + == True )