Skip to content

Commit

Permalink
Merge pull request #66 from multinet-app/65-multiple-workspaces
Browse files Browse the repository at this point in the history
Return distinct workspaces from get_queryset
  • Loading branch information
jjnesbitt authored Aug 19, 2021
2 parents dd3b5c5 + a6185b5 commit b548ee1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
10 changes: 9 additions & 1 deletion multinet/api/models/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 25 additions & 1 deletion multinet/api/tests/test_workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion multinet/api/views/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit b548ee1

Please sign in to comment.