AP0.2: Auth, Tenant, Actor Foundation mit Sessions, TenantContext und Tests.
Deploy Development / deploy (push) Successful in 38s
Test Suite / pytest-backend (push) Failing after 7s
Test Suite / k6 /api/health Baseline (push) Has been skipped
Test Suite / playwright-smoke (push) Has been skipped
Test Suite / lint-backend (push) Successful in 2s
Test Suite / compose-smoke (push) Has been skipped
Deploy Development / deploy (push) Successful in 38s
Test Suite / pytest-backend (push) Failing after 7s
Test Suite / k6 /api/health Baseline (push) Has been skipped
Test Suite / playwright-smoke (push) Has been skipped
Test Suite / lint-backend (push) Successful in 2s
Test Suite / compose-smoke (push) Has been skipped
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
b5efdbbc2e
commit
564008be13
25 files changed
+1340
-3
No files matched your search
@@ -0,0 +1,49 @@
|
||||
"""Shared pytest fixtures for AP0.2 auth tests."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
|
||||
import pytest
|
||||
|
||||
os.environ.setdefault("SKIP_DB_MIGRATE", "1")
|
||||
os.environ.setdefault("SKIP_BOOTSTRAP", "1")
|
||||
|
||||
|
||||
def _db_available() -> bool:
|
||||
try:
|
||||
import psycopg2
|
||||
|
||||
conn = psycopg2.connect(
|
||||
host=os.getenv("DB_HOST", "localhost"),
|
||||
port=os.getenv("DB_PORT", "5432"),
|
||||
dbname=os.getenv("DB_NAME", "kairo_dev"),
|
||||
user=os.getenv("DB_USER", "kairo_dev"),
|
||||
password=os.getenv("DB_PASSWORD", "dev_password"),
|
||||
)
|
||||
conn.close()
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
pytestmark = pytest.mark.skipif(not _db_available(), reason="PostgreSQL nicht erreichbar")
|
||||
|
||||
|
||||
@pytest.fixture(scope="session", autouse=True)
|
||||
def _run_migrations():
|
||||
import run_migrations
|
||||
|
||||
assert run_migrations.main() == 0
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def client():
|
||||
import importlib
|
||||
|
||||
import main as main_module
|
||||
|
||||
importlib.reload(main_module)
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
return TestClient(main_module.app)
|
||||
@@ -0,0 +1,91 @@
|
||||
"""Test data factories (PostgreSQL required)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import uuid
|
||||
|
||||
from auth import hash_password
|
||||
from db import get_connection
|
||||
from services.actors import create_actor
|
||||
|
||||
|
||||
def create_tenant(*, slug: str | None = None, name: str = "Test Tenant") -> str:
|
||||
slug = slug or f"t-{uuid.uuid4().hex[:10]}"
|
||||
conn = get_connection()
|
||||
try:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"INSERT INTO tenants (slug, name) VALUES (%s, %s) RETURNING id",
|
||||
(slug, name),
|
||||
)
|
||||
tenant_id = str(cur.fetchone()[0])
|
||||
conn.commit()
|
||||
return tenant_id
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def create_user(
|
||||
*,
|
||||
email: str | None = None,
|
||||
password: str = "test-password-123",
|
||||
display_name: str = "Test User",
|
||||
portal_role: str = "user",
|
||||
) -> dict:
|
||||
email = email or f"user-{uuid.uuid4().hex[:8]}@test.local"
|
||||
conn = get_connection()
|
||||
try:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO users (email, password_hash, display_name, portal_role)
|
||||
VALUES (%s, %s, %s, %s)
|
||||
RETURNING id, email, display_name, portal_role
|
||||
""",
|
||||
(email, hash_password(password), display_name, portal_role),
|
||||
)
|
||||
row = cur.fetchone()
|
||||
conn.commit()
|
||||
return {
|
||||
"id": str(row[0]),
|
||||
"email": row[1],
|
||||
"display_name": row[2],
|
||||
"portal_role": row[3],
|
||||
"password": password,
|
||||
}
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def add_membership(*, tenant_id: str, user_id: str, tenant_role: str = "member") -> None:
|
||||
conn = get_connection()
|
||||
try:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO tenant_memberships (tenant_id, user_id, tenant_role)
|
||||
VALUES (%s, %s, %s)
|
||||
ON CONFLICT (tenant_id, user_id) DO UPDATE SET tenant_role = EXCLUDED.tenant_role
|
||||
""",
|
||||
(tenant_id, user_id, tenant_role),
|
||||
)
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def provision_user_in_tenant(
|
||||
*,
|
||||
tenant_role: str = "member",
|
||||
portal_role: str = "user",
|
||||
) -> dict:
|
||||
tenant_id = create_tenant()
|
||||
user = create_user(portal_role=portal_role)
|
||||
add_membership(tenant_id=tenant_id, user_id=user["id"], tenant_role=tenant_role)
|
||||
actor = create_actor(
|
||||
tenant_id=tenant_id,
|
||||
actor_type="human",
|
||||
name=user["display_name"],
|
||||
user_id=user["id"],
|
||||
)
|
||||
return {**user, "tenant_id": tenant_id, "actor_id": actor["id"]}
|
||||
@@ -0,0 +1,80 @@
|
||||
"""Auth endpoint and session tests."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from auth import AUTH_HEADER, get_session
|
||||
from tests.factories import provision_user_in_tenant
|
||||
|
||||
|
||||
def test_login_logout_and_session(client):
|
||||
user = provision_user_in_tenant()
|
||||
|
||||
login = client.post(
|
||||
"/api/auth/login",
|
||||
json={"email": user["email"], "password": user["password"]},
|
||||
)
|
||||
assert login.status_code == 200
|
||||
payload = login.json()
|
||||
assert "token" in payload
|
||||
assert payload["user"]["email"] == user["email"]
|
||||
token = payload["token"]
|
||||
|
||||
me = client.get("/api/me", headers={AUTH_HEADER: token})
|
||||
assert me.status_code == 200
|
||||
assert me.json()["email"] == user["email"]
|
||||
assert me.json()["active_tenant_id"] == user["tenant_id"]
|
||||
|
||||
logout = client.post("/api/auth/logout", headers={AUTH_HEADER: token})
|
||||
assert logout.status_code == 200
|
||||
assert get_session(token) is None
|
||||
|
||||
me_after = client.get("/api/me", headers={AUTH_HEADER: token})
|
||||
assert me_after.status_code == 401
|
||||
|
||||
|
||||
def test_login_invalid_password(client):
|
||||
user = provision_user_in_tenant()
|
||||
response = client.post(
|
||||
"/api/auth/login",
|
||||
json={"email": user["email"], "password": "wrong-password"},
|
||||
)
|
||||
assert response.status_code == 401
|
||||
|
||||
|
||||
def test_me_requires_auth(client):
|
||||
assert client.get("/api/me").status_code == 401
|
||||
|
||||
|
||||
def test_me_context_returns_tenant_and_actor(client):
|
||||
user = provision_user_in_tenant(tenant_role="admin")
|
||||
login = client.post(
|
||||
"/api/auth/login",
|
||||
json={"email": user["email"], "password": user["password"]},
|
||||
)
|
||||
token = login.json()["token"]
|
||||
|
||||
ctx = client.get("/api/me/context", headers={AUTH_HEADER: token})
|
||||
assert ctx.status_code == 200
|
||||
body = ctx.json()
|
||||
assert body["tenant"]["id"] == user["tenant_id"]
|
||||
assert body["tenant"]["role"] == "admin"
|
||||
assert body["actor"]["id"] == user["actor_id"]
|
||||
assert body["actor"]["type"] == "human"
|
||||
|
||||
|
||||
def test_tenant_switch_rejects_invalid_membership(client):
|
||||
user = provision_user_in_tenant()
|
||||
other = provision_user_in_tenant()
|
||||
|
||||
login = client.post(
|
||||
"/api/auth/login",
|
||||
json={"email": user["email"], "password": user["password"]},
|
||||
)
|
||||
token = login.json()["token"]
|
||||
|
||||
switch = client.post(
|
||||
"/api/me/tenant",
|
||||
headers={AUTH_HEADER: token},
|
||||
json={"tenant_id": other["tenant_id"]},
|
||||
)
|
||||
assert switch.status_code == 403
|
||||
@@ -7,6 +7,7 @@ from fastapi.testclient import TestClient
|
||||
@pytest.fixture()
|
||||
def client(monkeypatch):
|
||||
monkeypatch.setenv("SKIP_DB_MIGRATE", "1")
|
||||
monkeypatch.setenv("SKIP_BOOTSTRAP", "1")
|
||||
import importlib
|
||||
|
||||
import main as main_module
|
||||
|
||||
@@ -32,6 +32,7 @@ def test_migration_runner_finds_migrations():
|
||||
files = run_migrations.migration_files(migrations_dir)
|
||||
names = [name for name, _ in files]
|
||||
assert "001_init_core" in names
|
||||
assert "002_auth_identity_tenant_actor" in names
|
||||
|
||||
|
||||
def test_migration_runner_is_idempotent():
|
||||
@@ -42,6 +43,7 @@ def test_migration_runner_is_idempotent():
|
||||
executed = run_migrations.executed_migrations(conn)
|
||||
conn.close()
|
||||
assert "001_init_core" in executed
|
||||
assert "002_auth_identity_tenant_actor" in executed
|
||||
|
||||
|
||||
def test_core_table_exists():
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
"""Actor and TenantContext tests."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from auth import AUTH_HEADER
|
||||
from services.actors import create_actor
|
||||
from tests.factories import create_tenant, provision_user_in_tenant
|
||||
|
||||
|
||||
def test_human_actor_linked_to_user(client):
|
||||
user = provision_user_in_tenant()
|
||||
login = client.post(
|
||||
"/api/auth/login",
|
||||
json={"email": user["email"], "password": user["password"]},
|
||||
)
|
||||
token = login.json()["token"]
|
||||
ctx = client.get("/api/me/context", headers={AUTH_HEADER: token}).json()
|
||||
assert ctx["actor"]["type"] == "human"
|
||||
assert ctx["actor"]["id"] == user["actor_id"]
|
||||
|
||||
|
||||
def test_agent_actor_without_user(client):
|
||||
tenant_id = create_tenant()
|
||||
agent = create_actor(tenant_id=tenant_id, actor_type="agent", name="Planner Agent")
|
||||
assert agent["user_id"] is None
|
||||
assert agent["actor_type"] == "agent"
|
||||
|
||||
|
||||
def test_working_group_and_external_system_actors(client):
|
||||
tenant_id = create_tenant()
|
||||
wg = create_actor(tenant_id=tenant_id, actor_type="working_group", name="Steering Group")
|
||||
ext = create_actor(tenant_id=tenant_id, actor_type="external_system", name="Jira")
|
||||
assert wg["actor_type"] == "working_group"
|
||||
assert ext["actor_type"] == "external_system"
|
||||
|
||||
|
||||
def test_portal_role_separate_from_tenant_role(client):
|
||||
user = provision_user_in_tenant(tenant_role="member", portal_role="admin")
|
||||
login = client.post(
|
||||
"/api/auth/login",
|
||||
json={"email": user["email"], "password": user["password"]},
|
||||
)
|
||||
token = login.json()["token"]
|
||||
ctx = client.get("/api/me/context", headers={AUTH_HEADER: token}).json()
|
||||
assert ctx["portal_role"] == "admin"
|
||||
assert ctx["tenant"]["role"] == "member"
|
||||
Reference in new issue
Block a user