-
Notifications
You must be signed in to change notification settings - Fork 403
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add list user workspaces endpoint (#3308)
# Description This PR adds a new endpoint to API v1 to list the workspaces to which a user belongs. Closes #3273 **Type of change** - [x] New feature (non-breaking change which adds functionality) **How Has This Been Tested** Manually and unit tests have been added to test this new endpoint. **Checklist** - [ ] I added relevant documentation - [x] follows the style guidelines of this project - [x] I did a self-review of my code - [ ] I made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works - [ ] I filled out [the contributor form](https://tally.so/r/n9XrxK) (see text above) - [x] I have added relevant notes to the CHANGELOG.md file (See https://keepachangelog.com/) --------- Co-authored-by: Alvaro Bartolome <alvaro@argilla.io>
- Loading branch information
1 parent
60c6258
commit 1594037
Showing
7 changed files
with
125 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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,39 @@ | ||
# Copyright 2021-present, the Recognai S.L. team. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from uuid import UUID | ||
|
||
from fastapi import APIRouter, Depends, Security | ||
from sqlalchemy.ext.asyncio import AsyncSession | ||
|
||
from argilla.server.contexts import accounts | ||
from argilla.server.database import get_async_db | ||
from argilla.server.models import User | ||
from argilla.server.policies import UserPolicyV1, authorize | ||
from argilla.server.schemas.v1.workspaces import Workspaces | ||
from argilla.server.security import auth | ||
|
||
router = APIRouter(tags=["users"]) | ||
|
||
|
||
@router.get("/users/{user_id}/workspaces", response_model=Workspaces) | ||
async def list_user_workspaces( | ||
*, | ||
db: AsyncSession = Depends(get_async_db), | ||
user_id: UUID, | ||
current_user: User = Security(auth.get_current_user), | ||
): | ||
await authorize(current_user, UserPolicyV1.list_workspaces) | ||
workspaces = await accounts.list_workspaces_by_user_id(db, user_id) | ||
return Workspaces(items=workspaces) |
This file contains 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 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 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 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 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,58 @@ | ||
# Copyright 2021-present, the Recognai S.L. team. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
import pytest | ||
from argilla._constants import API_KEY_HEADER_NAME | ||
from argilla.server.models import UserRole | ||
|
||
from tests.factories import UserFactory, WorkspaceFactory | ||
|
||
if TYPE_CHECKING: | ||
from fastapi.testclient import TestClient | ||
|
||
|
||
@pytest.mark.asyncio | ||
class TestsUsersV1Endpoints: | ||
async def test_list_user_workspaces(self, client: "TestClient", owner_auth_header: dict): | ||
workspaces = await WorkspaceFactory.create_batch(3) | ||
user = await UserFactory.create(workspaces=workspaces) | ||
|
||
response = client.get(f"/api/v1/users/{user.id}/workspaces", headers=owner_auth_header) | ||
|
||
assert response.status_code == 200 | ||
assert response.json() == { | ||
"items": [ | ||
{ | ||
"id": str(workspace.id), | ||
"name": workspace.name, | ||
"inserted_at": workspace.inserted_at.isoformat(), | ||
"updated_at": workspace.updated_at.isoformat(), | ||
} | ||
for workspace in workspaces | ||
] | ||
} | ||
|
||
@pytest.mark.parametrize("role", [UserRole.annotator, UserRole.admin]) | ||
async def test_list_user_workspaces_as_restricted_user(self, client: "TestClient", role: UserRole): | ||
workspaces = await WorkspaceFactory.create_batch(3) | ||
user = await UserFactory.create(workspaces=workspaces) | ||
requesting_user = await UserFactory.create(role=role) | ||
|
||
response = client.get( | ||
f"/api/v1/users/{user.id}/workspaces", headers={API_KEY_HEADER_NAME: requesting_user.api_key} | ||
) | ||
|
||
assert response.status_code == 403 |