diff --git a/multinet/api/models/workspace.py b/multinet/api/models/workspace.py index 4174f73..7634aeb 100644 --- a/multinet/api/models/workspace.py +++ b/multinet/api/models/workspace.py @@ -108,19 +108,27 @@ def set_owner(self, new_owner): self.save() def set_user_permissions_bulk( - self, readers: List[User], writers: List[User], maintainers: List[User] + self, + readers: Optional[List[User]] = None, + writers: Optional[List[User]] = None, + maintainers: Optional[List[User]] = None, ): """Replace all existing permissions on this workspace.""" WorkspaceRole.objects.filter(workspace=self).delete() + readers = readers or [] new_reader_roles = [ WorkspaceRole(workspace=self, user=user, role=WorkspaceRoleChoice.READER) for user in readers ] + + writers = writers or [] new_writer_roles = [ WorkspaceRole(workspace=self, user=user, role=WorkspaceRoleChoice.WRITER) for user in writers ] + + maintainers = maintainers or [] new_maintainer_roles = [ WorkspaceRole(workspace=self, user=user, role=WorkspaceRoleChoice.MAINTAINER) for user in maintainers diff --git a/multinet/api/tests/test_workspace.py b/multinet/api/tests/test_workspace.py index fff7200..b352dc3 100644 --- a/multinet/api/tests/test_workspace.py +++ b/multinet/api/tests/test_workspace.py @@ -14,7 +14,7 @@ from multinet.api.tests.utils import create_users_with_permissions from multinet.api.utils.arango import arango_system_db -from .fuzzy import TIMESTAMP_RE +from .fuzzy import TIMESTAMP_RE, workspace_re @pytest.mark.django_db @@ -53,6 +53,30 @@ def test_workspace_rest_list( assert sysdb.has_database(workspace['arango_db_name']) +@pytest.mark.django_db +def test_workspace_rest_list_no_duplicates( + workspace: Workspace, + user_factory: UserFactory, + user: User, + authenticated_api_client: APIClient, +): + """Test that multiple roles on a workspace results in no duplicates.""" + # Set authenticated user as owner + workspace.set_owner(user) + + # Give multiple users permissions on the workspace + workspace.set_user_permissions_bulk(readers=[user_factory() for _ in range(5)]) + + # Test that there's only one copy of this workspace returned + r = authenticated_api_client.get('/api/workspaces/') + assert r.json() == { + 'count': 1, + 'next': None, + 'previous': None, + 'results': [workspace_re(workspace)], + } + + @pytest.mark.django_db def test_workspace_rest_create(authenticated_api_client: APIClient): fake = Faker() diff --git a/multinet/api/views/workspace.py b/multinet/api/views/workspace.py index 18a2c88..b7e9136 100644 --- a/multinet/api/views/workspace.py +++ b/multinet/api/views/workspace.py @@ -52,7 +52,7 @@ def get_queryset(self): public_workspaces = Q(public=True) return self.queryset.filter( public_workspaces | readable_private_workspaces | owned_workspaces - ) + ).distinct() @swagger_auto_schema( request_body=WorkspaceCreateSerializer(),