Skip to content

Commit

Permalink
Merge pull request #111 from neuroforgede/wip/tenant-api#main
Browse files Browse the repository at this point in the history
improve validate_user function to properly validate for already exist…
  • Loading branch information
s4ke authored Mar 30, 2024
2 parents 659de7e + 4284e43 commit e6893e1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
10 changes: 9 additions & 1 deletion skipper/skipper/core/components/tenant/tenant_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,16 @@ def to_representation(self, obj: Any) -> Any:
return repr

def validate_user(self, value: User) -> User:
if Tenant_User.objects.filter(user=value).exists():
test_qs = Tenant_User.objects.filter(user=value)
if 'pk' in self.context['view'].kwargs:
test_qs = test_qs.exclude(id=self.context['view'].kwargs['pk'])
if test_qs.exists():
raise ValidationError("User is already assigned to a tenant")

if 'pk' in self.context['view'].kwargs:
tenant_user: Tenant_User = get_object_or_404(Tenant_User.objects.filter(id=self.context['view'].kwargs['pk']))
if tenant_user.user.username != value.username:
raise ValidationError("Changing User is not allowed for a Tenant_User object. Instead, simply delete the assignment.")
return value

def create(self, validated_data: Any) -> Any:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def base_add_bare_user(self) -> None:
)


class CoreTenantUserListPermissionGETTest(CoreTenantUserDetailPermissionBaseTest):
class CoreTenantUserDetailPermissionGETTest(CoreTenantUserDetailPermissionBaseTest):

def permission_code_name(self) -> str:
return core_permission_for_rest_method('tenant', 'GET', 'tenant-user')
Expand All @@ -89,7 +89,7 @@ def method_under_test_proper(self) -> Union[HttpResponse, TestHttpResponse]:
))


class CoreTenantUserListPermissionHEADTest(CoreTenantUserDetailPermissionBaseTest):
class CoreTenantUserDetailPermissionHEADTest(CoreTenantUserDetailPermissionBaseTest):

def permission_code_name(self) -> str:
return core_permission_for_rest_method('tenant', 'HEAD', 'tenant-user')
Expand All @@ -110,7 +110,7 @@ def method_under_test_proper(self) -> Union[HttpResponse, TestHttpResponse]:
)


class CoreTenantUserListPermissionPOSTTest(CoreTenantUserDetailPermissionBaseTest):
class CoreTenantUserDetailPermissionPOSTTest(CoreTenantUserDetailPermissionBaseTest):

def permission_code_name(self) -> str:
return core_permission_for_rest_method('tenant', 'POST', 'tenant-user')
Expand All @@ -136,7 +136,7 @@ def method_under_test_proper(self) -> Union[HttpResponse, TestHttpResponse]:
)


class CoreTenantUserListPermissionOPTIONSTest(CoreTenantUserDetailPermissionBaseTest):
class CoreTenantUserDetailPermissionOPTIONSTest(CoreTenantUserDetailPermissionBaseTest):

def permission_code_name(self) -> str:
return core_permission_for_rest_method('tenant', 'OPTIONS', 'tenant-user')
Expand All @@ -156,7 +156,7 @@ def method_under_test_proper(self) -> Union[HttpResponse, TestHttpResponse]:
)


class CoreTenantUserListPermissionPUTTest(CoreTenantUserDetailPermissionBaseTest):
class CoreTenantUserDetailPermissionPUTTest(CoreTenantUserDetailPermissionBaseTest):

def permission_code_name(self) -> str:
return core_permission_for_rest_method('tenant', 'PUT', 'tenant-user')
Expand All @@ -182,7 +182,7 @@ def method_under_test_proper(self) -> Union[HttpResponse, TestHttpResponse]:
)


class CoreTenantUserListPermissionPATCHTest(CoreTenantUserDetailPermissionBaseTest):
class CoreTenantUserDetailPermissionPATCHTest(CoreTenantUserDetailPermissionBaseTest):

def permission_code_name(self) -> str:
return core_permission_for_rest_method('tenant', 'PATCH', 'tenant-user')
Expand All @@ -208,7 +208,7 @@ def method_under_test_proper(self) -> Union[HttpResponse, TestHttpResponse]:
)


class CoreTenantUserListPermissionDELETETest(CoreTenantUserDetailPermissionBaseTest):
class CoreTenantUserDetailPermissionDELETETest(CoreTenantUserDetailPermissionBaseTest):

def permission_code_name(self) -> str:
return core_permission_for_rest_method('tenant', 'DELETE', 'tenant-user')
Expand Down

0 comments on commit e6893e1

Please sign in to comment.