2026-06-13 16:32:33 +02:00
|
|
|
# retoor <retoor@molodetz.nl>
|
|
|
|
|
|
|
|
|
|
from datetime import datetime, timezone
|
|
|
|
|
from starlette.requests import Request
|
|
|
|
|
from tests.conftest import BASE_URL
|
|
|
|
|
from devplacepy.database import get_table, set_setting
|
|
|
|
|
from devplacepy.utils import generate_uid
|
|
|
|
|
from devplacepy.services.openai_gateway import GatewayService
|
|
|
|
|
from devplacepy.services.openai_gateway.analytics import build_user_usage
|
|
|
|
|
from devplacepy.services.devii.config import (
|
|
|
|
|
build_settings,
|
|
|
|
|
FIELD_AI_URL,
|
|
|
|
|
FIELD_AI_MODEL,
|
|
|
|
|
FIELD_AI_KEY,
|
|
|
|
|
FIELD_MAX_ITERATIONS,
|
|
|
|
|
FIELD_PLAN_REQUIRED,
|
|
|
|
|
FIELD_VERIFY_REQUIRED,
|
|
|
|
|
)
|
|
|
|
|
def _now_iso_ai_usage_profile():
|
|
|
|
|
return datetime.now(timezone.utc).isoformat()
|
|
|
|
|
def _make_user_ai_usage_profile(role="Member"):
|
|
|
|
|
username = f"aiu_{generate_uid()[:8]}"
|
|
|
|
|
api_key = generate_uid()
|
|
|
|
|
uid = generate_uid()
|
|
|
|
|
get_table("users").insert(
|
|
|
|
|
{
|
|
|
|
|
"uid": uid,
|
|
|
|
|
"username": username,
|
Add the trust and safety subsystem and the App Store compliance work
Implements the moderation and consent obligations a social platform carries,
so the web version and any client that speaks to it enforce the same rules.
Moderation core (services/moderation/, database/moderation.py): a reportable
target registry, the content filter and its choke points, the report queue with
atomic resolution, enforcement actions, consent tracking, maturity gating, and
account deletion with a grace window.
Surfaces: POST /reports plus the member report list, /admin/moderation and the
per-report admin view, /workspaces, terms acceptance at /auth/terms, consent and
account deletion under /profile, the report button and dialog partials, the
maturity gate, and the moderation stylesheet and ReportDialog client.
Every user-generated surface stays reportable by construction: new content tables
are registered in REPORTABLE_TARGETS or listed in UNREPORTABLE_TABLES with a
reason, and the registry test fails the suite on anything left unclassified.
Docs: community guidelines, content moderation, intellectual property, privacy,
terms, contact, and the admin-only moderation operations page, plus the
moderation API group and the Devii moderation actions.
Compliance record: applecomp.md is the requirement register, applechanges.md the
gap analysis against this codebase, and appleimpl.md the implementation design
they resolve to.
Tests cover the report flow, admin moderation, consent, account deletion, terms
acceptance, workspaces, and the registry invariant across the unit, api, and e2e
tiers.
2026-08-09 00:18:20 +02:00
|
|
|
"terms_version": "1",
|
2026-06-13 16:32:33 +02:00
|
|
|
"email": f"{username}@t.dev",
|
|
|
|
|
"api_key": api_key,
|
|
|
|
|
"role": role,
|
|
|
|
|
"is_active": True,
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
return uid, username, api_key
|
|
|
|
|
def _make_request_ai_usage_profile(headers):
|
|
|
|
|
raw = [(k.lower().encode(), v.encode()) for k, v in headers.items()]
|
|
|
|
|
return Request(
|
|
|
|
|
{
|
|
|
|
|
"type": "http",
|
|
|
|
|
"method": "POST",
|
|
|
|
|
"path": "/openai/v1/chat/completions",
|
|
|
|
|
"query_string": b"",
|
|
|
|
|
"headers": raw,
|
|
|
|
|
"state": {},
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
def _seed_gateway(uid, n=3, cost=0.01, kind="user"):
|
|
|
|
|
table = get_table("gateway_usage_ledger")
|
|
|
|
|
for _ in range(n):
|
|
|
|
|
table.insert(
|
|
|
|
|
{
|
|
|
|
|
"created_at": _now_iso_ai_usage_profile(),
|
|
|
|
|
"owner_kind": kind,
|
|
|
|
|
"owner_id": uid,
|
|
|
|
|
"backend": "chat",
|
|
|
|
|
"model": "molodetz",
|
|
|
|
|
"success": 1,
|
|
|
|
|
"status_code": 200,
|
|
|
|
|
"prompt_tokens": 1000,
|
|
|
|
|
"completion_tokens": 300,
|
|
|
|
|
"total_tokens": 1300,
|
|
|
|
|
"upstream_latency_ms": 1500.0,
|
|
|
|
|
"tokens_per_second": 40.0,
|
|
|
|
|
"cost_usd": cost,
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
def _devii_cfg():
|
|
|
|
|
return {
|
|
|
|
|
FIELD_AI_URL: "",
|
|
|
|
|
FIELD_AI_MODEL: "",
|
|
|
|
|
FIELD_AI_KEY: "INTERNAL-KEY",
|
|
|
|
|
FIELD_MAX_ITERATIONS: "40",
|
|
|
|
|
FIELD_PLAN_REQUIRED: "1",
|
|
|
|
|
FIELD_VERIFY_REQUIRED: "1",
|
|
|
|
|
}
|
|
|
|
|
def _goto_profile(page, username):
|
|
|
|
|
page.goto(f"{BASE_URL}/profile/{username}", wait_until="domcontentloaded")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_admin_user_ai_usage_endpoint_returns_data(alice, seeded_db):
|
|
|
|
|
page, _ = alice
|
|
|
|
|
alice_uid = get_table("users").find_one(username=seeded_db["alice"]["username"])[
|
|
|
|
|
"uid"
|
|
|
|
|
]
|
|
|
|
|
resp = page.request.get(f"{BASE_URL}/admin/users/{alice_uid}/ai-usage?hours=24")
|
|
|
|
|
assert resp.status == 200
|
|
|
|
|
data = resp.json()
|
|
|
|
|
assert data["owner_id"] == alice_uid
|
|
|
|
|
assert "cost" in data and "requests" in data and "tokens" in data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_member_forbidden_from_user_ai_usage_endpoint(bob, seeded_db):
|
|
|
|
|
page, _ = bob
|
|
|
|
|
bob_uid = get_table("users").find_one(username=seeded_db["bob"]["username"])["uid"]
|
2026-07-04 22:24:59 +02:00
|
|
|
resp = page.request.get(
|
|
|
|
|
f"{BASE_URL}/admin/users/{bob_uid}/ai-usage",
|
|
|
|
|
headers={"Accept": "application/json"},
|
|
|
|
|
)
|
2026-06-13 16:32:33 +02:00
|
|
|
assert resp.status == 403
|