Skip to content

Commit

Permalink
Merge pull request #64 from multinet-app/rename-workspace
Browse files Browse the repository at this point in the history
Create workspace renaming endpoint
  • Loading branch information
eagw authored Aug 20, 2021
2 parents 462af1b + 947c559 commit 686a330
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
45 changes: 45 additions & 0 deletions multinet/api/tests/test_workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,51 @@ def test_workspace_rest_create(authenticated_api_client: APIClient):
Workspace.objects.get(name=workspace_name)


@pytest.mark.django_db
@pytest.mark.parametrize(
'permission,is_owner,status_code,success',
[
(None, False, 404, False),
(WorkspaceRoleChoice.READER, False, 403, False),
(WorkspaceRoleChoice.WRITER, False, 403, False),
(WorkspaceRoleChoice.MAINTAINER, False, 200, True),
(None, True, 200, True),
],
)
def test_workspace_rest_rename(
workspace: Workspace,
user: User,
authenticated_api_client: APIClient,
permission: WorkspaceRoleChoice,
is_owner: bool,
status_code: int,
success: bool,
):
if permission is not None:
workspace.set_user_permission(user, permission)
elif is_owner:
workspace.set_owner(user)

old_name = workspace.name
new_name = Faker().pystr()

r = authenticated_api_client.put(
f'/api/workspaces/{workspace.name}/',
{
'name': new_name,
},
format='json',
)
assert r.status_code == status_code

# Retrieve workspace to ensure it's up to date
workspace = Workspace.objects.get(id=workspace.pk)

# Assert name is as expected
expected_name = new_name if success else old_name
assert workspace.name == expected_name


@pytest.mark.django_db
@pytest.mark.parametrize(
'permission,is_owner,status_code,success',
Expand Down
8 changes: 8 additions & 0 deletions multinet/api/views/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ class Meta:
read_only_fields = ['created']


class WorkspaceRenameSerializer(serializers.ModelSerializer):
class Meta:
model = Workspace
fields = [
'name',
]


class WorkspaceSerializer(serializers.ModelSerializer):
class Meta:
model = Workspace
Expand Down
16 changes: 16 additions & 0 deletions multinet/api/views/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
PermissionsCreateSerializer,
PermissionsReturnSerializer,
WorkspaceCreateSerializer,
WorkspaceRenameSerializer,
WorkspaceSerializer,
)
from multinet.auth.decorators import require_workspace_ownership, require_workspace_permission
Expand Down Expand Up @@ -72,6 +73,21 @@ def create(self, request):
workspace.save()
return Response(WorkspaceSerializer(workspace).data, status=status.HTTP_200_OK)

@swagger_auto_schema(
request_body=WorkspaceRenameSerializer(),
responses={200: WorkspaceSerializer()},
)
@require_workspace_permission(WorkspaceRoleChoice.MAINTAINER)
def update(self, request, name):
workspace: Workspace = get_object_or_404(Workspace, name=name)
serializer = WorkspaceRenameSerializer(data=request.data)
serializer.is_valid(raise_exception=True)

workspace.name = serializer.validated_data['name']
workspace.save()

return Response(WorkspaceSerializer(workspace).data, status=status.HTTP_200_OK)

@require_workspace_ownership
def destroy(self, request, name):
workspace: Workspace = get_object_or_404(Workspace, name=name)
Expand Down

0 comments on commit 686a330

Please sign in to comment.