Skip to content
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
14 changes: 14 additions & 0 deletions backend/app/crud/onboarding.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
get_project_by_name,
get_user_by_email,
)
from app.crud.user_project import add_user_to_project
from app.models import (
APIKey,
Credential,
Expand Down Expand Up @@ -90,6 +91,19 @@ def onboard_project(
session.add(user)
session.flush()

_, mapping_status = add_user_to_project(
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why user can't map to other project ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because then we would have to build the entire flow where a user can switch between multiple projects using a single login. For example, if a user belongs to multiple projects, we would need to show a dropdown in the UI to let them switch projects, and the data would then load based on the selected project. This would require a lot of changes, so for now we are avoiding this.

session=session,
email=user.email,
organization_id=organization.id,
project_id=project.id,
full_name=onboard_in.user_name,
)
if mapping_status == "different_project":
raise HTTPException(
status_code=409,
detail=(f"User '{user.email}' is already associated with another project"),
)

raw_key, key_prefix, key_hash = api_key_manager.generate()

api_key = APIKey(
Expand Down
28 changes: 28 additions & 0 deletions backend/app/tests/crud/test_onboarding.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
get_user_by_email,
get_organization_by_id,
)
from app.crud.user_project import add_user_to_project
from app.models import (
OnboardingRequest,
OnboardingResponse,
Expand Down Expand Up @@ -151,6 +152,33 @@ def test_onboard_project_existing_user(db: Session) -> None:
assert project is not None


def test_onboard_project_user_already_in_another_project(db: Session) -> None:
"""Onboarding must 409 when the user is already mapped to a different project."""
existing_project = create_test_project(db)
existing_user = create_random_user(db)

add_user_to_project(
session=db,
email=existing_user.email,
organization_id=existing_project.organization_id,
project_id=existing_project.id,
)
db.commit()

onboard_request = OnboardingRequest(
organization_name="TestOrgOnboardDifferent",
project_name="TestProjectOnboardDifferent",
email=existing_user.email,
password=random_lower_string(),
)

with pytest.raises(HTTPException) as exc_info:
onboard_project(session=db, onboard_in=onboard_request)

assert exc_info.value.status_code == 409
assert "already associated with another project" in str(exc_info.value.detail)


def test_onboard_project_duplicate_project_name(db: Session) -> None:
"""Test that onboarding fails when project name already exists in organization."""
# Create existing project
Expand Down
Loading