Files
smart-org/apps/api/tests/test_seed.py
T
mjalmoudy94 d532929189 feat(integration): prepaid billing, engine UI, credits console, direct tasks, v2 final
- Prepaid credit system: org wallet, debit on usage, chat gate, admin credit console
- Engine UI: /manager, /tasks, /tasks/[id], /workforce, /dashboard, /traces pages
- Direct task creation (HTTP POST /tasks, MCP tasks_create) at pending_manager_review
- Pause/resume/cancel/answer actions in engine API and UI
- Employee cap (7 per org default), review pipeline guard (active reviewer required)
- Scheduler: wide window prevents org starvation; archived employees excluded from cap
- Tenant isolation: project-id cross-org 404, engine never uses another tenant's sandbox
- Order route fix: manager built with pipeline states + employee profiles
- Contract extension: ModelGrantPort.get_org_policy with DefaultOrgPolicy
- Engine event types in @agy/contracts (task.step, task.gate, run.log, run.tool, team.changed)
- api-engine.ts typed client; api-credit.ts client
- Alembic migration c4d5e6f7a8b9: prepaid credit + engine UI tables
- 859 tests passed, 1 skipped; ruff/mypy/tsc/eslint/vitest all green

Refs: PLAN-v2.md §9 (T1–T43), todo-v2-final.md
2026-10-14 23:19:05 +03:30

255 lines
8.9 KiB
Python

