-
Notifications
You must be signed in to change notification settings - Fork 5.2k
feat(api): add endpoint to list current user's workspaces #9503
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Syed-Ali-Abbas-Zaidi
wants to merge
2
commits into
makeplane:preview
Choose a base branch
from
Syed-Ali-Abbas-Zaidi:feat/9427-list-user-workspaces
base: preview
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
apps/api/plane/tests/contract/api/test_user_workspaces.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| # Copyright (c) 2023-present Plane Software, Inc. and contributors | ||
| # SPDX-License-Identifier: AGPL-3.0-only | ||
| # See the LICENSE file for details. | ||
|
|
||
| """Contract tests for the current user's workspaces endpoint. | ||
|
|
||
| GET /api/v1/users/me/workspaces/ | ||
| """ | ||
|
|
||
| import pytest | ||
| from django.utils import timezone | ||
| from rest_framework import status | ||
|
|
||
| from plane.db.models import Workspace, WorkspaceMember | ||
|
|
||
| URL = "/api/v1/users/me/workspaces/" | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def other_workspace(db, create_bot_user): | ||
| """A workspace the requesting user is not a member of.""" | ||
| return Workspace.objects.create( | ||
| name="Other Workspace", | ||
| owner=create_bot_user, | ||
| slug="other-workspace", | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def inactive_workspace(db, create_user, create_bot_user): | ||
| """A workspace the requesting user's membership has been deactivated on.""" | ||
| workspace = Workspace.objects.create( | ||
| name="Inactive Workspace", | ||
| owner=create_bot_user, | ||
| slug="inactive-workspace", | ||
| ) | ||
| WorkspaceMember.objects.create( | ||
| workspace=workspace, | ||
| member=create_user, | ||
| role=20, | ||
| is_active=False, | ||
| ) | ||
| return workspace | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def deleted_workspace(db, create_user, create_bot_user): | ||
| """A soft deleted workspace the requesting user still has an active membership row on.""" | ||
| workspace = Workspace.objects.create( | ||
| name="Deleted Workspace", | ||
| owner=create_bot_user, | ||
| slug="deleted-workspace", | ||
| ) | ||
| WorkspaceMember.objects.create( | ||
| workspace=workspace, | ||
| member=create_user, | ||
| role=20, | ||
| is_active=True, | ||
| ) | ||
| # Stamp deleted_at directly instead of calling delete(), which queues a Celery | ||
| # task to soft delete related objects. | ||
| Workspace.all_objects.filter(id=workspace.id).update(deleted_at=timezone.now()) | ||
| return workspace | ||
|
|
||
|
|
||
| @pytest.mark.contract | ||
| class TestUserWorkspaces: | ||
| @pytest.mark.django_db | ||
| def test_returns_workspaces_of_the_current_user(self, api_key_client, workspace): | ||
| response = api_key_client.get(URL) | ||
| assert response.status_code == status.HTTP_200_OK | ||
| slugs = {item["slug"] for item in response.data} | ||
| assert workspace.slug in slugs | ||
|
|
||
| @pytest.mark.django_db | ||
| def test_returns_only_lite_fields(self, api_key_client, workspace): | ||
| response = api_key_client.get(URL) | ||
| assert response.status_code == status.HTTP_200_OK | ||
| item = response.data[0] | ||
| assert set(item.keys()) == {"id", "name", "slug"} | ||
|
|
||
| @pytest.mark.django_db | ||
| def test_excludes_workspaces_the_user_is_not_a_member_of(self, api_key_client, workspace, other_workspace): | ||
| response = api_key_client.get(URL) | ||
| assert response.status_code == status.HTTP_200_OK | ||
| slugs = {item["slug"] for item in response.data} | ||
| assert other_workspace.slug not in slugs | ||
|
|
||
| @pytest.mark.django_db | ||
| def test_excludes_inactive_memberships(self, api_key_client, workspace, inactive_workspace): | ||
| response = api_key_client.get(URL) | ||
| assert response.status_code == status.HTTP_200_OK | ||
| slugs = {item["slug"] for item in response.data} | ||
| assert inactive_workspace.slug not in slugs | ||
|
|
||
| @pytest.mark.django_db | ||
| def test_excludes_soft_deleted_workspaces(self, api_key_client, workspace, deleted_workspace): | ||
| response = api_key_client.get(URL) | ||
| assert response.status_code == status.HTTP_200_OK | ||
| slugs = {item["slug"] for item in response.data} | ||
| assert deleted_workspace.slug not in slugs | ||
|
|
||
| @pytest.mark.django_db | ||
| def test_requires_authentication(self, api_client, workspace): | ||
| response = api_client.get(URL) | ||
| assert response.status_code in ( | ||
| status.HTTP_401_UNAUTHORIZED, | ||
| status.HTTP_403_FORBIDDEN, | ||
| ) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.