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
22 changes: 10 additions & 12 deletions api/core/ops/base_trace_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from core.ops.entities.config_entity import BaseTracingConfig
from core.ops.entities.trace_entity import BaseTraceInfo
from extensions.ext_database import db
from models import Account, App, TenantAccountJoin
from models import Account, App


class BaseTraceInstance(ABC):
Expand All @@ -31,16 +31,19 @@ def trace(self, trace_info: BaseTraceInfo):

def get_service_account_with_tenant(self, app_id: str) -> Account:
"""
Get service account for an app and set up its tenant.
Get the app creator with the app's tenant and membership role.

Trace tasks may run after the creator switches workspaces. Bind only the
returned account's context without changing the creator's current workspace.

Args:
app_id: The ID of the app

Returns:
Account: The service account with tenant set up
Account: The service account with the app's tenant set up

Raises:
ValueError: If app, creator account or tenant cannot be found
ValueError: If the app, creator account, app tenant or membership cannot be found
"""
with Session(db.engine, expire_on_commit=False) as session:
# Get the app to find its creator
Expand All @@ -56,13 +59,8 @@ def get_service_account_with_tenant(self, app_id: str) -> Account:
if not service_account:
raise ValueError(f"Creator account with id {app.created_by} not found for app {app_id}")

current_tenant = session.scalar(
select(TenantAccountJoin)
.where(TenantAccountJoin.account_id == service_account.id, TenantAccountJoin.current.is_(True))
.limit(1)
)
if not current_tenant:
raise ValueError(f"Current tenant not found for account {service_account.id}")
service_account.set_tenant_id_with_session(current_tenant.tenant_id, session=session)
service_account.set_tenant_id_with_session(app.tenant_id, session=session)
if service_account.current_tenant_id != app.tenant_id:
raise ValueError(f"App tenant {app.tenant_id} not found for account {service_account.id}")

return service_account
96 changes: 92 additions & 4 deletions api/tests/unit_tests/core/ops/test_base_trace_instance.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from types import SimpleNamespace

import pytest
from sqlalchemy import Engine
from sqlalchemy import Engine, select
from sqlalchemy.orm import Session

from core.ops.base_trace_instance import BaseTraceInstance
Expand Down Expand Up @@ -90,12 +90,13 @@ def test_get_service_account_with_tenant_tenant_not_found(sqlite_session: Sessio
_persist_app(sqlite_session, created_by="creator_id")
_persist_account(sqlite_session)

with pytest.raises(ValueError, match="Current tenant not found for account creator_id"):
with pytest.raises(ValueError, match="App tenant tenant_id not found for account creator_id"):
_trace_instance().get_service_account_with_tenant("some_app_id")


@pytest.mark.parametrize("current", [True, False])
@pytest.mark.parametrize("sqlite_session", [TABLES], indirect=True)
def test_get_service_account_with_tenant_success(sqlite_session: Session):
def test_get_service_account_with_tenant_success(sqlite_session: Session, current: bool):
_persist_app(sqlite_session, created_by="creator_id")
_persist_account(sqlite_session)
tenant = Tenant(name="Workspace")
Expand All @@ -106,7 +107,7 @@ def test_get_service_account_with_tenant_success(sqlite_session: Session):
TenantAccountJoin(
tenant_id=tenant.id,
account_id="creator_id",
current=True,
current=current,
role=TenantAccountRole.OWNER,
),
]
Expand All @@ -118,3 +119,90 @@ def test_get_service_account_with_tenant_success(sqlite_session: Session):
assert result.id == "creator_id"
assert result.current_tenant_id == "tenant_id"
assert result.current_role == TenantAccountRole.OWNER


@pytest.mark.parametrize("sqlite_session", [TABLES], indirect=True)
def test_get_service_account_with_tenant_uses_app_workspace(sqlite_session: Session):
_persist_app(sqlite_session, created_by="creator_id")
_persist_account(sqlite_session)
app_tenant = Tenant(name="App workspace")
app_tenant.id = "tenant_id"
current_tenant = Tenant(name="Current workspace")
current_tenant.id = "other_tenant_id"
sqlite_session.add_all(
[
app_tenant,
current_tenant,
TenantAccountJoin(
tenant_id="tenant_id",
account_id="creator_id",
current=False,
role=TenantAccountRole.EDITOR,
),
TenantAccountJoin(
tenant_id="other_tenant_id",
account_id="creator_id",
current=True,
role=TenantAccountRole.OWNER,
),
]
)
sqlite_session.commit()

result = _trace_instance().get_service_account_with_tenant("some_app_id")

assert result.id == "creator_id"
assert result.current_tenant_id == "tenant_id"
assert result.current_role == TenantAccountRole.EDITOR
sqlite_session.expire_all()
memberships = sqlite_session.scalars(
select(TenantAccountJoin).where(TenantAccountJoin.account_id == "creator_id")
).all()
assert {membership.tenant_id: membership.current for membership in memberships} == {
"tenant_id": False,
"other_tenant_id": True,
}


@pytest.mark.parametrize("sqlite_session", [TABLES], indirect=True)
def test_get_service_account_with_tenant_requires_app_membership(sqlite_session: Session):
_persist_app(sqlite_session, created_by="creator_id")
_persist_account(sqlite_session)
app_tenant = Tenant(name="App workspace")
app_tenant.id = "tenant_id"
current_tenant = Tenant(name="Current workspace")
current_tenant.id = "other_tenant_id"
sqlite_session.add_all(
[
app_tenant,
current_tenant,
TenantAccountJoin(
tenant_id="other_tenant_id",
account_id="creator_id",
current=True,
role=TenantAccountRole.OWNER,
),
]
)
sqlite_session.commit()

with pytest.raises(ValueError, match="App tenant tenant_id not found for account creator_id"):
_trace_instance().get_service_account_with_tenant("some_app_id")


@pytest.mark.parametrize("sqlite_session", [TABLES], indirect=True)
def test_get_service_account_with_tenant_requires_existing_app_tenant(sqlite_session: Session):
_persist_app(sqlite_session, created_by="creator_id")
_persist_account(sqlite_session)
sqlite_session.add(
TenantAccountJoin(
tenant_id="tenant_id",
account_id="creator_id",
current=True,
role=TenantAccountRole.OWNER,
)
)
sqlite_session.commit()

with pytest.raises(ValueError, match="App tenant tenant_id not found for account creator_id"):
_trace_instance().get_service_account_with_tenant("some_app_id")
Loading