Skip to content
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

Raise EntityAlreadyExistsError for new endpoint handlers #2556

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/argilla/server/apis/v0/handlers/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from argilla.server.commons import telemetry
from argilla.server.contexts import accounts
from argilla.server.database import get_db
from argilla.server.errors import EntityNotFoundError
from argilla.server.errors import EntityAlreadyExistsError, EntityNotFoundError
from argilla.server.policies import UserPolicy, authorize
from argilla.server.security import auth
from argilla.server.security.model import User, UserCreate
Expand Down Expand Up @@ -89,6 +89,9 @@ def create_user(
):
authorize(current_user, UserPolicy.create)

if accounts.get_user_by_username(db, user_create.username):
raise EntityAlreadyExistsError(name=user_create.username, type=User)

user = accounts.create_user(db, user_create)

return User.from_orm(user)
Expand Down
8 changes: 7 additions & 1 deletion src/argilla/server/apis/v0/handlers/workspaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from argilla.server.contexts import accounts
from argilla.server.database import get_db
from argilla.server.errors import EntityNotFoundError
from argilla.server.errors import EntityAlreadyExistsError, EntityNotFoundError
from argilla.server.policies import WorkspacePolicy, WorkspaceUserPolicy, authorize
from argilla.server.security import auth
from argilla.server.security.model import (
Expand Down Expand Up @@ -52,6 +52,9 @@ def create_workspace(
):
authorize(current_user, WorkspacePolicy.create)

if accounts.get_workspace_by_name(db, workspace_create.name):
raise EntityAlreadyExistsError(name=workspace_create.name, type=Workspace)

workspace = accounts.create_workspace(db, workspace_create)

return Workspace.from_orm(workspace)
Expand Down Expand Up @@ -106,6 +109,9 @@ def create_workspace_user(
if not user:
raise EntityNotFoundError(name=str(user_id), type=User)

if accounts.get_workspace_user_by_workspace_id_and_user_id(db, workspace_id, user_id):
raise EntityAlreadyExistsError(name=str(user_id), type=User)

workspace_user = accounts.create_workspace_user(db, WorkspaceUserCreate(workspace_id=workspace_id, user_id=user_id))

return User.from_orm(workspace_user.user)
Expand Down
2 changes: 1 addition & 1 deletion src/argilla/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ def configure_middleware(app: FastAPI):

def configure_api_exceptions(api: FastAPI):
"""Configures fastapi exception handlers"""
api.exception_handler(EntityNotFoundError)(APIErrorHandler.common_exception_handler)
api.exception_handler(Exception)(APIErrorHandler.common_exception_handler)
api.exception_handler(EntityNotFoundError)(APIErrorHandler.common_exception_handler)
api.exception_handler(UnauthorizedError)(APIErrorHandler.common_exception_handler)
api.exception_handler(ForbiddenOperationError)(APIErrorHandler.common_exception_handler)
api.exception_handler(EntityAlreadyExistsError)(APIErrorHandler.common_exception_handler)
Expand Down
10 changes: 10 additions & 0 deletions tests/server/api/v0/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,16 @@ def test_create_user_as_annotator(client: TestClient, db: Session):
assert db.query(User).count() == 1


def test_create_user_with_existent_username(client: TestClient, db: Session, admin_auth_header: dict):
UserFactory.create(username="username")
user = {"first_name": "first-name", "username": "username", "password": "12345678"}

response = client.post("/api/users", headers=admin_auth_header, json=user)

assert response.status_code == 409
assert db.query(User).count() == 2


def test_create_user_with_invalid_min_length_first_name(client: TestClient, db: Session, admin_auth_header: dict):
user = {"first_name": "", "username": "username", "password": "12345678"}

Expand Down
21 changes: 21 additions & 0 deletions tests/server/api/v0/test_workspaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ def test_create_workspace_as_annotator(client: TestClient, db: Session):
assert db.query(Workspace).count() == 0


def test_create_workspace_with_existent_name(client: TestClient, db: Session, admin_auth_header: dict):
WorkspaceFactory.create(name="workspace")

response = client.post("/api/workspaces", headers=admin_auth_header, json={"name": "workspace"})

assert response.status_code == 409
assert db.query(Workspace).count() == 1


def test_create_workspace_with_invalid_min_length_name(client: TestClient, db: Session, admin_auth_header: dict):
response = client.post("/api/workspaces", headers=admin_auth_header, json={"name": ""})

Expand Down Expand Up @@ -205,6 +214,18 @@ def test_create_workspace_user_with_nonexistent_user_id(client: TestClient, db:
assert db.query(WorkspaceUser).count() == 0


def test_create_workspace_user_with_existent_workspace_id_and_user_id(
client: TestClient, db: Session, admin: User, admin_auth_header: dict
):
workspace = WorkspaceFactory.create()
WorkspaceUserFactory.create(workspace_id=workspace.id, user_id=admin.id)

response = client.post(f"/api/workspaces/{workspace.id}/users/{admin.id}", headers=admin_auth_header)

assert response.status_code == 409
assert db.query(WorkspaceUser).count() == 1


def test_delete_workspace_user(client: TestClient, db: Session, admin_auth_header: dict):
workspace_user = WorkspaceUserFactory.create(
workspace_id=WorkspaceFactory.create().id,
Expand Down