"""§17 — the seed produces a stack a developer can actually log into.
This file exists because of two real bugs, both found by trying to *use* a
seeded install rather than by reading the code:
1. The seed created ``demo@agy.local`` by writing straight to the database,
bypassing request validation — while the login endpoint rejects ``.local`` as
a special-use TLD. The account existed and was unreachable.
2. Even the account that *could* log in belonged to no organization, so every
screen rendered empty and the demo looked broken.
Neither was caught by the rest of the suite, because every other test signs up a
fresh user at ``@example.com`` and creates its own organization. The seed was
simply never exercised end to end. These tests close that hole.
"""
from __future__ import annotations
import pytest_asyncio
from httpx import AsyncClient
from sqlalchemy import func, select
from tests.conftest import auth_header
@pytest_asyncio.fixture
async def seeded_install(monkeypatch, engine, session_factory):
"""Run the real ``app.seed`` against the in-memory test database.
``app.seed`` opens its own engine and session factory, so both module-level
names are swapped for the test ones. The seed code itself is untouched —
testing a copy of it would prove nothing.
"""
import app.seed as seed_module
monkeypatch.setattr(seed_module, "engine", engine)
monkeypatch.setattr(seed_module, "SessionFactory", session_factory)
await seed_module.seed()
return seed_module
async def test_seeded_demo_user_can_log_in(client: AsyncClient, seeded_install):
"""The credentials the seed prints must actually work."""
response = await client.post(
"/api/auth/login",
json={
"email": seeded_install.DEMO_EMAIL,
"password": seeded_install.DEMO_PASSWORD,
},
)
assert response.status_code == 200, (
f"the seeded demo account cannot log in: {response.text}"
)
assert response.json()["access_token"]
async def test_seeded_platform_owner_can_log_in(client: AsyncClient, seeded_install):
response = await client.post(
"/api/auth/login",
json={
"email": seeded_install.PLATFORM_OWNER_EMAIL,
"password": seeded_install.PLATFORM_OWNER_PASSWORD,
},
)
assert response.status_code == 200, response.text
me = await client.get(
"/api/auth/me", headers=auth_header(response.json()["access_token"])
)
assert me.status_code == 200, me.text
assert me.json()["user"]["is_platform_owner"] is True
async def test_seeded_demo_manager_owns_the_sample_organization(
client: AsyncClient, seeded_install
):
"""A login that lands on an empty dashboard is a broken demo.
The demo account is a *manager* — the tier that creates organizations — so
it must own the sample organization and land on a populated dashboard.
"""
login = await client.post(
"/api/auth/login",
json={
"email": seeded_install.DEMO_EMAIL,
"password": seeded_install.DEMO_PASSWORD,
},
)
assert login.status_code == 200, login.text
assert login.json()["user"]["platform_role"] == "manager"
assert login.json()["user"]["is_manager"] is True
assert login.json()["user"]["is_platform_owner"] is False
me = await client.get(
"/api/auth/me", headers=auth_header(login.json()["access_token"])
)
organizations = me.json()["organizations"]
assert organizations, "the demo manager belongs to no organization"
assert organizations[0]["name"] == seeded_install.DEMO_ORG_NAME
assert organizations[0]["role"] == "owner"
async def test_platform_owner_is_not_joined_to_any_organization(
client: AsyncClient, seeded_install
):
"""The product rule: the owner observes tenants from the console.
Auto-joining the owner to a tenant would put the single owner seat inside
customer data, which is exactly what the hierarchy is designed to avoid.
"""
login = await client.post(
"/api/auth/login",
json={
"email": seeded_install.PLATFORM_OWNER_EMAIL,
"password": seeded_install.PLATFORM_OWNER_PASSWORD,
},
)
assert login.status_code == 200, login.text
me = await client.get(
"/api/auth/me", headers=auth_header(login.json()["access_token"])
)
assert me.status_code == 200, me.text
assert me.json()["user"]["is_platform_owner"] is True
assert me.json()["user"]["platform_role"] == "owner"
assert me.json()["organizations"] == []
async def test_seeded_install_can_use_the_product(
client: AsyncClient, seeded_install, session_factory
):
"""The seed's platform rows are enough to create a project and run a task.
A seed that satisfies the login test but leaves the catalogue empty would
still fail the moment a developer tried anything real.
Decision D-D: the run is the legacy path's, so the task is a pre-v2 row (no pipeline
state). A task made over HTTP enters the engine and is refused by the run button.
"""
from tests.legacy_tasks import legacy_task
login = await client.post(
"/api/auth/login",
json={
"email": seeded_install.DEMO_EMAIL,
"password": seeded_install.DEMO_PASSWORD,
},
)
headers = auth_header(login.json()["access_token"])
me = (await client.get("/api/auth/me", headers=headers)).json()
org_id = me["organizations"][0]["id"]
# The platform catalogue the seed installs must be visible to a tenant.
templates = await client.get(
f"/api/organizations/{org_id}/agent-templates", headers=headers
)
assert templates.status_code == 200, templates.text
assert templates.json(), "the seed must install agent templates"
sandbox_templates = await client.get(
f"/api/organizations/{org_id}/sandbox/templates", headers=headers
)
assert sandbox_templates.json(), "the seed must install sandbox templates"
project_id = (
await client.post(
f"/api/organizations/{org_id}/projects",
json={"name": "پروژهٔ seed"},
headers=headers,
)
).json()["id"]
template = templates.json()[0]
employee_id = (
await client.post(
f"/api/organizations/{org_id}/employees",
json={
"name": "کارمند seed",
"role_key": template["role_key"],
"template_id": template["id"],
},
headers=headers,
)
).json()["id"]
task_id = await legacy_task(
session_factory,
org_id,
me["user"]["id"],
title="کار seed",
project_id=project_id,
assigned_employee_id=employee_id,
)
run = await client.post(
f"/api/organizations/{org_id}/tasks/{task_id}/runs",
json={"employee_id": employee_id},
headers=headers,
)
assert run.status_code in (200, 201), run.text
assert run.json()["status"] == "completed", (
"a seeded install must be able to route to a model and finish a run"
)
async def test_seed_is_idempotent(client: AsyncClient, seeded_install, session_factory):
"""Re-running the seed must not duplicate anything.
The memberships and the wallet are now created by get-or-create rather than
as a side effect of creating the organization, so this guards the parts that
were easy to double up.
"""
import app.seed as seed_module
from app.infrastructure.db.models import (
AgentTemplateRow,
CreditWalletRow,
MembershipRow,
OrganizationRow,
UserRow,
)
async def counts() -> dict[str, int]:
async with session_factory() as session:
return {
"organizations": int(
(await session.execute(select(func.count()).select_from(OrganizationRow)))
.scalar_one()
),
"memberships": int(
(await session.execute(select(func.count()).select_from(MembershipRow)))
.scalar_one()
),
"wallets": int(
(await session.execute(select(func.count()).select_from(CreditWalletRow)))
.scalar_one()
),
"users": int(
(await session.execute(select(func.count()).select_from(UserRow)))
.scalar_one()
),
"templates": int(
(await session.execute(select(func.count()).select_from(AgentTemplateRow)))
.scalar_one()
),
}
before = await counts()
await seed_module.seed()
after = await counts()
assert after == before, f"re-seeding changed row counts: {before} -> {after}"
assert after["organizations"] == 1
# Only the demo manager: the platform owner stays out of tenants.
assert after["memberships"] == 1