Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

feat(gateway): workspace, invitation and member routes (CHOO-2722)

Wojciech Wojtyniakb8821ee139b4
diff --git a/core/switch_core/db/stores/agent_store.py b/core/switch_core/db/stores/agent_store.py
index af2e65e2b..29d0db7a0 100644
--- a/core/switch_core/db/stores/agent_store.py
+++ b/core/switch_core/db/stores/agent_store.py
@@ -89,6 +89,16 @@ class AgentStore:
result = await session.execute(select(Agent))
return list(result.scalars().all())
+ async def get_by_owner(self, session: AsyncSession, owner_id: str) -> list[Agent]:
+ """Every agent `owner_id` owns in the bound tenant.
+
+ Backs member removal: an owner's agents authenticate with a key that
+ carries the same `user_id`, so this is what a removal cascade walks
+ to find them.
+ """
+ result = await session.execute(select(Agent).where(Agent.owner_id == owner_id))
+ return list(result.scalars().all())
+
async def get_children(
self, session: AsyncSession, parent_agent_ids: list[str]
) -> list[Agent]:
diff --git a/core/switch_core/db/stores/invitation_store.py b/core/switch_core/db/stores/invitation_store.py
index 0e6b73d1e..ac433bad8 100644
--- a/core/switch_core/db/stores/invitation_store.py
+++ b/core/switch_core/db/stores/invitation_store.py
@@ -164,3 +164,15 @@ class InvitationStore:
invitation.revoked_at = datetime.now(UTC)
await session.flush()
return invitation
+
+ async def consume(
+ self, session: AsyncSession, invitation: Invitation
+ ) -> Invitation:
+ """Record one use of `invitation`.
+
+ Called only after every validity check (expiry, revocation, email,
+ remaining uses) has already passed; it does not repeat any of them.
+ """
+ invitation.uses_remaining -= 1
+ await session.flush()
+ return invitation
diff --git a/core/switch_core/db/stores/user_store.py b/core/switch_core/db/stores/user_store.py
index 3506b9741..4071c2dc4 100644
--- a/core/switch_core/db/stores/user_store.py
+++ b/core/switch_core/db/stores/user_store.py
@@ -300,6 +300,26 @@ class UserStore:
result = await session.execute(select(User))
return list(result.scalars().all())
+ async def add_membership(
+ self, session: AsyncSession, *, tenant_id: str, user_id: str, role: str
+ ) -> TenantMember:
+ """Insert a `role` membership for `user_id` in `tenant_id`.
+
+ Distinct from `ensure_membership`, which derives the role from the
+ caller's global bit and is a no-op when a membership already exists:
+ this is the explicit write for the two places that grant a *specific*
+ role by a caller's own action — creating a workspace (the creator
+ becomes its `owner`) and accepting an invitation (the invited role).
+ It does not check for an existing row first; a caller that needs that
+ checks before calling. Kept as the one place `TenantMember` is
+ constructed (`tests/switch_core/test_seed_admin_membership.py` pins
+ that), so a route never writes one directly.
+ """
+ membership = TenantMember(tenant_id=tenant_id, user_id=user_id, role=role)
+ session.add(membership)
+ await session.flush()
+ return membership
+
async def tenant_role(
self, session: AsyncSession, tenant_id: str, user_id: str
) -> str | None:
@@ -312,6 +332,34 @@ class UserStore:
membership = await session.get(TenantMember, (tenant_id, user_id))
return membership.role if membership is not None else None
+ async def list_tenant_members(
+ self, session: AsyncSession
+ ) -> list[tuple[User, TenantMember]]:
+ """Every member of the session's bound tenant, joined with their user row.
+
+ No `WHERE tenant_id = …` of its own: the policy on `tenant_members` is
+ what narrows this, the same as `InvitationStore.list_for_tenant`.
+ """
+ result = await session.execute(
+ select(User, TenantMember).join(
+ TenantMember, TenantMember.user_id == User.id
+ )
+ )
+ return [(row[0], row[1]) for row in result.all()]
+
+ async def count_owners(self, session: AsyncSession) -> int:
+ """How many `owner` memberships exist in the session's bound tenant.
+
+ Backs "a workspace must always have an owner": removing or demoting a
+ member is refused when they are the one row this counts.
+ """
+ result = await session.execute(
+ select(func.count())
+ .select_from(TenantMember)
+ .where(TenantMember.role == "owner")
+ )
+ return result.scalar_one()
+
async def administers(self, session: AsyncSession, user: User) -> bool:
"""Whether `user` may administer the tenant bound to `session`'s
context — the operator bypass, or an owner/admin membership in it.
diff --git a/core/switch_core/gateway/app.py b/core/switch_core/gateway/app.py
index 376ca1e27..6f1b46d2f 100644
--- a/core/switch_core/gateway/app.py
+++ b/core/switch_core/gateway/app.py
@@ -18,6 +18,7 @@ from switch_core.db.stores.agent_store import AgentStore
from switch_core.db.stores.api_key_store import ApiKeyStore
from switch_core.db.stores.collaboration_bridge_store import CollaborationBridgeStore
from switch_core.db.stores.external_user_store import ExternalUserStore
+from switch_core.db.stores.invitation_store import InvitationStore
from switch_core.db.stores.room_group_store import RoomGroupStore
from switch_core.db.stores.room_store import RoomStore
from switch_core.db.stores.server_connector_store import ServerConnectorStore
@@ -59,6 +60,7 @@ def create_gateway_app(
user_store: UserStore,
external_user_store: ExternalUserStore,
api_key_store: ApiKeyStore,
+ invitation_store: InvitationStore,
template_store: TemplateStore,
resource_service: ResourceService,
protocol: ProtocolService,
@@ -79,6 +81,7 @@ def create_gateway_app(
user_store=user_store,
external_user_store=external_user_store,
api_key_store=api_key_store,
+ invitation_store=invitation_store,
template_store=template_store,
resource_service=resource_service,
protocol=protocol,
diff --git a/core/switch_core/gateway/auth.py b/core/switch_core/gateway/auth.py
index 546da4767..8d9d4ad52 100644
--- a/core/switch_core/gateway/auth.py
+++ b/core/switch_core/gateway/auth.py
@@ -3,6 +3,7 @@ from __future__ import annotations
import datetime
import logging
from collections.abc import AsyncIterator
+from dataclasses import dataclass
from typing import Annotated
import bcrypt
@@ -16,7 +17,7 @@ from switch_core.db.models import Room, Tenant, User
from switch_core.db.session_scope import tenant_session
from switch_core.db.stores.room_store import RoomStore
from switch_core.db.stores.user_store import UserStore
-from switch_core.db.tenant_lookup import tenants_of_user
+from switch_core.db.tenant_lookup import tenant_of_invitation, tenants_of_user
from switch_core.gateway.dependencies import (
get_config,
get_session,
@@ -162,6 +163,56 @@ async def get_authenticated_user_id(
return payload["sub"] # type: ignore[no-any-return]
+@dataclass(frozen=True)
+class AuthenticatedCaller:
+ """An authenticated caller with no tenant bound — id and email, read from
+ `users`, which carries neither a tenant nor a policy."""
+
+ id: str
+ email: str
+
+
+async def get_authenticated_caller(
+ request: Request,
+ session_factory: Annotated[
+ async_sessionmaker[AsyncSession], Depends(get_session_factory)
+ ],
+ user_store: Annotated[UserStore, Depends(get_user_store)],
+ config: Annotated[SwitchConfig, Depends(get_config)],
+) -> AuthenticatedCaller:
+ """Like `get_authenticated_user_id`, but with the caller's own email too.
+
+ Backs `POST /invitations/{token}/accept` (`gateway/tenants.py`), which has
+ to check an email-bound invitation against the caller's *current* address
+ before any tenant is bound — the same "no tenant chosen yet" shape as
+ `get_authenticated_user_id`, plus one more column of the same global,
+ unscoped `users` row. Reads the row fresh rather than trusting the JWT's
+ own `email` claim, which is a snapshot from login and can go stale.
+ """
+ payload = await _authenticate(request, session_factory, user_store, config)
+ async with session_factory() as system_session:
+ user = await user_store.get(system_session, payload["sub"])
+ if user is None:
+ raise HTTPException(status_code=401, detail="User not found")
+ return AuthenticatedCaller(id=user.id, email=user.email)
+
+
+async def tenant_of_invitation_token(
+ session_factory: async_sessionmaker[AsyncSession], token_hash: str
+) -> str | None:
+ """Which tenant an invitation token belongs to, or `None` if it names none.
+
+ A thin wrapper around `db.tenant_lookup.tenant_of_invitation`, kept here
+ rather than called directly from `gateway/tenants.py`: the exemption from
+ row-level security is meant to be reachable through this one module,
+ never through a store or lookup any endpoint could reach for itself (see
+ `db/tenant_lookup.py` and `test_tenant_exemption_allowlist.py`) — the same
+ reason `_resolve_tenant_id` and `list_tenant_memberships` live here rather
+ than being called from the gateway directly.
+ """
+ return await tenant_of_invitation(session_factory, token_hash)
+
+
async def is_tenant_member(
session_factory: async_sessionmaker[AsyncSession], user_id: str, tenant_id: str
) -> bool:
diff --git a/core/switch_core/gateway/dependencies.py b/core/switch_core/gateway/dependencies.py
index 8a07e7e62..abc1e3782 100644
--- a/core/switch_core/gateway/dependencies.py
+++ b/core/switch_core/gateway/dependencies.py
@@ -20,6 +20,7 @@ from switch_core.db.stores.agent_store import AgentStore
from switch_core.db.stores.api_key_store import ApiKeyStore
from switch_core.db.stores.collaboration_bridge_store import CollaborationBridgeStore
from switch_core.db.stores.external_user_store import ExternalUserStore
+from switch_core.db.stores.invitation_store import InvitationStore
from switch_core.db.stores.room_group_store import RoomGroupStore
from switch_core.db.stores.room_store import RoomStore
from switch_core.db.stores.server_connector_store import ServerConnectorStore
@@ -47,6 +48,7 @@ def init_dependencies(
user_store: UserStore,
external_user_store: ExternalUserStore,
api_key_store: ApiKeyStore,
+ invitation_store: InvitationStore,
template_store: TemplateStore,
resource_service: ResourceService,
protocol: ProtocolService,
@@ -66,6 +68,7 @@ def init_dependencies(
_state["user_store"] = user_store
_state["external_user_store"] = external_user_store
_state["api_key_store"] = api_key_store
+ _state["invitation_store"] = invitation_store
_state["template_store"] = template_store
_state["resource_service"] = resource_service
_state["protocol"] = protocol
@@ -187,6 +190,10 @@ def get_api_key_store() -> ApiKeyStore:
return _state["api_key_store"] # type: ignore[no-any-return]
+def get_invitation_store() -> InvitationStore:
+ return _state["invitation_store"] # type: ignore[no-any-return]
+
+
def get_connector_lifecycle() -> ServerSideConnectorLifecycleService:
return _state["connector_lifecycle"] # type: ignore[no-any-return]
diff --git a/core/switch_core/gateway/schemas.py b/core/switch_core/gateway/schemas.py
index cd0d40794..a9a81fc21 100644
--- a/core/switch_core/gateway/schemas.py
+++ b/core/switch_core/gateway/schemas.py
@@ -673,6 +673,48 @@ class TenantMembershipResponse(BaseModel):
role: str
+class TenantCreateRequest(BaseModel):
+ name: str
+
+
+class InvitationCreateRequest(BaseModel):
+ role: str = "member"
+ # None mints a shareable link; set, the invitation is addressed to one
+ # email and accepting it with any other is refused.
+ email: str | None = None
+ expires_in_hours: int = Field(default=168, gt=0)
+ uses_remaining: int = Field(default=1, ge=1)
+
+
+class InvitationDetail(BaseModel):
+ id: str
+ role: str
+ email: str | None
+ expires_at: str
+ uses_remaining: int
+ revoked_at: str | None
+ created_by: str
+ created_at: str
+
+
+class InvitationCreateResponse(InvitationDetail):
+ # The plaintext token. Present only here — see
+ # `InvitationStore.create`, which is the one call that can hand it back.
+ token: str
+
+
+class MemberDetail(BaseModel):
+ user_id: str
+ name: str
+ email: str
+ role: str
+ created_at: str
+
+
+class MemberUpdateRequest(BaseModel):
+ role: str
+
+
class AuthConfigResponse(BaseModel):
# Read unauthenticated by the login page to decide which login methods to
# show. `oidc_provider_label` is the button text (e.g. "Okta").
diff --git a/core/switch_core/gateway/tenants.py b/core/switch_core/gateway/tenants.py
index 00d7d49c2..9609e5231 100644
--- a/core/switch_core/gateway/tenants.py
+++ b/core/switch_core/gateway/tenants.py
@@ -1,29 +1,149 @@
from __future__ import annotations
+import hashlib
+import re
+from datetime import UTC, datetime, timedelta
from typing import Annotated
from fastapi import APIRouter, Depends, HTTPException, Response
+from sqlalchemy.exc import IntegrityError
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
+from switch_core.bridges.agent.protocol.service import ProtocolService
+from switch_core.clients.client_lifecycle_service import ClientLifecycleService
from switch_core.config import SwitchConfig
+from switch_core.db.models import Invitation, Tenant, TenantMember, User
+from switch_core.db.session_scope import tenant_session
+from switch_core.db.stores.agent_store import AgentStore
+from switch_core.db.stores.api_key_store import ApiKeyStore
+from switch_core.db.stores.invitation_store import InvitationStore
from switch_core.db.stores.user_store import UserStore
from switch_core.gateway.auth import (
+ AuthenticatedCaller,
+ get_authenticated_caller,
get_authenticated_user_id,
+ get_current_user,
is_tenant_member,
list_tenant_memberships,
+ require_tenant_admin,
set_session_cookie,
+ tenant_of_invitation_token,
)
from switch_core.gateway.auth_routes import _session_response
from switch_core.gateway.dependencies import (
+ get_agent_store,
+ get_api_key_store,
+ get_client_lifecycle,
get_config,
+ get_invitation_store,
+ get_protocol,
+ get_session,
get_session_factory,
get_system_session,
get_user_store,
)
-from switch_core.gateway.schemas import SessionUserResponse, TenantMembershipResponse
+from switch_core.gateway.schemas import (
+ InvitationCreateRequest,
+ InvitationCreateResponse,
+ InvitationDetail,
+ MemberDetail,
+ MemberUpdateRequest,
+ SessionUserResponse,
+ TenantCreateRequest,
+ TenantMembershipResponse,
+)
+from switch_core.tenant_context import current_tenant_id
router = APIRouter()
+TENANT_MEMBER_ROLES = ("owner", "admin", "member")
+
+_SLUG_INVALID_CHARS = re.compile(r"[^a-z0-9]+")
+
+
+def _derive_slug(name: str) -> str:
+ """A URL-safe slug from a workspace name.
+
+ A taken slug is a 409 (`create_tenant` below), never a silently
+ suffixed alternative — the design is explicit that a caller must be told
+ rather than handed a workspace under a name it did not ask for.
+ """
+ slug = _SLUG_INVALID_CHARS.sub("-", name.strip().lower()).strip("-")
+ if not slug:
+ raise HTTPException(
+ status_code=400,
+ detail="Name must contain at least one letter or digit",
+ )
+ return slug
+
+
+def _require_bound_tenant(tenant_id: str) -> None:
+ """Raise 403 unless `tenant_id` names the tenant this request is bound to.
+
+ Every write below this point uses the bound session, which writes into
+ the caller's *bound* tenant regardless of what a path segment says —
+ `TenantScoped.tenant_id` defaults to `require_tenant_id()`, not to
+ anything an endpoint parses. Without this check, a member of workspace A
+ naming workspace B in the path would not fail; it would silently act on A
+ instead, which is the one thing the error-handling philosophy this
+ codebase follows refuses to do.
+ """
+ if current_tenant_id() != tenant_id:
+ raise HTTPException(status_code=403, detail="Not authorized for this tenant")
+
+
+def _invitation_fields(invitation: Invitation) -> dict[str, object]:
+ return {
+ "id": invitation.id,
+ "role": invitation.role,
+ "email": invitation.email,
+ "expires_at": str(invitation.expires_at),
+ "uses_remaining": invitation.uses_remaining,
+ "revoked_at": str(invitation.revoked_at) if invitation.revoked_at else None,
+ "created_by": invitation.created_by,
+ "created_at": str(invitation.created_at),
+ }
+
+
+def _invitation_detail(invitation: Invitation) -> InvitationDetail:
+ return InvitationDetail(**_invitation_fields(invitation))
+
+
+def _member_detail(user: User, membership: TenantMember) -> MemberDetail:
+ return MemberDetail(
+ user_id=user.id,
+ name=user.name,
+ email=user.email,
+ role=membership.role,
+ created_at=str(membership.created_at),
+ )
+
+
+def _require_invitation_usable(invitation: Invitation, caller_email: str) -> None:
+ """Raise 403 unless `invitation` may still be accepted by `caller_email`.
+
+ Checked in this order, but every branch is independent: revocation,
+ expiry, remaining uses and an addressed email are four separate ways an
+ invitation stops working, none of them optional
+ (`docs/old/multi-tenancy-phase2-tenants.md`, §5).
+ """
+ if invitation.revoked_at is not None:
+ raise HTTPException(status_code=403, detail="This invitation has been revoked")
+ if invitation.expires_at < datetime.now(UTC): # type: ignore[operator]
+ raise HTTPException(status_code=403, detail="This invitation has expired")
+ if invitation.uses_remaining <= 0:
+ raise HTTPException(
+ status_code=403, detail="This invitation has already been used"
+ )
+ if (
+ invitation.email is not None
+ and invitation.email.lower() != caller_email.lower()
+ ):
+ raise HTTPException(
+ status_code=403,
+ detail="This invitation is addressed to a different email",
+ )
+
@router.get("/tenants")
async def list_tenants(
@@ -45,6 +165,44 @@ async def list_tenants(
return await list_tenant_memberships(session_factory, user_store, user_id)
[email protected]("/tenants", status_code=201)
+async def create_tenant(
+ req: TenantCreateRequest,
+ user_id: Annotated[str, Depends(get_authenticated_user_id)],
+ session_factory: Annotated[
+ async_sessionmaker[AsyncSession], Depends(get_session_factory)
+ ],
+ user_store: Annotated[UserStore, Depends(get_user_store)],
+ client_lifecycle: Annotated[ClientLifecycleService, Depends(get_client_lifecycle)],
+) -> TenantMembershipResponse:
+ """Create a workspace. The caller becomes its `owner`.
+
+ Provisioning goes through `ClientLifecycleService.create_tenant` — "the
+ one seam a tenant comes into existence through" — rather than inserting a
+ `Tenant` row here, so a workspace created through this route gets the same
+ admin client every other tenant does. The membership row is not part of
+ that call (it provisions the tenant, not any particular person's place in
+ it), so it is written here, in its own session bound to the new tenant.
+ """
+ slug = _derive_slug(req.name)
+ try:
+ tenant = await client_lifecycle.create_tenant(req.name, slug)
+ except IntegrityError as exc:
+ raise HTTPException(
+ status_code=409, detail=f"Slug already taken: {slug}"
+ ) from exc
+
+ async with tenant_session(session_factory, tenant.id) as session:
+ await user_store.add_membership(
+ session, tenant_id=tenant.id, user_id=user_id, role="owner"
+ )
+ await session.commit()
+
+ return TenantMembershipResponse(
+ id=tenant.id, slug=tenant.slug, name=tenant.name, role="owner"
+ )
+
+
@router.post("/tenants/{tenant_id}/switch")
async def switch_tenant(
tenant_id: str,
@@ -78,3 +236,227 @@ async def switch_tenant(
response, user, config.jwt_secret_key, config.gateway_cookie_secure, tenant_id
)
return _session_response(user)
+
+
+# ── Invitations ───────────────────────────────────────────────────────────────
+
+
[email protected]("/tenants/{tenant_id}/invitations", status_code=201)
+async def create_invitation(
+ tenant_id: str,
+ req: InvitationCreateRequest,
+ session: Annotated[AsyncSession, Depends(get_session)],
+ invitation_store: Annotated[InvitationStore, Depends(get_invitation_store)],
+ user: Annotated[User, Depends(require_tenant_admin)],
+) -> InvitationCreateResponse:
+ """Mint an invitation to the bound tenant. `owner`/`admin` only."""
+ _require_bound_tenant(tenant_id)
+ if req.role not in TENANT_MEMBER_ROLES:
+ raise HTTPException(status_code=400, detail=f"Invalid role: {req.role}")
+
+ expires_at = datetime.now(UTC) + timedelta(hours=req.expires_in_hours)
+ invitation, token = await invitation_store.create(
+ session,
+ role=req.role,
+ email=req.email,
+ expires_at=expires_at,
+ uses_remaining=req.uses_remaining,
+ created_by=user.id,
+ )
+ await session.commit()
+ return InvitationCreateResponse(token=token, **_invitation_fields(invitation))
+
+
[email protected]("/tenants/{tenant_id}/invitations")
+async def list_invitations(
+ tenant_id: str,
+ session: Annotated[AsyncSession, Depends(get_session)],
+ invitation_store: Annotated[InvitationStore, Depends(get_invitation_store)],
+ _user: Annotated[User, Depends(require_tenant_admin)],
+) -> list[InvitationDetail]:
+ """Every invitation of the bound tenant. `owner`/`admin` only."""
+ _require_bound_tenant(tenant_id)
+ invitations = await invitation_store.list_for_tenant(session)
+ return [_invitation_detail(i) for i in invitations]
+
+
[email protected]("/tenants/{tenant_id}/invitations/{invitation_id}")
+async def revoke_invitation(
+ tenant_id: str,
+ invitation_id: str,
+ session: Annotated[AsyncSession, Depends(get_session)],
+ invitation_store: Annotated[InvitationStore, Depends(get_invitation_store)],
+ _user: Annotated[User, Depends(require_tenant_admin)],
+) -> InvitationDetail:
+ """Revoke an invitation of the bound tenant. `owner`/`admin` only."""
+ _require_bound_tenant(tenant_id)
+ try:
+ invitation = await invitation_store.revoke(session, invitation_id)
+ except ValueError as exc:
+ raise HTTPException(status_code=404, detail=str(exc)) from exc
+ await session.commit()
+ return _invitation_detail(invitation)
+
+
[email protected]("/invitations/{token}/accept")
+async def accept_invitation(
+ token: str,
+ caller: Annotated[AuthenticatedCaller, Depends(get_authenticated_caller)],
+ session_factory: Annotated[
+ async_sessionmaker[AsyncSession], Depends(get_session_factory)
+ ],
+ user_store: Annotated[UserStore, Depends(get_user_store)],
+ invitation_store: Annotated[InvitationStore, Depends(get_invitation_store)],
+) -> TenantMembershipResponse:
+ """Accept an invitation, joining its tenant.
+
+ Authenticated with `get_authenticated_caller`, not `get_current_user`:
+ the caller's own session may be bound to a different tenant than the
+ invitation names, or to none at all, and inserting a membership for a
+ tenant other than the one a session is bound to is refused by the
+ policy — rebinding an already-open session raises
+ (`TenantBindingDriftError`, `db/tenant_session.py`). So this handler
+ resolves the token's tenant through the exempt lookup and does all of its
+ own work inside a fresh `tenant_session` bound to exactly that tenant,
+ rather than touching the request's session at all
+ (`docs/old/multi-tenancy-phase2-tenants.md`, §5).
+ """
+ token_hash = hashlib.sha256(token.encode()).hexdigest()
+ tenant_id = await tenant_of_invitation_token(session_factory, token_hash)
+ if tenant_id is None:
+ raise HTTPException(status_code=404, detail="Invitation not found")
+
+ async with tenant_session(session_factory, tenant_id) as session:
+ invitation = await invitation_store.get_by_token_hash(session, token_hash)
+ if invitation is None:
+ raise HTTPException(status_code=404, detail="Invitation not found")
+ _require_invitation_usable(invitation, caller.email)
+
+ existing_role = await user_store.tenant_role(session, tenant_id, caller.id)
+ role = existing_role if existing_role is not None else invitation.role
+ if existing_role is None:
+ await user_store.add_membership(
+ session, tenant_id=tenant_id, user_id=caller.id, role=role
+ )
+ await invitation_store.consume(session, invitation)
+
+ tenant = await session.get(Tenant, tenant_id)
+ assert tenant is not None
+ await session.commit()
+
+ return TenantMembershipResponse(
+ id=tenant.id, slug=tenant.slug, name=tenant.name, role=role
+ )
+
+
+# ── Members ───────────────────────────────────────────────────────────────────
+
+
[email protected]("/tenants/{tenant_id}/members")
+async def list_members(
+ tenant_id: str,
+ session: Annotated[AsyncSession, Depends(get_session)],
+ user_store: Annotated[UserStore, Depends(get_user_store)],
+ _user: Annotated[User, Depends(get_current_user)],
+) -> list[MemberDetail]:
+ """Every member of the bound tenant. Any member may list their own
+ workspace's roster; changing or removing one is admin-gated below."""
+ _require_bound_tenant(tenant_id)
+ members = await user_store.list_tenant_members(session)
+ return [_member_detail(user, membership) for user, membership in members]
+
+
[email protected]("/tenants/{tenant_id}/members/{user_id}")
+async def update_member_role(
+ tenant_id: str,
+ user_id: str,
+ req: MemberUpdateRequest,
+ session: Annotated[AsyncSession, Depends(get_session)],
+ user_store: Annotated[UserStore, Depends(get_user_store)],
+ _admin: Annotated[User, Depends(require_tenant_admin)],
+) -> MemberDetail:
+ """Change a member's role. `owner`/`admin` only.
+
+ Refuses to demote the last `owner`: with no workspace deletion in this
+ phase, there is no legitimate route to a workspace with none.
+ """
+ _require_bound_tenant(tenant_id)
+ if req.role not in TENANT_MEMBER_ROLES:
+ raise HTTPException(status_code=400, detail=f"Invalid role: {req.role}")
+
+ membership = await session.get(TenantMember, (tenant_id, user_id))
+ if membership is None:
+ raise HTTPException(status_code=404, detail="Not a member of this tenant")
+
+ if membership.role == "owner" and req.role != "owner":
+ if await user_store.count_owners(session) <= 1:
+ raise HTTPException(status_code=409, detail="Cannot demote the last owner")
+
+ membership.role = req.role
+ await session.commit()
+
+ user = await user_store.get(session, user_id)
+ assert user is not None
+ return _member_detail(user, membership)
+
+
[email protected]("/tenants/{tenant_id}/members/{user_id}")
+async def remove_member(
+ tenant_id: str,
+ user_id: str,
+ session: Annotated[AsyncSession, Depends(get_session)],
+ user_store: Annotated[UserStore, Depends(get_user_store)],
+ agent_store: Annotated[AgentStore, Depends(get_agent_store)],
+ api_key_store: Annotated[ApiKeyStore, Depends(get_api_key_store)],
+ protocol: Annotated[ProtocolService, Depends(get_protocol)],
+ _admin: Annotated[User, Depends(require_tenant_admin)],
+) -> dict[str, bool]:
+ """Remove a member from the bound tenant. `owner`/`admin` only.
+
+ Refuses to remove the last `owner`, same as demoting one.
+
+ Removal also revokes what the member's own credentials in this tenant let
+ them do here, in the same transaction as the membership row going away —
+ a bearer credential resolves its tenant from its own row and never
+ consults membership, so leaving the row behind would leave the person
+ with working, invisible access (`docs/old/multi-tenancy-phase2-tenants.md`,
+ §3). Concretely: every agent this user owns here is deleted outright —
+ there is no "disabled" flag on `Agent` to set instead, and an agent's own
+ bearer key (`agents.api_key_id`) is a `NOT NULL` column with no `ON
+ DELETE` action, so the key cannot be revoked while the agent still
+ references it. Deleting the agent first, through `AgentStore.delete`
+ rather than `ProtocolService.delete_agent`, frees that key to be deleted
+ with the rest of this user's keys right after — `delete_agent` spans
+ several sessions and commits of its own and reaches out to the live
+ Matrix client and collaboration bridges, none of which can be folded into
+ one transaction, so it is deliberately not used here. What this does not
+ do is tear down a live Matrix session such an agent already holds; its
+ next call to Switch fails at authentication instead, since the credential
+ behind it is gone.
+ """
+ _require_bound_tenant(tenant_id)
+ membership = await session.get(TenantMember, (tenant_id, user_id))
+ if membership is None:
+ raise HTTPException(status_code=404, detail="Not a member of this tenant")
+
+ if membership.role == "owner" and await user_store.count_owners(session) <= 1:
+ raise HTTPException(status_code=409, detail="Cannot remove the last owner")
+
+ revoked_agent_ids = [a.id for a in await agent_store.get_by_owner(session, user_id)]
+ for agent_id in revoked_agent_ids:
+ await agent_store.delete(session, agent_id)
+
+ keys = await api_key_store.get_by_user(session, user_id)
+ revoked_key_hashes = [key.key_hash for key in keys]
+ for key in keys:
+ await api_key_store.delete(session, key.id)
+
+ await session.delete(membership)
+ await session.commit()
+
+ for agent_id in revoked_agent_ids:
+ protocol.api_key_cache.invalidate_agent(agent_id)
+ for key_hash in revoked_key_hashes:
+ protocol.api_key_cache.invalidate(key_hash)
+
+ return {"ok": True}
diff --git a/core/switch_core/main.py b/core/switch_core/main.py
index 4750e9f67..321eafc47 100644
--- a/core/switch_core/main.py
+++ b/core/switch_core/main.py
@@ -97,6 +97,7 @@ from switch_core.db.stores.client_store import ClientStore
from switch_core.db.stores.collaboration_bridge_store import CollaborationBridgeStore
from switch_core.db.stores.document_store import DocumentStore
from switch_core.db.stores.external_user_store import ExternalUserStore
+from switch_core.db.stores.invitation_store import InvitationStore
from switch_core.db.stores.media_store import MediaStore
from switch_core.db.stores.message_store import MessageStore
from switch_core.db.stores.package_store import PackageStore
@@ -281,6 +282,7 @@ async def run(config: SwitchConfig) -> None:
bridge_message_map_store = BridgeMessageMapStore()
user_store = UserStore()
api_key_store = ApiKeyStore()
+ invitation_store = InvitationStore()
tenant_store = TenantStore()
reference_store = ReferenceStore()
reference_type_store = ReferenceTypeStore()
@@ -489,6 +491,7 @@ async def run(config: SwitchConfig) -> None:
user_store=user_store,
external_user_store=external_user_store,
api_key_store=api_key_store,
+ invitation_store=invitation_store,
template_store=template_store,
resource_service=resource_service,
protocol=protocol,
diff --git a/core/tests/switch_core/gateway/test_tenant_api_routes.py b/core/tests/switch_core/gateway/test_tenant_api_routes.py
new file mode 100644
index 000000000..54f2a811b
--- /dev/null
+++ b/core/tests/switch_core/gateway/test_tenant_api_routes.py
@@ -0,0 +1,716 @@
+"""The route half of CHOO-2722: `gateway/tenants.py`'s workspace, invitation
+and member routes.
+
+Builds route-scoped apps the same way `test_tenant_resolution.py` does —
+`switch_core.gateway.dependencies`' functions overridden individually rather
+than through `init_dependencies` — so nothing here leaks into another test and
+nothing but `switch_core.gateway.tenants` itself is exercised for real.
+
+`_FakeClientLifecycle.create_tenant` mirrors
+`ClientLifecycleService.create_tenant`'s DB half (insert the tenant row inside
+a `tenant_session` bound to its own id) without the Matrix admin-client
+provisioning, which needs a running homeserver these tests have no business
+depending on. The unique-slug behaviour under test comes from the same
+`tenants.slug` column either way.
+"""
+
+from __future__ import annotations
+
+import uuid
+from datetime import UTC, datetime, timedelta
+from types import SimpleNamespace
+
+import httpx
+from fastapi import FastAPI
+from sqlalchemy import select
+from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
+
+from switch_core.db.models import (
+ Agent,
+ ApiKey,
+ Client,
+ Invitation,
+ Tenant,
+ TenantMember,
+ User,
+)
+from switch_core.db.session_scope import tenant_session
+from switch_core.db.stores.agent_store import AgentStore
+from switch_core.db.stores.api_key_store import ApiKeyStore
+from switch_core.db.stores.invitation_store import InvitationStore
+from switch_core.db.stores.user_store import UserStore
+from switch_core.gateway import dependencies as gw_deps
+from switch_core.gateway.auth import create_jwt
+from switch_core.gateway.tenants import router as tenants_router
+
+_SECRET = "unit-test-jwt-key-unit-test-jwt-key-unit-test" # gitleaks:allow
+TENANT_A = "tenant-api-routes-a"
+TENANT_B = "tenant-api-routes-b"
+
+
+class _FakeClientLifecycle:
+ def __init__(self, session_factory: async_sessionmaker[AsyncSession]) -> None:
+ self._session_factory = session_factory
+
+ async def create_tenant(self, name: str, slug: str) -> Tenant:
+ tenant = Tenant(id=str(uuid.uuid4()), name=name, slug=slug)
+ async with tenant_session(self._session_factory, tenant.id) as session:
+ session.add(tenant)
+ await session.commit()
+ return tenant
+
+
+def _fake_protocol() -> SimpleNamespace:
+ return SimpleNamespace(
+ api_key_cache=SimpleNamespace(
+ invalidate=lambda key_hash: None,
+ invalidate_agent=lambda agent_id: None,
+ )
+ )
+
+
+def _app(
+ session_factory: async_sessionmaker[AsyncSession],
+ *,
+ client_lifecycle: object | None = None,
+) -> FastAPI:
+ async def _session_dep():
+ async with session_factory() as session:
+ yield session
+
+ app = FastAPI()
+ app.include_router(tenants_router)
+ app.dependency_overrides[gw_deps.get_session] = _session_dep
+ app.dependency_overrides[gw_deps.get_system_session] = _session_dep
+ app.dependency_overrides[gw_deps.get_session_factory] = lambda: session_factory
+ app.dependency_overrides[gw_deps.get_user_store] = lambda: UserStore()
+ app.dependency_overrides[gw_deps.get_agent_store] = lambda: AgentStore()
+ app.dependency_overrides[gw_deps.get_api_key_store] = lambda: ApiKeyStore()
+ app.dependency_overrides[gw_deps.get_invitation_store] = lambda: InvitationStore()
+ app.dependency_overrides[gw_deps.get_protocol] = lambda: _fake_protocol()
+ app.dependency_overrides[gw_deps.get_client_lifecycle] = lambda: (
+ client_lifecycle or _FakeClientLifecycle(session_factory)
+ )
+ app.dependency_overrides[gw_deps.get_config] = lambda: SimpleNamespace(
+ jwt_secret_key=_SECRET,
+ gateway_cookie_secure=False,
+ gateway_tenant_choice_enabled=False,
+ )
+ return app
+
+
+def _client(app: FastAPI, token: str) -> httpx.AsyncClient:
+ return httpx.AsyncClient(
+ transport=httpx.ASGITransport(app=app),
+ base_url="http://test",
+ cookies={"switch_auth": token},
+ )
+
+
+async def _make_tenant(
+ session_factory: async_sessionmaker[AsyncSession], tenant_id: str
+) -> None:
+ async with session_factory() as session:
+ session.add(Tenant(id=tenant_id, slug=tenant_id, name=tenant_id))
+ await session.commit()
+
+
+async def _make_member(
+ session_factory: async_sessionmaker[AsyncSession],
+ *,
+ name: str,
+ tenant_id: str,
+ role: str,
+ email: str | None = None,
+) -> str:
+ async with session_factory() as session:
+ user = User(name=name, email=email or f"{name}@example.invalid", role="user")
+ session.add(user)
+ await session.flush()
+ session.add(TenantMember(tenant_id=tenant_id, user_id=user.id, role=role))
+ await session.commit()
+ return user.id
+
+
+def _token(user_id: str, email: str, tenant_id: str) -> str:
+ return create_jwt(user_id, email, "user", _SECRET, tenant_id)
+
+
+async def _make_api_key(
+ session_factory: async_sessionmaker[AsyncSession],
+ *,
+ tenant_id: str,
+ user_id: str,
+ key_type: str = "registration",
+) -> str:
+ """A personal (non-agent) API key for `user_id` in `tenant_id`. Returns
+ its `key_hash`."""
+ key_hash = uuid.uuid4().hex
+ async with session_factory() as session:
+ session.add(
+ ApiKey(
+ tenant_id=tenant_id,
+ user_id=user_id,
+ key_hash=key_hash,
+ encrypted_key="unused-in-tests",
+ label="test-key",
+ type=key_type,
+ )
+ )
+ await session.commit()
+ return key_hash
+
+
+async def _make_agent_with_key(
+ session_factory: async_sessionmaker[AsyncSession],
+ *,
+ tenant_id: str,
+ owner_id: str,
+) -> tuple[str, str]:
+ """An agent owned by `owner_id`, plus the `agents`-type key backing it.
+
+ Returns `(agent_id, key_hash)`. The agent's own client and api_key rows
+ are constructed first — `agents.client_id` and `agents.api_key_id` are
+ both foreign keys, not free-form strings.
+ """
+ key_hash = uuid.uuid4().hex
+ async with session_factory() as session:
+ client = Client(
+ tenant_id=tenant_id,
+ matrix_user_id=f"@bot-{uuid.uuid4().hex[:8]}:test",
+ display_name="test-agent-bot",
+ type="agent",
+ )
+ session.add(client)
+ await session.flush()
+
+ api_key = ApiKey(
+ tenant_id=tenant_id,
+ user_id=owner_id,
+ key_hash=key_hash,
+ encrypted_key="unused-in-tests",
+ label="agent-key",
+ type="agent",
+ )
+ session.add(api_key)
+ await session.flush()
+
+ agent = Agent(
+ tenant_id=tenant_id,
+ name=f"agent-{uuid.uuid4().hex[:8]}",
+ description="test agent",
+ agent_type="other",
+ connector_type="claude-code",
+ integration_profile={},
+ client_id=client.id,
+ api_key_id=api_key.id,
+ owner_id=owner_id,
+ )
+ session.add(agent)
+ await session.commit()
+ return agent.id, key_hash
+
+
+class TestCreateTenant:
+ async def test_creating_a_tenant_makes_the_caller_its_owner(
+ self, session_factory: async_sessionmaker[AsyncSession]
+ ) -> None:
+ await _make_tenant(session_factory, TENANT_A)
+ user_id = await _make_member(
+ session_factory, name="founder", tenant_id=TENANT_A, role="member"
+ )
+ token = _token(user_id, "[email protected]", TENANT_A)
+
+ async with _client(_app(session_factory), token) as client:
+ response = await client.post("/tenants", json={"name": "Acme Corp"})
+
+ assert response.status_code == 201, response.text
+ body = response.json()
+ assert body["slug"] == "acme-corp"
+ assert body["role"] == "owner"
+
+ async with session_factory() as session:
+ membership = await session.get(TenantMember, (body["id"], user_id))
+ assert membership is not None
+ assert membership.role == "owner"
+
+ async def test_a_taken_slug_is_409_not_a_silent_suffix(
+ self, session_factory: async_sessionmaker[AsyncSession]
+ ) -> None:
+ await _make_tenant(session_factory, TENANT_A)
+ user_id = await _make_member(
+ session_factory, name="second-founder", tenant_id=TENANT_A, role="member"
+ )
+ token = _token(user_id, "[email protected]", TENANT_A)
+
+ async with _client(_app(session_factory), token) as client:
+ first = await client.post("/tenants", json={"name": "Widgets Inc"})
+ assert first.status_code == 201, first.text
+ second = await client.post("/tenants", json={"name": "Widgets Inc"})
+
+ assert second.status_code == 409
+
+ async def test_a_name_with_no_slug_characters_is_400(
+ self, session_factory: async_sessionmaker[AsyncSession]
+ ) -> None:
+ await _make_tenant(session_factory, TENANT_A)
+ user_id = await _make_member(
+ session_factory, name="emoji-fan", tenant_id=TENANT_A, role="member"
+ )
+ token = _token(user_id, "[email protected]", TENANT_A)
+
+ async with _client(_app(session_factory), token) as client:
+ response = await client.post("/tenants", json={"name": "!!!"})
+
+ assert response.status_code == 400
+
+
+class TestInvitationAuthorisation:
+ """A member of workspace A cannot mint an invitation to workspace B —
+ each request is bound to exactly one tenant, and the path segment cannot
+ override that."""
+
+ async def test_a_plain_member_cannot_mint_an_invitation(
+ self, session_factory: async_sessionmaker[AsyncSession]
+ ) -> None:
+ await _make_tenant(session_factory, TENANT_A)
+ user_id = await _make_member(
+ session_factory, name="rank-and-file", tenant_id=TENANT_A, role="member"
+ )
+ token = _token(user_id, "[email protected]", TENANT_A)
+
+ async with _client(_app(session_factory), token) as client:
+ response = await client.post(
+ f"/tenants/{TENANT_A}/invitations", json={"role": "member"}
+ )
+
+ assert response.status_code == 403
+
+ async def test_an_admin_of_a_cannot_mint_an_invitation_naming_b(
+ self, session_factory: async_sessionmaker[AsyncSession]
+ ) -> None:
+ await _make_tenant(session_factory, TENANT_A)
+ await _make_tenant(session_factory, TENANT_B)
+ user_id = await _make_member(
+ session_factory, name="a-admin", tenant_id=TENANT_A, role="admin"
+ )
+ token = _token(user_id, "[email protected]", TENANT_A)
+
+ async with _client(_app(session_factory), token) as client:
+ response = await client.post(
+ f"/tenants/{TENANT_B}/invitations", json={"role": "member"}
+ )
+
+ assert response.status_code == 403
+
+ async with tenant_session(session_factory, TENANT_B) as scoped:
+ assert await InvitationStore().list_for_tenant(scoped) == []
+
+ async def test_an_owner_can_mint_an_invitation_to_their_own_tenant(
+ self, session_factory: async_sessionmaker[AsyncSession]
+ ) -> None:
+ await _make_tenant(session_factory, TENANT_A)
+ user_id = await _make_member(
+ session_factory, name="a-owner", tenant_id=TENANT_A, role="owner"
+ )
+ token = _token(user_id, "[email protected]", TENANT_A)
+
+ async with _client(_app(session_factory), token) as client:
+ response = await client.post(
+ f"/tenants/{TENANT_A}/invitations",
+ json={"role": "member", "uses_remaining": 3},
+ )
+
+ assert response.status_code == 201, response.text
+ body = response.json()
+ assert body["role"] == "member"
+ assert body["uses_remaining"] == 3
+ assert "token" in body and body["token"]
+
+
+class TestInvitationLifecycle:
+ async def _mint(
+ self,
+ session_factory: async_sessionmaker[AsyncSession],
+ *,
+ tenant_id: str,
+ admin_id: str,
+ role: str = "member",
+ email: str | None = None,
+ uses_remaining: int = 1,
+ expires_in_hours: int = 168,
+ ) -> tuple[str, str]:
+ """Mint directly through the store (not the route) so a lifecycle
+ test does not depend on `TestInvitationAuthorisation` passing first.
+ Returns `(invitation_id, token)`."""
+ async with tenant_session(session_factory, tenant_id) as session:
+ invitation, token = await InvitationStore().create(
+ session,
+ role=role,
+ email=email,
+ expires_at=datetime.now(UTC) + timedelta(hours=expires_in_hours),
+ uses_remaining=uses_remaining,
+ created_by=admin_id,
+ )
+ await session.commit()
+ return invitation.id, token
+
+ async def test_accepting_grants_membership_in_the_invited_role(
+ self, session_factory: async_sessionmaker[AsyncSession]
+ ) -> None:
+ await _make_tenant(session_factory, TENANT_A)
+ admin_id = await _make_member(
+ session_factory, name="inviter", tenant_id=TENANT_A, role="owner"
+ )
+ _id, token = await self._mint(
+ session_factory, tenant_id=TENANT_A, admin_id=admin_id, role="admin"
+ )
+ await _make_tenant(session_factory, TENANT_B)
+ invitee_id = await _make_member(
+ session_factory, name="invitee", tenant_id=TENANT_B, role="member"
+ )
+ caller_token = _token(invitee_id, "[email protected]", TENANT_B)
+
+ async with _client(_app(session_factory), caller_token) as client:
+ response = await client.post(f"/invitations/{token}/accept")
+
+ assert response.status_code == 200, response.text
+ body = response.json()
+ assert body["id"] == TENANT_A
+ assert body["role"] == "admin"
+
+ async with session_factory() as session:
+ membership = await session.get(TenantMember, (TENANT_A, invitee_id))
+ assert membership is not None
+ assert membership.role == "admin"
+
+ async def test_accepting_twice_fails_the_second_time(
+ self, session_factory: async_sessionmaker[AsyncSession]
+ ) -> None:
+ await _make_tenant(session_factory, TENANT_A)
+ admin_id = await _make_member(
+ session_factory, name="inviter2", tenant_id=TENANT_A, role="owner"
+ )
+ _id, token = await self._mint(
+ session_factory, tenant_id=TENANT_A, admin_id=admin_id, uses_remaining=1
+ )
+ await _make_tenant(session_factory, TENANT_B)
+ first_invitee = await _make_member(
+ session_factory, name="first-invitee", tenant_id=TENANT_B, role="member"
+ )
+ second_invitee = await _make_member(
+ session_factory, name="second-invitee", tenant_id=TENANT_B, role="member"
+ )
+
+ app = _app(session_factory)
+ async with _client(
+ app, _token(first_invitee, "[email protected]", TENANT_B)
+ ) as client:
+ first = await client.post(f"/invitations/{token}/accept")
+ assert first.status_code == 200, first.text
+
+ async with _client(
+ app, _token(second_invitee, "[email protected]", TENANT_B)
+ ) as client:
+ second = await client.post(f"/invitations/{token}/accept")
+
+ assert second.status_code == 403
+ assert "used" in second.json()["detail"].lower()
+
+ async with session_factory() as session:
+ assert await session.get(TenantMember, (TENANT_A, second_invitee)) is None
+
+ async def test_a_revoked_invitation_is_refused(
+ self, session_factory: async_sessionmaker[AsyncSession]
+ ) -> None:
+ await _make_tenant(session_factory, TENANT_A)
+ admin_id = await _make_member(
+ session_factory, name="revoker", tenant_id=TENANT_A, role="owner"
+ )
+ invitation_id, token = await self._mint(
+ session_factory, tenant_id=TENANT_A, admin_id=admin_id
+ )
+ async with tenant_session(session_factory, TENANT_A) as session:
+ await InvitationStore().revoke(session, invitation_id)
+ await session.commit()
+
+ await _make_tenant(session_factory, TENANT_B)
+ invitee_id = await _make_member(
+ session_factory, name="too-late", tenant_id=TENANT_B, role="member"
+ )
+
+ async with _client(
+ _app(session_factory),
+ _token(invitee_id, "[email protected]", TENANT_B),
+ ) as client:
+ response = await client.post(f"/invitations/{token}/accept")
+
+ assert response.status_code == 403
+ assert "revoked" in response.json()["detail"].lower()
+
+ async def test_an_expired_invitation_is_refused(
+ self, session_factory: async_sessionmaker[AsyncSession]
+ ) -> None:
+ await _make_tenant(session_factory, TENANT_A)
+ admin_id = await _make_member(
+ session_factory, name="expiry-setter", tenant_id=TENANT_A, role="owner"
+ )
+ _id, token = await self._mint(
+ session_factory,
+ tenant_id=TENANT_A,
+ admin_id=admin_id,
+ expires_in_hours=1,
+ )
+ # Force it into the past directly — the store only accepts a future
+ # `expires_at` implicitly by convention, not by constraint, so this is
+ # the straightforward way to get an already-expired row.
+ async with session_factory() as session:
+ result = await session.execute(select(Invitation))
+ invitation = result.scalars().one()
+ invitation.expires_at = datetime.now(UTC) - timedelta(hours=1) # type: ignore[assignment]
+ await session.commit()
+
+ await _make_tenant(session_factory, TENANT_B)
+ invitee_id = await _make_member(
+ session_factory, name="too-slow", tenant_id=TENANT_B, role="member"
+ )
+
+ async with _client(
+ _app(session_factory),
+ _token(invitee_id, "[email protected]", TENANT_B),
+ ) as client:
+ response = await client.post(f"/invitations/{token}/accept")
+
+ assert response.status_code == 403
+ assert "expired" in response.json()["detail"].lower()
+
+ async def test_an_email_bound_invitation_refuses_a_different_address(
+ self, session_factory: async_sessionmaker[AsyncSession]
+ ) -> None:
+ await _make_tenant(session_factory, TENANT_A)
+ admin_id = await _make_member(
+ session_factory, name="picky-inviter", tenant_id=TENANT_A, role="owner"
+ )
+ _id, token = await self._mint(
+ session_factory,
+ tenant_id=TENANT_A,
+ admin_id=admin_id,
+ )
+ await _make_tenant(session_factory, TENANT_B)
+ wrong_person = await _make_member(
+ session_factory,
+ name="wrong-person",
+ tenant_id=TENANT_B,
+ role="member",
+ )
+
+ async with _client(
+ _app(session_factory),
+ _token(wrong_person, "[email protected]", TENANT_B),
+ ) as client:
+ response = await client.post(f"/invitations/{token}/accept")
+
+ assert response.status_code == 403
+ assert "email" in response.json()["detail"].lower()
+
+ async def test_an_email_bound_invitation_accepts_the_addressed_email(
+ self, session_factory: async_sessionmaker[AsyncSession]
+ ) -> None:
+ await _make_tenant(session_factory, TENANT_A)
+ admin_id = await _make_member(
+ session_factory, name="picky-inviter2", tenant_id=TENANT_A, role="owner"
+ )
+ _id, token = await self._mint(
+ session_factory,
+ tenant_id=TENANT_A,
+ admin_id=admin_id,
+ )
+ await _make_tenant(session_factory, TENANT_B)
+ right_person = await _make_member(
+ session_factory,
+ name="right-person",
+ tenant_id=TENANT_B,
+ role="member",
+ )
+
+ async with _client(
+ _app(session_factory),
+ _token(right_person, "[email protected]", TENANT_B),
+ ) as client:
+ response = await client.post(f"/invitations/{token}/accept")
+
+ assert response.status_code == 200, response.text
+
+
+class TestMemberRoutes:
+ async def test_listing_shows_every_member_and_their_role(
+ self, session_factory: async_sessionmaker[AsyncSession]
+ ) -> None:
+ await _make_tenant(session_factory, TENANT_A)
+ owner_id = await _make_member(
+ session_factory, name="list-owner", tenant_id=TENANT_A, role="owner"
+ )
+ await _make_member(
+ session_factory, name="list-member", tenant_id=TENANT_A, role="member"
+ )
+ token = _token(owner_id, "[email protected]", TENANT_A)
+
+ async with _client(_app(session_factory), token) as client:
+ response = await client.get(f"/tenants/{TENANT_A}/members")
+
+ assert response.status_code == 200, response.text
+ roles = {row["name"]: row["role"] for row in response.json()}
+ assert roles == {"list-owner": "owner", "list-member": "member"}
+
+ async def test_the_last_owner_cannot_be_demoted(
+ self, session_factory: async_sessionmaker[AsyncSession]
+ ) -> None:
+ await _make_tenant(session_factory, TENANT_A)
+ owner_id = await _make_member(
+ session_factory, name="sole-owner", tenant_id=TENANT_A, role="owner"
+ )
+ token = _token(owner_id, "[email protected]", TENANT_A)
+
+ async with _client(_app(session_factory), token) as client:
+ response = await client.patch(
+ f"/tenants/{TENANT_A}/members/{owner_id}", json={"role": "member"}
+ )
+
+ assert response.status_code == 409
+
+ async with session_factory() as session:
+ membership = await session.get(TenantMember, (TENANT_A, owner_id))
+ assert membership is not None
+ assert membership.role == "owner"
+
+ async def test_an_owner_can_be_demoted_when_another_owner_remains(
+ self, session_factory: async_sessionmaker[AsyncSession]
+ ) -> None:
+ await _make_tenant(session_factory, TENANT_A)
+ owner_id = await _make_member(
+ session_factory, name="owner-one", tenant_id=TENANT_A, role="owner"
+ )
+ other_owner_id = await _make_member(
+ session_factory, name="owner-two", tenant_id=TENANT_A, role="owner"
+ )
+ token = _token(owner_id, "[email protected]", TENANT_A)
+
+ async with _client(_app(session_factory), token) as client:
+ response = await client.patch(
+ f"/tenants/{TENANT_A}/members/{other_owner_id}",
+ json={"role": "member"},
+ )
+
+ assert response.status_code == 200, response.text
+ assert response.json()["role"] == "member"
+
+ async def test_the_last_owner_cannot_be_removed(
+ self, session_factory: async_sessionmaker[AsyncSession]
+ ) -> None:
+ await _make_tenant(session_factory, TENANT_A)
+ owner_id = await _make_member(
+ session_factory, name="undeletable-owner", tenant_id=TENANT_A, role="owner"
+ )
+ token = _token(owner_id, "[email protected]", TENANT_A)
+
+ async with _client(_app(session_factory), token) as client:
+ response = await client.delete(f"/tenants/{TENANT_A}/members/{owner_id}")
+
+ assert response.status_code == 409
+
+ async with session_factory() as session:
+ assert await session.get(TenantMember, (TENANT_A, owner_id)) is not None
+
+ async def test_removing_a_member_deletes_their_membership(
+ self, session_factory: async_sessionmaker[AsyncSession]
+ ) -> None:
+ await _make_tenant(session_factory, TENANT_A)
+ owner_id = await _make_member(
+ session_factory, name="remover", tenant_id=TENANT_A, role="owner"
+ )
+ target_id = await _make_member(
+ session_factory, name="removed", tenant_id=TENANT_A, role="member"
+ )
+ token = _token(owner_id, "[email protected]", TENANT_A)
+
+ async with _client(_app(session_factory), token) as client:
+ response = await client.delete(f"/tenants/{TENANT_A}/members/{target_id}")
+
+ assert response.status_code == 200, response.text
+
+ async with session_factory() as session:
+ assert await session.get(TenantMember, (TENANT_A, target_id)) is None
+
+ async def test_removing_a_member_stops_their_api_key_working(
+ self, session_factory: async_sessionmaker[AsyncSession]
+ ) -> None:
+ """The bite test: after removal, the credential the member minted in
+ this tenant no longer resolves to anything — not merely hidden, gone.
+ A live bearer-auth attempt with the same hash would fail at the very
+ first lookup `ApiKeyStore.get_by_hash` performs.
+ """
+ await _make_tenant(session_factory, TENANT_A)
+ owner_id = await _make_member(
+ session_factory, name="key-remover", tenant_id=TENANT_A, role="owner"
+ )
+ target_id = await _make_member(
+ session_factory, name="key-holder", tenant_id=TENANT_A, role="member"
+ )
+ key_hash = await _make_api_key(
+ session_factory, tenant_id=TENANT_A, user_id=target_id
+ )
+ token = _token(owner_id, "[email protected]", TENANT_A)
+
+ async with session_factory() as session:
+ assert await ApiKeyStore().get_by_hash(session, key_hash) is not None
+
+ async with _client(_app(session_factory), token) as client:
+ response = await client.delete(f"/tenants/{TENANT_A}/members/{target_id}")
+ assert response.status_code == 200, response.text
+
+ async with session_factory() as session:
+ assert await ApiKeyStore().get_by_hash(session, key_hash) is None
+
+ async def test_removing_a_member_deletes_the_agents_they_own_here(
+ self, session_factory: async_sessionmaker[AsyncSession]
+ ) -> None:
+ await _make_tenant(session_factory, TENANT_A)
+ owner_id = await _make_member(
+ session_factory, name="agent-remover", tenant_id=TENANT_A, role="owner"
+ )
+ target_id = await _make_member(
+ session_factory, name="agent-owner", tenant_id=TENANT_A, role="member"
+ )
+ agent_id, agent_key_hash = await _make_agent_with_key(
+ session_factory, tenant_id=TENANT_A, owner_id=target_id
+ )
+ token = _token(owner_id, "[email protected]", TENANT_A)
+
+ async with _client(_app(session_factory), token) as client:
+ response = await client.delete(f"/tenants/{TENANT_A}/members/{target_id}")
+ assert response.status_code == 200, response.text
+
+ async with session_factory() as session:
+ assert await AgentStore().get(session, agent_id) is None
+ assert await ApiKeyStore().get_by_hash(session, agent_key_hash) is None
+
+ async def test_a_plain_member_cannot_remove_anyone(
+ self, session_factory: async_sessionmaker[AsyncSession]
+ ) -> None:
+ await _make_tenant(session_factory, TENANT_A)
+ member_id = await _make_member(
+ session_factory, name="powerless", tenant_id=TENANT_A, role="member"
+ )
+ target_id = await _make_member(
+ session_factory, name="untouchable", tenant_id=TENANT_A, role="member"
+ )
+ token = _token(member_id, "[email protected]", TENANT_A)
+
+ async with _client(_app(session_factory), token) as client:
+ response = await client.delete(f"/tenants/{TENANT_A}/members/{target_id}")
+
+ assert response.status_code == 403