[verified] harden Sales Trainer and add PostgreSQL foundation
This commit is contained in:
1 parent
6811dc1db9
commit
dbfce9aa54
179 files changed
+19955
-1505
No files matched your search
+14
-8
@@ -1,12 +1,18 @@
|
||||
**/.venv/
|
||||
venv/
|
||||
**/__pycache__/
|
||||
*.pyc
|
||||
**/node_modules/
|
||||
backend/data/
|
||||
data/
|
||||
# Secrets and local state
|
||||
.env
|
||||
.env.*
|
||||
!.env.example
|
||||
backend/data/
|
||||
backend/.venv/
|
||||
|
||||
# Build and tooling
|
||||
**/node_modules/
|
||||
frontend/dist/
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
.pytest_cache/
|
||||
**/*.log
|
||||
.DS_Store
|
||||
.git/
|
||||
.tmp/
|
||||
.git/
|
||||
.gitignore
|
||||
@@ -10,6 +10,7 @@ build/
|
||||
# Node / Vue
|
||||
node_modules/
|
||||
frontend/dist/
|
||||
frontend/test-results/
|
||||
|
||||
# Env / secrets
|
||||
.env
|
||||
|
||||
+19
-26
@@ -1,39 +1,32 @@
|
||||
# Sales Trainer — single-container runtime (Python 3.11 + Flask)
|
||||
#
|
||||
# The Vue frontend is BUILT LOCALLY (`cd frontend && npm run build`) and the
|
||||
# resulting `frontend/dist/` is committed to the repo. This image does NOT run
|
||||
# npm/node at all — it just copies the prebuilt SPA and serves it via Flask.
|
||||
# This eliminates `vite: not found` / npm postinstall flakiness from the build.
|
||||
#
|
||||
# To update the UI: edit frontend/src, run `npm run build` locally, commit dist/.
|
||||
# Sales Trainer — reproducible single-container build.
|
||||
# The frontend is built inside the image so a clean clone never depends on
|
||||
# locally generated/stale frontend/dist files.
|
||||
|
||||
FROM node:22-bookworm-slim AS frontend-builder
|
||||
WORKDIR /build/frontend
|
||||
COPY frontend/package.json frontend/package-lock.json ./
|
||||
RUN npm ci
|
||||
COPY frontend/ ./
|
||||
RUN npm run build
|
||||
|
||||
FROM python:3.11-slim
|
||||
|
||||
# Runtime deps (none required beyond Python + pip packages; curl for healthcheck)
|
||||
RUN apt-get update \
|
||||
&& apt-get install -y --no-install-recommends curl ca-certificates \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# 1) Copy prebuilt frontend SPA (built locally, committed to repo)
|
||||
COPY frontend/dist/ ./frontend/dist/
|
||||
# Copy the exact artifact produced by the builder stage, not the build context.
|
||||
COPY --from=frontend-builder /build/frontend/dist/ ./frontend/dist/
|
||||
|
||||
# 2) Install backend deps
|
||||
COPY backend/requirements.txt ./backend/
|
||||
RUN pip install --no-cache-dir -r backend/requirements.txt
|
||||
|
||||
# 3) Copy backend source (and stub frontend dir so static serving finds dist)
|
||||
COPY backend/requirements.txt backend/requirements.lock.txt ./backend/
|
||||
RUN pip install --no-cache-dir --require-hashes -r backend/requirements.lock.txt
|
||||
COPY backend/ ./backend/
|
||||
|
||||
# Run
|
||||
WORKDIR /app/backend
|
||||
# Explicit data dir so operators mount a persistent volume here.
|
||||
# Without a volume, ALL user data (incl. the admin password change from first-run
|
||||
# setup) is lost on every container recreate -> re-login with the new password fails.
|
||||
# Persist with: docker run -v ./data:/app/backend/data (or compose ./data)
|
||||
ENV DATA_DIR=/app/backend/data
|
||||
ENV FLASK_DEBUG=0
|
||||
EXPOSE 5001
|
||||
VOLUME ["/app/backend/data"]
|
||||
CMD ["python", "run.py"]
|
||||
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
|
||||
CMD python -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:5001/ready', timeout=4)" || exit 1
|
||||
# Keep one worker while JSON stores are authoritative. Multi-worker operation
|
||||
# is gated on the PostgreSQL repository cutover and shared rate-limit store.
|
||||
CMD ["gunicorn", "--bind", "0.0.0.0:5001", "--workers", "1", "--threads", "4", "--access-logfile", "-", "--error-logfile", "-", "run:app"]
|
||||
@@ -45,7 +45,7 @@ Built on patterns from the **CrowdSight / MiroFish** swarm engine and clean-room
|
||||
cd backend
|
||||
uv venv --python 3.11 .venv
|
||||
uv pip install -r requirements.txt --python .venv/bin/python
|
||||
cp .env.example .env # edit LLM keys + JWT_SECRET
|
||||
cp .env.example .env # set LLM keys + JWT_SECRET + BOOTSTRAP_ADMIN_PASSWORD
|
||||
uv run python run.py # Flask on :5001
|
||||
|
||||
# frontend (separate terminal)
|
||||
@@ -54,13 +54,15 @@ npm install
|
||||
npm run dev # Vite on :3000 -> proxies /api to :5001
|
||||
```
|
||||
|
||||
The first run creates a default super-admin: **username `admin` / password `1234`**.
|
||||
On first login you'll be **forced to set your email + change the password** before using the app.
|
||||
The first run creates a default super-admin named **`admin`** using the
|
||||
`BOOTSTRAP_ADMIN_PASSWORD` environment variable. The value is never printed or
|
||||
hard-coded by the application. On first login you'll be **forced to set your
|
||||
email + change the password** before using the app.
|
||||
|
||||
### Docker / EasyPanel
|
||||
|
||||
```bash
|
||||
cp .env.example .env # set LLM_API_KEY + a strong JWT_SECRET
|
||||
cp .env.example .env # set LLM_API_KEY + strong JWT_SECRET + bootstrap password
|
||||
docker compose up -d # single container serving frontend + API on :5001
|
||||
```
|
||||
|
||||
@@ -115,7 +117,7 @@ Real-model verification requires a live `LLM_API_KEY` in `.env`.
|
||||
|
||||
| Role | Username | Password |
|
||||
|------|----------|----------|
|
||||
| super_admin | `admin` | `1234` (forced to set email + change on first login) |
|
||||
| super_admin | `admin` | `BOOTSTRAP_ADMIN_PASSWORD` (forced to set email + change on first login) |
|
||||
|
||||
Admins create additional users (users/login has no self-registration).
|
||||
|
||||
|
||||
@@ -4,8 +4,10 @@ LLM_BASE_URL=
|
||||
LLM_MODEL_NAME=deepseek-chat
|
||||
LLM_API_KEY=replace_me
|
||||
|
||||
# Auth
|
||||
JWT_SECRET=change_this_secret
|
||||
# Auth — set both values explicitly; never commit real values.
|
||||
APP_ENV=development
|
||||
JWT_SECRET=replace_with_a_random_secret_at_least_32_chars
|
||||
BOOTSTRAP_ADMIN_PASSWORD=replace_with_a_strong_initial_password
|
||||
|
||||
# Storage root (relative to backend/)
|
||||
DATA_DIR=./data
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
[alembic]
|
||||
script_location = %(here)s/migrations
|
||||
prepend_sys_path = %(here)s
|
||||
path_separator = os
|
||||
sqlalchemy.url =
|
||||
|
||||
[loggers]
|
||||
keys = root,sqlalchemy,alembic
|
||||
|
||||
[handlers]
|
||||
keys = console
|
||||
|
||||
[formatters]
|
||||
keys = generic
|
||||
|
||||
[logger_root]
|
||||
level = WARN
|
||||
handlers = console
|
||||
qualname =
|
||||
|
||||
[logger_sqlalchemy]
|
||||
level = WARN
|
||||
handlers =
|
||||
qualname = sqlalchemy.engine
|
||||
|
||||
[logger_alembic]
|
||||
level = INFO
|
||||
handlers =
|
||||
qualname = alembic
|
||||
|
||||
[handler_console]
|
||||
class = StreamHandler
|
||||
args = (sys.stderr,)
|
||||
level = NOTSET
|
||||
formatter = generic
|
||||
|
||||
[formatter_generic]
|
||||
format = %(levelname)-5.5s [%(name)s] %(message)s
|
||||
datefmt = %H:%M:%S
|
||||
+124
-39
@@ -5,7 +5,14 @@ from flask import Blueprint, jsonify, request
|
||||
|
||||
from ..auth.users import AuthError
|
||||
from ..config import Config
|
||||
from .helpers import ApiError, current_user, require_auth, require_roles
|
||||
from .helpers import (
|
||||
ApiError,
|
||||
current_user,
|
||||
internal_error,
|
||||
request_json_object,
|
||||
require_auth,
|
||||
require_roles,
|
||||
)
|
||||
|
||||
admin_bp = Blueprint("admin", __name__)
|
||||
|
||||
@@ -58,30 +65,37 @@ def list_orgs():
|
||||
@require_roles("super_admin")
|
||||
def update_org(org_id: str):
|
||||
"""Platform: set org plan / seats / active. super_admin only."""
|
||||
data = request.get_json(silent=True) or {}
|
||||
org = _store().orgs.get_or_none(org_id)
|
||||
if not org:
|
||||
raise ApiError("org not found", 404)
|
||||
data = request_json_object()
|
||||
fields = {}
|
||||
if "plan" in data:
|
||||
plan = str(data["plan"]).strip()
|
||||
if not isinstance(data["plan"], str):
|
||||
raise ApiError("plan must be a string", 400)
|
||||
plan = data["plan"].strip()
|
||||
if plan not in ("trial", "paid", "enterprise"):
|
||||
raise ApiError("invalid plan (trial/paid/enterprise)")
|
||||
fields["plan"] = plan
|
||||
if "seats" in data:
|
||||
try:
|
||||
seats = int(data["seats"])
|
||||
except (TypeError, ValueError):
|
||||
raise ApiError("invalid seats")
|
||||
seats_raw = data["seats"]
|
||||
if isinstance(seats_raw, bool) or not isinstance(seats_raw, int):
|
||||
raise ApiError("seats must be an integer", 400)
|
||||
seats = seats_raw
|
||||
if seats < 1:
|
||||
raise ApiError("seats must be >= 1")
|
||||
fields["seats"] = seats
|
||||
if "active" in data:
|
||||
fields["active"] = bool(data["active"])
|
||||
if fields:
|
||||
_store().orgs.update(org_id, **fields)
|
||||
if not isinstance(data["active"], bool):
|
||||
raise ApiError("active must be a boolean")
|
||||
fields["active"] = data["active"]
|
||||
org_store = _store().orgs
|
||||
with org_store.record_lock(org_id):
|
||||
org = org_store.get_or_none(org_id)
|
||||
if not org:
|
||||
raise ApiError("org not found", 404)
|
||||
if fields:
|
||||
org_store.update(org_id, **fields)
|
||||
updated = org_store.get(org_id)
|
||||
_log_audit("org.update", org_id, detail=fields)
|
||||
return jsonify({"org": _store().orgs.get(org_id)})
|
||||
return jsonify({"org": updated})
|
||||
|
||||
|
||||
@admin_bp.post("/users")
|
||||
@@ -90,24 +104,64 @@ def update_org(org_id: str):
|
||||
def create_user():
|
||||
"""Create a user + provision a password (invite). Admin or super-admin only."""
|
||||
data = request.get_json(silent=True) or {}
|
||||
name = (data.get("name") or "").strip()
|
||||
username = (data.get("username") or data.get("email") or "").strip().lower()
|
||||
password = data.get("password") or ""
|
||||
role = (data.get("role") or "user").strip()
|
||||
org_id = (data.get("org_id") or current_user().get("org_id") or "org-default").strip()
|
||||
if not isinstance(data, dict):
|
||||
raise ApiError("invalid request", 400)
|
||||
name_raw = data.get("name")
|
||||
if name_raw is not None and not isinstance(name_raw, str):
|
||||
raise ApiError("name must be a string", 400)
|
||||
name = (name_raw or "").strip()
|
||||
username_raw = data.get("username")
|
||||
if username_raw is None:
|
||||
username_raw = data.get("email")
|
||||
if username_raw is not None and not isinstance(username_raw, str):
|
||||
raise ApiError("username must be a string", 400)
|
||||
username = (username_raw or "").strip().lower()
|
||||
password = data.get("password")
|
||||
role_raw = data.get("role", "user")
|
||||
if role_raw is None:
|
||||
role_raw = "user"
|
||||
if not isinstance(role_raw, str):
|
||||
raise ApiError("role must be a string", 400)
|
||||
role = role_raw.strip()
|
||||
actor = current_user()
|
||||
actor_role = actor.get("role")
|
||||
actor_org_id = actor.get("org_id")
|
||||
requested_org_id_raw = data.get("org_id")
|
||||
if requested_org_id_raw is not None and not isinstance(requested_org_id_raw, str):
|
||||
raise ApiError("org_id must be a string", 400)
|
||||
requested_org_id = (requested_org_id_raw or "").strip()
|
||||
if actor_role == "super_admin":
|
||||
org_id = requested_org_id or actor_org_id
|
||||
else:
|
||||
if requested_org_id and requested_org_id != actor_org_id:
|
||||
raise ApiError("permission denied", 403)
|
||||
org_id = actor_org_id
|
||||
if not isinstance(org_id, str) or not org_id:
|
||||
raise ApiError("permission denied", 403)
|
||||
|
||||
if not username or not password:
|
||||
if not username or password is None or password == "":
|
||||
raise ApiError("username and password are required")
|
||||
try:
|
||||
_store().validate_password(password)
|
||||
except AuthError as exc:
|
||||
raise internal_error("user could not be created", exc, 400)
|
||||
try:
|
||||
normalized_email = _store().validate_email(data.get("email"))
|
||||
except AuthError as exc:
|
||||
raise internal_error("user could not be created", exc, 400)
|
||||
if role not in Config.ROLES:
|
||||
raise ApiError(f"invalid role: {role}")
|
||||
# Only super_admin can create another admin/super_admin
|
||||
actor_role = current_user().get("role")
|
||||
if role in ("admin", "super_admin") and actor_role != "super_admin":
|
||||
# Super-admin accounts are bootstrap/platform identities, not tenant-provisioned users.
|
||||
if role == "super_admin":
|
||||
raise ApiError("super_admin accounts are provisioned only by bootstrap", 403)
|
||||
if role == "admin" and actor_role != "super_admin":
|
||||
raise ApiError("only super_admin can grant admin roles", 403)
|
||||
|
||||
# Multi-tenant: provisioning a brand-new org (super_admin only). The created user
|
||||
# becomes that org's first admin (tenant owner). Everything is isolated per org.
|
||||
new_org = bool(data.get("new_org"))
|
||||
if "new_org" in data and not isinstance(data["new_org"], bool):
|
||||
raise ApiError("new_org must be a boolean")
|
||||
new_org = data.get("new_org") is True
|
||||
if new_org:
|
||||
if actor_role != "super_admin":
|
||||
raise ApiError("only super_admin can create a new organization", 403)
|
||||
@@ -115,14 +169,26 @@ def create_user():
|
||||
raise ApiError("new-org first user must be role=admin", 400)
|
||||
org = _store().create_org(name or username)
|
||||
org_id = org["id"]
|
||||
_log_audit("org.create", org_id, detail={"name": name or username})
|
||||
|
||||
try:
|
||||
user = _store().create_user(
|
||||
org_id=org_id, username=username, password=password, name=name, role=role
|
||||
org_id=org_id,
|
||||
username=username,
|
||||
password=password,
|
||||
name=name,
|
||||
role=role,
|
||||
email=normalized_email,
|
||||
)
|
||||
except AuthError as exc:
|
||||
raise ApiError(str(exc))
|
||||
if new_org:
|
||||
_store().orgs.delete(org_id)
|
||||
raise internal_error("user could not be created", exc, 400)
|
||||
except Exception as exc:
|
||||
if new_org:
|
||||
_store().orgs.delete(org_id)
|
||||
raise internal_error("user could not be created", exc, 500)
|
||||
if new_org:
|
||||
_log_audit("org.create", org_id, detail={"name": name or username})
|
||||
return jsonify({"user": _store().public_user(user), "org_id": org_id}), 201
|
||||
|
||||
|
||||
@@ -143,35 +209,54 @@ def list_users():
|
||||
@require_roles("admin")
|
||||
def update_user(username: str):
|
||||
data = request.get_json(silent=True) or {}
|
||||
if not isinstance(data, dict):
|
||||
raise ApiError("invalid request", 400)
|
||||
username = username.strip().lower()
|
||||
actor = current_user()
|
||||
target = _store().get_user_or_none(username)
|
||||
if not target:
|
||||
raise ApiError("user not found", 404)
|
||||
if actor.get("role") != "super_admin" and (
|
||||
target.get("org_id") != actor.get("org_id")
|
||||
or target.get("role") == "super_admin"
|
||||
):
|
||||
raise ApiError("user not found", 404)
|
||||
|
||||
if target.get("role") == "super_admin":
|
||||
raise ApiError("super_admin accounts cannot be changed through admin API", 403)
|
||||
|
||||
# Role changes / admin-modification restricted to super_admin
|
||||
updates: dict[str, object] = {}
|
||||
if "role" in data:
|
||||
role = (data.get("role") or "").strip()
|
||||
role_raw = data.get("role")
|
||||
if not isinstance(role_raw, str):
|
||||
raise ApiError("role must be a string", 400)
|
||||
role = role_raw.strip()
|
||||
if role not in Config.ROLES:
|
||||
raise ApiError(f"invalid role: {role}")
|
||||
if actor.get("role") != "super_admin":
|
||||
raise ApiError("only super_admin can change roles")
|
||||
_store().set_role(username, role)
|
||||
raise ApiError("permission denied", 403)
|
||||
if role == "super_admin":
|
||||
_log_audit("user.promote_super_admin", username)
|
||||
raise ApiError("super_admin accounts are provisioned only by bootstrap", 403)
|
||||
updates["role"] = role
|
||||
|
||||
if "active" in data:
|
||||
if actor.get("role") != "super_admin":
|
||||
raise ApiError("only super_admin can activate/deactivate users")
|
||||
_store().set_active(username, bool(data.get("active")))
|
||||
raise ApiError("permission denied", 403)
|
||||
if not isinstance(data["active"], bool):
|
||||
raise ApiError("active must be a boolean")
|
||||
updates["active"] = data["active"]
|
||||
|
||||
if "password" in data and data.get("password"):
|
||||
_store().set_password(username, data.get("password"))
|
||||
if "password" in data:
|
||||
updates["password"] = data.get("password")
|
||||
|
||||
if "email" in data:
|
||||
try:
|
||||
_store().set_email(username, data.get("email"))
|
||||
except AuthError as exc:
|
||||
raise ApiError(str(exc))
|
||||
updates["email"] = data.get("email")
|
||||
|
||||
try:
|
||||
if updates:
|
||||
_store().update_user_fields(username, **updates)
|
||||
except AuthError as exc:
|
||||
raise internal_error("profile update failed", exc, 400)
|
||||
|
||||
return jsonify({"user": _store().public_user(_store().get_user(username))})
|
||||
@@ -2,9 +2,15 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import datetime
|
||||
from flask import Blueprint, jsonify, request
|
||||
import io
|
||||
import math
|
||||
from typing import Callable, Iterator, cast
|
||||
from flask import Blueprint, g, jsonify, request
|
||||
|
||||
from .helpers import ApiError, current_user, require_auth, require_roles
|
||||
from ..auth.users import AuthError, normalize_identifier
|
||||
from ..config import Config
|
||||
from ..storage.store import StoreError
|
||||
from .helpers import ApiError, current_user, is_valid_tenant_id, require_auth, require_roles
|
||||
|
||||
analytics_bp = Blueprint("analytics", __name__)
|
||||
|
||||
@@ -43,9 +49,65 @@ def _log_audit(action: str, subject: str, *, detail: dict | None = None) -> None
|
||||
pass
|
||||
|
||||
|
||||
def _csv_cell(value: object, *, max_chars: int | None = None) -> str:
|
||||
"""Keep exported cells from being interpreted as spreadsheet formulas."""
|
||||
text = "" if value is None else str(value)
|
||||
if max_chars is not None:
|
||||
text = text[:max_chars]
|
||||
probe = text
|
||||
while probe and (probe[0].isspace() or ord(probe[0]) < 32 or probe[0] == "\ufeff"):
|
||||
probe = probe[1:]
|
||||
if probe.startswith(("=", "+", "-", "@")):
|
||||
return "'" + text
|
||||
return text
|
||||
|
||||
|
||||
def _debrief_score(session: dict, default: int | float = 0) -> int | float:
|
||||
raw_debrief = session.get("debrief")
|
||||
if not isinstance(raw_debrief, dict):
|
||||
return default
|
||||
score = raw_debrief.get("score", default)
|
||||
if isinstance(score, bool) or not isinstance(score, (int, float)):
|
||||
return default
|
||||
try:
|
||||
if not math.isfinite(score):
|
||||
return default
|
||||
return max(0, min(100, score))
|
||||
except (OverflowError, TypeError, ValueError):
|
||||
return default
|
||||
|
||||
|
||||
def _is_finished_outcome(session: dict) -> bool:
|
||||
return (
|
||||
isinstance(session, dict)
|
||||
and session.get("status") == "finished"
|
||||
and session.get("outcome") in {"won", "lost"}
|
||||
)
|
||||
|
||||
|
||||
def _is_safe_export_identity(value: object) -> bool:
|
||||
if not isinstance(value, str):
|
||||
return False
|
||||
try:
|
||||
return normalize_identifier(value) == value
|
||||
except AuthError:
|
||||
return False
|
||||
|
||||
|
||||
def _is_safe_export_token_id(value: object) -> bool:
|
||||
if not isinstance(value, str):
|
||||
return False
|
||||
try:
|
||||
normalize_identifier(value)
|
||||
return True
|
||||
except AuthError:
|
||||
return False
|
||||
|
||||
|
||||
# Short-lived signed link for CSV export (HMAC-SHA256, expiring). Not the long-lived JWT.
|
||||
import hmac
|
||||
import hashlib
|
||||
import secrets
|
||||
import time as _t
|
||||
|
||||
_EXPORT_TTL = 300 # 5 minutes
|
||||
@@ -54,11 +116,41 @@ _EXPORT_TTL = 300 # 5 minutes
|
||||
def _sign_export_token(actor: dict) -> str:
|
||||
from ..config import Config
|
||||
|
||||
payload = "{}:{}:{}".format(actor.get("org_id") or "", actor.get("role") or "", int(_t.time()) + _EXPORT_TTL)
|
||||
user_id = actor.get("id") or actor.get("username") or ""
|
||||
org_id = actor.get("org_id") or ""
|
||||
role = actor.get("role") or ""
|
||||
if (
|
||||
not _is_safe_export_identity(user_id)
|
||||
or role not in Config.ROLES
|
||||
or (role != "super_admin" and not is_valid_tenant_id(org_id))
|
||||
or (role == "super_admin" and org_id and not is_valid_tenant_id(org_id))
|
||||
):
|
||||
raise ApiError("invalid export authorization", 403)
|
||||
expires_at = int(_t.time()) + _EXPORT_TTL
|
||||
jti = secrets.token_urlsafe(18)
|
||||
payload = f"{user_id}:{org_id}:{role}:{expires_at}:{jti}"
|
||||
_export_token_store().create(
|
||||
{
|
||||
"id": jti,
|
||||
"user_id": user_id,
|
||||
"org_id": org_id,
|
||||
"role": role,
|
||||
"expires_at": expires_at,
|
||||
"used": False,
|
||||
},
|
||||
key=jti,
|
||||
)
|
||||
sig = hmac.new(Config.SECRET_KEY.encode(), payload.encode(), hashlib.sha256).hexdigest()
|
||||
return "{}.{}".format(sig, payload)
|
||||
|
||||
|
||||
def _export_token_store():
|
||||
from ..config import Config
|
||||
from ..storage.store import JsonStore
|
||||
|
||||
return JsonStore(Config.DATA_DIR / "export_tokens")
|
||||
|
||||
|
||||
def _verify_export_token(token: str) -> dict | None:
|
||||
from ..config import Config
|
||||
|
||||
@@ -70,15 +162,149 @@ def _verify_export_token(token: str) -> dict | None:
|
||||
if not hmac.compare_digest(sig, expected):
|
||||
return None
|
||||
parts = payload.split(":")
|
||||
if len(parts) != 3:
|
||||
if len(parts) != 5:
|
||||
return None
|
||||
user_id, org_id, role, exp_s, jti = parts
|
||||
if (
|
||||
not _is_safe_export_identity(user_id)
|
||||
or not _is_safe_export_token_id(jti)
|
||||
or role not in Config.ROLES
|
||||
or (role != "super_admin" and not is_valid_tenant_id(org_id))
|
||||
or (role == "super_admin" and org_id and not is_valid_tenant_id(org_id))
|
||||
):
|
||||
return None
|
||||
org_id, role, exp_s = parts
|
||||
try:
|
||||
if int(exp_s) < _t.time():
|
||||
expires_at = int(exp_s)
|
||||
if expires_at < _t.time():
|
||||
return None # expired
|
||||
except ValueError:
|
||||
return None
|
||||
return {"org_id": org_id or None, "role": role, "active": True}
|
||||
from ..storage.store import StoreError
|
||||
|
||||
try:
|
||||
record = _export_token_store().get_or_none(jti)
|
||||
except (OSError, StoreError, TypeError, UnicodeError, ValueError):
|
||||
return None
|
||||
if (
|
||||
record is None
|
||||
or not isinstance(record, dict)
|
||||
or record.get("used") is not False
|
||||
or record.get("user_id") != user_id
|
||||
or record.get("org_id") != org_id
|
||||
or record.get("role") != role
|
||||
or record.get("expires_at") != expires_at
|
||||
):
|
||||
return None
|
||||
return {
|
||||
"org_id": org_id or None,
|
||||
"user_id": user_id or None,
|
||||
"role": role,
|
||||
"active": True,
|
||||
"jti": jti,
|
||||
"expires_at": expires_at,
|
||||
}
|
||||
|
||||
|
||||
def _consume_export_token(token_actor: dict) -> bool:
|
||||
"""Atomically consume a previously authorized signed export token."""
|
||||
if not isinstance(token_actor, dict):
|
||||
return False
|
||||
jti = token_actor.get("jti")
|
||||
expires_at = token_actor.get("expires_at")
|
||||
if not isinstance(jti, str) or not isinstance(expires_at, int):
|
||||
return False
|
||||
try:
|
||||
consumed = _export_token_store().update_if(
|
||||
jti,
|
||||
lambda row: (
|
||||
row.get("used") is False
|
||||
and row.get("user_id") == token_actor.get("user_id")
|
||||
and row.get("org_id") == (token_actor.get("org_id") or "")
|
||||
and row.get("role") == token_actor.get("role")
|
||||
and row.get("expires_at") == expires_at
|
||||
and expires_at >= int(_t.time())
|
||||
),
|
||||
used=True,
|
||||
used_at=int(_t.time()),
|
||||
)
|
||||
except (OSError, StoreError, TypeError, UnicodeError, ValueError, AttributeError):
|
||||
return False
|
||||
return consumed is not None
|
||||
|
||||
|
||||
def _authenticated_export_actor():
|
||||
"""Reuse the normal Bearer auth/role guards for the non-signed path."""
|
||||
return current_user()
|
||||
|
||||
|
||||
_authenticated_export_actor = require_auth(
|
||||
require_roles("admin")(_authenticated_export_actor)
|
||||
)
|
||||
|
||||
|
||||
def _signed_export_actor(token: str) -> tuple[dict, dict]:
|
||||
token_actor = _verify_export_token(token)
|
||||
if token_actor is None:
|
||||
raise ApiError("invalid or expired export link", 403)
|
||||
|
||||
from ..storage.store import StoreError
|
||||
|
||||
stores = _stores()
|
||||
token_user_id = token_actor.get("user_id")
|
||||
token_org_id = token_actor.get("org_id")
|
||||
global_super_admin = token_actor.get("role") == "super_admin" and token_org_id is None
|
||||
if not _is_safe_export_identity(token_user_id) or (
|
||||
not global_super_admin and not is_valid_tenant_id(token_org_id)
|
||||
):
|
||||
raise ApiError("export link authorization is stale", 403)
|
||||
try:
|
||||
actor = stores["users"].get_user_or_none(token_user_id)
|
||||
org = None if global_super_admin else stores["users"].get_org_or_none(token_org_id)
|
||||
except (AuthError, OSError, StoreError, TypeError, UnicodeError, ValueError):
|
||||
raise ApiError("export link authorization is stale", 403)
|
||||
if actor is not None and not isinstance(actor, dict):
|
||||
raise ApiError("export link authorization is stale", 403)
|
||||
if org is not None and not isinstance(org, dict):
|
||||
raise ApiError("export link authorization is stale", 403)
|
||||
actor_id = actor.get("id") if isinstance(actor, dict) else None
|
||||
if (
|
||||
not actor
|
||||
or actor.get("active") is not True
|
||||
or (
|
||||
not global_super_admin
|
||||
and not is_valid_tenant_id(actor.get("org_id"))
|
||||
)
|
||||
or (
|
||||
global_super_admin
|
||||
and actor.get("org_id") not in (None, "")
|
||||
and not is_valid_tenant_id(actor.get("org_id"))
|
||||
)
|
||||
or token_actor.get("user_id") != actor_id
|
||||
or token_actor.get("role") != actor.get("role")
|
||||
):
|
||||
raise ApiError("export link authorization is stale", 403)
|
||||
if global_super_admin:
|
||||
if actor.get("role") != "super_admin":
|
||||
raise ApiError("export link authorization is stale", 403)
|
||||
elif (
|
||||
not org
|
||||
or org.get("active") is not True
|
||||
or token_actor.get("org_id") != actor.get("org_id")
|
||||
):
|
||||
raise ApiError("export link authorization is stale", 403)
|
||||
if actor.get("role") not in {"admin", "super_admin"}:
|
||||
raise ApiError("permission denied", 403)
|
||||
|
||||
if request.headers.get("Authorization"):
|
||||
bearer_actor = _authenticated_export_actor()
|
||||
if any(
|
||||
bearer_actor.get(key) != actor.get(key)
|
||||
for key in ("id", "org_id", "role")
|
||||
):
|
||||
raise ApiError("export link authorization is stale", 403)
|
||||
g.user = actor
|
||||
g.org_id = actor.get("org_id")
|
||||
return actor, token_actor
|
||||
|
||||
|
||||
@analytics_bp.get("/export/token")
|
||||
@@ -127,15 +353,55 @@ def analytics():
|
||||
return True
|
||||
|
||||
if actor.get("role") == "super_admin":
|
||||
users = s["users"].list_users()
|
||||
sessions = [x for x in s["sessions"].sessions.all() if _in_window(x)]
|
||||
else:
|
||||
org_id = actor.get("org_id")
|
||||
users = s["users"].list_users(org_id=org_id)
|
||||
user_ids = {u["id"] for u in users}
|
||||
users = [
|
||||
user
|
||||
for user in s["users"].list_users()
|
||||
if isinstance(user, dict)
|
||||
]
|
||||
trainee_users = [user for user in users if user.get("role") == "user"]
|
||||
trainee_count = len(trainee_users)
|
||||
user_ids = {
|
||||
user["id"] for user in trainee_users if isinstance(user.get("id"), str)
|
||||
}
|
||||
sessions = [
|
||||
x for x in s["sessions"].sessions.all()
|
||||
if x.get("user_id") in user_ids and _in_window(x)
|
||||
if isinstance(x, dict)
|
||||
and x.get("user_id") in user_ids
|
||||
and (x.get("mode") or "trainee") == "trainee"
|
||||
and _is_finished_outcome(x)
|
||||
and _in_window(x)
|
||||
]
|
||||
else:
|
||||
org_id = actor.get("org_id")
|
||||
if not is_valid_tenant_id(org_id):
|
||||
raise ApiError("permission denied", 403)
|
||||
users = [
|
||||
user
|
||||
for user in s["users"].list_users(org_id=org_id)
|
||||
if isinstance(user, dict)
|
||||
]
|
||||
trainee_users = [user for user in users if user.get("role") == "user"]
|
||||
trainee_count = len(trainee_users)
|
||||
user_ids = {
|
||||
user["id"] for user in trainee_users if isinstance(user.get("id"), str)
|
||||
}
|
||||
group_ids = {
|
||||
group.get("id")
|
||||
for group in s["groups"].groups.all()
|
||||
if isinstance(group, dict)
|
||||
and isinstance(group.get("id"), str)
|
||||
and group.get("org_id") == org_id
|
||||
and "owner_user_id" not in group
|
||||
}
|
||||
sessions = [
|
||||
x for x in s["sessions"].sessions.all()
|
||||
if isinstance(x, dict)
|
||||
and x.get("user_id") in user_ids
|
||||
and x.get("org_id") == org_id
|
||||
and x.get("group_id") in group_ids
|
||||
and (x.get("mode") or "trainee") == "trainee"
|
||||
and _is_finished_outcome(x)
|
||||
and _in_window(x)
|
||||
]
|
||||
|
||||
overall = {
|
||||
@@ -148,7 +414,7 @@ def analytics():
|
||||
) if overall["total_sessions"] else 0
|
||||
|
||||
# average score
|
||||
scores = [ (x.get("debrief") or {}).get("score", 0) for x in sessions if x.get("outcome") ]
|
||||
scores = [_debrief_score(x) for x in sessions]
|
||||
overall["avg_score"] = round(sum(scores) / len(scores), 1) if scores else 0
|
||||
|
||||
# hardest personas = personas with most losses (lowest avg score)
|
||||
@@ -159,7 +425,7 @@ def analytics():
|
||||
by_persona[key] = {"plays": 0, "losses": 0, "wins": 0, "scores": []}
|
||||
rec = by_persona[key]
|
||||
rec["plays"] += 1
|
||||
rec["scores"].append((x.get("debrief") or {}).get("score", 0))
|
||||
rec["scores"].append(_debrief_score(x))
|
||||
if x.get("outcome") == "won":
|
||||
rec["wins"] += 1
|
||||
elif x.get("outcome") == "lost":
|
||||
@@ -175,77 +441,179 @@ def analytics():
|
||||
}
|
||||
for k, v in by_persona.items()
|
||||
),
|
||||
key=lambda r: (r["losses"], -r["avg_score"]),
|
||||
key=lambda r: (-r["losses"], r["avg_score"]),
|
||||
)[:10]
|
||||
|
||||
return jsonify({
|
||||
"overall": overall,
|
||||
"trainee_count": len(users),
|
||||
"trainee_count": trainee_count,
|
||||
"hardest_personas": hardest,
|
||||
})
|
||||
|
||||
|
||||
@analytics_bp.get("/export")
|
||||
@require_auth
|
||||
@require_roles("admin")
|
||||
def _export_with_bearer():
|
||||
return _export_csv_for_actor(current_user())
|
||||
|
||||
|
||||
@analytics_bp.get("/export")
|
||||
def export_csv():
|
||||
"""Export via a one-time signed link or the normal Bearer JWT."""
|
||||
token = request.args.get("token")
|
||||
if token:
|
||||
actor, token_actor = _signed_export_actor(token)
|
||||
response = _export_csv_for_actor(actor, audit=False)
|
||||
if not _consume_export_token(token_actor):
|
||||
raise ApiError("invalid or expired export link", 403)
|
||||
_log_audit(
|
||||
"analytics.export",
|
||||
actor.get("org_id") or "?",
|
||||
detail={"signed": True},
|
||||
)
|
||||
return response
|
||||
return _export_with_bearer()
|
||||
|
||||
|
||||
def _export_csv_for_actor(actor: dict, *, audit: bool = True):
|
||||
"""Export per-trainee finished session results as CSV (for HR/offline review)."""
|
||||
import csv
|
||||
import io
|
||||
|
||||
from flask import Response, current_app
|
||||
|
||||
s = _stores()
|
||||
# Support a short-lived signed link (?token=) OR the normal Bearer JWT.
|
||||
q_token = request.args.get("token")
|
||||
actor = _verify_export_token(q_token) if q_token else current_user()
|
||||
if actor is None:
|
||||
raise ApiError("invalid or expired export link", 403)
|
||||
# Tenant: an admin exports only their own org's sessions; super_admin sees all.
|
||||
export_org = actor.get("org_id") if actor.get("role") != "super_admin" else None
|
||||
is_super_admin = actor.get("role") == "super_admin"
|
||||
export_org = None if is_super_admin else actor.get("org_id")
|
||||
if not is_super_admin and not is_valid_tenant_id(export_org):
|
||||
raise ApiError("permission denied", 403)
|
||||
s = _stores()
|
||||
users = {}
|
||||
try:
|
||||
user_store = current_app.extensions.get("user_store")
|
||||
if user_store and hasattr(user_store, "list_users"):
|
||||
for rec in user_store.list_users(org_id=export_org):
|
||||
users[rec.get("id") or rec.get("username")] = rec.get("username") or rec.get("id")
|
||||
except Exception:
|
||||
pass
|
||||
if not isinstance(rec, dict):
|
||||
continue
|
||||
if rec.get("role") != "user":
|
||||
continue
|
||||
user_id = rec.get("id") or rec.get("username")
|
||||
if isinstance(user_id, str):
|
||||
users[user_id] = rec.get("username") or rec.get("id")
|
||||
except (OSError, StoreError, TypeError, ValueError) as exc:
|
||||
raise ApiError("analytics unavailable", 503) from exc
|
||||
|
||||
max_rows = Config.ANALYTICS_EXPORT_MAX_ROWS
|
||||
max_bytes = Config.ANALYTICS_EXPORT_MAX_BYTES
|
||||
max_scan_records = Config.ANALYTICS_EXPORT_MAX_SCAN_RECORDS
|
||||
if (
|
||||
isinstance(max_rows, bool)
|
||||
or not isinstance(max_rows, int)
|
||||
or max_rows <= 0
|
||||
or isinstance(max_bytes, bool)
|
||||
or not isinstance(max_bytes, int)
|
||||
or max_bytes <= 0
|
||||
or isinstance(max_scan_records, bool)
|
||||
or not isinstance(max_scan_records, int)
|
||||
or max_scan_records <= 0
|
||||
):
|
||||
raise ApiError("analytics export is unavailable", 503)
|
||||
sessions = []
|
||||
try:
|
||||
grp_store = s["groups"]
|
||||
sess_store = s["sessions"]
|
||||
if hasattr(grp_store, "groups"):
|
||||
group_ids = [g.get("id") for g in grp_store.groups.all() if export_org is None or g.get("org_id") == export_org]
|
||||
elif hasattr(grp_store, "all"):
|
||||
group_ids = [g.get("id") for g in grp_store.all() if export_org is None or g.get("org_id") == export_org]
|
||||
else:
|
||||
group_ids = []
|
||||
for gid in group_ids:
|
||||
try:
|
||||
ss = sess_store.sessions.where(
|
||||
lambda r, _gid=gid: r.get("group_id") == _gid and r.get("outcome")
|
||||
group_collection = getattr(grp_store, "groups", grp_store)
|
||||
session_collection = getattr(sess_store, "sessions", sess_store)
|
||||
raw_group_iter = getattr(group_collection, "iter_all", None)
|
||||
raw_session_iter = getattr(session_collection, "iter_all", None)
|
||||
if not callable(raw_group_iter) or not callable(raw_session_iter):
|
||||
raise StoreError("analytics stores lack bounded iteration")
|
||||
group_iter = cast(Callable[[], Iterator[dict]], raw_group_iter)
|
||||
session_iter = cast(Callable[[], Iterator[dict]], raw_session_iter)
|
||||
|
||||
scanned_records = 0
|
||||
group_ids: set[str] = set()
|
||||
for group in group_iter():
|
||||
scanned_records += 1
|
||||
if scanned_records > max_scan_records:
|
||||
raise ApiError("analytics export scan limit exceeded", 413)
|
||||
if (
|
||||
isinstance(group, dict)
|
||||
and isinstance(group.get("id"), str)
|
||||
and (export_org is None or group.get("org_id") == export_org)
|
||||
and (export_org is None or "owner_user_id" not in group)
|
||||
):
|
||||
group_ids.add(group["id"])
|
||||
|
||||
for row in session_iter():
|
||||
scanned_records += 1
|
||||
if scanned_records > max_scan_records:
|
||||
raise ApiError("analytics export scan limit exceeded", 413)
|
||||
if not (
|
||||
isinstance(row, dict)
|
||||
and row.get("group_id") in group_ids
|
||||
and (row.get("mode") or "trainee") == "trainee"
|
||||
and row.get("status") == "finished"
|
||||
and row.get("outcome") in {"won", "lost"}
|
||||
and row.get("user_id") in users
|
||||
and (
|
||||
export_org is None
|
||||
or row.get("org_id") == export_org
|
||||
)
|
||||
sessions.extend(ss)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
):
|
||||
continue
|
||||
sessions.append(row)
|
||||
if len(sessions) > max_rows:
|
||||
raise ApiError("analytics export has too many rows", 413)
|
||||
except (OSError, StoreError, TypeError, ValueError) as exc:
|
||||
raise ApiError("analytics unavailable", 503) from exc
|
||||
|
||||
_log_audit("analytics.export", actor.get("org_id") or "?", detail={"rows": len(sessions)})
|
||||
if audit:
|
||||
_log_audit("analytics.export", actor.get("org_id") or "?", detail={"rows": len(sessions)})
|
||||
|
||||
buf = io.StringIO()
|
||||
class _BoundedStringIO(io.StringIO):
|
||||
def __init__(self, byte_limit: int):
|
||||
super().__init__()
|
||||
self._byte_limit = byte_limit
|
||||
self._bytes_written = 0
|
||||
|
||||
def write(self, value: str) -> int:
|
||||
try:
|
||||
byte_count = len(value.encode("utf-8"))
|
||||
except UnicodeError as exc:
|
||||
raise ApiError("analytics export contains invalid text", 400) from exc
|
||||
if self._bytes_written + byte_count > self._byte_limit:
|
||||
raise ApiError("analytics export is too large", 413)
|
||||
self._bytes_written += byte_count
|
||||
return super().write(value)
|
||||
|
||||
buf = _BoundedStringIO(max_bytes)
|
||||
w = csv.writer(buf)
|
||||
w.writerow(["username", "persona", "scenario", "outcome", "score", "created_at"])
|
||||
for sess in sessions:
|
||||
w.writerow([
|
||||
users.get(sess.get("user_id"), sess.get("user_id", "")),
|
||||
sess.get("persona_name", ""),
|
||||
sess.get("scenario", ""),
|
||||
sess.get("outcome", ""),
|
||||
(sess.get("debrief") or {}).get("score", ""),
|
||||
sess.get("created_at", ""),
|
||||
_csv_cell(
|
||||
users.get(sess.get("user_id"), sess.get("user_id", "")),
|
||||
max_chars=Config.ANALYTICS_EXPORT_MAX_CELL_CHARS,
|
||||
),
|
||||
_csv_cell(
|
||||
sess.get("persona_name", ""),
|
||||
max_chars=Config.ANALYTICS_EXPORT_MAX_CELL_CHARS,
|
||||
),
|
||||
_csv_cell(
|
||||
sess.get("scenario", ""),
|
||||
max_chars=Config.ANALYTICS_EXPORT_MAX_CELL_CHARS,
|
||||
),
|
||||
_csv_cell(
|
||||
sess.get("outcome", ""),
|
||||
max_chars=Config.ANALYTICS_EXPORT_MAX_CELL_CHARS,
|
||||
),
|
||||
_csv_cell(
|
||||
_debrief_score(sess),
|
||||
max_chars=Config.ANALYTICS_EXPORT_MAX_CELL_CHARS,
|
||||
),
|
||||
_csv_cell(
|
||||
sess.get("created_at", ""),
|
||||
max_chars=Config.ANALYTICS_EXPORT_MAX_CELL_CHARS,
|
||||
),
|
||||
])
|
||||
return Response(
|
||||
buf.getvalue(),
|
||||
|
||||
@@ -3,8 +3,8 @@ from __future__ import annotations
|
||||
|
||||
from flask import Blueprint, jsonify, request
|
||||
|
||||
from ..auth.users import AuthError
|
||||
from .helpers import ApiError, current_user, require_auth
|
||||
from ..auth.users import AuthError, SetupAlreadyCompletedError
|
||||
from .helpers import ApiError, current_user, request_json_object, require_auth
|
||||
|
||||
auth_bp = Blueprint("auth", __name__)
|
||||
|
||||
@@ -17,15 +17,20 @@ def _store():
|
||||
|
||||
def _login_body(data: dict) -> str:
|
||||
# Accept `username` (primary) or `email` (fallback), lower-cased.
|
||||
return (data.get("username") or data.get("email") or "").strip().lower()
|
||||
ident = data.get("username")
|
||||
if ident is None:
|
||||
ident = data.get("email")
|
||||
if not isinstance(ident, str):
|
||||
return ""
|
||||
return ident.strip().lower()
|
||||
|
||||
|
||||
@auth_bp.post("/login")
|
||||
def login():
|
||||
data = request.get_json(silent=True) or {}
|
||||
data = request_json_object()
|
||||
username = _login_body(data)
|
||||
password = data.get("password") or ""
|
||||
if not username or not password:
|
||||
password = data.get("password")
|
||||
if not username or not isinstance(password, str) or not password:
|
||||
raise ApiError("username and password are required")
|
||||
# Slow down brute-force / abuse: per-IP + per-username window.
|
||||
from ..services.rate_limit import check as ratelimit
|
||||
@@ -38,12 +43,12 @@ def login():
|
||||
try:
|
||||
user = _store().verify(username, password)
|
||||
token = _store().issue_token(user)
|
||||
except AuthError as exc:
|
||||
raise ApiError(str(exc), 401)
|
||||
except AuthError:
|
||||
raise ApiError("invalid credentials", 401)
|
||||
return jsonify({
|
||||
"token": token,
|
||||
"user": _store().public_user(user),
|
||||
"must_setup": bool(user.get("must_setup")),
|
||||
"must_setup": user.get("must_setup") is True,
|
||||
})
|
||||
|
||||
|
||||
@@ -58,22 +63,41 @@ def me():
|
||||
def setup():
|
||||
"""First-time admin setup: set email + change password, then clear must_setup."""
|
||||
user = current_user()
|
||||
data = request.get_json(silent=True) or {}
|
||||
username = (data.get("username") or user.get("username") or user.get("id") or "").strip().lower()
|
||||
email = (data.get("email") or "").strip()
|
||||
new_password = data.get("password") or ""
|
||||
data = request_json_object()
|
||||
raw_username = user.get("username") or user.get("id")
|
||||
if not isinstance(raw_username, str):
|
||||
raise ApiError("invalid account state", 401)
|
||||
username = raw_username.strip().lower()
|
||||
requested_username_raw = data.get("username")
|
||||
if requested_username_raw is not None and not isinstance(requested_username_raw, str):
|
||||
raise ApiError("permission denied", 403)
|
||||
requested_username = (requested_username_raw or "").strip().lower()
|
||||
if requested_username and requested_username != username:
|
||||
raise ApiError("permission denied", 403)
|
||||
if user.get("must_setup") is not True:
|
||||
raise ApiError("setup already completed", 409)
|
||||
email = data.get("email")
|
||||
new_password = data.get("password")
|
||||
# SaaS: consent to Terms/Privacy is required before use.
|
||||
if not data.get("accepted_terms"):
|
||||
if data.get("accepted_terms") is not True:
|
||||
raise ApiError("you must accept the Terms of Service and Privacy Policy to continue", 400)
|
||||
if not email or not new_password:
|
||||
if email is None or email == "" or new_password is None or new_password == "":
|
||||
raise ApiError("email and new password are required")
|
||||
try:
|
||||
updated = _store().complete_setup(username, email, new_password)
|
||||
_store().users.update(_store()._norm(username), accepted_terms=True, accepted_terms_at=__import__("time").strftime("%Y-%m-%dT%H:%M:%SZ"))
|
||||
except AuthError as exc:
|
||||
raise ApiError(str(exc), 400)
|
||||
updated = _store().complete_setup(
|
||||
username,
|
||||
email,
|
||||
new_password,
|
||||
accepted_terms=data.get("accepted_terms"),
|
||||
accepted_terms_at=__import__("time").strftime("%Y-%m-%dT%H:%M:%SZ"),
|
||||
)
|
||||
except SetupAlreadyCompletedError:
|
||||
raise ApiError("setup already completed", 409)
|
||||
except AuthError:
|
||||
raise ApiError("setup could not be completed", 400)
|
||||
return jsonify({
|
||||
"ok": True,
|
||||
"token": _store().issue_token(updated),
|
||||
"user": _store().public_user(updated),
|
||||
"must_setup": False,
|
||||
})
|
||||
@@ -84,13 +108,58 @@ def setup():
|
||||
def profile():
|
||||
"""Self-service profile update: name (and optional email). Any authenticated user."""
|
||||
user = current_user()
|
||||
data = request.get_json(silent=True) or {}
|
||||
data = request_json_object()
|
||||
username = user.get("username") or user.get("id")
|
||||
updates: dict[str, object] = {}
|
||||
if "name" in data:
|
||||
updates["name"] = data.get("name")
|
||||
if "email" in data:
|
||||
updates["email"] = data.get("email")
|
||||
try:
|
||||
if "name" in data:
|
||||
_store().set_name(username, data.get("name"))
|
||||
if "email" in data:
|
||||
_store().set_email(username, data.get("email"))
|
||||
updated = _store().update_user_fields(username, **updates)
|
||||
except AuthError:
|
||||
raise ApiError("profile update failed", 400)
|
||||
return jsonify({"user": _store().public_user(updated)})
|
||||
|
||||
|
||||
@auth_bp.post("/password")
|
||||
@require_auth
|
||||
def change_password():
|
||||
"""Change the authenticated user's password after current-password verification."""
|
||||
data = request_json_object()
|
||||
if "username" in data:
|
||||
# The target is always the authenticated subject; never accept a caller-
|
||||
# supplied username as an alternate mutation target.
|
||||
raise ApiError("permission denied", 403)
|
||||
current_password = data.get("current_password")
|
||||
new_password = data.get("new_password")
|
||||
if not isinstance(current_password, str) or not isinstance(new_password, str):
|
||||
raise ApiError("current and new password are required", 400)
|
||||
|
||||
from ..services.rate_limit import check as ratelimit
|
||||
|
||||
user = current_user()
|
||||
username = user.get("username") or user.get("id")
|
||||
client_ip = request.remote_addr or "?"
|
||||
if not isinstance(username, str) or not username:
|
||||
raise ApiError("invalid account state", 401)
|
||||
if not ratelimit("password-change:user", username, limit=5, window=300):
|
||||
raise ApiError("too many attempts, try again later", 429)
|
||||
if not ratelimit("password-change:ip", client_ip, limit=20, window=300):
|
||||
raise ApiError("too many attempts, try again later", 429)
|
||||
|
||||
try:
|
||||
updated = _store().change_password(
|
||||
username,
|
||||
current_password,
|
||||
new_password,
|
||||
)
|
||||
except AuthError as exc:
|
||||
raise ApiError(str(exc), 400)
|
||||
return jsonify({"user": _store().public_user(_store().get_user(username))})
|
||||
if str(exc) == "current password is invalid":
|
||||
raise ApiError("current password is incorrect", 401)
|
||||
raise ApiError("password change failed", 400)
|
||||
return jsonify({
|
||||
"ok": True,
|
||||
"token": _store().issue_token(updated),
|
||||
"user": _store().public_user(updated),
|
||||
})
|
||||
+411
-158
@@ -1,11 +1,20 @@
|
||||
"""Chat/session API: start a one-shot session, send messages, finish + debrief."""
|
||||
from __future__ import annotations
|
||||
|
||||
import functools
|
||||
|
||||
from flask import Blueprint, jsonify, request
|
||||
|
||||
from ..llm import LLMError
|
||||
from ..services.simulator import Simulator
|
||||
from .helpers import ApiError, current_user, require_auth, require_roles
|
||||
from .helpers import (
|
||||
ApiError,
|
||||
current_user,
|
||||
internal_error,
|
||||
request_json_object,
|
||||
require_auth,
|
||||
require_roles,
|
||||
)
|
||||
|
||||
chat_bp = Blueprint("chat", __name__)
|
||||
|
||||
@@ -20,6 +29,24 @@ def _stores():
|
||||
}
|
||||
|
||||
|
||||
def _session_mutation(fn):
|
||||
"""Serialize chat read/LLM/write transactions per session."""
|
||||
@functools.wraps(fn)
|
||||
def wrapped(gid: str, pid: str, *args, **kwargs):
|
||||
s = _stores()
|
||||
actor = current_user()
|
||||
mode = "preview" if actor.get("role") in ("admin", "super_admin") else "trainee"
|
||||
session = s["sessions"].active_for_scope(
|
||||
org_id=actor.get("org_id"), user_id=actor["id"],
|
||||
group_id=gid, persona_id=pid, mode=mode,
|
||||
)
|
||||
if session:
|
||||
with s["sessions"].mutation_lock(session["id"]):
|
||||
return fn(gid, pid, *args, **kwargs)
|
||||
return fn(gid, pid, *args, **kwargs)
|
||||
return wrapped
|
||||
|
||||
|
||||
def _sim(group, persona):
|
||||
llm = _stores()["llm"]
|
||||
if not llm:
|
||||
@@ -56,55 +83,49 @@ def _scenarios(locale: str = "th"):
|
||||
|
||||
def _scenario_config(scenario: str, persona: dict, locale: str = "th"):
|
||||
cfg = _scenarios(locale).get(scenario, _scenarios(locale)["social"])
|
||||
return cfg, cfg["init"]
|
||||
# Initiation belongs to the persona. Scenario changes channel/tone, but a
|
||||
# customer-first persona must not silently become seller-first because the
|
||||
# trainee selected the phone/f2f preset.
|
||||
persona_mode = persona.get("initiation_mode")
|
||||
init_mode = persona_mode if persona_mode in ("customer", "seller") else cfg["init"]
|
||||
return cfg, init_mode
|
||||
|
||||
|
||||
def _build_abbrev_debrief(outcome: str, persona: dict, internal: dict, locale: str = "th") -> dict:
|
||||
"""Build a lightweight debrief from the persona's own decision + per-turn signals."""
|
||||
signals = internal.get("signals", [])
|
||||
en = locale == "en"
|
||||
turning_points = []
|
||||
for sig in signals:
|
||||
if sig.get("type") in ("annoy", "warm"):
|
||||
turn = sig.get("turn", "?")
|
||||
if sig.get("mood", 0) > 0:
|
||||
turning_points.append(
|
||||
f"Turn {turn}: customer warmed up" if en else f"รอบที่ {turn}: ลูกค้าเริ่มใจขึ้น"
|
||||
)
|
||||
else:
|
||||
turning_points.append(
|
||||
f"Turn {turn}: customer annoyed/hesitant" if en else f"รอบที่ {turn}: ลูกค้าเริ่มหงุดหงิด/ลังเล"
|
||||
)
|
||||
why = (
|
||||
("Customer decided to buy (pain resolved + offer accepted)" if en else "ลูกค้าตัดสินใจซื้อ (แก้ปัญหาและยอมรับข้อเสนอแล้ว)")
|
||||
if outcome == "won"
|
||||
else (
|
||||
"Customer decided not to buy — value not enough, or responses missed the need"
|
||||
if en
|
||||
else "ลูกค้าตัดสินใจไม่ซื้อ — ยังไม่เห็นคุณค่าพอ หรือการตอบไม่ตรงความต้องการ"
|
||||
)
|
||||
)
|
||||
coaching = (
|
||||
["Great job — the customer closed with you"] if en and outcome == "won"
|
||||
else ["ทำได้ดีมาก — ลูกค้าปิดการขายกับคุณ"] if outcome == "won"
|
||||
else (
|
||||
turning_points + ["Ask deeper about the need", "Handle objections more directly"]
|
||||
if en
|
||||
else turning_points + ["ลองถามความต้องการให้ลึกกว่าเดิม", "รับมือข้อโต้แย้งให้ตรงจุด"]
|
||||
)
|
||||
)
|
||||
def _bounded_text(value: object, limit: int = 2000) -> str:
|
||||
return value.strip()[:limit] if isinstance(value, str) else ""
|
||||
|
||||
|
||||
def _string_list(value: object, *, max_items: int = 20, item_limit: int = 1000) -> list[str]:
|
||||
if not isinstance(value, list):
|
||||
return []
|
||||
return [item.strip()[:item_limit] for item in value[:max_items] if isinstance(item, str) and item.strip()]
|
||||
|
||||
|
||||
def _safe_judge_debrief(verdict: object, outcome: str, persona: dict) -> dict:
|
||||
"""Keep provider-controlled judge output inside a closed response envelope."""
|
||||
raw = verdict if isinstance(verdict, dict) else {}
|
||||
try:
|
||||
score = max(0, min(100, int(raw.get("score", 0))))
|
||||
except (TypeError, ValueError):
|
||||
score = 0
|
||||
progress = {}
|
||||
raw_progress = raw.get("painProgress")
|
||||
if isinstance(raw_progress, dict):
|
||||
for key, value in list(raw_progress.items())[:20]:
|
||||
if not isinstance(key, str):
|
||||
continue
|
||||
try:
|
||||
progress[key[:100]] = max(0, min(100, int(value)))
|
||||
except (TypeError, ValueError):
|
||||
continue
|
||||
return {
|
||||
"outcome": outcome,
|
||||
"score": 60 if outcome == "won" else 25,
|
||||
"pain": (persona.get("pains") or [{}])[0].get("description", "") if persona.get("pains") else "",
|
||||
"why": why,
|
||||
"failurePoints": [] if outcome == "won" else (
|
||||
["Failed to close the sale", "Customer walked away before deciding"]
|
||||
if en else ["ปิดการขายไม่สำเร็จ", "ลูกค้าถอยก่อนตัดสินใจซื้อ"]
|
||||
),
|
||||
"coaching": coaching,
|
||||
"turning_points": turning_points,
|
||||
"signals": signals,
|
||||
"score": score,
|
||||
"pain": _bounded_text(raw.get("pain")),
|
||||
"why": _bounded_text(raw.get("why")),
|
||||
"failurePoints": _string_list(raw.get("failurePoints")),
|
||||
"coaching": _string_list(raw.get("coaching")),
|
||||
"painProgress": progress,
|
||||
"revealed_persona": {
|
||||
"pains": persona.get("pains", []),
|
||||
"income": persona.get("income", ""),
|
||||
@@ -113,30 +134,174 @@ def _build_abbrev_debrief(outcome: str, persona: dict, internal: dict, locale: s
|
||||
"negotiation_levers": persona.get("negotiation_levers", []),
|
||||
"opener": persona.get("opener", ""),
|
||||
"background": persona.get("background", ""),
|
||||
"tolerance": persona.get("tolerance", 3),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def _safe_session_messages(value: object) -> list[dict[str, str]]:
|
||||
if not isinstance(value, list):
|
||||
return []
|
||||
allowed_roles = {"system", "seller", "customer", "assistant"}
|
||||
messages = []
|
||||
for item in value[-100:]:
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
role = item.get("role")
|
||||
text = item.get("text")
|
||||
if isinstance(role, str) and role in allowed_roles and isinstance(text, str):
|
||||
messages.append({"role": role, "text": text[:4000]})
|
||||
return messages
|
||||
|
||||
|
||||
def serialize_session(session: dict) -> dict:
|
||||
"""Return only the trainee-visible session envelope; omit hidden judge state."""
|
||||
meta = session.get("persona_meta")
|
||||
meta = meta if isinstance(meta, dict) else {}
|
||||
safe_meta = {
|
||||
key: meta[key]
|
||||
for key in ("tier", "initiation_mode", "channel", "scenario", "locale")
|
||||
if key in meta and isinstance(meta[key], (str, int, float, type(None)))
|
||||
}
|
||||
outcome = session.get("outcome")
|
||||
if outcome not in (None, "won", "lost", "abandoned"):
|
||||
outcome = None
|
||||
raw_debrief = session.get("debrief")
|
||||
if isinstance(raw_debrief, dict):
|
||||
revealed = raw_debrief.get("revealed_persona")
|
||||
revealed = revealed if isinstance(revealed, dict) else {}
|
||||
debrief = _safe_judge_debrief(raw_debrief, outcome or "lost", revealed)
|
||||
else:
|
||||
debrief = None
|
||||
mode = session.get("mode") or "trainee"
|
||||
if mode not in ("trainee", "preview"):
|
||||
mode = "trainee"
|
||||
return {
|
||||
key: session.get(key)
|
||||
for key in ("id", "group_id", "persona_id", "persona_name", "created_at", "updated_at")
|
||||
if isinstance(session.get(key), str)
|
||||
} | {
|
||||
"persona_meta": safe_meta,
|
||||
"mode": mode,
|
||||
"status": session.get("status") if session.get("status") in ("active", "finished") else "active",
|
||||
"outcome": outcome,
|
||||
"messages": _safe_session_messages(session.get("messages")),
|
||||
"debrief": debrief,
|
||||
}
|
||||
|
||||
|
||||
def _finalize_session(s, session: dict, group: dict, persona: dict) -> tuple[dict, dict]:
|
||||
"""Run the single final-judge path for automatic and manual completion."""
|
||||
if session.get("status") == "finished":
|
||||
existing = session.get("debrief")
|
||||
existing_outcome = session.get("outcome")
|
||||
if existing_outcome not in ("won", "lost"):
|
||||
existing_outcome = "lost"
|
||||
debrief = _safe_judge_debrief(
|
||||
existing if isinstance(existing, dict) else {},
|
||||
existing_outcome,
|
||||
persona,
|
||||
)
|
||||
return session, debrief
|
||||
|
||||
messages = list(session.get("messages", []))
|
||||
internal = session.get("internal", {})
|
||||
internal = dict(internal) if isinstance(internal, dict) else {}
|
||||
# Persist the latest transcript before the provider call. A failed judge
|
||||
# therefore leaves an active, retryable session rather than losing turns.
|
||||
s["sessions"].update(session["id"], messages=messages, internal=internal)
|
||||
sim = _sim(group, persona)
|
||||
verdict = sim.judge(persona=persona, messages=messages, internal=internal)
|
||||
outcome = "won" if isinstance(verdict, dict) and verdict.get("outcome") == "won" else "lost"
|
||||
debrief = _safe_judge_debrief(verdict, outcome, persona)
|
||||
updated = s["sessions"].update(
|
||||
session["id"],
|
||||
status="finished",
|
||||
outcome=outcome,
|
||||
messages=messages,
|
||||
internal=internal,
|
||||
debrief=debrief,
|
||||
)
|
||||
return updated, debrief
|
||||
|
||||
|
||||
def _get_ready_group(s, gid: str) -> dict:
|
||||
"""Org-scoped group access for trainees + require ready status (IDOR defense)."""
|
||||
group = s["groups"].get_or_none(gid)
|
||||
if not group or group.get("status") != "ready":
|
||||
if not isinstance(group, dict) or group.get("id") != gid or group.get("status") != "ready":
|
||||
raise ApiError("group not ready", 404)
|
||||
actor = current_user()
|
||||
# super_admin can access any; otherwise owner (for personal groups) + same org.
|
||||
owner = group.get("owner_user_id")
|
||||
if actor.get("role") != "super_admin":
|
||||
if owner and owner != actor["id"]:
|
||||
raise ApiError("permission denied", 403)
|
||||
if "owner_user_id" in group:
|
||||
if not isinstance(owner, str) or not owner or owner != actor["id"]:
|
||||
raise ApiError("permission denied", 403)
|
||||
if group.get("org_id") != actor.get("org_id"):
|
||||
raise ApiError("permission denied", 403)
|
||||
return group
|
||||
|
||||
|
||||
def _authorize_session_context(
|
||||
s,
|
||||
session: object,
|
||||
*,
|
||||
gid: str | None = None,
|
||||
pid: str | None = None,
|
||||
required_status: str | None = None,
|
||||
) -> tuple[dict, dict]:
|
||||
"""Re-check the group context before exposing or using a session.
|
||||
|
||||
Session rows are scoped by actor identifiers, but persisted rows can outlive
|
||||
group ownership changes or deletion. Never trust those identifiers alone:
|
||||
the referenced group must still be ready, tenant-authorized, and contain the
|
||||
referenced persona. Mismatches intentionally look like a missing session.
|
||||
"""
|
||||
if not isinstance(session, dict):
|
||||
raise ApiError("session not found", 404)
|
||||
actor = current_user()
|
||||
actor_id = actor.get("id")
|
||||
org_id = actor.get("org_id")
|
||||
expected_mode = "preview" if actor.get("role") in ("admin", "super_admin") else "trainee"
|
||||
session_mode = session.get("mode")
|
||||
if session_mode is None:
|
||||
session_mode = "trainee" # legacy rows before explicit mode was added
|
||||
session_status = session.get("status")
|
||||
session_gid = session.get("group_id")
|
||||
session_pid = session.get("persona_id")
|
||||
if (
|
||||
not isinstance(actor_id, str)
|
||||
or not actor_id
|
||||
or not isinstance(org_id, str)
|
||||
or not org_id
|
||||
or session.get("user_id") != actor_id
|
||||
or session.get("org_id") != org_id
|
||||
or session_mode != expected_mode
|
||||
or not isinstance(session_gid, str)
|
||||
or not session_gid
|
||||
or not isinstance(session_pid, str)
|
||||
or not session_pid
|
||||
or (gid is not None and session_gid != gid)
|
||||
or (pid is not None and session_pid != pid)
|
||||
or session_status not in ("active", "finished")
|
||||
or (required_status is not None and session_status != required_status)
|
||||
):
|
||||
raise ApiError("session not found", 404)
|
||||
|
||||
try:
|
||||
group = _get_ready_group(s, session_gid)
|
||||
except ApiError as exc:
|
||||
if exc.status in (403, 404):
|
||||
raise ApiError("session not found", 404) from exc
|
||||
raise
|
||||
persona = s["groups"].get_persona(session_gid, session_pid)
|
||||
if not isinstance(persona, dict):
|
||||
raise ApiError("session context missing", 404)
|
||||
return group, persona
|
||||
|
||||
|
||||
@chat_bp.post("/<gid>/personas/<pid>/chat/start")
|
||||
@require_auth
|
||||
@require_roles("user")
|
||||
@require_roles("user", "admin")
|
||||
def start_session(gid: str, pid: str):
|
||||
s = _stores()
|
||||
group = _get_ready_group(s, gid)
|
||||
@@ -144,61 +309,104 @@ def start_session(gid: str, pid: str):
|
||||
if not persona:
|
||||
raise ApiError("persona not found", 404)
|
||||
actor = current_user()
|
||||
org_id = actor.get("org_id")
|
||||
if not isinstance(org_id, str) or not org_id:
|
||||
raise ApiError("permission denied", 403)
|
||||
# Scenario chosen by the trainee at chat start (not baked into the persona).
|
||||
body = request.get_json(silent=True) or {}
|
||||
scenario = (body.get("scenario") or "social").strip().lower()
|
||||
body = request_json_object(allow_empty=True)
|
||||
requested_mode = body.get("mode", "trainee")
|
||||
if requested_mode not in ("trainee", "preview"):
|
||||
raise ApiError("session mode is invalid", 400)
|
||||
if requested_mode == "preview" and actor.get("role") not in ("admin", "super_admin"):
|
||||
raise ApiError("permission denied", 403)
|
||||
if actor.get("role") in ("admin", "super_admin") and requested_mode == "trainee":
|
||||
requested_mode = "preview"
|
||||
scenario_raw = body.get("scenario", "social")
|
||||
scenario = scenario_raw.strip().lower() if isinstance(scenario_raw, str) else "social"
|
||||
if scenario not in ("social", "f2f_call"):
|
||||
scenario = "social"
|
||||
locale = (body.get("locale") or "th").strip().lower()
|
||||
locale_raw = body.get("locale", "th")
|
||||
locale = locale_raw.strip().lower() if isinstance(locale_raw, str) else "th"
|
||||
if locale not in ("en", "th"):
|
||||
locale = "th"
|
||||
scenario_meta, init_mode = _scenario_config(scenario, persona, locale)
|
||||
|
||||
# RESUME: if this user already has an ACTIVE (unfinished) session for this persona, keep
|
||||
# chatting it — do NOT create a new one and do NOT force re-picking the scenario.
|
||||
existing = s["sessions"].active_for_persona(actor["id"], pid)
|
||||
if existing and existing.get("group_id") == gid:
|
||||
return jsonify({
|
||||
"session": existing,
|
||||
"initiation_mode": existing.get("persona_meta", {}).get("initiation_mode") or "customer",
|
||||
"scenario": existing.get("scenario", scenario),
|
||||
"scenario_meta": _scenario_config(existing.get("scenario", scenario), persona, locale),
|
||||
})
|
||||
|
||||
# One-shot: reject if already finished this persona
|
||||
try:
|
||||
session = s["sessions"].create(
|
||||
user_id=actor["id"], group_id=gid, persona_id=pid,
|
||||
persona_name=persona.get("name", "?"),
|
||||
persona_meta={
|
||||
"tier": persona.get("tier"),
|
||||
"initiation_mode": init_mode,
|
||||
"channel": persona.get("channel"),
|
||||
"scenario": scenario,
|
||||
"locale": locale,
|
||||
},
|
||||
)
|
||||
except ValueError as exc:
|
||||
raise ApiError(str(exc), 400)
|
||||
|
||||
s["sessions"].update(
|
||||
session["id"],
|
||||
scenario=scenario,
|
||||
locale=locale,
|
||||
internal={**(session.get("internal") or {}), "turns": 0, "score": 50, "signals": []},
|
||||
)
|
||||
sim = _sim(group, persona)
|
||||
# Seed messages: localized scenario preamble, then customer opener for customer-first scenarios.
|
||||
seeded = list(session.get("messages", []))
|
||||
# Build the complete initial record before publishing it. A concurrent send
|
||||
# must never observe an active session with an empty/partially seeded transcript.
|
||||
seeded = []
|
||||
if scenario_meta.get("preamble"):
|
||||
seeded.append({"role": "system", "text": scenario_meta["preamble"]})
|
||||
if init_mode == "customer":
|
||||
opener = persona.get("opener") or "Hi, I saw your product and had a question."
|
||||
seeded.append({"role": "customer", "text": opener})
|
||||
s["sessions"].update(session["id"], messages=seeded)
|
||||
initial_internal = {"turns": 0, "score": 50, "signals": []}
|
||||
|
||||
# RESUME/create is one atomic operation over the complete tenant scope. A
|
||||
# second worker gets the same active record instead of creating a duplicate.
|
||||
resumed_payload = None
|
||||
try:
|
||||
# Group deletion holds the same lock while cascading sessions. Recheck
|
||||
# the ready group and persona inside that lock immediately before
|
||||
# publishing the session, so deletion cannot leave a new orphan.
|
||||
with s["groups"].record_lock(gid):
|
||||
group = _get_ready_group(s, gid)
|
||||
persona = s["groups"].get_persona(gid, pid)
|
||||
if not persona:
|
||||
raise ApiError("persona not found", 404)
|
||||
scenario_meta, init_mode = _scenario_config(scenario, persona, locale)
|
||||
seeded = []
|
||||
if scenario_meta.get("preamble"):
|
||||
seeded.append({"role": "system", "text": scenario_meta["preamble"]})
|
||||
if init_mode == "customer":
|
||||
opener = persona.get("opener") or "Hi, I saw your product and had a question."
|
||||
seeded.append({"role": "customer", "text": opener})
|
||||
session, resumed = s["sessions"].start(
|
||||
org_id=org_id,
|
||||
user_id=actor["id"],
|
||||
group_id=gid,
|
||||
persona_id=pid,
|
||||
persona_name=persona.get("name", "?"),
|
||||
mode=requested_mode,
|
||||
persona_meta={
|
||||
"tier": persona.get("tier"),
|
||||
"initiation_mode": init_mode,
|
||||
"channel": persona.get("channel"),
|
||||
"scenario": scenario,
|
||||
"locale": locale,
|
||||
},
|
||||
messages=seeded,
|
||||
internal=initial_internal,
|
||||
scenario=scenario,
|
||||
locale=locale,
|
||||
)
|
||||
if resumed:
|
||||
_authorize_session_context(
|
||||
s, session, gid=gid, pid=pid, required_status="active"
|
||||
)
|
||||
existing_scenario = session.get("scenario") or (
|
||||
session.get("persona_meta") or {}
|
||||
).get("scenario") or "social"
|
||||
existing_locale = session.get("locale") or (
|
||||
session.get("persona_meta") or {}
|
||||
).get("locale") or "th"
|
||||
existing_meta, existing_init = _scenario_config(
|
||||
existing_scenario, persona, existing_locale
|
||||
)
|
||||
resumed_payload = {
|
||||
"session": serialize_session(session),
|
||||
"initiation_mode": existing_init,
|
||||
"scenario": existing_scenario,
|
||||
"scenario_meta": existing_meta,
|
||||
}
|
||||
except ValueError as exc:
|
||||
raise internal_error("could not start session", exc, 400)
|
||||
|
||||
if resumed:
|
||||
return jsonify(resumed_payload)
|
||||
|
||||
sess = s["sessions"].get(session["id"])
|
||||
return jsonify({
|
||||
"session": sess,
|
||||
"session": serialize_session(sess),
|
||||
"initiation_mode": init_mode,
|
||||
"scenario": scenario,
|
||||
"scenario_meta": scenario_meta,
|
||||
@@ -207,16 +415,33 @@ def start_session(gid: str, pid: str):
|
||||
|
||||
@chat_bp.post("/<gid>/personas/<pid>/chat/send")
|
||||
@require_auth
|
||||
@require_roles("user")
|
||||
@require_roles("user", "admin")
|
||||
@_session_mutation
|
||||
def send_message(gid: str, pid: str):
|
||||
s = _stores()
|
||||
actor = current_user()
|
||||
session = s["sessions"].active_for_persona(actor["id"], pid)
|
||||
if not session or session.get("group_id") != gid:
|
||||
org_id = actor.get("org_id")
|
||||
if not isinstance(org_id, str) or not org_id:
|
||||
raise ApiError("permission denied", 403)
|
||||
session = s["sessions"].active_for_scope(
|
||||
org_id=org_id,
|
||||
user_id=actor["id"],
|
||||
group_id=gid,
|
||||
persona_id=pid,
|
||||
mode="preview" if actor.get("role") in ("admin", "super_admin") else "trainee",
|
||||
)
|
||||
if not session:
|
||||
raise ApiError("no active session for this persona", 404)
|
||||
|
||||
data = request.get_json(silent=True) or {}
|
||||
text = (data.get("text") or "").strip()
|
||||
group, persona = _authorize_session_context(
|
||||
s, session, gid=gid, pid=pid, required_status="active"
|
||||
)
|
||||
|
||||
data = request_json_object()
|
||||
text_raw = data.get("text")
|
||||
if not isinstance(text_raw, str):
|
||||
raise ApiError("message is invalid", 400)
|
||||
text = text_raw.strip()
|
||||
if not text:
|
||||
raise ApiError("message is empty")
|
||||
if len(text) > 2000:
|
||||
@@ -229,10 +454,6 @@ def send_message(gid: str, pid: str):
|
||||
if not ratelimit("chat:user", actor_rl.get("id") or actor_rl.get("username") or "?", limit=30, window=60):
|
||||
raise ApiError("slow down — too many messages", 429)
|
||||
|
||||
group = s["groups"].get_or_none(gid)
|
||||
persona = s["groups"].get_persona(gid, pid) if group else None
|
||||
if not group or not persona:
|
||||
raise ApiError("session context missing", 404)
|
||||
messages = list(session.get("messages", []))
|
||||
messages.append({"role": "seller", "text": text})
|
||||
scenario = session.get("scenario", "social") or "social"
|
||||
@@ -250,7 +471,7 @@ def send_message(gid: str, pid: str):
|
||||
scenario_adapt=adapt,
|
||||
)
|
||||
except LLMError as exc:
|
||||
raise ApiError(f"LLM error: {exc}", 500)
|
||||
raise internal_error("LLM service unavailable", exc)
|
||||
messages.append({"role": "customer", "text": reply})
|
||||
|
||||
# Update internal state: track misses (poor answers) and mood trend.
|
||||
@@ -299,23 +520,20 @@ def send_message(gid: str, pid: str):
|
||||
internal["signals"].append({"turn": internal["turns"], "mood": mood, "type": "warm"})
|
||||
|
||||
if decision in ("buy", "walk"):
|
||||
outcome = "won" if decision == "buy" else "lost"
|
||||
debrief = _build_abbrev_debrief(outcome, persona, internal, slocale)
|
||||
s["sessions"].update(
|
||||
session["id"],
|
||||
status="finished",
|
||||
outcome=outcome,
|
||||
messages=messages,
|
||||
internal=internal,
|
||||
debrief=debrief,
|
||||
finalized, debrief = _finalize_session(
|
||||
s,
|
||||
{**session, "messages": messages, "internal": internal},
|
||||
group,
|
||||
persona,
|
||||
)
|
||||
outcome = finalized.get("outcome")
|
||||
return jsonify({
|
||||
"reply": reply,
|
||||
"messages": messages,
|
||||
"finished": True,
|
||||
"outcome": outcome,
|
||||
"debrief": debrief,
|
||||
"session": s["sessions"].get(session["id"]),
|
||||
"session": serialize_session(finalized),
|
||||
})
|
||||
|
||||
s["sessions"].update(session["id"], messages=messages, internal=internal)
|
||||
@@ -324,77 +542,112 @@ def send_message(gid: str, pid: str):
|
||||
|
||||
@chat_bp.post("/<gid>/personas/<pid>/chat/finish")
|
||||
@require_auth
|
||||
@require_roles("user")
|
||||
@require_roles("user", "admin")
|
||||
@_session_mutation
|
||||
def finish_session(gid: str, pid: str):
|
||||
"""End the chat and produce the debrief via the judge-LLM (reveals latent fields)."""
|
||||
s = _stores()
|
||||
actor = current_user()
|
||||
session = s["sessions"].active_for_persona(actor["id"], pid)
|
||||
if not session or session.get("group_id") != gid:
|
||||
raise ApiError("no active session for this persona", 404)
|
||||
group = s["groups"].get_or_none(gid)
|
||||
persona = s["groups"].get_persona(gid, pid)
|
||||
|
||||
sim = _sim(group, persona)
|
||||
messages = session.get("messages", [])
|
||||
try:
|
||||
verdict = sim.judge(
|
||||
persona=persona, messages=messages, internal=session.get("internal", {})
|
||||
)
|
||||
except LLMError as exc:
|
||||
raise ApiError(f"LLM error: {exc}", 500)
|
||||
|
||||
outcome = "won" if verdict.get("outcome") == "won" else "lost"
|
||||
debrief = {
|
||||
**verdict,
|
||||
"revealed_persona": {
|
||||
"pains": persona.get("pains", []),
|
||||
"income": persona.get("income", ""),
|
||||
"personality": persona.get("personality", ""),
|
||||
"budget": persona.get("budget", ""),
|
||||
"negotiation_levers": persona.get("negotiation_levers", []),
|
||||
"opener": persona.get("opener", ""),
|
||||
"background": persona.get("background", ""),
|
||||
},
|
||||
}
|
||||
s["sessions"].update(
|
||||
session["id"],
|
||||
status="finished",
|
||||
outcome=outcome,
|
||||
debrief=debrief,
|
||||
internal=session.get("internal", {}),
|
||||
org_id = actor.get("org_id")
|
||||
if not isinstance(org_id, str) or not org_id:
|
||||
raise ApiError("permission denied", 403)
|
||||
session = s["sessions"].active_for_scope(
|
||||
org_id=org_id,
|
||||
user_id=actor["id"],
|
||||
group_id=gid,
|
||||
persona_id=pid,
|
||||
mode="preview" if actor.get("role") in ("admin", "super_admin") else "trainee",
|
||||
)
|
||||
return jsonify({"session": s["sessions"].get(session["id"]), "debrief": debrief})
|
||||
if not session:
|
||||
finished = s["sessions"].latest_for_scope(
|
||||
org_id=org_id,
|
||||
user_id=actor["id"],
|
||||
group_id=gid,
|
||||
persona_id=pid,
|
||||
mode="preview" if actor.get("role") in ("admin", "super_admin") else "trainee",
|
||||
)
|
||||
if finished and finished.get("status") == "finished":
|
||||
_group, persona = _authorize_session_context(
|
||||
s, finished, gid=gid, pid=pid, required_status="finished"
|
||||
)
|
||||
finished_outcome = finished.get("outcome")
|
||||
if finished_outcome not in ("won", "lost"):
|
||||
finished_outcome = "lost"
|
||||
return jsonify({
|
||||
"session": serialize_session(finished),
|
||||
"debrief": _safe_judge_debrief(
|
||||
finished.get("debrief") or {},
|
||||
finished_outcome,
|
||||
persona,
|
||||
),
|
||||
})
|
||||
raise ApiError("no active session for this persona", 404)
|
||||
group, persona = _authorize_session_context(
|
||||
s, session, gid=gid, pid=pid, required_status="active"
|
||||
)
|
||||
try:
|
||||
finalized, debrief = _finalize_session(s, session, group, persona)
|
||||
except LLMError as exc:
|
||||
raise internal_error("LLM service unavailable", exc)
|
||||
return jsonify({"session": serialize_session(finalized), "debrief": debrief})
|
||||
|
||||
|
||||
@chat_bp.get("/sessions")
|
||||
@require_auth
|
||||
def my_sessions():
|
||||
s = _stores()
|
||||
uid = current_user()["id"]
|
||||
sessions = s["sessions"].list_for_user(uid)
|
||||
return jsonify({"sessions": sessions})
|
||||
actor = current_user()
|
||||
org_id = actor.get("org_id")
|
||||
if not isinstance(org_id, str) or not org_id:
|
||||
raise ApiError("permission denied", 403)
|
||||
sessions = s["sessions"].list_for_user(actor["id"], org_id=org_id)
|
||||
visible_sessions = []
|
||||
for session in sessions:
|
||||
try:
|
||||
_authorize_session_context(s, session)
|
||||
except ApiError:
|
||||
continue
|
||||
visible_sessions.append(serialize_session(session))
|
||||
return jsonify({"sessions": visible_sessions})
|
||||
|
||||
|
||||
@chat_bp.get("/sessions/<sid>")
|
||||
@require_auth
|
||||
@require_roles("user")
|
||||
@require_roles("user", "admin")
|
||||
def get_session(sid: str):
|
||||
s = _stores()
|
||||
session = s["sessions"].get_or_none(sid)
|
||||
if not session or session.get("user_id") != current_user()["id"]:
|
||||
actor = current_user()
|
||||
if (
|
||||
not isinstance(session, dict)
|
||||
or session.get("user_id") != actor["id"]
|
||||
or session.get("org_id") != actor.get("org_id")
|
||||
):
|
||||
raise ApiError("session not found", 404)
|
||||
return jsonify({"session": session})
|
||||
_authorize_session_context(s, session)
|
||||
return jsonify({"session": serialize_session(session)})
|
||||
|
||||
|
||||
@chat_bp.get("/<gid>/personas/<pid>/chat/resume")
|
||||
@require_auth
|
||||
@require_roles("user")
|
||||
@require_roles("user", "admin")
|
||||
def resume_session(gid: str, pid: str):
|
||||
"""Resume an active (unfinished) session for this persona so the trainee can continue."""
|
||||
s = _stores()
|
||||
actor = current_user()
|
||||
session = s["sessions"].active_for_persona(actor["id"], pid)
|
||||
if not session or session.get("group_id") != gid:
|
||||
org_id = actor.get("org_id")
|
||||
if not isinstance(org_id, str) or not org_id:
|
||||
raise ApiError("permission denied", 403)
|
||||
session = s["sessions"].active_for_scope(
|
||||
org_id=org_id,
|
||||
user_id=actor["id"],
|
||||
group_id=gid,
|
||||
persona_id=pid,
|
||||
mode="preview" if actor.get("role") in ("admin", "super_admin") else "trainee",
|
||||
)
|
||||
if not session:
|
||||
raise ApiError("no active session for this persona", 404)
|
||||
return jsonify({"session": session, "scenario": session.get("scenario", "social")})
|
||||
_authorize_session_context(
|
||||
s, session, gid=gid, pid=pid, required_status="active"
|
||||
)
|
||||
return jsonify({"session": serialize_session(session), "scenario": session.get("scenario", "social")})
|
||||
+579
-159
@@ -1,19 +1,47 @@
|
||||
"""Group API: create, analyze (sales kit + personas), read, edit, report."""
|
||||
from __future__ import annotations
|
||||
|
||||
import functools
|
||||
import math
|
||||
import threading
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
from flask import Blueprint, jsonify, request
|
||||
from flask import Blueprint, Response, current_app, jsonify, request
|
||||
from werkzeug.exceptions import RequestEntityTooLarge
|
||||
|
||||
from ..config import Config
|
||||
from ..llm import LLMClient, LLMError
|
||||
from ..services.groups import GroupStore
|
||||
from ..services.store import ensure_persona_shape, revealable_view
|
||||
from .helpers import ApiError, current_user, require_auth, require_roles
|
||||
from ..services.store import (
|
||||
PERSONA_CHANNELS,
|
||||
ensure_persona_shape,
|
||||
revealable_view,
|
||||
validate_persona_traits,
|
||||
)
|
||||
from ..storage.store import contains_control_characters
|
||||
from .helpers import (
|
||||
ApiError,
|
||||
current_user,
|
||||
internal_error,
|
||||
is_valid_tenant_id,
|
||||
request_json_object,
|
||||
require_auth,
|
||||
require_roles,
|
||||
)
|
||||
|
||||
groups_bp = Blueprint("groups", __name__)
|
||||
|
||||
|
||||
def _group_analysis_lock(fn):
|
||||
"""Serialize analysis and prevent stale LLM results from overwriting a group."""
|
||||
@functools.wraps(fn)
|
||||
def wrapped(gid: str, *args, **kwargs):
|
||||
s = _stores()
|
||||
with s["groups"].record_lock(gid):
|
||||
return fn(gid, *args, **kwargs)
|
||||
return wrapped
|
||||
|
||||
# Fields that encode the "formula"/process of a persona. Only super_admin may see/edit
|
||||
# them; admins get the persona but NOT these — so a casual copy yields inferior results.
|
||||
SECRET_PERSONA_FIELDS = {
|
||||
@@ -21,21 +49,81 @@ SECRET_PERSONA_FIELDS = {
|
||||
"rootCause", "resolutionConditions", "tolerance",
|
||||
}
|
||||
|
||||
# Tenant admins receive the approved review/edit surface, not arbitrary fields that
|
||||
# may be added to the persona schema later. Add new fields deliberately.
|
||||
ADMIN_VISIBLE_PERSONA_FIELDS = {
|
||||
"id", "name", "tier", "initiation_mode", "channel", "profession", "age_group",
|
||||
"location", "product_context", "background", "income", "lifestyle", "personality",
|
||||
"communication_style", "budget", "decision_timeline", "goal", "difficulty", "notes",
|
||||
"recontact",
|
||||
}
|
||||
ADMIN_EDITABLE_PERSONA_FIELDS = ADMIN_VISIBLE_PERSONA_FIELDS - {"id"}
|
||||
|
||||
# Keep the group envelope closed as well as the nested persona envelope. New
|
||||
# internal fields must be deliberately added here before they can cross the API.
|
||||
GROUP_VISIBLE_FIELDS = {
|
||||
"id", "org_id", "creator_id", "owner_user_id", "title", "status",
|
||||
"created_at", "updated_at", "input", "sales_kit", "personas", "report", "error",
|
||||
}
|
||||
|
||||
|
||||
def strip_secret_fields(persona: dict) -> dict:
|
||||
"""Return a copy of a persona with secret/process fields removed."""
|
||||
out = dict(persona)
|
||||
for f in SECRET_PERSONA_FIELDS:
|
||||
out.pop(f, None)
|
||||
pains = out.get("pains")
|
||||
if isinstance(pains, list):
|
||||
cleaned = []
|
||||
for p in pains:
|
||||
if isinstance(p, dict):
|
||||
p = {k: v for k, v in p.items() if k not in ("rootCause", "resolutionConditions")}
|
||||
cleaned.append(p)
|
||||
out["pains"] = cleaned
|
||||
return out
|
||||
"""Return only explicitly approved tenant-admin persona fields."""
|
||||
return {
|
||||
field: persona[field]
|
||||
for field in ADMIN_VISIBLE_PERSONA_FIELDS
|
||||
if field in persona and field not in SECRET_PERSONA_FIELDS
|
||||
}
|
||||
|
||||
|
||||
def serialize_persona(persona: dict, actor: dict) -> dict:
|
||||
"""Serialize a persona through the single role-aware policy."""
|
||||
shaped = ensure_persona_shape(persona)
|
||||
if actor.get("role") == "super_admin":
|
||||
return shaped
|
||||
if actor.get("role") == "user":
|
||||
return revealable_view(shaped)
|
||||
return strip_secret_fields(shaped)
|
||||
|
||||
|
||||
def serialize_group(group: dict, actor: dict) -> dict:
|
||||
"""Serialize a group without exposing latent persona recipe data."""
|
||||
raw_input = group.get("input")
|
||||
source = raw_input if isinstance(raw_input, dict) else {}
|
||||
raw_personas = group.get("personas")
|
||||
personas = raw_personas if isinstance(raw_personas, list) else []
|
||||
view = {
|
||||
field: group[field]
|
||||
for field in GROUP_VISIBLE_FIELDS
|
||||
if field in group
|
||||
}
|
||||
# Historical groups may contain legacy raw exception text. Normalize at the API
|
||||
# boundary so old records cannot leak paths/provider responses either.
|
||||
if view.get("error"):
|
||||
view["error"] = "analysis_failed" if view.get("status") == "failed" else "operation_failed"
|
||||
view["personas"] = [
|
||||
serialize_persona(persona, actor) for persona in personas if isinstance(persona, dict)
|
||||
]
|
||||
if actor.get("role") == "user":
|
||||
# Trainees need only the high-level offer context. Do not return uploaded
|
||||
# filenames or parsed source text from the admin's group brief.
|
||||
view["input"] = {
|
||||
key: source[key]
|
||||
for key in ("product", "segment", "channel", "language")
|
||||
if key in source
|
||||
}
|
||||
elif actor.get("role") != "super_admin":
|
||||
# Tenant admins can manage personas, but uploaded filenames, parsed source
|
||||
# text, and future internal input fields remain server-side only.
|
||||
view["input"] = {
|
||||
key: source[key]
|
||||
for key in ("product", "segment", "channel", "language")
|
||||
if key in source
|
||||
}
|
||||
if actor.get("role") != "super_admin":
|
||||
view["sales_kit"] = None
|
||||
view["report"] = None
|
||||
return view
|
||||
|
||||
_ANALYZE_LOCKS: dict[str, threading.Lock] = {}
|
||||
_ANALYZE_GUARD = threading.Lock()
|
||||
@@ -58,11 +146,18 @@ def _authorize_group(group: dict) -> None:
|
||||
Private/personal groups (owner_user_id set) are only accessible by their owner
|
||||
(or super_admin), even within the same org.
|
||||
"""
|
||||
if not isinstance(group, dict):
|
||||
raise ApiError("group state unavailable", 503)
|
||||
actor = current_user()
|
||||
if actor.get("role") == "super_admin":
|
||||
return
|
||||
owner_marker_present = "owner_user_id" in group
|
||||
owner = group.get("owner_user_id")
|
||||
if owner and owner != actor["id"]:
|
||||
if owner_marker_present and (
|
||||
not isinstance(owner, str) or not owner.strip() or owner != actor.get("id")
|
||||
):
|
||||
raise ApiError("permission denied", 403)
|
||||
if not is_valid_tenant_id(actor.get("org_id")) or not is_valid_tenant_id(group.get("org_id")):
|
||||
raise ApiError("permission denied", 403)
|
||||
if group.get("org_id") != actor.get("org_id"):
|
||||
raise ApiError("permission denied", 403)
|
||||
@@ -70,9 +165,13 @@ def _authorize_group(group: dict) -> None:
|
||||
|
||||
def _get_owned_group(s, gid: str) -> dict:
|
||||
group = s["groups"].get_or_none(gid)
|
||||
if not group:
|
||||
if group is None:
|
||||
raise ApiError("group not found", 404)
|
||||
if not isinstance(group, dict):
|
||||
raise ApiError("group state unavailable", 503)
|
||||
_authorize_group(group)
|
||||
if current_user().get("role") == "user" and group.get("status") != "ready":
|
||||
raise ApiError("group not ready", 403)
|
||||
return group
|
||||
|
||||
|
||||
@@ -82,6 +181,72 @@ def _upload_dir():
|
||||
return d
|
||||
|
||||
|
||||
def _cleanup_uploads(names: list[str]) -> None:
|
||||
for name in names:
|
||||
if (
|
||||
not isinstance(name, str)
|
||||
or not name
|
||||
or Path(name).name != name
|
||||
or "/" in name
|
||||
or "\\" in name
|
||||
or any(ord(char) < 32 or ord(char) == 127 for char in name)
|
||||
):
|
||||
current_app.logger.warning("skipping unsafe uploaded-file cleanup name")
|
||||
continue
|
||||
try:
|
||||
name.encode("utf-8")
|
||||
except UnicodeError:
|
||||
current_app.logger.warning("skipping invalid uploaded-file cleanup name")
|
||||
continue
|
||||
try:
|
||||
destination = _upload_dir() / name
|
||||
destination.resolve(strict=False).relative_to(_upload_dir().resolve(strict=True))
|
||||
destination.unlink(missing_ok=True)
|
||||
except (UnicodeError, ValueError):
|
||||
current_app.logger.warning("skipping uploaded-file cleanup outside upload directory")
|
||||
except OSError as exc:
|
||||
current_app.logger.error(
|
||||
"uploaded-file cleanup failed (error_type=%s)", type(exc).__name__
|
||||
)
|
||||
|
||||
|
||||
def _inspect_upload_size(stream) -> int:
|
||||
"""Return a known nonnegative upload size; unknown sizes fail closed."""
|
||||
try:
|
||||
stream.seek(0, 2)
|
||||
size = stream.tell()
|
||||
stream.seek(0)
|
||||
except (OSError, AttributeError, TypeError, ValueError) as exc:
|
||||
raise ApiError("could not inspect uploaded file", 400) from exc
|
||||
if isinstance(size, bool) or not isinstance(size, int) or size < 0:
|
||||
raise ApiError("could not determine uploaded file size", 400)
|
||||
return size
|
||||
|
||||
|
||||
def _validate_upload_budget(
|
||||
*, content_length: int | None, file_count: int, total_bytes: int
|
||||
) -> None:
|
||||
"""Fail closed before saving files when the request budget is unknowable/exceeded."""
|
||||
max_bytes = Config.UPLOAD_MAX_MB * 1024 * 1024
|
||||
if content_length is None:
|
||||
raise ApiError("upload request size is unknown", 413)
|
||||
if (
|
||||
isinstance(content_length, bool)
|
||||
or not isinstance(content_length, int)
|
||||
or content_length < 0
|
||||
or content_length > max_bytes
|
||||
):
|
||||
raise ApiError("upload request too large", 413)
|
||||
if isinstance(file_count, bool) or not isinstance(file_count, int) or file_count < 0:
|
||||
raise ApiError("invalid upload file count", 400)
|
||||
if file_count > Config.UPLOAD_MAX_FILES:
|
||||
raise ApiError("too many uploaded files", 413)
|
||||
if isinstance(total_bytes, bool) or not isinstance(total_bytes, int) or total_bytes < 0:
|
||||
raise ApiError("invalid upload size", 400)
|
||||
if total_bytes > max_bytes:
|
||||
raise ApiError("uploaded files too large", 413)
|
||||
|
||||
|
||||
@groups_bp.post("")
|
||||
@require_auth
|
||||
@require_roles("admin")
|
||||
@@ -91,61 +256,139 @@ def create_group():
|
||||
file_text = ""
|
||||
saved_files = []
|
||||
|
||||
if request.files:
|
||||
for file in request.files.getlist("files"):
|
||||
try:
|
||||
is_multipart = bool(
|
||||
request.content_type and "multipart/form-data" in request.content_type
|
||||
)
|
||||
if is_multipart:
|
||||
_validate_upload_budget(
|
||||
content_length=request.content_length,
|
||||
file_count=0,
|
||||
total_bytes=0,
|
||||
)
|
||||
files = request.files.getlist("files") if is_multipart else []
|
||||
total_bytes = 0
|
||||
destinations = []
|
||||
for file in files:
|
||||
raw_name = file.filename or ""
|
||||
# Path traversal defense: take only the basename, drop any directory
|
||||
# segments and reject empty/unsafe names. Never trust client filename as a path.
|
||||
safe_name = Path(raw_name).name
|
||||
if not safe_name or safe_name in (".", "..", "/", "\\") or "/" in raw_name or "\\" in raw_name:
|
||||
if (
|
||||
not safe_name
|
||||
or safe_name in (".", "..", "/", "\\")
|
||||
or "/" in raw_name
|
||||
or "\\" in raw_name
|
||||
or contains_control_characters(safe_name)
|
||||
):
|
||||
raise ApiError("invalid file name")
|
||||
ext = safe_name.rsplit(".", 1)[-1].lower()
|
||||
if ext not in Config.ALLOWED_UPLOAD_EXTS:
|
||||
raise ApiError(f"unsupported file type: {ext}")
|
||||
dest = _upload_dir() / f"{current_user()['id'].replace('@','_')}__{safe_name}"
|
||||
raise ApiError("unsupported file type")
|
||||
max_bytes = Config.UPLOAD_MAX_MB * 1024 * 1024
|
||||
size = _inspect_upload_size(file.stream)
|
||||
if size > max_bytes:
|
||||
raise ApiError("uploaded file too large", 413)
|
||||
total_bytes += size
|
||||
_validate_upload_budget(
|
||||
content_length=request.content_length,
|
||||
file_count=len(files),
|
||||
total_bytes=total_bytes,
|
||||
)
|
||||
dest = _upload_dir() / (
|
||||
f"{current_user()['id'].replace('@','_')}__{uuid.uuid4().hex[:12]}__{safe_name}"
|
||||
)
|
||||
# Ensure resolved path stays inside the upload dir (defense in depth).
|
||||
try:
|
||||
dest.resolve(strict=False).relative_to(_upload_dir().resolve(strict=True))
|
||||
except ValueError:
|
||||
raise ApiError("invalid file path")
|
||||
file.save(dest)
|
||||
# Register the destination before saving so a partial write is also
|
||||
# removed if the storage operation raises.
|
||||
destinations.append((file, dest))
|
||||
for file, dest in destinations:
|
||||
saved_files.append(dest.name)
|
||||
file.save(dest)
|
||||
except RequestEntityTooLarge:
|
||||
_cleanup_uploads(saved_files)
|
||||
raise
|
||||
except ApiError:
|
||||
_cleanup_uploads(saved_files)
|
||||
raise
|
||||
except Exception as exc:
|
||||
_cleanup_uploads(saved_files)
|
||||
raise internal_error("could not save uploaded file", exc, 400)
|
||||
|
||||
if request.content_type and "multipart/form-data" in request.content_type:
|
||||
data = request.form.to_dict()
|
||||
else:
|
||||
data = request.get_json(silent=True) or {}
|
||||
|
||||
from ..services.file_parser import parse_document
|
||||
|
||||
for name in saved_files:
|
||||
keep_uploads = False
|
||||
try:
|
||||
try:
|
||||
file_text += "\n\n" + parse_document(_upload_dir() / name)
|
||||
if request.content_type and "multipart/form-data" in request.content_type:
|
||||
data = request.form.to_dict()
|
||||
else:
|
||||
data = request_json_object()
|
||||
except RequestEntityTooLarge:
|
||||
raise
|
||||
except Exception as exc:
|
||||
raise ApiError(f"could not parse file {name}: {exc}")
|
||||
raise internal_error("invalid upload request", exc, 400)
|
||||
|
||||
product = (data.get("product") or "").strip()
|
||||
if product == "" and not file_text.strip():
|
||||
raise ApiError("provide product info in the form or via file upload")
|
||||
# Keep parser import and every post-save parse operation inside the same
|
||||
# cleanup-safe boundary. Import failures can happen after files are saved.
|
||||
try:
|
||||
from ..services.file_parser import ParseError, parse_document
|
||||
|
||||
group = s["groups"].create(
|
||||
org_id=current_user().get("org_id") or "org-default",
|
||||
creator_id=current_user()["id"],
|
||||
title=(product or file_text[:80] or "Untitled group").strip()[:200],
|
||||
)
|
||||
s["groups"].update(
|
||||
group["id"],
|
||||
input={
|
||||
"product": product,
|
||||
"segment": (data.get("segment") or ""),
|
||||
"description": (data.get("description") or ""),
|
||||
"channel": (data.get("channel") or "social"),
|
||||
"language": (data.get("language") or "th"),
|
||||
"files": saved_files,
|
||||
"file_text": file_text[:60000],
|
||||
},
|
||||
)
|
||||
return jsonify({"group": s["groups"].get(group["id"])}), 201
|
||||
max_chars = Config.UPLOAD_MAX_EXTRACTED_CHARS
|
||||
for name in saved_files:
|
||||
parsed = parse_document(_upload_dir() / name)
|
||||
if not isinstance(parsed, str):
|
||||
raise ValueError("parser returned non-text output")
|
||||
remaining = max_chars - len(file_text) - 2
|
||||
if remaining < 0 or len(parsed) > remaining:
|
||||
raise ParseError("extracted text too large")
|
||||
file_text += "\n\n" + parsed
|
||||
except Exception as exc:
|
||||
raise internal_error("could not parse uploaded file", exc, 400)
|
||||
|
||||
string_fields = {"product": "", "segment": "", "description": "", "channel": "facebook", "language": "th"}
|
||||
for field, default in string_fields.items():
|
||||
value = data.get(field)
|
||||
if value is not None and not isinstance(value, str):
|
||||
raise ApiError(f"{field} must be a string", 400)
|
||||
string_fields[field] = (value if value is not None else default).strip()
|
||||
product = string_fields["product"]
|
||||
if product == "" and not file_text.strip():
|
||||
raise ApiError("provide product info in the form or via file upload")
|
||||
actor_org_id = current_user().get("org_id")
|
||||
if not is_valid_tenant_id(actor_org_id):
|
||||
raise ApiError("permission denied", 403)
|
||||
|
||||
try:
|
||||
group = s["groups"].create(
|
||||
org_id=actor_org_id,
|
||||
creator_id=current_user()["id"],
|
||||
title=(product or file_text[:80] or "Untitled group").strip()[:200],
|
||||
)
|
||||
s["groups"].update(
|
||||
group["id"],
|
||||
input={
|
||||
"product": product,
|
||||
"segment": string_fields["segment"],
|
||||
"description": string_fields["description"],
|
||||
"channel": string_fields["channel"],
|
||||
"language": string_fields["language"],
|
||||
"files": saved_files,
|
||||
"file_text": file_text[:60000],
|
||||
},
|
||||
)
|
||||
except Exception as exc:
|
||||
raise internal_error("could not create group", exc, 500)
|
||||
response = jsonify({
|
||||
"group": serialize_group(s["groups"].get(group["id"]), current_user())
|
||||
})
|
||||
keep_uploads = True
|
||||
return response, 201
|
||||
finally:
|
||||
if not keep_uploads:
|
||||
_cleanup_uploads(saved_files)
|
||||
|
||||
|
||||
@groups_bp.get("")
|
||||
@@ -153,29 +396,35 @@ def create_group():
|
||||
def list_groups():
|
||||
s = _stores()
|
||||
actor = current_user()
|
||||
if actor.get("role") != "super_admin" and (
|
||||
not is_valid_tenant_id(actor.get("org_id"))
|
||||
):
|
||||
raise ApiError("invalid tenant context", 403)
|
||||
visible = s["groups"].list_visible_to(
|
||||
role=actor.get("role"), org_id=actor.get("org_id")
|
||||
role=actor.get("role"),
|
||||
# Platform super-admins need the cross-tenant group index; all other
|
||||
# roles remain constrained to the authenticated organization's scope.
|
||||
org_id=None if actor.get("role") == "super_admin" else actor.get("org_id"),
|
||||
user_id=actor.get("id"),
|
||||
)
|
||||
# Expose personal/private groups only to their owner (IDOR defense in listing).
|
||||
if actor.get("role") != "super_admin":
|
||||
visible = [
|
||||
g
|
||||
for g in visible
|
||||
if not g.get("owner_user_id") or g.get("owner_user_id") == actor["id"]
|
||||
]
|
||||
# ``list_visible_to`` applies the trainee owner/redaction policy at the
|
||||
# service boundary; keep this route limited to lightweight summaries.
|
||||
# Lightweight summaries only — never send the full personas/sales_kit/report to a
|
||||
# list view (huge, heavy, leaks the recipe). Training shows title + persona count.
|
||||
summaries = []
|
||||
for g in visible:
|
||||
personas = g.get("personas") or []
|
||||
raw_personas = g.get("personas")
|
||||
personas = raw_personas if isinstance(raw_personas, list) else []
|
||||
raw_input = g.get("input")
|
||||
group_input = raw_input if isinstance(raw_input, dict) else {}
|
||||
summaries.append({
|
||||
"id": g.get("id"),
|
||||
"title": g.get("title", ""),
|
||||
"status": g.get("status", "draft"),
|
||||
"channel": g.get("channel"),
|
||||
"channel": group_input.get("channel"),
|
||||
"org_id": g.get("org_id"),
|
||||
"persona_count": len(personas),
|
||||
"input": {"product": (g.get("input") or {}).get("product", "")},
|
||||
"input": {"product": group_input.get("product", "")},
|
||||
})
|
||||
return jsonify({"groups": summaries})
|
||||
|
||||
@@ -183,14 +432,13 @@ def list_groups():
|
||||
@groups_bp.post("/<gid>/analyze")
|
||||
@require_auth
|
||||
@require_roles("admin")
|
||||
@_group_analysis_lock
|
||||
def analyze_group(gid: str):
|
||||
"""Run analysis: sales kit + 15 personas. Synchronous for v1 (replaces gen).
|
||||
|
||||
?append=true generates MORE personas and appends to existing ones instead of
|
||||
replacing them (used by 'create more personas')."""
|
||||
"""Run one deterministic analysis: sales kit + exactly 15 personas."""
|
||||
s = _stores()
|
||||
group = _get_owned_group(s, gid)
|
||||
append = request.args.get("append") == "true"
|
||||
if request.args.get("append") == "true":
|
||||
raise ApiError("append mode is no longer supported", 400)
|
||||
|
||||
inp = group.get("input", {})
|
||||
if not s["llm"]:
|
||||
@@ -199,47 +447,52 @@ def analyze_group(gid: str):
|
||||
from ..services.analyzer import Analyzer
|
||||
from ..services.persona_generator import PersonaGenerator
|
||||
|
||||
# If appending, reuse the existing sales kit; else re-run the full analysis.
|
||||
sales_kit = group.get("sales_kit")
|
||||
if not append or not sales_kit:
|
||||
s["groups"].update(gid, status="analyzing", error=None)
|
||||
try:
|
||||
sales_kit = Analyzer(s["llm"]).analyze(
|
||||
product=inp.get("product", ""),
|
||||
segment=inp.get("segment", ""),
|
||||
description=inp.get("description", ""),
|
||||
file_text=inp.get("file_text", ""),
|
||||
channel=inp.get("channel", "social"),
|
||||
)
|
||||
except Exception as exc:
|
||||
s["groups"].update(gid, status="failed", error=str(exc))
|
||||
raise ApiError(f"analysis failed: {exc}", 500)
|
||||
s["groups"].update(gid, sales_kit=sales_kit, status="ready", error=None)
|
||||
|
||||
existing = []
|
||||
if append:
|
||||
existing = s["groups"].get_or_none(gid).get("personas", []) or []
|
||||
s["groups"].update(gid, status="analyzing", error=None)
|
||||
try:
|
||||
sales_kit = Analyzer(s["llm"]).analyze(
|
||||
product=inp.get("product", ""),
|
||||
segment=inp.get("segment", ""),
|
||||
description=inp.get("description", ""),
|
||||
file_text=inp.get("file_text", ""),
|
||||
channel=inp.get("channel", "social"),
|
||||
)
|
||||
except Exception as exc:
|
||||
s["groups"].update(gid, status="failed", error="analysis_failed")
|
||||
raise internal_error("analysis failed", exc)
|
||||
try:
|
||||
personas = PersonaGenerator(s["llm"]).generate(
|
||||
sales_kit=sales_kit,
|
||||
language=inp.get("language", "th"),
|
||||
channel=inp.get("channel", "social"),
|
||||
channel=inp.get("channel") if inp.get("channel") in PERSONA_CHANNELS else "facebook",
|
||||
)
|
||||
personas = existing + personas
|
||||
except Exception as exc:
|
||||
s["groups"].update(gid, status="failed", error=str(exc))
|
||||
raise ApiError(f"analysis failed: {exc}", 500)
|
||||
s["groups"].update(gid, status="failed", error="analysis_failed")
|
||||
raise internal_error("analysis failed", exc)
|
||||
|
||||
from ..services.report import build_report
|
||||
|
||||
report = build_report(sales_kit=sales_kit, personas=personas, language=inp.get("language", "th"))
|
||||
s["groups"].update(gid, sales_kit=sales_kit, status="ready", error=None)
|
||||
s["groups"].set_personas(gid, personas)
|
||||
s["groups"].update(gid, report=report)
|
||||
try:
|
||||
report = build_report(sales_kit=sales_kit, personas=personas, language=inp.get("language", "th"))
|
||||
except Exception as exc:
|
||||
s["groups"].update(gid, status="failed", error="analysis_failed")
|
||||
raise internal_error("analysis failed", exc)
|
||||
# Publish the completed analysis as one atomic record replacement. Readers
|
||||
# do not take the group mutation lock, so status must never become ready
|
||||
# before personas/report/sales_kit are visible together.
|
||||
published = s["groups"].get(gid)
|
||||
published.update(
|
||||
sales_kit=sales_kit,
|
||||
personas=personas,
|
||||
report=report,
|
||||
status="ready",
|
||||
error=None,
|
||||
)
|
||||
s["groups"].groups.replace(gid, published)
|
||||
serialized = serialize_group(published, current_user())
|
||||
return jsonify({
|
||||
"group": s["groups"].get(gid),
|
||||
"sales_kit": sales_kit,
|
||||
"personas": s["groups"].get(gid)["personas"],
|
||||
"group": serialized,
|
||||
"sales_kit": serialized.get("sales_kit"),
|
||||
"personas": serialized["personas"],
|
||||
})
|
||||
|
||||
|
||||
@@ -250,20 +503,100 @@ def get_group(gid: str):
|
||||
group = _get_owned_group(s, gid)
|
||||
actor = current_user()
|
||||
|
||||
view = dict(group)
|
||||
if actor.get("role") == "user":
|
||||
# Trainee: hide latent persona fields + sales kit + report (they contain
|
||||
# pain analysis / latent data a real seller wouldn't know before a result).
|
||||
view["personas"] = [revealable_view(p) for p in group.get("personas", [])]
|
||||
view["sales_kit"] = None
|
||||
view["report"] = None
|
||||
elif actor.get("role") != "super_admin":
|
||||
# admin: see group, but not secret/process persona fields nor the sales-kit/report
|
||||
# (pain-fit analysis is the IP to protect).
|
||||
view["personas"] = [strip_secret_fields(p) for p in group.get("personas", [])]
|
||||
view["sales_kit"] = None
|
||||
view["report"] = None
|
||||
return jsonify({"group": view})
|
||||
return jsonify({"group": serialize_group(group, actor)})
|
||||
|
||||
|
||||
def _report_scalar(value, *, limit: int = 1000) -> str | None:
|
||||
"""Allow only bounded scalar report values; never stringify nested internals."""
|
||||
if isinstance(value, bool):
|
||||
return None
|
||||
if isinstance(value, int):
|
||||
text = str(value)
|
||||
elif isinstance(value, float):
|
||||
if not math.isfinite(value):
|
||||
return None
|
||||
text = str(value)
|
||||
elif isinstance(value, str):
|
||||
text = value.strip()
|
||||
else:
|
||||
return None
|
||||
return text[:limit] if text else None
|
||||
|
||||
|
||||
def _report_scalar_list(value) -> list[str]:
|
||||
if not isinstance(value, list):
|
||||
return []
|
||||
return [
|
||||
scalar
|
||||
for item in value[:20]
|
||||
if (scalar := _report_scalar(item)) is not None
|
||||
]
|
||||
|
||||
|
||||
@groups_bp.get("/<gid>/report")
|
||||
@require_auth
|
||||
@require_roles("admin")
|
||||
def group_report(gid: str):
|
||||
"""Return a human-readable report without latent persona recipe fields."""
|
||||
s = _stores()
|
||||
group = _get_owned_group(s, gid)
|
||||
raw_sales_kit = group.get("sales_kit")
|
||||
sales_kit: dict = raw_sales_kit if isinstance(raw_sales_kit, dict) else {}
|
||||
raw_group_input = group.get("input")
|
||||
group_input: dict = raw_group_input if isinstance(raw_group_input, dict) else {}
|
||||
title = _report_scalar(group.get("title"), limit=200) or "Sales training report"
|
||||
product = (
|
||||
_report_scalar(sales_kit.get("productName"), limit=1000)
|
||||
or _report_scalar(group_input.get("product"), limit=1000)
|
||||
or "-"
|
||||
)
|
||||
lines = [
|
||||
f"# {title}",
|
||||
"",
|
||||
"## Sales Kit",
|
||||
f"- Product: {product}",
|
||||
]
|
||||
for label, key in (("Category", "category"), ("Value props", "valueProps"), ("Features", "features"), ("Pricing", "pricingAnchors")):
|
||||
value = sales_kit.get(key)
|
||||
if isinstance(value, list):
|
||||
values = _report_scalar_list(value)
|
||||
if values:
|
||||
lines.append(f"- {label}: {', '.join(values)}")
|
||||
else:
|
||||
scalar = _report_scalar(value)
|
||||
if scalar is not None:
|
||||
lines.append(f"- {label}: {scalar}")
|
||||
audience = sales_kit.get("targetAudience")
|
||||
if isinstance(audience, dict):
|
||||
segment = _report_scalar(audience.get("segment"))
|
||||
if segment is not None:
|
||||
lines.append(f"- Target segment: {segment}")
|
||||
lines.extend(["", "## Personas"])
|
||||
persona_fields = (
|
||||
"name", "tier", "difficulty", "profession", "age_group", "location",
|
||||
"product_context", "goal", "decision_timeline", "channel", "initiation_mode",
|
||||
)
|
||||
raw_personas = group.get("personas")
|
||||
persona_records = raw_personas if isinstance(raw_personas, list) else []
|
||||
for persona in persona_records:
|
||||
if not isinstance(persona, dict):
|
||||
continue
|
||||
name = _report_scalar(persona.get("name"), limit=200) or "Persona"
|
||||
lines.append(f"### {name}")
|
||||
for field in persona_fields[1:]:
|
||||
scalar = _report_scalar(persona.get(field))
|
||||
if scalar is not None:
|
||||
lines.append(f"- {field}: {scalar}")
|
||||
lines.append("")
|
||||
markdown = "\n".join(lines).strip() + "\n"
|
||||
if request.args.get("format") == "json":
|
||||
title = _report_scalar(group.get("title"), limit=200) or "Sales training report"
|
||||
return jsonify({"title": title, "markdown": markdown})
|
||||
return Response(
|
||||
markdown,
|
||||
mimetype="text/markdown",
|
||||
headers={"Content-Disposition": "attachment; filename=sales-training-report.md"},
|
||||
)
|
||||
|
||||
|
||||
@groups_bp.get("/<gid>/personas")
|
||||
@@ -272,20 +605,33 @@ def list_personas(gid: str):
|
||||
s = _stores()
|
||||
group = _get_owned_group(s, gid)
|
||||
actor = current_user()
|
||||
raw_personas = group.get("personas")
|
||||
persona_records = raw_personas if isinstance(raw_personas, list) else []
|
||||
if actor.get("role") == "user":
|
||||
if group.get("status") != "ready":
|
||||
raise ApiError("group not ready", 403)
|
||||
personas = [revealable_view(p) for p in group.get("personas", [])]
|
||||
personas = [serialize_persona(p, actor) for p in persona_records if isinstance(p, dict)]
|
||||
elif actor.get("role") == "super_admin":
|
||||
personas = group.get("personas", [])
|
||||
personas = [serialize_persona(p, actor) for p in persona_records if isinstance(p, dict)]
|
||||
else:
|
||||
# admin: see persona but not the secret/process fields (IP protection)
|
||||
personas = [strip_secret_fields(p) for p in group.get("personas", [])]
|
||||
personas = [serialize_persona(p, actor) for p in persona_records if isinstance(p, dict)]
|
||||
# attach per-user status (won/lost/not-tried) for trainees
|
||||
if actor.get("role") == "user":
|
||||
sess = _stores().get("session_store")
|
||||
store = sess.sessions if sess else None
|
||||
mine = store.where(lambda r: r.get("user_id") == actor["id"] and r.get("group_id") == gid) if store else []
|
||||
mine = (
|
||||
store.where(
|
||||
lambda r: isinstance(r, dict)
|
||||
and r.get("org_id") == actor.get("org_id")
|
||||
and r.get("user_id") == actor["id"]
|
||||
and r.get("group_id") == gid
|
||||
and (r.get("mode") or "trainee") == "trainee"
|
||||
and r.get("status") == "finished"
|
||||
and r.get("outcome") in {"won", "lost"}
|
||||
)
|
||||
if store else []
|
||||
)
|
||||
outcome_by_pid = {r.get("persona_id"): r.get("outcome") for r in mine}
|
||||
for p in personas:
|
||||
p["my_outcome"] = outcome_by_pid.get(p.get("id"), "not_tried")
|
||||
@@ -305,41 +651,52 @@ def get_persona(gid: str, pid: str):
|
||||
if actor.get("role") == "user" and group.get("status") != "ready":
|
||||
raise ApiError("group not ready", 403)
|
||||
ensure = ensure_persona_shape(p)
|
||||
if actor.get("role") == "user":
|
||||
return jsonify({"persona": revealable_view(ensure)})
|
||||
if actor.get("role") == "super_admin":
|
||||
return jsonify({"persona": ensure})
|
||||
# admin: hidden secret/process fields (IP protection)
|
||||
return jsonify({"persona": strip_secret_fields(ensure)})
|
||||
return jsonify({"persona": serialize_persona(ensure, actor)})
|
||||
|
||||
|
||||
@groups_bp.put("/<gid>/personas/<pid>")
|
||||
@require_auth
|
||||
@require_roles("admin")
|
||||
@_group_analysis_lock
|
||||
def update_persona(gid: str, pid: str):
|
||||
s = _stores()
|
||||
_get_owned_group(s, gid)
|
||||
data = request.get_json(silent=True) or {}
|
||||
data = request_json_object()
|
||||
actor = current_user()
|
||||
# IP protection: only super_admin may set/alter secret formula fields.
|
||||
if actor.get("role") != "super_admin":
|
||||
for f in SECRET_PERSONA_FIELDS:
|
||||
if f in data:
|
||||
raise ApiError(f"field '{f}' is locked (super_admin only)", 403)
|
||||
unknown = sorted(set(data) - ADMIN_EDITABLE_PERSONA_FIELDS)
|
||||
if unknown:
|
||||
raise ApiError(f"field '{unknown[0]}' is locked (super_admin only)", 403)
|
||||
current = s["groups"].get_persona(gid, pid)
|
||||
if current is None:
|
||||
raise ApiError("persona not found", 404)
|
||||
try:
|
||||
validate_persona_traits(ensure_persona_shape({**current, **data, "id": pid}))
|
||||
except ValueError:
|
||||
raise ApiError("invalid persona traits", 400)
|
||||
try:
|
||||
updated = s["groups"].update_persona(gid, pid, data)
|
||||
except ValueError as exc:
|
||||
raise ApiError(str(exc), 404)
|
||||
full = ensure_persona_shape(updated["personas"][
|
||||
next(i for i, p in enumerate(updated["personas"]) if p["id"] == pid)
|
||||
])
|
||||
if actor.get("role") == "super_admin":
|
||||
return jsonify({"persona": full})
|
||||
return jsonify({"persona": strip_secret_fields(full)})
|
||||
raise internal_error("persona not found", exc, 404)
|
||||
raw_personas = updated.get("personas") if isinstance(updated, dict) else None
|
||||
persona = next(
|
||||
(
|
||||
candidate
|
||||
for candidate in raw_personas or []
|
||||
if isinstance(candidate, dict) and candidate.get("id") == pid
|
||||
),
|
||||
None,
|
||||
) if isinstance(raw_personas, list) else None
|
||||
if persona is None:
|
||||
raise internal_error("persona update unavailable", ValueError("updated persona missing"))
|
||||
full = ensure_persona_shape(persona)
|
||||
return jsonify({"persona": serialize_persona(full, actor)})
|
||||
|
||||
|
||||
@groups_bp.post("/<gid>/personas/<pid>/variant")
|
||||
@require_auth
|
||||
@_group_analysis_lock
|
||||
def create_persona_variant(gid: str, pid: str):
|
||||
"""Create a NEW persona cloned from an existing one (fresh identity, same core traits).
|
||||
|
||||
@@ -352,7 +709,9 @@ def create_persona_variant(gid: str, pid: str):
|
||||
group = _get_owned_group(s, gid)
|
||||
if group.get("status") != "ready":
|
||||
raise ApiError("group not ready", 403)
|
||||
src = next((p for p in group.get("personas", []) if p.get("id") == pid), None)
|
||||
raw_personas = group.get("personas")
|
||||
persona_records = raw_personas if isinstance(raw_personas, list) else []
|
||||
src = next((p for p in persona_records if isinstance(p, dict) and p.get("id") == pid), None)
|
||||
if src is None:
|
||||
raise ApiError("persona not found", 404)
|
||||
|
||||
@@ -365,24 +724,64 @@ def create_persona_variant(gid: str, pid: str):
|
||||
language=lang,
|
||||
)
|
||||
except Exception as exc:
|
||||
raise ApiError(f"variant failed: {exc}", 500)
|
||||
raise internal_error("variant failed", exc)
|
||||
|
||||
# Assign a unique id and append to the group (keeps existing personas/sessions intact).
|
||||
# Assign a unique id. Trainee-created variants belong to the trainee's private
|
||||
# group; only admin-created variants extend the shared admin pool.
|
||||
import uuid as _uuid
|
||||
variant["id"] = f"persona-{_uuid.uuid4().hex[:10]}"
|
||||
variant["source_persona_id"] = pid
|
||||
personas = group.get("personas", []) + [variant]
|
||||
s["groups"].update(gid, personas=personas)
|
||||
actor = current_user()
|
||||
target_group_id = gid
|
||||
if actor.get("role") == "user":
|
||||
actor_org_id = actor.get("org_id")
|
||||
if not is_valid_tenant_id(actor_org_id):
|
||||
raise ApiError("permission denied", 403)
|
||||
try:
|
||||
private_group = s["groups"].get_or_create_private_group(
|
||||
org_id=actor_org_id,
|
||||
owner_user_id=actor["id"],
|
||||
owner_name=actor.get("name", "User"),
|
||||
input_data=group.get("input") or None,
|
||||
sales_kit=group.get("sales_kit") or None,
|
||||
)
|
||||
except ValueError as exc:
|
||||
raise internal_error("private group unavailable", exc, 409)
|
||||
target_group_id = private_group["id"]
|
||||
# Re-read while holding the target record lock. The snapshot returned by
|
||||
# get_or_create_private_group may be stale when two trainee requests append
|
||||
# variants concurrently.
|
||||
with s["groups"].record_lock(target_group_id):
|
||||
current_private = s["groups"].get(target_group_id)
|
||||
raw_private_personas = current_private.get("personas")
|
||||
private_personas = (
|
||||
[p for p in raw_private_personas if isinstance(p, dict)]
|
||||
if isinstance(raw_private_personas, list)
|
||||
else []
|
||||
)
|
||||
s["groups"].set_personas(
|
||||
target_group_id, private_personas + [variant]
|
||||
)
|
||||
else:
|
||||
# Admin variants extend the shared group. Serialize and re-read the
|
||||
# append so concurrent admins cannot overwrite one another.
|
||||
with s["groups"].record_lock(gid):
|
||||
current_group = s["groups"].get(gid)
|
||||
raw_current_personas = current_group.get("personas")
|
||||
current_personas = (
|
||||
[p for p in raw_current_personas if isinstance(p, dict)]
|
||||
if isinstance(raw_current_personas, list)
|
||||
else []
|
||||
)
|
||||
s["groups"].update(gid, personas=current_personas + [variant])
|
||||
full = ensure_persona_shape(variant)
|
||||
|
||||
actor = current_user()
|
||||
if actor.get("role") == "super_admin":
|
||||
persona_out = full
|
||||
elif actor.get("role") == "user":
|
||||
persona_out = revealable_view(full)
|
||||
else:
|
||||
persona_out = strip_secret_fields(full)
|
||||
return jsonify({"persona": persona_out}), 201
|
||||
persona_out = serialize_persona(full, actor)
|
||||
return jsonify({
|
||||
"persona": persona_out,
|
||||
"group_id": target_group_id,
|
||||
"source_group_id": gid,
|
||||
}), 201
|
||||
|
||||
|
||||
@groups_bp.delete("/<gid>")
|
||||
@@ -391,19 +790,40 @@ def create_persona_variant(gid: str, pid: str):
|
||||
def delete_group(gid: str):
|
||||
"""Delete a persona group (admin only, own org)."""
|
||||
s = _stores()
|
||||
group = _get_owned_group(s, gid) # also enforces tenant org (super_admin global)
|
||||
s["groups"].delete(gid)
|
||||
# cascade: also remove finished/active sessions for this group's personas
|
||||
try:
|
||||
# Hold the group lock while checking/deleting the group and every related
|
||||
# session. Session start also takes this lock, so it cannot publish a new
|
||||
# session after the cascade's snapshot and before group deletion.
|
||||
with s["groups"].record_lock(gid):
|
||||
group = _get_owned_group(s, gid) # also enforces tenant org (super_admin global)
|
||||
raw_input = group.get("input")
|
||||
upload_names = (
|
||||
[name for name in raw_input.get("files", []) if isinstance(name, str)]
|
||||
if isinstance(raw_input, dict) and isinstance(raw_input.get("files"), list)
|
||||
else []
|
||||
)
|
||||
sess = s.get("session_store")
|
||||
group_org_id = group.get("org_id")
|
||||
if sess and hasattr(sess, "sessions"):
|
||||
for r in sess.sessions.all():
|
||||
if r.get("group_id") == gid:
|
||||
key = r.get("id") or r.get("sid")
|
||||
if key:
|
||||
if (
|
||||
not isinstance(r, dict)
|
||||
or r.get("group_id") != gid
|
||||
or r.get("org_id") != group_org_id
|
||||
):
|
||||
continue
|
||||
key = r.get("id") or r.get("sid")
|
||||
if not key:
|
||||
continue
|
||||
with sess.sessions.record_lock(key):
|
||||
current = sess.sessions.get_or_none(key)
|
||||
if (
|
||||
isinstance(current, dict)
|
||||
and current.get("group_id") == gid
|
||||
and current.get("org_id") == group_org_id
|
||||
):
|
||||
sess.sessions.delete(key)
|
||||
except Exception:
|
||||
pass
|
||||
s["groups"].delete(gid)
|
||||
_cleanup_uploads(upload_names)
|
||||
return jsonify({"ok": True, "deleted": gid})
|
||||
|
||||
|
||||
|
||||
+109
-11
@@ -4,10 +4,12 @@ from __future__ import annotations
|
||||
import functools
|
||||
from typing import Any, Callable
|
||||
|
||||
from flask import g, jsonify, request
|
||||
from flask import current_app, g, jsonify, request
|
||||
from werkzeug.exceptions import RequestEntityTooLarge
|
||||
|
||||
from ..auth.users import AuthError
|
||||
from ..auth.users import AuthError, is_valid_tenant_id
|
||||
from ..config import Config
|
||||
from ..storage.store import StoreError
|
||||
|
||||
|
||||
class ApiError(Exception):
|
||||
@@ -17,6 +19,9 @@ class ApiError(Exception):
|
||||
self.status = status
|
||||
|
||||
|
||||
_MISSING = object()
|
||||
|
||||
|
||||
def _get_store():
|
||||
from flask import current_app
|
||||
|
||||
@@ -27,8 +32,22 @@ def current_user() -> dict[str, Any]:
|
||||
return g.user
|
||||
|
||||
|
||||
def request_json_object(*, allow_empty: bool = False) -> dict[str, Any]:
|
||||
"""Parse a JSON object request body and reject other JSON shapes safely."""
|
||||
data = request.get_json(silent=True)
|
||||
if data is None and allow_empty:
|
||||
return {}
|
||||
if not isinstance(data, dict):
|
||||
raise ApiError("invalid request", 400)
|
||||
return data
|
||||
|
||||
|
||||
def current_org_id() -> str:
|
||||
return getattr(g, "org_id", None) or (g.user or {}).get("org_id") or "org-default"
|
||||
value = getattr(g, "org_id", _MISSING)
|
||||
if value is _MISSING:
|
||||
user = getattr(g, "user", None)
|
||||
value = user.get("org_id") if isinstance(user, dict) else None
|
||||
return value if is_valid_tenant_id(value) else ""
|
||||
|
||||
|
||||
def assert_tenant(org_id: str | None) -> None:
|
||||
@@ -37,7 +56,7 @@ def assert_tenant(org_id: str | None) -> None:
|
||||
super_admin is the platform operator and is exempt (can see all orgs)."""
|
||||
if (g.user or {}).get("role") == "super_admin":
|
||||
return
|
||||
if (org_id or "org-default") != current_org_id():
|
||||
if not is_valid_tenant_id(org_id) or org_id != current_org_id():
|
||||
raise ApiError("permission denied", 403)
|
||||
|
||||
|
||||
@@ -50,16 +69,74 @@ def require_auth(fn: Callable) -> Callable:
|
||||
raise ApiError("authentication required", 401)
|
||||
try:
|
||||
payload = _get_store().decode_token(token)
|
||||
except AuthError as exc:
|
||||
raise ApiError(str(exc), 401)
|
||||
user = _get_store().get_user_or_none(payload.get("sub", ""))
|
||||
if not user or not user.get("active", True):
|
||||
except AuthError:
|
||||
raise ApiError("invalid or expired token", 401)
|
||||
if not isinstance(payload, dict):
|
||||
raise ApiError("invalid token claims", 401)
|
||||
subject = payload.get("sub")
|
||||
token_org_id = payload.get("org_id")
|
||||
if not isinstance(subject, str) or not subject:
|
||||
raise ApiError("invalid token claims", 401)
|
||||
if not is_valid_tenant_id(token_org_id):
|
||||
raise ApiError("invalid tenant context", 401)
|
||||
try:
|
||||
user = _get_store().get_user_or_none(subject)
|
||||
except (AuthError, OSError, StoreError, TypeError, UnicodeError, ValueError):
|
||||
raise ApiError("account state unavailable", 503)
|
||||
if user is None:
|
||||
raise ApiError("account is inactive", 401)
|
||||
if not isinstance(user, dict):
|
||||
raise ApiError("account state unavailable", 503)
|
||||
try:
|
||||
_get_store().validate_persisted_identity(user)
|
||||
except AuthError:
|
||||
raise ApiError("invalid account state", 401)
|
||||
if user.get("active") is not True:
|
||||
raise ApiError("account is inactive", 401)
|
||||
if (
|
||||
not isinstance(user.get("id"), str)
|
||||
or user.get("id") != subject
|
||||
or user.get("username") != subject
|
||||
or subject != subject.strip().lower()
|
||||
or not isinstance(user.get("role"), str)
|
||||
or user.get("role") not in Config.ROLES
|
||||
or payload.get("role") != user.get("role")
|
||||
):
|
||||
raise ApiError("invalid account state", 401)
|
||||
if not isinstance(user.get("must_setup"), bool):
|
||||
raise ApiError("invalid account state", 401)
|
||||
token_auth_version = payload.get("auth_version", 0)
|
||||
user_auth_version = user.get("auth_version", 0)
|
||||
if (
|
||||
isinstance(token_auth_version, bool)
|
||||
or not isinstance(token_auth_version, int)
|
||||
or token_auth_version < 0
|
||||
or isinstance(user_auth_version, bool)
|
||||
or not isinstance(user_auth_version, int)
|
||||
or user_auth_version < 0
|
||||
or token_auth_version != user_auth_version
|
||||
):
|
||||
raise ApiError("invalid or revoked token", 401)
|
||||
user_org_id = user.get("org_id")
|
||||
if not is_valid_tenant_id(user_org_id) or token_org_id != user_org_id:
|
||||
raise ApiError("invalid tenant context", 401)
|
||||
try:
|
||||
org = _get_store().get_org_or_none(user_org_id)
|
||||
except (AuthError, OSError, StoreError, TypeError, UnicodeError, ValueError):
|
||||
raise ApiError("account state unavailable", 503)
|
||||
if org is None:
|
||||
raise ApiError("organization is inactive or missing", 401)
|
||||
if not isinstance(org, dict):
|
||||
raise ApiError("account state unavailable", 503)
|
||||
if org.get("active") is not True:
|
||||
raise ApiError("organization is inactive or missing", 401)
|
||||
g.user = user
|
||||
g.token_payload = payload
|
||||
# Tenant context: every request carries the actor's org id so downstream guards
|
||||
# can enforce per-org isolation without re-reading the user each time.
|
||||
g.org_id = (user or {}).get("org_id") or "org-default"
|
||||
g.org_id = user_org_id
|
||||
if user["must_setup"] is True and request.endpoint not in {"auth.me", "auth.setup"}:
|
||||
raise ApiError("account setup required", 403)
|
||||
return fn(*args, **kwargs)
|
||||
|
||||
return wrapper
|
||||
@@ -72,7 +149,7 @@ def require_roles(*roles: str) -> Callable:
|
||||
role = g.user.get("role")
|
||||
# super_admin passes any role gate
|
||||
allowed = {"super_admin", *roles}
|
||||
if role not in allowed:
|
||||
if not isinstance(role, str) or role not in allowed:
|
||||
raise ApiError("permission denied", 403)
|
||||
return fn(*args, **kwargs)
|
||||
|
||||
@@ -85,6 +162,27 @@ def api_error_handler(err: ApiError):
|
||||
return jsonify({"error": err.message}), err.status
|
||||
|
||||
|
||||
def internal_error(message: str, exc: BaseException, status: int = 500) -> ApiError:
|
||||
"""Log only the exception type; never expose exception text to clients."""
|
||||
current_app.logger.error(
|
||||
"%s (error_type=%s)", message, type(exc).__name__
|
||||
)
|
||||
return ApiError(message, status)
|
||||
|
||||
|
||||
def request_too_large_handler(_err: RequestEntityTooLarge):
|
||||
return jsonify({"error": "uploaded file too large"}), 413
|
||||
|
||||
|
||||
def unhandled_error_handler(err: Exception):
|
||||
current_app.logger.error(
|
||||
"unhandled request error (error_type=%s)", type(err).__name__
|
||||
)
|
||||
return jsonify({"error": "internal server error"}), 500
|
||||
|
||||
|
||||
def register_error_handlers(app) -> None:
|
||||
app.register_error_handler(ApiError, api_error_handler)
|
||||
app.register_error_handler(ValueError, lambda e: (jsonify({"error": str(e)}), 400))
|
||||
app.register_error_handler(RequestEntityTooLarge, request_too_large_handler)
|
||||
app.register_error_handler(ValueError, lambda _e: (jsonify({"error": "invalid request"}), 400))
|
||||
app.register_error_handler(Exception, unhandled_error_handler)
|
||||
+116
-47
@@ -1,11 +1,21 @@
|
||||
"""Trainee routes: win/lose board, weak-areas, generate own persona."""
|
||||
from __future__ import annotations
|
||||
|
||||
import uuid
|
||||
|
||||
from flask import Blueprint, jsonify, request
|
||||
|
||||
from ..llm import LLMError
|
||||
from ..services.trainee import MyPersonaStore, analyze_weak_areas
|
||||
from .helpers import ApiError, current_user, require_auth, require_roles
|
||||
from .helpers import (
|
||||
ApiError,
|
||||
current_user,
|
||||
internal_error,
|
||||
is_valid_tenant_id,
|
||||
request_json_object,
|
||||
require_auth,
|
||||
require_roles,
|
||||
)
|
||||
|
||||
me_bp = Blueprint("me", __name__)
|
||||
|
||||
@@ -21,26 +31,70 @@ def _stores():
|
||||
}
|
||||
|
||||
|
||||
def _authorized_finished_trainee_sessions(s, actor) -> list[dict]:
|
||||
"""Return only current, actor-authorized sessions usable as evidence."""
|
||||
org_id = actor.get("org_id")
|
||||
user_id = actor.get("id")
|
||||
if not is_valid_tenant_id(org_id) or not isinstance(user_id, str) or not user_id:
|
||||
raise ApiError("permission denied", 403)
|
||||
sessions = s["sessions"].list_for_user(user_id, org_id=org_id)
|
||||
from .chat_routes import _authorize_session_context
|
||||
|
||||
authorized = []
|
||||
for session in sessions:
|
||||
if (
|
||||
not isinstance(session, dict)
|
||||
or (session.get("mode") or "trainee") != "trainee"
|
||||
or session.get("status") != "finished"
|
||||
or session.get("outcome") not in ("won", "lost")
|
||||
):
|
||||
continue
|
||||
try:
|
||||
_authorize_session_context(s, session, required_status="finished")
|
||||
except ApiError:
|
||||
continue
|
||||
authorized.append(session)
|
||||
return authorized
|
||||
|
||||
|
||||
@me_bp.get("/board")
|
||||
@require_auth
|
||||
def win_lose_board():
|
||||
"""Per-persona won/lost/not-tried across all groups the user sees (any authenticated user)."""
|
||||
s = _stores()
|
||||
uid = current_user()["id"]
|
||||
my_sessions = s["sessions"].list_for_user(uid)
|
||||
outcome_by = {(x.get("group_id"), x.get("persona_id")): x.get("outcome") for x in my_sessions}
|
||||
actor = current_user()
|
||||
uid = actor["id"]
|
||||
org_id = actor.get("org_id")
|
||||
if not is_valid_tenant_id(org_id):
|
||||
raise ApiError("permission denied", 403)
|
||||
my_sessions = s["sessions"].list_for_user(uid, org_id=org_id)
|
||||
outcome_by = {
|
||||
(x.get("group_id"), x.get("persona_id")): (
|
||||
"active" if x.get("status") == "active" else x.get("outcome")
|
||||
)
|
||||
for x in my_sessions
|
||||
if isinstance(x, dict)
|
||||
}
|
||||
|
||||
groups = s["groups"].list_visible_to(role="user", org_id=current_user().get("org_id"))
|
||||
# Only the owner sees their personal groups (IDOR defense).
|
||||
groups = [g for g in groups if not g.get("owner_user_id") or g.get("owner_user_id") == uid]
|
||||
groups = s["groups"].list_visible_to(
|
||||
role="user", org_id=org_id, user_id=uid
|
||||
)
|
||||
items = []
|
||||
for g in groups:
|
||||
for p in g.get("personas", []):
|
||||
key = (g["id"], p["id"])
|
||||
raw_personas = g.get("personas")
|
||||
personas = raw_personas if isinstance(raw_personas, list) else []
|
||||
for p in personas:
|
||||
if not isinstance(p, dict):
|
||||
continue
|
||||
group_id = g.get("id")
|
||||
persona_id = p.get("id")
|
||||
if not isinstance(group_id, str) or not group_id or not isinstance(persona_id, str) or not persona_id:
|
||||
continue
|
||||
key = (group_id, persona_id)
|
||||
items.append({
|
||||
"group_id": g["id"],
|
||||
"group_id": group_id,
|
||||
"group_title": g.get("title"),
|
||||
"persona_id": p["id"],
|
||||
"persona_id": persona_id,
|
||||
"persona_name": p.get("name"),
|
||||
"tier": p.get("tier"),
|
||||
"my_outcome": outcome_by.get(key, "not_tried"),
|
||||
@@ -50,73 +104,88 @@ def win_lose_board():
|
||||
|
||||
@me_bp.get("/weak-areas")
|
||||
@require_auth
|
||||
@require_roles("user")
|
||||
def weak_areas():
|
||||
s = _stores()
|
||||
uid = current_user()["id"]
|
||||
sessions = s["sessions"].list_for_user(uid)
|
||||
insight = analyze_weak_areas(sessions)
|
||||
actor = current_user()
|
||||
uid = actor["id"]
|
||||
org_id = actor.get("org_id")
|
||||
if not is_valid_tenant_id(org_id):
|
||||
raise ApiError("permission denied", 403)
|
||||
sessions = _authorized_finished_trainee_sessions(s, actor)
|
||||
insight = analyze_weak_areas(sessions, user_id=uid, org_id=org_id)
|
||||
return jsonify({"insight": insight})
|
||||
|
||||
|
||||
def _personal_group(s, actor) -> dict:
|
||||
"""Return (or create) the user's private group holding their own personas."""
|
||||
groups = s["groups"].list_for_org(org_id=actor.get("org_id"))
|
||||
for g in groups:
|
||||
if g.get("owner_user_id") == actor["id"]:
|
||||
return g
|
||||
g = s["groups"].create(
|
||||
org_id=actor.get("org_id") or "org-default",
|
||||
creator_id=actor["id"],
|
||||
title=f"{actor.get('name','User')}'s private personas",
|
||||
)
|
||||
s["groups"].update(
|
||||
g["id"],
|
||||
status="ready",
|
||||
owner_user_id=actor["id"],
|
||||
input={"channel": "social", "language": "th"},
|
||||
sales_kit={"productName": "personal practice", "valueProps": [], "features": []},
|
||||
)
|
||||
return s["groups"].get(g["id"])
|
||||
org_id = actor.get("org_id")
|
||||
if not is_valid_tenant_id(org_id):
|
||||
raise ApiError("permission denied", 403)
|
||||
try:
|
||||
return s["groups"].get_or_create_private_group(
|
||||
org_id=org_id,
|
||||
owner_user_id=actor["id"],
|
||||
owner_name=actor.get("name", "User"),
|
||||
)
|
||||
except ValueError as exc:
|
||||
raise internal_error("private group unavailable", exc, 409)
|
||||
|
||||
|
||||
@me_bp.get("/personas")
|
||||
@require_auth
|
||||
@require_roles("user")
|
||||
def my_personas():
|
||||
s = _stores()
|
||||
uid = current_user()["id"]
|
||||
group = _personal_group(s, current_user())
|
||||
return jsonify({"group": group, "personas": group.get("personas", [])})
|
||||
actor = current_user()
|
||||
group = _personal_group(s, actor)
|
||||
from .group_routes import serialize_group, serialize_persona
|
||||
|
||||
raw_personas = group.get("personas")
|
||||
personas = raw_personas if isinstance(raw_personas, list) else []
|
||||
return jsonify({
|
||||
"group": serialize_group(group, actor),
|
||||
"personas": [serialize_persona(p, actor) for p in personas if isinstance(p, dict)],
|
||||
})
|
||||
|
||||
|
||||
@me_bp.post("/personas/generate")
|
||||
@require_auth
|
||||
@require_roles("user")
|
||||
def generate_persona():
|
||||
s = _stores()
|
||||
actor = current_user()
|
||||
data = request.get_json(silent=True) or {}
|
||||
data = request_json_object()
|
||||
mode = data.get("mode", "manual") # "weak-area" | "manual"
|
||||
spec = data.get("spec") or {}
|
||||
if not isinstance(mode, str) or not isinstance(spec, dict):
|
||||
raise ApiError("invalid request", 400)
|
||||
llm = s["llm"]
|
||||
if not llm:
|
||||
raise ApiError("LLM not configured", 500)
|
||||
if mode == "weak-area" and not spec:
|
||||
# auto-detect weak areas from this user's losses if no spec given
|
||||
sessions = s["sessions"].list_for_user(actor["id"])
|
||||
spec = analyze_weak_areas(sessions)
|
||||
org_id = actor.get("org_id")
|
||||
if not is_valid_tenant_id(org_id):
|
||||
raise ApiError("permission denied", 403)
|
||||
sessions = _authorized_finished_trainee_sessions(s, actor)
|
||||
spec = analyze_weak_areas(sessions, user_id=actor["id"], org_id=org_id)
|
||||
from ..services.own_persona import generate_own_persona
|
||||
|
||||
try:
|
||||
persona = generate_own_persona(llm, mode=mode, spec=spec)
|
||||
except (LLMError, ValueError) as exc:
|
||||
raise ApiError(f"generation failed: {exc}", 500)
|
||||
raise internal_error("persona generation failed", exc)
|
||||
|
||||
group = _personal_group(s, actor)
|
||||
group = s["groups"].get(group["id"])
|
||||
existing = group.get("personas", [])
|
||||
persona["id"] = f"myp-{len(existing)+1:02d}"
|
||||
existing.append(persona)
|
||||
s["groups"].set_personas(group["id"], existing)
|
||||
return jsonify({"persona": persona, "group": s["groups"].get(group["id"])}), 201
|
||||
with s["groups"].record_lock(group["id"]):
|
||||
group = s["groups"].get(group["id"])
|
||||
raw_personas = group.get("personas")
|
||||
existing = [p for p in raw_personas if isinstance(p, dict)] if isinstance(raw_personas, list) else []
|
||||
persona["id"] = f"myp-{uuid.uuid4().hex[:10]}"
|
||||
existing.append(persona)
|
||||
s["groups"].set_personas(group["id"], existing)
|
||||
from .group_routes import serialize_group, serialize_persona
|
||||
|
||||
fresh_group = s["groups"].get(group["id"])
|
||||
return jsonify({
|
||||
"persona": serialize_persona(persona, actor),
|
||||
"group": serialize_group(fresh_group, actor),
|
||||
}), 201
|
||||
+457
-91
@@ -7,23 +7,95 @@ separate field that admins/users can set; the default admin must set an email
|
||||
from __future__ import annotations
|
||||
|
||||
import datetime
|
||||
import math
|
||||
import re
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
from typing import Any, TypeGuard
|
||||
|
||||
import jwt
|
||||
from werkzeug.security import check_password_hash, generate_password_hash
|
||||
|
||||
from ..config import Config
|
||||
from ..storage.store import JsonStore, StoreError, new_id
|
||||
from ..storage.store import (
|
||||
JsonStore,
|
||||
StoreError,
|
||||
contains_control_characters,
|
||||
new_id,
|
||||
)
|
||||
|
||||
EMAIL_RE = re.compile(r"^[^@\s]+@[^@\s]+\.[^@\s]+$")
|
||||
USERNAME_RE = re.compile(r"^[a-zA-Z0-9_.-]{2,64}$")
|
||||
MAX_LOGIN_IDENTIFIER_LENGTH = 254
|
||||
_UNSET = object()
|
||||
PUBLIC_USER_FIELDS = (
|
||||
"id",
|
||||
"username",
|
||||
"email",
|
||||
"org_id",
|
||||
"org_name",
|
||||
"name",
|
||||
"role",
|
||||
"must_setup",
|
||||
"created_at",
|
||||
"active",
|
||||
"accepted_terms",
|
||||
"accepted_terms_at",
|
||||
)
|
||||
|
||||
|
||||
class AuthError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class SetupAlreadyCompletedError(AuthError):
|
||||
"""Raised when setup is attempted after the one-time transition."""
|
||||
pass
|
||||
|
||||
|
||||
def is_valid_tenant_id(value: object) -> TypeGuard[str]:
|
||||
"""Accept only canonical, nonblank tenant identifiers."""
|
||||
if (
|
||||
not isinstance(value, str)
|
||||
or not value
|
||||
or len(value) > 200
|
||||
or value != value.strip()
|
||||
or "/" in value
|
||||
or "\\" in value
|
||||
or value == "."
|
||||
or ".." in value
|
||||
or contains_control_characters(value)
|
||||
):
|
||||
return False
|
||||
try:
|
||||
if len(value.encode("utf-8")) > 200:
|
||||
return False
|
||||
except UnicodeError:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def normalize_identifier(value: object) -> str:
|
||||
"""Normalize a login/storage identifier without allowing unsafe path input."""
|
||||
if not isinstance(value, str):
|
||||
raise AuthError("invalid identifier")
|
||||
normalized = value.strip().lower()
|
||||
if (
|
||||
not normalized
|
||||
or len(normalized) > MAX_LOGIN_IDENTIFIER_LENGTH
|
||||
or "/" in normalized
|
||||
or "\\" in normalized
|
||||
or ".." in normalized
|
||||
or contains_control_characters(normalized)
|
||||
):
|
||||
raise AuthError("invalid identifier")
|
||||
try:
|
||||
if len(normalized.encode("utf-8")) > 200:
|
||||
raise AuthError("invalid identifier")
|
||||
except UnicodeError as exc:
|
||||
raise AuthError("invalid identifier") from exc
|
||||
return normalized
|
||||
|
||||
|
||||
class UserStore:
|
||||
def __init__(self, data_dir: Path) -> None:
|
||||
self.users = JsonStore(data_dir / "users")
|
||||
@@ -31,7 +103,9 @@ class UserStore:
|
||||
|
||||
# ── org ────────────────────────────────────────────────────────────
|
||||
def create_org(self, name: str, *, org_id: str | None = None) -> dict[str, Any]:
|
||||
oid = org_id or new_id("org")
|
||||
oid = new_id("org") if org_id is None else org_id
|
||||
if not is_valid_tenant_id(oid):
|
||||
raise AuthError("invalid organization id")
|
||||
return self.orgs.create(
|
||||
{
|
||||
"name": name,
|
||||
@@ -47,10 +121,112 @@ class UserStore:
|
||||
def get_org(self, org_id: str) -> dict[str, Any]:
|
||||
return self.orgs.get(org_id)
|
||||
|
||||
def get_org_or_none(self, org_id: str | None) -> dict[str, Any] | None:
|
||||
"""Return an organization without turning a missing tenant into a default."""
|
||||
if not is_valid_tenant_id(org_id):
|
||||
return None
|
||||
return self.orgs.get_or_none(org_id)
|
||||
|
||||
# ── users ──────────────────────────────────────────────────────────
|
||||
@staticmethod
|
||||
def _norm(username: str) -> str:
|
||||
return username.strip().lower()
|
||||
try:
|
||||
return normalize_identifier(username)
|
||||
except AuthError as exc:
|
||||
raise AuthError("invalid username") from exc
|
||||
|
||||
@staticmethod
|
||||
def validate_password(password: object) -> str:
|
||||
"""Validate credentials before any caller performs a mutation."""
|
||||
if not isinstance(password, str) or not password:
|
||||
raise AuthError("password is required")
|
||||
if len(password) < Config.MIN_PASSWORD_LENGTH:
|
||||
raise AuthError(
|
||||
f"password must be at least {Config.MIN_PASSWORD_LENGTH} characters"
|
||||
)
|
||||
return password
|
||||
|
||||
@staticmethod
|
||||
def _next_auth_version(user: dict[str, Any]) -> int:
|
||||
"""Return the next session version for password-bearing mutations."""
|
||||
return UserStore._auth_version(user) + 1
|
||||
|
||||
@staticmethod
|
||||
def _auth_version(user: dict[str, Any]) -> int:
|
||||
if "auth_version" not in user:
|
||||
return 0 # Legacy records without the field use the initial version.
|
||||
current = user.get("auth_version")
|
||||
if isinstance(current, bool) or not isinstance(current, int) or current < 0:
|
||||
raise AuthError("invalid account state")
|
||||
return current
|
||||
|
||||
@staticmethod
|
||||
def _password_matches(password_hash: object, password: str) -> bool:
|
||||
"""Treat malformed persisted hashes as invalid credentials, never 500."""
|
||||
if not isinstance(password_hash, str) or not password_hash:
|
||||
return False
|
||||
try:
|
||||
return check_password_hash(password_hash, password)
|
||||
except (TypeError, ValueError):
|
||||
return False
|
||||
|
||||
@staticmethod
|
||||
def _token_identity(user: dict[str, Any]) -> tuple[str, str, str]:
|
||||
username = user.get("username")
|
||||
user_id = user.get("id")
|
||||
org_id = user.get("org_id")
|
||||
role = user.get("role")
|
||||
try:
|
||||
normalized_username = normalize_identifier(username)
|
||||
normalized_user_id = normalize_identifier(user_id)
|
||||
except AuthError as exc:
|
||||
raise AuthError("invalid account state") from exc
|
||||
if (
|
||||
not isinstance(username, str)
|
||||
or not username.strip()
|
||||
or USERNAME_RE.fullmatch(username) is None
|
||||
or normalized_username != username
|
||||
or not isinstance(user_id, str)
|
||||
or not user_id.strip()
|
||||
or normalized_user_id != user_id
|
||||
or user_id != username
|
||||
or not is_valid_tenant_id(org_id)
|
||||
or not isinstance(role, str)
|
||||
or role not in Config.ROLES
|
||||
):
|
||||
raise AuthError("invalid account state")
|
||||
persisted_email = user.get("email")
|
||||
if persisted_email is not None:
|
||||
try:
|
||||
canonical_email = UserStore.validate_email(
|
||||
persisted_email, allow_empty=False
|
||||
)
|
||||
except AuthError as exc:
|
||||
raise AuthError("invalid account state") from exc
|
||||
if canonical_email != persisted_email:
|
||||
raise AuthError("invalid account state")
|
||||
return username, org_id, role
|
||||
|
||||
def validate_persisted_identity(self, user: dict[str, Any]) -> None:
|
||||
"""Fail closed when an existing account record loses canonical identity."""
|
||||
self._token_identity(user)
|
||||
|
||||
@staticmethod
|
||||
def validate_email(email: object, *, allow_empty: bool = True) -> str | None:
|
||||
"""Normalize an email and reject malformed/non-string values safely."""
|
||||
if email is None or (isinstance(email, str) and not email.strip()):
|
||||
if allow_empty:
|
||||
return None
|
||||
raise AuthError("invalid email")
|
||||
if not isinstance(email, str):
|
||||
raise AuthError("invalid email")
|
||||
try:
|
||||
normalized = normalize_identifier(email)
|
||||
except AuthError as exc:
|
||||
raise AuthError("invalid email") from exc
|
||||
if not EMAIL_RE.fullmatch(normalized):
|
||||
raise AuthError("invalid email")
|
||||
return normalized
|
||||
|
||||
def create_user(
|
||||
self,
|
||||
@@ -63,45 +239,67 @@ class UserStore:
|
||||
email: str | None = None,
|
||||
must_setup: bool = False,
|
||||
) -> dict[str, Any]:
|
||||
if role not in Config.ROLES:
|
||||
if not is_valid_tenant_id(org_id):
|
||||
raise AuthError("invalid organization id")
|
||||
if not isinstance(role, str) or role not in Config.ROLES:
|
||||
raise AuthError(f"invalid role: {role}")
|
||||
org = self.orgs.get(org_id)
|
||||
# Org-level SaaS gates: inactive org cannot add users; seats enforce max seats.
|
||||
if not org.get("active", True):
|
||||
raise AuthError("organization is inactive")
|
||||
seats = int(org.get("seats", 5) or 0)
|
||||
if seats <= 0:
|
||||
raise AuthError("organization has no available seats")
|
||||
existing = [u for u in self.users.all() if u.get("org_id") == org_id]
|
||||
if seats is not None and len(existing) >= seats:
|
||||
raise AuthError("organization seat limit reached")
|
||||
if not isinstance(must_setup, bool):
|
||||
raise AuthError("must_setup must be boolean")
|
||||
username = self._norm(username)
|
||||
if not username or not password:
|
||||
raise AuthError("username and password are required")
|
||||
if not re.fullmatch(r"[a-zA-Z0-9_.-]{2,64}", username):
|
||||
password = self.validate_password(password)
|
||||
if USERNAME_RE.fullmatch(username) is None:
|
||||
raise AuthError("invalid username (letters/numbers/._- only, 2-64 chars)")
|
||||
if self.users.get_or_none(username) is not None:
|
||||
raise AuthError("a user with this username already exists")
|
||||
email = (email or "").strip().lower() or None
|
||||
if email:
|
||||
if not EMAIL_RE.fullmatch(email):
|
||||
raise AuthError("invalid email")
|
||||
if self.email_exists(email):
|
||||
raise AuthError("a user with this email already exists")
|
||||
user = {
|
||||
"id": username,
|
||||
"username": username,
|
||||
"email": email,
|
||||
"org_id": org_id,
|
||||
"org_name": self.orgs.get(org_id).get("name", ""),
|
||||
"name": name.strip() or username,
|
||||
"password_hash": generate_password_hash(password),
|
||||
"role": role,
|
||||
"must_setup": must_setup,
|
||||
"created_at": datetime.datetime.now(datetime.timezone.utc).isoformat(),
|
||||
"active": True,
|
||||
}
|
||||
return self.users.create(user, key=username)
|
||||
if not isinstance(name, str):
|
||||
raise AuthError("name must be a string")
|
||||
email = self.validate_email(email)
|
||||
# Username/email uniqueness and seat capacity are collection-wide
|
||||
# invariants. Keep their checks with the create so setup/profile writes
|
||||
# cannot reserve the same email concurrently in another worker.
|
||||
# Seat capacity and org activation must share a lock with org updates;
|
||||
# otherwise an admin can deactivate/shrink a tenant between this check
|
||||
# and the user write.
|
||||
try:
|
||||
with self.orgs.record_lock(org_id):
|
||||
org = self.orgs.get_or_none(org_id)
|
||||
if org is None:
|
||||
raise AuthError("organization not found")
|
||||
if org.get("active") is not True:
|
||||
raise AuthError("organization is inactive")
|
||||
raw_seats = org.get("seats", 5)
|
||||
if type(raw_seats) is not int or raw_seats < 0:
|
||||
raise AuthError("invalid organization state")
|
||||
seats = raw_seats
|
||||
if seats <= 0:
|
||||
raise AuthError("organization has no available seats")
|
||||
with self.users.collection_lock():
|
||||
existing = [u for u in self.users.all() if u.get("org_id") == org_id]
|
||||
if len(existing) >= seats:
|
||||
raise AuthError("organization seat limit reached")
|
||||
if self.users.get_or_none(username) is not None:
|
||||
raise AuthError("a user with this username already exists")
|
||||
if email and self.email_exists(email):
|
||||
raise AuthError("a user with this email already exists")
|
||||
user = {
|
||||
"id": username,
|
||||
"username": username,
|
||||
"email": email,
|
||||
"org_id": org_id,
|
||||
"org_name": org.get("name", ""),
|
||||
"name": name.strip() or username,
|
||||
"password_hash": generate_password_hash(password),
|
||||
"role": role,
|
||||
"must_setup": must_setup,
|
||||
"auth_version": 0,
|
||||
"created_at": datetime.datetime.now(datetime.timezone.utc).isoformat(),
|
||||
"active": True,
|
||||
}
|
||||
return self.users.create(user, key=username)
|
||||
except AuthError:
|
||||
raise
|
||||
except (OSError, StoreError, TypeError, UnicodeError, ValueError, OverflowError) as exc:
|
||||
raise AuthError("account storage unavailable") from exc
|
||||
|
||||
def get_user(self, username: str) -> dict[str, Any]:
|
||||
return self.users.get(self._norm(username))
|
||||
@@ -109,12 +307,21 @@ class UserStore:
|
||||
def get_user_or_none(self, username: str) -> dict[str, Any] | None:
|
||||
return self.users.get_or_none(self._norm(username))
|
||||
|
||||
def by_email(self, email: str) -> dict[str, Any] | None:
|
||||
email = (email or "").strip().lower()
|
||||
if not email:
|
||||
def by_email(self, email: object) -> dict[str, Any] | None:
|
||||
email = self.validate_email(email)
|
||||
if email is None:
|
||||
return None
|
||||
for u in self.users.all():
|
||||
if u.get("email") and u["email"] == email:
|
||||
stored_email = u.get("email")
|
||||
if stored_email is None:
|
||||
continue
|
||||
try:
|
||||
canonical_email = self.validate_email(stored_email, allow_empty=False)
|
||||
except AuthError as exc:
|
||||
raise AuthError("invalid account state") from exc
|
||||
if canonical_email != stored_email:
|
||||
raise AuthError("invalid account state")
|
||||
if canonical_email == email:
|
||||
return u
|
||||
return None
|
||||
|
||||
@@ -122,75 +329,198 @@ class UserStore:
|
||||
return self.by_email(email) is not None
|
||||
|
||||
def list_users(self, *, org_id: str | None = None) -> list[dict[str, Any]]:
|
||||
if org_id is not None and not is_valid_tenant_id(org_id):
|
||||
return []
|
||||
users = self.users.all()
|
||||
if org_id:
|
||||
users = [u for u in users if u.get("org_id") == org_id]
|
||||
for u in users:
|
||||
u.pop("password_hash", None)
|
||||
return users
|
||||
if org_id is None:
|
||||
return [self.public_user(user) for user in users]
|
||||
users = [u for u in users if u.get("org_id") == org_id]
|
||||
return [self.public_user(user) for user in users]
|
||||
|
||||
def set_active(self, username: str, active: bool) -> dict[str, Any]:
|
||||
return self.users.update(self._norm(username), active=active)
|
||||
return self.update_user_fields(username, active=active)
|
||||
|
||||
def set_role(self, username: str, role: str) -> dict[str, Any]:
|
||||
if role not in Config.ROLES:
|
||||
raise AuthError(f"invalid role: {role}")
|
||||
return self.users.update(self._norm(username), role=role)
|
||||
return self.update_user_fields(username, role=role)
|
||||
|
||||
def set_password(self, username: str, new_password: str) -> dict[str, Any]:
|
||||
if not new_password:
|
||||
raise AuthError("password is required")
|
||||
return self.users.update(
|
||||
self._norm(username),
|
||||
password_hash=generate_password_hash(new_password),
|
||||
)
|
||||
return self.update_user_fields(username, password=new_password)
|
||||
|
||||
def change_password(
|
||||
self,
|
||||
username: str,
|
||||
current_password: object,
|
||||
new_password: object,
|
||||
) -> dict[str, Any]:
|
||||
"""Change one user's password after verifying the current password."""
|
||||
username = self._norm(username)
|
||||
if not isinstance(current_password, str) or not current_password:
|
||||
raise AuthError("current password is invalid")
|
||||
new_password = self.validate_password(new_password)
|
||||
with self.users.record_lock(username):
|
||||
current = self.users.get_or_none(username)
|
||||
if current is None or current.get("active") is not True:
|
||||
raise AuthError("current password is invalid")
|
||||
if not self._password_matches(current.get("password_hash"), current_password):
|
||||
raise AuthError("current password is invalid")
|
||||
return self.users.update(
|
||||
username,
|
||||
password_hash=generate_password_hash(new_password),
|
||||
auth_version=self._next_auth_version(current),
|
||||
)
|
||||
|
||||
def set_email(self, username: str, email: str) -> dict[str, Any]:
|
||||
email = (email or "").strip().lower()
|
||||
if not EMAIL_RE.fullmatch(email):
|
||||
raise AuthError("invalid email")
|
||||
existing = self.by_email(email)
|
||||
if existing and existing["id"] != self._norm(username):
|
||||
raise AuthError("a user with this email already exists")
|
||||
return self.users.update(self._norm(username), email=email)
|
||||
return self.update_user_fields(username, email=email)
|
||||
|
||||
def set_name(self, username: str, name: str) -> dict[str, Any]:
|
||||
name = (name or "").strip()
|
||||
if not name:
|
||||
raise AuthError("name is required")
|
||||
return self.users.update(self._norm(username), name=name)
|
||||
return self.update_user_fields(username, name=name)
|
||||
|
||||
def complete_setup(self, username: str, email: str, new_password: str) -> dict[str, Any]:
|
||||
"""First-time admin setup: set email + password, clear must_setup."""
|
||||
if not new_password or len(new_password) < 4:
|
||||
raise AuthError("password must be at least 4 characters")
|
||||
self.set_email(username, email)
|
||||
self.set_password(username, new_password)
|
||||
return self.users.update(self._norm(username), must_setup=False)
|
||||
def update_user_fields(
|
||||
self,
|
||||
username: str,
|
||||
*,
|
||||
name: object = _UNSET,
|
||||
email: object = _UNSET,
|
||||
password: object = _UNSET,
|
||||
role: object = _UNSET,
|
||||
active: object = _UNSET,
|
||||
) -> dict[str, Any]:
|
||||
"""Validate and apply related user fields as one locked mutation."""
|
||||
username = self._norm(username)
|
||||
fields: dict[str, Any] = {}
|
||||
if name is not _UNSET:
|
||||
if not isinstance(name, str) or not name.strip():
|
||||
raise AuthError("name is required")
|
||||
fields["name"] = name.strip()
|
||||
normalized_email: str | None = None
|
||||
if email is not _UNSET:
|
||||
normalized_email = self.validate_email(email, allow_empty=False)
|
||||
if normalized_email is None: # defensive: required email validation above
|
||||
raise AuthError("invalid email")
|
||||
fields["email"] = normalized_email
|
||||
if password is not _UNSET:
|
||||
password = self.validate_password(password)
|
||||
fields["password_hash"] = generate_password_hash(password)
|
||||
if role is not _UNSET:
|
||||
if not isinstance(role, str) or role not in Config.ROLES:
|
||||
raise AuthError("invalid role")
|
||||
fields["role"] = role
|
||||
if active is not _UNSET:
|
||||
if not isinstance(active, bool):
|
||||
raise AuthError("active must be a boolean")
|
||||
fields["active"] = active
|
||||
|
||||
with self.users.collection_lock():
|
||||
with self.users.record_lock(username):
|
||||
current = self.users.get_or_none(username)
|
||||
if current is None:
|
||||
raise AuthError("user not found")
|
||||
if normalized_email is not None:
|
||||
existing = self.by_email(normalized_email)
|
||||
if existing and existing["id"] != username:
|
||||
raise AuthError("a user with this email already exists")
|
||||
if password is not _UNSET or role is not _UNSET or active is not _UNSET:
|
||||
fields["auth_version"] = self._next_auth_version(current)
|
||||
if not fields:
|
||||
return current
|
||||
return self.users.update(username, **fields)
|
||||
|
||||
def complete_setup(
|
||||
self,
|
||||
username: str,
|
||||
email: str,
|
||||
new_password: str,
|
||||
*,
|
||||
accepted_terms: object,
|
||||
accepted_terms_at: str,
|
||||
) -> dict[str, Any]:
|
||||
"""Complete setup for a user that is still marked ``must_setup``.
|
||||
|
||||
Keep the setup fields in one store update so a caller cannot leave an
|
||||
account half-configured after changing only its email or password.
|
||||
"""
|
||||
username = self._norm(username)
|
||||
if accepted_terms is not True:
|
||||
raise AuthError("terms acceptance is required")
|
||||
new_password = self.validate_password(new_password)
|
||||
email = self.validate_email(email, allow_empty=False)
|
||||
if email is None: # defensive: required email validation above
|
||||
raise AuthError("invalid email")
|
||||
password_hash = generate_password_hash(new_password)
|
||||
with self.users.collection_lock():
|
||||
with self.users.record_lock(username):
|
||||
current = self.users.get_or_none(username)
|
||||
if current is None:
|
||||
raise AuthError("user not found")
|
||||
if current.get("must_setup") is not True:
|
||||
raise SetupAlreadyCompletedError("setup already completed")
|
||||
existing = self.by_email(email)
|
||||
if existing and existing["id"] != username:
|
||||
raise AuthError("a user with this email already exists")
|
||||
updated = self.users.update_if(
|
||||
username,
|
||||
lambda latest: latest.get("must_setup") is True,
|
||||
email=email,
|
||||
password_hash=password_hash,
|
||||
must_setup=False,
|
||||
accepted_terms=True,
|
||||
accepted_terms_at=accepted_terms_at,
|
||||
auth_version=self._next_auth_version(current),
|
||||
)
|
||||
if updated is None:
|
||||
raise SetupAlreadyCompletedError("setup already completed")
|
||||
return updated
|
||||
|
||||
# ── auth ───────────────────────────────────────────────────────────
|
||||
def verify(self, ident: str, password: str) -> dict[str, Any]:
|
||||
# Resolve by EITHER username OR email (the login form doesn't distinguish,
|
||||
# and users naturally type their email after setup). Fall back to username.
|
||||
user = self.get_user_or_none(ident) or self.by_email(ident)
|
||||
if not user or not user.get("active", True):
|
||||
if not isinstance(ident, str) or not isinstance(password, str):
|
||||
raise AuthError("invalid credentials")
|
||||
try:
|
||||
normalized_ident = normalize_identifier(ident)
|
||||
except AuthError as exc:
|
||||
raise AuthError("invalid credentials") from exc
|
||||
try:
|
||||
user = self.get_user_or_none(normalized_ident) or self.by_email(normalized_ident)
|
||||
except (AuthError, OSError, StoreError, TypeError, UnicodeError, ValueError) as exc:
|
||||
raise AuthError("invalid credentials") from exc
|
||||
if not user or user.get("active") is not True:
|
||||
raise AuthError("invalid credentials")
|
||||
if not isinstance(user.get("must_setup"), bool):
|
||||
raise AuthError("invalid credentials")
|
||||
# SaaS gate: orphaned or inactive organizations cannot sign in.
|
||||
if not isinstance(user.get("role"), str) or user.get("role") not in Config.ROLES:
|
||||
raise AuthError("invalid credentials")
|
||||
# SaaS gate: inactive organization cannot sign in (platform can disable a tenant).
|
||||
oid = user.get("org_id")
|
||||
if oid:
|
||||
org = self.orgs.get_or_none(oid)
|
||||
if org is not None and not org.get("active", True):
|
||||
raise AuthError("organization is inactive")
|
||||
if not check_password_hash(user["password_hash"], password):
|
||||
try:
|
||||
org = self.get_org_or_none(oid)
|
||||
except (OSError, StoreError, TypeError, UnicodeError, ValueError) as exc:
|
||||
raise AuthError("invalid credentials") from exc
|
||||
if not org or org.get("active") is not True:
|
||||
raise AuthError("invalid credentials")
|
||||
try:
|
||||
self._auth_version(user)
|
||||
except AuthError as exc:
|
||||
raise AuthError("invalid credentials") from exc
|
||||
try:
|
||||
self._token_identity(user)
|
||||
except AuthError as exc:
|
||||
raise AuthError("invalid credentials") from exc
|
||||
if not self._password_matches(user.get("password_hash"), password):
|
||||
raise AuthError("invalid credentials")
|
||||
return user
|
||||
|
||||
def issue_token(self, user: dict[str, Any]) -> str:
|
||||
now = datetime.datetime.now(datetime.timezone.utc)
|
||||
subject, org_id, role = self._token_identity(user)
|
||||
if user.get("active") is not True:
|
||||
raise AuthError("invalid account state")
|
||||
payload = {
|
||||
"sub": user.get("username") or user.get("id"),
|
||||
"org_id": user["org_id"],
|
||||
"role": user["role"],
|
||||
"sub": subject,
|
||||
"org_id": org_id,
|
||||
"role": role,
|
||||
"auth_version": self._auth_version(user),
|
||||
"iat": now,
|
||||
"exp": now + datetime.timedelta(hours=Config.JWT_EXPIRES_HOURS),
|
||||
}
|
||||
@@ -198,11 +528,47 @@ class UserStore:
|
||||
|
||||
def decode_token(self, token: str) -> dict[str, Any]:
|
||||
try:
|
||||
return jwt.decode(token, Config.SECRET_KEY, algorithms=[Config.JWT_ALGO])
|
||||
except jwt.PyJWTError as exc:
|
||||
payload = jwt.decode(
|
||||
token,
|
||||
Config.SECRET_KEY,
|
||||
algorithms=[Config.JWT_ALGO],
|
||||
options={"require": ["sub", "org_id", "role", "auth_version", "iat", "exp"]},
|
||||
)
|
||||
if not isinstance(payload, dict):
|
||||
raise AuthError("invalid or expired token")
|
||||
subject = payload.get("sub")
|
||||
try:
|
||||
normalized_subject = normalize_identifier(subject)
|
||||
except AuthError as exc:
|
||||
raise AuthError("invalid or expired token") from exc
|
||||
if (
|
||||
not isinstance(subject, str)
|
||||
or normalized_subject != subject
|
||||
or USERNAME_RE.fullmatch(subject) is None
|
||||
or not is_valid_tenant_id(payload.get("org_id"))
|
||||
or not isinstance(payload.get("role"), str)
|
||||
or payload["role"] not in Config.ROLES
|
||||
or isinstance(payload.get("auth_version"), bool)
|
||||
or not isinstance(payload.get("auth_version"), int)
|
||||
or payload["auth_version"] < 0
|
||||
or isinstance(payload.get("iat"), bool)
|
||||
or not isinstance(payload.get("iat"), (int, float))
|
||||
or not math.isfinite(payload["iat"])
|
||||
or isinstance(payload.get("exp"), bool)
|
||||
or not isinstance(payload.get("exp"), (int, float))
|
||||
or not math.isfinite(payload["exp"])
|
||||
or payload["iat"] >= payload["exp"]
|
||||
):
|
||||
raise AuthError("invalid or expired token")
|
||||
return payload
|
||||
except AuthError:
|
||||
raise
|
||||
except (jwt.PyJWTError, OverflowError, TypeError, ValueError) as exc:
|
||||
raise AuthError("invalid or expired token") from exc
|
||||
|
||||
def public_user(self, user: dict[str, Any]) -> dict[str, Any]:
|
||||
u = dict(user)
|
||||
u.pop("password_hash", None)
|
||||
return u
|
||||
return {
|
||||
field: user[field]
|
||||
for field in PUBLIC_USER_FIELDS
|
||||
if field in user
|
||||
}
|
||||
+55
-3
@@ -7,7 +7,7 @@ from dotenv import load_dotenv
|
||||
|
||||
# Load .env from backend/ (project root for this app)
|
||||
_BACKEND_DIR = Path(__file__).resolve().parent.parent
|
||||
load_dotenv(_BACKEND_DIR / ".env", override=True)
|
||||
load_dotenv(_BACKEND_DIR / ".env", override=False)
|
||||
|
||||
|
||||
def _get_bool(name: str, default: bool = False) -> bool:
|
||||
@@ -47,26 +47,78 @@ def resolve_llm() -> tuple[str, str, str, str | None]:
|
||||
|
||||
class Config:
|
||||
APP_NAME = "Sales Trainer"
|
||||
SECRET_KEY = os.environ.get("JWT_SECRET", "dev-secret-change-me")
|
||||
# Secure-by-default: deployments must opt into development/test explicitly.
|
||||
APP_ENV = os.environ.get("APP_ENV", "production").strip().lower()
|
||||
BOOTSTRAP_ADMIN_PASSWORD = os.environ.get("BOOTSTRAP_ADMIN_PASSWORD", "").strip()
|
||||
SECRET_KEY = os.environ.get("JWT_SECRET", "").strip()
|
||||
JWT_ALGO = "HS256"
|
||||
JWT_EXPIRES_HOURS = int(os.environ.get("JWT_EXPIRES_HOURS", "24"))
|
||||
MIN_PASSWORD_LENGTH = 12
|
||||
|
||||
DATA_DIR = Path(
|
||||
os.environ.get("DATA_DIR", str(_BACKEND_DIR / "data"))
|
||||
).resolve()
|
||||
DATABASE_URL = os.environ.get("DATABASE_URL", "").strip()
|
||||
|
||||
FLASK_HOST = os.environ.get("FLASK_HOST", "0.0.0.0")
|
||||
FLASK_PORT = int(os.environ.get("FLASK_PORT", "5001"))
|
||||
FLASK_DEBUG = _get_bool("FLASK_DEBUG", True)
|
||||
FLASK_DEBUG = _get_bool("FLASK_DEBUG", False)
|
||||
CORS_ORIGINS = tuple(
|
||||
origin.strip()
|
||||
for origin in os.environ.get("CORS_ORIGINS", "").split(",")
|
||||
if origin.strip()
|
||||
)
|
||||
|
||||
UPLOAD_MAX_MB = int(os.environ.get("UPLOAD_MAX_MB", "15"))
|
||||
ALLOWED_UPLOAD_EXTS = {"pdf", "md", "txt"}
|
||||
UPLOAD_TEXT_MAX_KB = int(os.environ.get("UPLOAD_TEXT_MAX_KB", "256"))
|
||||
UPLOAD_TEXT_MAX_BYTES = UPLOAD_TEXT_MAX_KB * 1024
|
||||
UPLOAD_MAX_PDF_PAGES = int(os.environ.get("UPLOAD_MAX_PDF_PAGES", "100"))
|
||||
UPLOAD_MAX_EXTRACTED_CHARS = int(os.environ.get("UPLOAD_MAX_EXTRACTED_CHARS", "60000"))
|
||||
UPLOAD_MAX_FILES = int(os.environ.get("UPLOAD_MAX_FILES", "10"))
|
||||
UPLOAD_MAX_PDF_CHUNK_CHARS = int(os.environ.get("UPLOAD_MAX_PDF_CHUNK_CHARS", "8192"))
|
||||
|
||||
ANALYTICS_EXPORT_MAX_ROWS = int(os.environ.get("ANALYTICS_EXPORT_MAX_ROWS", "10000"))
|
||||
ANALYTICS_EXPORT_MAX_BYTES = int(
|
||||
os.environ.get("ANALYTICS_EXPORT_MAX_BYTES", str(4 * 1024 * 1024))
|
||||
)
|
||||
ANALYTICS_EXPORT_MAX_CELL_CHARS = int(
|
||||
os.environ.get("ANALYTICS_EXPORT_MAX_CELL_CHARS", "10000")
|
||||
)
|
||||
ANALYTICS_EXPORT_MAX_SCAN_RECORDS = int(
|
||||
os.environ.get("ANALYTICS_EXPORT_MAX_SCAN_RECORDS", "100000")
|
||||
)
|
||||
|
||||
# LLM
|
||||
LLM_BASE_URL, LLM_MODEL, LLM_API_KEY, LLM_PROVIDER = resolve_llm()
|
||||
|
||||
ROLES = ("super_admin", "admin", "user")
|
||||
|
||||
@staticmethod
|
||||
def _is_placeholder(value: str) -> bool:
|
||||
normalized = (value or "").strip().lower()
|
||||
return (
|
||||
not normalized
|
||||
or normalized in {"replace_me", "changeme", "change_me", "default", "password"}
|
||||
or normalized.startswith("replace_with_")
|
||||
or normalized.startswith("your_")
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def validate_runtime_security(cls, *, require_bootstrap: bool = False) -> None:
|
||||
"""Fail closed for production secrets and first-run initialization."""
|
||||
secure_runtime = cls.APP_ENV not in {"development", "test"} or not cls.FLASK_DEBUG
|
||||
if secure_runtime and (cls._is_placeholder(cls.SECRET_KEY) or len(cls.SECRET_KEY) < 32):
|
||||
raise RuntimeError("JWT_SECRET must be configured with at least 32 characters")
|
||||
if require_bootstrap:
|
||||
if cls._is_placeholder(cls.BOOTSTRAP_ADMIN_PASSWORD):
|
||||
raise RuntimeError("BOOTSTRAP_ADMIN_PASSWORD is required to initialize the first admin")
|
||||
if len(cls.BOOTSTRAP_ADMIN_PASSWORD) < cls.MIN_PASSWORD_LENGTH:
|
||||
raise RuntimeError(
|
||||
"BOOTSTRAP_ADMIN_PASSWORD must be at least "
|
||||
f"{cls.MIN_PASSWORD_LENGTH} characters"
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def ensure_dirs(cls) -> None:
|
||||
for name in ("users", "orgs", "groups", "sessions"):
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
"""SQLAlchemy engine and declarative metadata foundation for S4 persistence."""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import create_engine, event
|
||||
from sqlalchemy.engine import Engine, make_url
|
||||
from sqlalchemy.orm import DeclarativeBase, sessionmaker
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
"""Base class shared by all relational models."""
|
||||
|
||||
|
||||
SUPPORTED_DATABASE_DIALECTS = frozenset({"sqlite", "postgresql"})
|
||||
|
||||
|
||||
def database_url(configured: str | None = None) -> str:
|
||||
"""Resolve an explicit URL, then environment, then a local SQLite fallback.
|
||||
|
||||
The fallback keeps local schema and migration tests self-contained. Production
|
||||
deployment should provide ``DATABASE_URL`` pointing at PostgreSQL before the
|
||||
repository adapters are enabled.
|
||||
"""
|
||||
explicit = (configured or "").strip()
|
||||
if explicit:
|
||||
return explicit
|
||||
|
||||
from .config import Config
|
||||
|
||||
env_url = os.environ.get("DATABASE_URL", "").strip() or Config.DATABASE_URL
|
||||
if env_url:
|
||||
return env_url
|
||||
|
||||
data_dir = Path(os.environ.get("DATA_DIR", str(Config.DATA_DIR))).resolve()
|
||||
return f"sqlite:///{data_dir / 'sales_trainer.db'}"
|
||||
|
||||
|
||||
def validate_database_url(url: str | None = None) -> str:
|
||||
"""Resolve a database URL and reject dialects that weaken schema invariants."""
|
||||
resolved_url = database_url(url)
|
||||
dialect_name = make_url(resolved_url).get_backend_name()
|
||||
if dialect_name not in SUPPORTED_DATABASE_DIALECTS:
|
||||
raise ValueError("Unsupported database dialect; use sqlite or postgresql")
|
||||
return resolved_url
|
||||
|
||||
|
||||
def create_db_engine(url: str | None = None, **kwargs: Any) -> Engine:
|
||||
"""Create a SQLite/PostgreSQL engine with SQLite foreign keys enabled."""
|
||||
resolved_url = validate_database_url(url)
|
||||
|
||||
options: dict[str, Any] = {"pool_pre_ping": True, **kwargs}
|
||||
if resolved_url.startswith("sqlite"):
|
||||
connect_args = dict(options.pop("connect_args", {}))
|
||||
connect_args.setdefault("check_same_thread", False)
|
||||
options["connect_args"] = connect_args
|
||||
|
||||
engine = create_engine(resolved_url, **options)
|
||||
if resolved_url.startswith("sqlite"):
|
||||
|
||||
@event.listens_for(engine, "connect")
|
||||
def _enable_sqlite_foreign_keys(dbapi_connection: Any, _connection_record: Any) -> None:
|
||||
cursor = dbapi_connection.cursor()
|
||||
cursor.execute("PRAGMA foreign_keys=ON")
|
||||
cursor.close()
|
||||
|
||||
return engine
|
||||
|
||||
|
||||
def create_session_factory(engine: Engine):
|
||||
"""Return a bounded session factory for a supplied engine."""
|
||||
return sessionmaker(bind=engine, autoflush=False, expire_on_commit=False)
|
||||
|
||||
|
||||
__all__ = [
|
||||
"Base",
|
||||
"create_db_engine",
|
||||
"create_session_factory",
|
||||
"database_url",
|
||||
"validate_database_url",
|
||||
]
|
||||
+36
-20
@@ -11,31 +11,35 @@ from .config import Config
|
||||
|
||||
|
||||
def bootstrap_admin(users: UserStore) -> None:
|
||||
"""Ensure a default org + super-admin exists on first run (no self-registration).
|
||||
|
||||
Default admin logs in with username `admin` / `1234`, then MUST set an email
|
||||
and change the password on first login (`must_setup=True`).
|
||||
"""
|
||||
org = users.orgs.get_or_none("org-default")
|
||||
if org is None:
|
||||
org = users.create_org("Default Organization", org_id="org-default")
|
||||
if users.get_user_or_none("admin") is None:
|
||||
users.create_user(
|
||||
org_id=org["id"],
|
||||
username="admin",
|
||||
password="1234",
|
||||
name="Super Admin",
|
||||
role="super_admin",
|
||||
must_setup=True,
|
||||
)
|
||||
print("[bootstrap] created default super-admin: admin / 1234 (must set email + password)")
|
||||
"""Ensure a default org + super-admin exists on first run (no self-registration)."""
|
||||
# Use a stable org record lock even before the record exists. Every worker
|
||||
# therefore rechecks the store after the first worker finishes initialization.
|
||||
with users.orgs.record_lock("org-default"):
|
||||
Config.validate_runtime_security(require_bootstrap=not bool(users.users.all()))
|
||||
if users.users.all():
|
||||
return
|
||||
org = users.orgs.get_or_none("org-default")
|
||||
if org is None:
|
||||
org = users.create_org("Default Organization", org_id="org-default")
|
||||
if users.get_user_or_none("admin") is None:
|
||||
users.create_user(
|
||||
org_id=org["id"],
|
||||
username="admin",
|
||||
password=Config.BOOTSTRAP_ADMIN_PASSWORD,
|
||||
name="Super Admin",
|
||||
role="super_admin",
|
||||
must_setup=True,
|
||||
)
|
||||
print("[bootstrap] created initial super-admin; first-time setup is required")
|
||||
|
||||
|
||||
def create_app() -> Flask:
|
||||
Config.ensure_dirs()
|
||||
app = Flask(__name__)
|
||||
app.config["SECRET_KEY"] = Config.SECRET_KEY
|
||||
CORS(app, resources={r"/api/*": {"origins": "*"}})
|
||||
app.config["MAX_CONTENT_LENGTH"] = Config.UPLOAD_MAX_MB * 1024 * 1024
|
||||
if Config.CORS_ORIGINS:
|
||||
CORS(app, resources={r"/api/*": {"origins": list(Config.CORS_ORIGINS)}})
|
||||
|
||||
from .api.auth_routes import auth_bp
|
||||
from .api.admin_routes import admin_bp
|
||||
@@ -68,7 +72,7 @@ def create_app() -> Flask:
|
||||
try:
|
||||
app.extensions["llm"] = LLMClient()
|
||||
except LLMError as exc:
|
||||
print(f"[warn] LLM not configured yet: {exc}")
|
||||
print(f"[warn] LLM not configured yet (error_type={type(exc).__name__})")
|
||||
app.extensions["llm"] = None
|
||||
|
||||
bootstrap_admin(app.extensions["user_store"])
|
||||
@@ -77,6 +81,18 @@ def create_app() -> Flask:
|
||||
def health():
|
||||
return {"status": "ok", "service": Config.APP_NAME}
|
||||
|
||||
@app.get("/ready")
|
||||
def ready():
|
||||
"""Bounded readiness signal for load balancers; never expose config values."""
|
||||
stores_ok = all(
|
||||
app.extensions.get(name) is not None
|
||||
for name in ("user_store", "group_store", "session_store")
|
||||
)
|
||||
config_ok = bool(Config.DATA_DIR and Config.DATA_DIR.exists())
|
||||
checks = {"stores": "ok" if stores_ok else "failed", "config": "ok" if config_ok else "failed"}
|
||||
status = "ready" if stores_ok and config_ok else "not_ready"
|
||||
return {"status": status, "checks": checks}, (200 if status == "ready" else 503)
|
||||
|
||||
# Serve built Vue frontend if present (production single-app mode).
|
||||
_register_frontend(app)
|
||||
return app
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
"""Public model registry used by the application and Alembic."""
|
||||
|
||||
from .entities import AuditEvent, Group, Message, Organization, Persona, TrainingSession, User
|
||||
|
||||
__all__ = [
|
||||
"AuditEvent",
|
||||
"Group",
|
||||
"Message",
|
||||
"Organization",
|
||||
"Persona",
|
||||
"TrainingSession",
|
||||
"User",
|
||||
]
|
||||
@@ -0,0 +1,249 @@
|
||||
"""Relational entities for the S4.2 persistence foundation.
|
||||
|
||||
The JSON stores remain the active runtime stores until the repository migration
|
||||
packets are completed. These models define the tenant-safe relational contract
|
||||
without wiring a cutover implicitly.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import (
|
||||
JSON,
|
||||
BigInteger,
|
||||
Boolean,
|
||||
CheckConstraint,
|
||||
DateTime,
|
||||
ForeignKey,
|
||||
ForeignKeyConstraint,
|
||||
Index,
|
||||
Integer,
|
||||
String,
|
||||
Text,
|
||||
UniqueConstraint,
|
||||
false,
|
||||
func,
|
||||
true,
|
||||
text,
|
||||
)
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from ..db import Base
|
||||
|
||||
|
||||
class Organization(Base):
|
||||
__tablename__ = "organizations"
|
||||
|
||||
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
name: Mapped[str] = mapped_column(String(200), nullable=False)
|
||||
plan: Mapped[str] = mapped_column(
|
||||
String(64), nullable=False, default="trial", server_default=text("'trial'")
|
||||
)
|
||||
seats: Mapped[int] = mapped_column(Integer, nullable=False, default=5, server_default=text("5"))
|
||||
active: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True, server_default=true())
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), nullable=False, server_default=func.now()
|
||||
)
|
||||
|
||||
|
||||
class User(Base):
|
||||
__tablename__ = "users"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("username", name="uq_users_username"),
|
||||
UniqueConstraint("email", name="uq_users_email"),
|
||||
UniqueConstraint("org_id", "id", name="uq_users_org_id_id"),
|
||||
)
|
||||
|
||||
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
org_id: Mapped[str] = mapped_column(
|
||||
String(64), ForeignKey("organizations.id", ondelete="CASCADE"), nullable=False
|
||||
)
|
||||
username: Mapped[str] = mapped_column(String(160), nullable=False)
|
||||
password_hash: Mapped[str] = mapped_column(String(512), nullable=False)
|
||||
email: Mapped[str | None] = mapped_column(String(320))
|
||||
name: Mapped[str] = mapped_column(String(200), nullable=False)
|
||||
role: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
active: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True, server_default=true())
|
||||
must_setup: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, server_default=false())
|
||||
accepted_terms: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, server_default=false())
|
||||
accepted_terms_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
|
||||
auth_version: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default=text("0"))
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), nullable=False, server_default=func.now()
|
||||
)
|
||||
|
||||
|
||||
class Group(Base):
|
||||
__tablename__ = "groups"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("org_id", "id", name="uq_groups_org_id_id"),
|
||||
ForeignKeyConstraint(
|
||||
["org_id", "owner_user_id"],
|
||||
["users.org_id", "users.id"],
|
||||
name="fk_groups_owner_same_org",
|
||||
ondelete="RESTRICT",
|
||||
),
|
||||
ForeignKeyConstraint(
|
||||
["org_id", "creator_user_id"],
|
||||
["users.org_id", "users.id"],
|
||||
name="fk_groups_creator_same_org",
|
||||
ondelete="RESTRICT",
|
||||
),
|
||||
)
|
||||
|
||||
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
org_id: Mapped[str] = mapped_column(
|
||||
String(64), ForeignKey("organizations.id", ondelete="CASCADE"), nullable=False
|
||||
)
|
||||
owner_user_id: Mapped[str | None] = mapped_column(String(64))
|
||||
creator_user_id: Mapped[str | None] = mapped_column(String(64))
|
||||
name: Mapped[str] = mapped_column(String(200), nullable=False)
|
||||
status: Mapped[str] = mapped_column(
|
||||
String(32), nullable=False, default="draft", server_default=text("'draft'")
|
||||
)
|
||||
input_json: Mapped[dict[str, Any] | None] = mapped_column(JSON)
|
||||
sales_kit_json: Mapped[dict[str, Any] | None] = mapped_column(JSON)
|
||||
report_text: Mapped[str | None] = mapped_column(Text)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), nullable=False, server_default=func.now()
|
||||
)
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), nullable=False, server_default=func.now()
|
||||
)
|
||||
|
||||
|
||||
class Persona(Base):
|
||||
__tablename__ = "personas"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("group_id", "id", name="uq_personas_group_id_id"),
|
||||
)
|
||||
|
||||
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
group_id: Mapped[str] = mapped_column(
|
||||
String(64), ForeignKey("groups.id", ondelete="CASCADE"), nullable=False
|
||||
)
|
||||
source_persona_id: Mapped[str | None] = mapped_column(String(64))
|
||||
tier: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
public_json: Mapped[dict[str, Any]] = mapped_column(JSON, nullable=False, default=dict)
|
||||
latent_json: Mapped[dict[str, Any] | None] = mapped_column(JSON)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), nullable=False, server_default=func.now()
|
||||
)
|
||||
|
||||
|
||||
class TrainingSession(Base):
|
||||
__tablename__ = "sessions"
|
||||
__table_args__ = (
|
||||
ForeignKeyConstraint(
|
||||
["org_id", "user_id"],
|
||||
["users.org_id", "users.id"],
|
||||
name="fk_sessions_user_same_org",
|
||||
ondelete="RESTRICT",
|
||||
),
|
||||
ForeignKeyConstraint(
|
||||
["org_id", "group_id"],
|
||||
["groups.org_id", "groups.id"],
|
||||
name="fk_sessions_group_same_org",
|
||||
ondelete="RESTRICT",
|
||||
),
|
||||
ForeignKeyConstraint(
|
||||
["group_id", "persona_id"],
|
||||
["personas.group_id", "personas.id"],
|
||||
name="fk_sessions_persona_in_group",
|
||||
ondelete="RESTRICT",
|
||||
),
|
||||
Index(
|
||||
"uq_sessions_non_preview_attempt",
|
||||
"org_id",
|
||||
"user_id",
|
||||
"group_id",
|
||||
"persona_id",
|
||||
unique=True,
|
||||
sqlite_where=text("mode <> 'preview'"),
|
||||
postgresql_where=text("mode <> 'preview'"),
|
||||
),
|
||||
)
|
||||
|
||||
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
org_id: Mapped[str] = mapped_column(
|
||||
String(64), ForeignKey("organizations.id", ondelete="CASCADE"), nullable=False
|
||||
)
|
||||
user_id: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
group_id: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
persona_id: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
mode: Mapped[str] = mapped_column(
|
||||
String(32), nullable=False, default="trainee", server_default=text("'trainee'")
|
||||
)
|
||||
status: Mapped[str] = mapped_column(
|
||||
String(32), nullable=False, default="active", server_default=text("'active'")
|
||||
)
|
||||
outcome: Mapped[str | None] = mapped_column(String(32))
|
||||
scenario_json: Mapped[dict[str, Any] | None] = mapped_column(JSON)
|
||||
internal_json: Mapped[dict[str, Any] | None] = mapped_column(JSON)
|
||||
debrief_json: Mapped[dict[str, Any] | None] = mapped_column(JSON)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), nullable=False, server_default=func.now()
|
||||
)
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), nullable=False, server_default=func.now()
|
||||
)
|
||||
|
||||
|
||||
class Message(Base):
|
||||
__tablename__ = "messages"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("session_id", "sequence", name="uq_messages_session_sequence"),
|
||||
)
|
||||
|
||||
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
session_id: Mapped[str] = mapped_column(
|
||||
String(64), ForeignKey("sessions.id", ondelete="CASCADE"), nullable=False
|
||||
)
|
||||
sequence: Mapped[int] = mapped_column(Integer, nullable=False)
|
||||
role: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
text: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), nullable=False, server_default=func.now()
|
||||
)
|
||||
|
||||
|
||||
class AuditEvent(Base):
|
||||
__tablename__ = "audit_events"
|
||||
__table_args__ = (
|
||||
CheckConstraint(
|
||||
"actor_user_id IS NULL OR org_id IS NOT NULL",
|
||||
name="ck_audit_actor_requires_org",
|
||||
),
|
||||
ForeignKeyConstraint(
|
||||
["org_id", "actor_user_id"],
|
||||
["users.org_id", "users.id"],
|
||||
name="fk_audit_actor_same_org",
|
||||
ondelete="SET NULL",
|
||||
),
|
||||
)
|
||||
|
||||
id: Mapped[int] = mapped_column(
|
||||
BigInteger().with_variant(Integer, "sqlite"), primary_key=True, autoincrement=True
|
||||
)
|
||||
org_id: Mapped[str | None] = mapped_column(
|
||||
String(64), ForeignKey("organizations.id", ondelete="SET NULL")
|
||||
)
|
||||
actor_user_id: Mapped[str | None] = mapped_column(String(64))
|
||||
action: Mapped[str] = mapped_column(String(160), nullable=False)
|
||||
subject: Mapped[str | None] = mapped_column(String(200))
|
||||
detail_json: Mapped[dict[str, Any] | None] = mapped_column(JSON)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), nullable=False, server_default=func.now()
|
||||
)
|
||||
|
||||
|
||||
__all__ = [
|
||||
"AuditEvent",
|
||||
"Group",
|
||||
"Message",
|
||||
"Organization",
|
||||
"Persona",
|
||||
"TrainingSession",
|
||||
"User",
|
||||
]
|
||||
@@ -0,0 +1,21 @@
|
||||
"""Repository interfaces and current SQLAlchemy adapters."""
|
||||
|
||||
from .contracts import GroupRepository, OrganizationRepository, SessionRepository, UserRepository
|
||||
from .errors import EntityNotFoundError, InvalidRepositoryField, RepositoryError
|
||||
from .groups import SqlAlchemyGroupRepository
|
||||
from .sqlalchemy import SqlAlchemyOrganizationRepository, SqlAlchemyUserRepository
|
||||
from .sessions import SqlAlchemySessionRepository
|
||||
|
||||
__all__ = [
|
||||
"EntityNotFoundError",
|
||||
"GroupRepository",
|
||||
"InvalidRepositoryField",
|
||||
"OrganizationRepository",
|
||||
"RepositoryError",
|
||||
"SqlAlchemyOrganizationRepository",
|
||||
"SqlAlchemyGroupRepository",
|
||||
"SqlAlchemySessionRepository",
|
||||
"SqlAlchemyUserRepository",
|
||||
"SessionRepository",
|
||||
"UserRepository",
|
||||
]
|
||||
@@ -0,0 +1,124 @@
|
||||
"""Persistence contracts used by services during the S4 repository migration."""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
from typing import Protocol
|
||||
|
||||
from ..models import Group, Message, Organization, Persona, TrainingSession, User
|
||||
|
||||
|
||||
class OrganizationRepository(Protocol):
|
||||
"""Tenant organization persistence contract."""
|
||||
|
||||
def get(self, organization_id: str) -> Organization | None: ...
|
||||
|
||||
def list(self, *, active_only: bool = False) -> list[Organization]: ...
|
||||
|
||||
def create(
|
||||
self,
|
||||
*,
|
||||
organization_id: str,
|
||||
name: str,
|
||||
plan: str = "trial",
|
||||
seats: int = 5,
|
||||
active: bool = True,
|
||||
) -> Organization: ...
|
||||
|
||||
def update(self, organization_id: str, **fields: object) -> Organization: ...
|
||||
|
||||
|
||||
class UserRepository(Protocol):
|
||||
"""Tenant-scoped user persistence contract."""
|
||||
|
||||
def get(self, user_id: str, *, org_id: str) -> User | None: ...
|
||||
|
||||
def get_by_username(self, username: str, *, org_id: str) -> User | None: ...
|
||||
|
||||
def get_by_email(self, email: str | None, *, org_id: str) -> User | None: ...
|
||||
|
||||
def list_for_org(self, org_id: str, *, include_inactive: bool = True) -> list[User]: ...
|
||||
|
||||
def create(
|
||||
self,
|
||||
*,
|
||||
user_id: str,
|
||||
org_id: str,
|
||||
username: str,
|
||||
password_hash: str,
|
||||
name: str,
|
||||
role: str,
|
||||
email: str | None = None,
|
||||
active: bool = True,
|
||||
must_setup: bool = False,
|
||||
accepted_terms: bool = False,
|
||||
accepted_terms_at: datetime | None = None,
|
||||
auth_version: int = 0,
|
||||
) -> User: ...
|
||||
|
||||
def update(self, user_id: str, *, org_id: str, **fields: object) -> User: ...
|
||||
|
||||
|
||||
class GroupRepository(Protocol):
|
||||
"""Tenant-scoped group/persona aggregate contract."""
|
||||
|
||||
def get(self, group_id: str, *, org_id: str | None = None) -> Group | None: ...
|
||||
|
||||
def list_for_org(self, org_id: str) -> list[Group]: ...
|
||||
|
||||
def create(self, *, group_id: str, org_id: str, name: str, **fields: object) -> Group: ...
|
||||
|
||||
def update(self, group_id: str, *, org_id: str, **fields: object) -> Group: ...
|
||||
|
||||
def get_persona(
|
||||
self,
|
||||
group_id: str,
|
||||
persona_id: str,
|
||||
*,
|
||||
org_id: str | None = None,
|
||||
) -> Persona | None: ...
|
||||
|
||||
def list_personas(self, group_id: str, *, org_id: str | None = None) -> list[Persona]: ...
|
||||
|
||||
def create_persona(
|
||||
self,
|
||||
*,
|
||||
persona_id: str,
|
||||
group_id: str,
|
||||
tier: str,
|
||||
public_json: dict[str, object],
|
||||
**fields: object,
|
||||
) -> Persona: ...
|
||||
|
||||
|
||||
class SessionRepository(Protocol):
|
||||
"""Tenant-scoped training-session and message aggregate contract."""
|
||||
|
||||
def get(self, session_id: str, *, org_id: str) -> TrainingSession | None: ...
|
||||
|
||||
def list_for_user(self, org_id: str, user_id: str) -> list[TrainingSession]: ...
|
||||
|
||||
def create(
|
||||
self,
|
||||
*,
|
||||
session_id: str,
|
||||
org_id: str,
|
||||
user_id: str,
|
||||
group_id: str,
|
||||
persona_id: str,
|
||||
**fields: object,
|
||||
) -> TrainingSession: ...
|
||||
|
||||
def update(self, session_id: str, *, org_id: str, **fields: object) -> TrainingSession: ...
|
||||
|
||||
def list_messages(self, session_id: str, *, org_id: str) -> list[Message]: ...
|
||||
|
||||
def append_message(
|
||||
self,
|
||||
*,
|
||||
session_id: str,
|
||||
org_id: str,
|
||||
message_id: str,
|
||||
sequence: int,
|
||||
role: str,
|
||||
text: str,
|
||||
) -> Message: ...
|
||||
@@ -0,0 +1,13 @@
|
||||
"""Errors raised by repository adapters."""
|
||||
|
||||
|
||||
class RepositoryError(Exception):
|
||||
"""Base error for persistence boundary failures."""
|
||||
|
||||
|
||||
class EntityNotFoundError(RepositoryError):
|
||||
"""Raised when an update targets a missing entity."""
|
||||
|
||||
|
||||
class InvalidRepositoryField(RepositoryError):
|
||||
"""Raised when a caller attempts to mutate an immutable/unknown field."""
|
||||
@@ -0,0 +1,134 @@
|
||||
"""SQLAlchemy adapter for the group/persona aggregate."""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..models import Group, Persona
|
||||
from .errors import EntityNotFoundError, InvalidRepositoryField
|
||||
|
||||
|
||||
class SqlAlchemyGroupRepository:
|
||||
"""Tenant-scoped group and persona persistence adapter."""
|
||||
|
||||
_MUTABLE_GROUP_FIELDS = frozenset(
|
||||
{
|
||||
"owner_user_id",
|
||||
"name",
|
||||
"status",
|
||||
"input_json",
|
||||
"sales_kit_json",
|
||||
"report_text",
|
||||
}
|
||||
)
|
||||
|
||||
def __init__(self, session: Session):
|
||||
self._session = session
|
||||
|
||||
def get(self, group_id: str, *, org_id: str | None = None) -> Group | None:
|
||||
statement = select(Group).where(Group.id == group_id)
|
||||
if org_id is not None:
|
||||
statement = statement.where(Group.org_id == org_id)
|
||||
return self._session.scalars(statement).first()
|
||||
|
||||
def list_for_org(self, org_id: str) -> list[Group]:
|
||||
statement = select(Group).where(Group.org_id == org_id).order_by(Group.id)
|
||||
return list(self._session.scalars(statement).all())
|
||||
|
||||
def create(
|
||||
self,
|
||||
*,
|
||||
group_id: str,
|
||||
org_id: str,
|
||||
name: str,
|
||||
owner_user_id: str | None = None,
|
||||
creator_user_id: str | None = None,
|
||||
status: str = "draft",
|
||||
input_json: dict[str, Any] | None = None,
|
||||
sales_kit_json: dict[str, Any] | None = None,
|
||||
report_text: str | None = None,
|
||||
) -> Group:
|
||||
group = Group(
|
||||
id=group_id,
|
||||
org_id=org_id,
|
||||
owner_user_id=owner_user_id,
|
||||
creator_user_id=creator_user_id,
|
||||
name=name,
|
||||
status=status,
|
||||
input_json=input_json,
|
||||
sales_kit_json=sales_kit_json,
|
||||
report_text=report_text,
|
||||
)
|
||||
self._session.add(group)
|
||||
self._session.flush()
|
||||
return group
|
||||
|
||||
def update(self, group_id: str, *, org_id: str, **fields: object) -> Group:
|
||||
group = self.get(group_id, org_id=org_id)
|
||||
if group is None:
|
||||
raise EntityNotFoundError(f"group not found: {group_id}")
|
||||
self._validate_fields(fields)
|
||||
for field, value in fields.items():
|
||||
setattr(group, field, value)
|
||||
group.updated_at = datetime.now(timezone.utc)
|
||||
self._session.flush()
|
||||
return group
|
||||
|
||||
def get_persona(
|
||||
self,
|
||||
group_id: str,
|
||||
persona_id: str,
|
||||
*,
|
||||
org_id: str | None = None,
|
||||
) -> Persona | None:
|
||||
if self.get(group_id, org_id=org_id) is None:
|
||||
return None
|
||||
statement = select(Persona).where(
|
||||
Persona.group_id == group_id,
|
||||
Persona.id == persona_id,
|
||||
)
|
||||
return self._session.scalars(statement).first()
|
||||
|
||||
def list_personas(self, group_id: str, *, org_id: str | None = None) -> list[Persona]:
|
||||
self._require_group(group_id, org_id=org_id)
|
||||
statement = select(Persona).where(Persona.group_id == group_id).order_by(Persona.id)
|
||||
return list(self._session.scalars(statement).all())
|
||||
|
||||
def create_persona(
|
||||
self,
|
||||
*,
|
||||
persona_id: str,
|
||||
group_id: str,
|
||||
tier: str,
|
||||
public_json: dict[str, Any],
|
||||
latent_json: dict[str, Any] | None = None,
|
||||
source_persona_id: str | None = None,
|
||||
org_id: str | None = None,
|
||||
) -> Persona:
|
||||
self._require_group(group_id, org_id=org_id)
|
||||
persona = Persona(
|
||||
id=persona_id,
|
||||
group_id=group_id,
|
||||
source_persona_id=source_persona_id,
|
||||
tier=tier,
|
||||
public_json=public_json,
|
||||
latent_json=latent_json,
|
||||
)
|
||||
self._session.add(persona)
|
||||
self._session.flush()
|
||||
return persona
|
||||
|
||||
def _require_group(self, group_id: str, *, org_id: str | None) -> Group:
|
||||
group = self.get(group_id, org_id=org_id)
|
||||
if group is None:
|
||||
raise EntityNotFoundError(f"group not found: {group_id}")
|
||||
return group
|
||||
|
||||
def _validate_fields(self, fields: dict[str, object]) -> None:
|
||||
invalid = set(fields) - self._MUTABLE_GROUP_FIELDS
|
||||
if invalid:
|
||||
names = ", ".join(sorted(invalid))
|
||||
raise InvalidRepositoryField(f"immutable or unknown repository fields: {names}")
|
||||
@@ -0,0 +1,129 @@
|
||||
"""SQLAlchemy adapter for the session/message aggregate."""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..models import Message, TrainingSession
|
||||
from .errors import EntityNotFoundError, InvalidRepositoryField
|
||||
|
||||
|
||||
class SqlAlchemySessionRepository:
|
||||
"""Tenant-scoped training-session and message persistence adapter."""
|
||||
|
||||
_MUTABLE_SESSION_FIELDS = frozenset(
|
||||
{
|
||||
"status",
|
||||
"outcome",
|
||||
"scenario_json",
|
||||
"internal_json",
|
||||
"debrief_json",
|
||||
}
|
||||
)
|
||||
|
||||
def __init__(self, session: Session):
|
||||
self._session = session
|
||||
|
||||
def get(self, session_id: str, *, org_id: str) -> TrainingSession | None:
|
||||
statement = select(TrainingSession).where(
|
||||
TrainingSession.id == session_id,
|
||||
TrainingSession.org_id == org_id,
|
||||
)
|
||||
return self._session.scalars(statement).first()
|
||||
|
||||
def list_for_user(self, org_id: str, user_id: str) -> list[TrainingSession]:
|
||||
statement = (
|
||||
select(TrainingSession)
|
||||
.where(
|
||||
TrainingSession.org_id == org_id,
|
||||
TrainingSession.user_id == user_id,
|
||||
)
|
||||
.order_by(TrainingSession.id)
|
||||
)
|
||||
return list(self._session.scalars(statement).all())
|
||||
|
||||
def create(
|
||||
self,
|
||||
*,
|
||||
session_id: str,
|
||||
org_id: str,
|
||||
user_id: str,
|
||||
group_id: str,
|
||||
persona_id: str,
|
||||
mode: str = "trainee",
|
||||
status: str = "active",
|
||||
outcome: str | None = None,
|
||||
scenario_json: dict[str, Any] | None = None,
|
||||
internal_json: dict[str, Any] | None = None,
|
||||
debrief_json: dict[str, Any] | None = None,
|
||||
) -> TrainingSession:
|
||||
training_session = TrainingSession(
|
||||
id=session_id,
|
||||
org_id=org_id,
|
||||
user_id=user_id,
|
||||
group_id=group_id,
|
||||
persona_id=persona_id,
|
||||
mode=mode,
|
||||
status=status,
|
||||
outcome=outcome,
|
||||
scenario_json=scenario_json,
|
||||
internal_json=internal_json,
|
||||
debrief_json=debrief_json,
|
||||
)
|
||||
self._session.add(training_session)
|
||||
self._session.flush()
|
||||
return training_session
|
||||
|
||||
def update(self, session_id: str, *, org_id: str, **fields: object) -> TrainingSession:
|
||||
training_session = self.get(session_id, org_id=org_id)
|
||||
if training_session is None:
|
||||
raise EntityNotFoundError(f"session not found: {session_id}")
|
||||
invalid = set(fields) - self._MUTABLE_SESSION_FIELDS
|
||||
if invalid:
|
||||
names = ", ".join(sorted(invalid))
|
||||
raise InvalidRepositoryField(f"immutable or unknown repository fields: {names}")
|
||||
for field, value in fields.items():
|
||||
setattr(training_session, field, value)
|
||||
training_session.updated_at = datetime.now(timezone.utc)
|
||||
self._session.flush()
|
||||
return training_session
|
||||
|
||||
def list_messages(self, session_id: str, *, org_id: str) -> list[Message]:
|
||||
self._require_session(session_id, org_id=org_id)
|
||||
statement = (
|
||||
select(Message)
|
||||
.where(Message.session_id == session_id)
|
||||
.order_by(Message.sequence)
|
||||
)
|
||||
return list(self._session.scalars(statement).all())
|
||||
|
||||
def append_message(
|
||||
self,
|
||||
*,
|
||||
session_id: str,
|
||||
org_id: str,
|
||||
message_id: str,
|
||||
sequence: int,
|
||||
role: str,
|
||||
text: str,
|
||||
) -> Message:
|
||||
self._require_session(session_id, org_id=org_id)
|
||||
message = Message(
|
||||
id=message_id,
|
||||
session_id=session_id,
|
||||
sequence=sequence,
|
||||
role=role,
|
||||
text=text,
|
||||
)
|
||||
self._session.add(message)
|
||||
self._session.flush()
|
||||
return message
|
||||
|
||||
def _require_session(self, session_id: str, *, org_id: str) -> TrainingSession:
|
||||
training_session = self.get(session_id, org_id=org_id)
|
||||
if training_session is None:
|
||||
raise EntityNotFoundError(f"session not found: {session_id}")
|
||||
return training_session
|
||||
@@ -0,0 +1,161 @@
|
||||
"""SQLAlchemy implementations for the first S4 repository packet.
|
||||
|
||||
Repositories flush changes but never commit them. The caller owns the transaction
|
||||
boundary so a future service can atomically persist multiple aggregates.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..models import Organization, User
|
||||
from .errors import EntityNotFoundError, InvalidRepositoryField
|
||||
|
||||
|
||||
class SqlAlchemyOrganizationRepository:
|
||||
"""SQLAlchemy adapter for organization reads and mutations."""
|
||||
|
||||
_MUTABLE_FIELDS = frozenset({"name", "plan", "seats", "active"})
|
||||
|
||||
def __init__(self, session: Session):
|
||||
self._session = session
|
||||
|
||||
def get(self, organization_id: str) -> Organization | None:
|
||||
return self._session.get(Organization, organization_id)
|
||||
|
||||
def list(self, *, active_only: bool = False) -> list[Organization]:
|
||||
statement = select(Organization).order_by(Organization.id)
|
||||
if active_only:
|
||||
statement = statement.where(Organization.active.is_(True))
|
||||
return list(self._session.scalars(statement).all())
|
||||
|
||||
def create(
|
||||
self,
|
||||
*,
|
||||
organization_id: str,
|
||||
name: str,
|
||||
plan: str = "trial",
|
||||
seats: int = 5,
|
||||
active: bool = True,
|
||||
) -> Organization:
|
||||
organization = Organization(
|
||||
id=organization_id,
|
||||
name=name,
|
||||
plan=plan,
|
||||
seats=seats,
|
||||
active=active,
|
||||
)
|
||||
self._session.add(organization)
|
||||
self._session.flush()
|
||||
return organization
|
||||
|
||||
def update(self, organization_id: str, **fields: object) -> Organization:
|
||||
organization = self.get(organization_id)
|
||||
if organization is None:
|
||||
raise EntityNotFoundError(f"organization not found: {organization_id}")
|
||||
self._validate_fields(fields, self._MUTABLE_FIELDS)
|
||||
for field, value in fields.items():
|
||||
setattr(organization, field, value)
|
||||
self._session.flush()
|
||||
return organization
|
||||
|
||||
@staticmethod
|
||||
def _validate_fields(fields: dict[str, object], allowed: frozenset[str]) -> None:
|
||||
invalid = set(fields) - allowed
|
||||
if invalid:
|
||||
names = ", ".join(sorted(invalid))
|
||||
raise InvalidRepositoryField(f"immutable or unknown repository fields: {names}")
|
||||
|
||||
|
||||
class SqlAlchemyUserRepository:
|
||||
"""SQLAlchemy adapter for tenant-scoped user reads and mutations."""
|
||||
|
||||
_MUTABLE_FIELDS = frozenset(
|
||||
{
|
||||
"password_hash",
|
||||
"email",
|
||||
"name",
|
||||
"role",
|
||||
"active",
|
||||
"must_setup",
|
||||
"accepted_terms",
|
||||
"accepted_terms_at",
|
||||
"auth_version",
|
||||
}
|
||||
)
|
||||
|
||||
def __init__(self, session: Session):
|
||||
self._session = session
|
||||
|
||||
def get(self, user_id: str, *, org_id: str) -> User | None:
|
||||
statement = select(User).where(User.id == user_id, User.org_id == org_id)
|
||||
return self._session.scalars(statement).first()
|
||||
|
||||
def get_by_username(self, username: str, *, org_id: str) -> User | None:
|
||||
statement = select(User).where(User.username == username, User.org_id == org_id)
|
||||
return self._session.scalars(statement).first()
|
||||
|
||||
def get_by_email(self, email: str | None, *, org_id: str) -> User | None:
|
||||
if not email:
|
||||
return None
|
||||
statement = select(User).where(User.email == email, User.org_id == org_id)
|
||||
return self._session.scalars(statement).first()
|
||||
|
||||
def list_for_org(self, org_id: str, *, include_inactive: bool = True) -> list[User]:
|
||||
statement = select(User).where(User.org_id == org_id).order_by(User.id)
|
||||
if not include_inactive:
|
||||
statement = statement.where(User.active.is_(True))
|
||||
return list(self._session.scalars(statement).all())
|
||||
|
||||
def create(
|
||||
self,
|
||||
*,
|
||||
user_id: str,
|
||||
org_id: str,
|
||||
username: str,
|
||||
password_hash: str,
|
||||
name: str,
|
||||
role: str,
|
||||
email: str | None = None,
|
||||
active: bool = True,
|
||||
must_setup: bool = False,
|
||||
accepted_terms: bool = False,
|
||||
accepted_terms_at: datetime | None = None,
|
||||
auth_version: int = 0,
|
||||
) -> User:
|
||||
user = User(
|
||||
id=user_id,
|
||||
org_id=org_id,
|
||||
username=username,
|
||||
password_hash=password_hash,
|
||||
email=email,
|
||||
name=name,
|
||||
role=role,
|
||||
active=active,
|
||||
must_setup=must_setup,
|
||||
accepted_terms=accepted_terms,
|
||||
accepted_terms_at=accepted_terms_at,
|
||||
auth_version=auth_version,
|
||||
)
|
||||
self._session.add(user)
|
||||
self._session.flush()
|
||||
return user
|
||||
|
||||
def update(self, user_id: str, *, org_id: str, **fields: object) -> User:
|
||||
user = self.get(user_id, org_id=org_id)
|
||||
if user is None:
|
||||
raise EntityNotFoundError(f"user not found: {user_id}")
|
||||
self._validate_fields(fields)
|
||||
for field, value in fields.items():
|
||||
setattr(user, field, value)
|
||||
self._session.flush()
|
||||
return user
|
||||
|
||||
@classmethod
|
||||
def _validate_fields(cls, fields: dict[str, object]) -> None:
|
||||
invalid = set(fields) - cls._MUTABLE_FIELDS
|
||||
if invalid:
|
||||
names = ", ".join(sorted(invalid))
|
||||
raise InvalidRepositoryField(f"immutable or unknown repository fields: {names}")
|
||||
@@ -1,46 +1,122 @@
|
||||
"""File parsing for uploaded documents (pdf / markdown / txt)."""
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
from pathlib import Path
|
||||
|
||||
from ..config import Config
|
||||
|
||||
|
||||
class ParseError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def parse_pdf(path: Path) -> str:
|
||||
import fitz # PyMuPDF
|
||||
def _iter_bounded_page_text(page, fitz):
|
||||
"""Extract a page in clipped bands so one page cannot materialize whole text."""
|
||||
rect = getattr(page, "rect", None)
|
||||
try:
|
||||
x0 = float(getattr(rect, "x0"))
|
||||
y0 = float(getattr(rect, "y0"))
|
||||
x1 = float(getattr(rect, "x1"))
|
||||
y1 = float(getattr(rect, "y1"))
|
||||
except (AttributeError, TypeError, ValueError, OverflowError) as exc:
|
||||
raise ParseError("PDF page geometry unavailable") from exc
|
||||
height = y1 - y0
|
||||
if (
|
||||
not all(math.isfinite(value) for value in (x0, y0, x1, y1))
|
||||
or x1 <= x0
|
||||
or height <= 0
|
||||
):
|
||||
raise ParseError("PDF page geometry unavailable")
|
||||
band_count = max(1, min(128, int(height / 32) + 1))
|
||||
for index in range(band_count):
|
||||
band_y0 = y0 + height * index / band_count
|
||||
band_y1 = y0 + height * (index + 1) / band_count
|
||||
clip = fitz.Rect(x0, band_y0, x1, band_y1)
|
||||
chunk = page.get_text("text", clip=clip)
|
||||
text = chunk if isinstance(chunk, str) else str(chunk)
|
||||
max_chunk_chars = min(
|
||||
Config.UPLOAD_MAX_EXTRACTED_CHARS,
|
||||
Config.UPLOAD_MAX_PDF_CHUNK_CHARS,
|
||||
)
|
||||
if len(text) > max_chunk_chars:
|
||||
raise ParseError("extracted text too large")
|
||||
yield text
|
||||
|
||||
|
||||
def parse_pdf(path: Path) -> str:
|
||||
try:
|
||||
import fitz # PyMuPDF
|
||||
except ImportError as exc:
|
||||
raise ParseError("PDF parser is unavailable") from exc
|
||||
|
||||
try:
|
||||
too_large = path.exists() and path.stat().st_size > Config.UPLOAD_MAX_MB * 1024 * 1024
|
||||
except OSError as exc:
|
||||
raise ParseError("cannot inspect PDF") from exc
|
||||
if too_large:
|
||||
raise ParseError("PDF file too large")
|
||||
try:
|
||||
doc = fitz.open(path)
|
||||
except Exception as exc:
|
||||
raise ParseError(f"cannot open PDF: {exc}") from exc
|
||||
parts = []
|
||||
for page in doc:
|
||||
parts.append(page.get_text())
|
||||
doc.close()
|
||||
return "\n".join(parts)
|
||||
raise ParseError("cannot open PDF") from exc
|
||||
try:
|
||||
page_count = getattr(doc, "page_count", None)
|
||||
if page_count is None:
|
||||
page_count = len(doc)
|
||||
if int(page_count) > Config.UPLOAD_MAX_PDF_PAGES:
|
||||
raise ParseError("PDF has too many pages")
|
||||
parts = []
|
||||
extracted_chars = 0
|
||||
for page in doc:
|
||||
for chunk in _iter_bounded_page_text(page, fitz):
|
||||
text = chunk if isinstance(chunk, str) else str(chunk)
|
||||
separator_chars = 1 if parts else 0
|
||||
extracted_chars += separator_chars + len(text)
|
||||
if extracted_chars > Config.UPLOAD_MAX_EXTRACTED_CHARS:
|
||||
raise ParseError("extracted text too large")
|
||||
parts.append(text)
|
||||
return "\n".join(parts)
|
||||
except ParseError:
|
||||
raise
|
||||
except Exception as exc:
|
||||
raise ParseError("could not extract PDF text") from exc
|
||||
finally:
|
||||
doc.close()
|
||||
|
||||
|
||||
def parse_text(path: Path) -> str:
|
||||
import chardet
|
||||
|
||||
try:
|
||||
if path.stat().st_size > Config.UPLOAD_TEXT_MAX_BYTES:
|
||||
raise ParseError("text document too large")
|
||||
except FileNotFoundError as exc:
|
||||
raise ParseError("text document is unavailable") from exc
|
||||
raw = path.read_bytes()
|
||||
if len(raw) > Config.UPLOAD_TEXT_MAX_BYTES:
|
||||
raise ParseError("text document too large")
|
||||
# Try utf-8 first, else detect encoding
|
||||
try:
|
||||
return raw.decode("utf-8")
|
||||
except UnicodeDecodeError:
|
||||
pass
|
||||
guess = chardet.detect(raw)
|
||||
enc = guess.get("encoding") or "utf-8"
|
||||
try:
|
||||
from charset_normalizer import from_bytes
|
||||
match = from_bytes(raw).best()
|
||||
enc = getattr(match, "encoding", None) if match is not None else None
|
||||
except ImportError as exc:
|
||||
raise ParseError("text encoding detector is unavailable") from exc
|
||||
if not enc:
|
||||
raise ParseError("could not detect text encoding")
|
||||
try:
|
||||
return raw.decode(enc, errors="replace")
|
||||
except Exception:
|
||||
return raw.decode("utf-8", errors="replace")
|
||||
except (LookupError, UnicodeError) as exc:
|
||||
raise ParseError("could not decode text document") from exc
|
||||
|
||||
|
||||
def parse_document(path: Path) -> str:
|
||||
ext = path.suffix.lower().lstrip(".")
|
||||
if ext == "pdf":
|
||||
return parse_pdf(path)
|
||||
if ext not in {"md", "txt"}:
|
||||
raise ParseError("unsupported document type")
|
||||
return parse_text(path)
|
||||
+157
-29
@@ -11,8 +11,9 @@ import datetime
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from ..auth.users import is_valid_tenant_id
|
||||
from ..storage.store import JsonStore, new_id
|
||||
from .store import ensure_persona_shape
|
||||
from .store import ensure_persona_shape, revealable_view, validate_persona_traits
|
||||
|
||||
DEFAULT_TIERS = ["A", "B", "C"]
|
||||
PERSONAS_PER_TIER = 5
|
||||
@@ -26,22 +27,38 @@ class GroupStore:
|
||||
def __init__(self, data_dir: Path) -> None:
|
||||
self.groups = JsonStore(data_dir / "groups")
|
||||
|
||||
def create(self, *, org_id: str, creator_id: str, title: str) -> dict[str, Any]:
|
||||
def record_lock(self, gid: str):
|
||||
"""Hold a group transaction lock across analysis work."""
|
||||
return self.groups.record_lock(gid)
|
||||
|
||||
def create(
|
||||
self,
|
||||
*,
|
||||
org_id: str,
|
||||
creator_id: str,
|
||||
title: str,
|
||||
status: str = "draft",
|
||||
owner_user_id: str | None = None,
|
||||
input_data: dict[str, Any] | None = None,
|
||||
sales_kit: dict[str, Any] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
gid = new_id("group")
|
||||
group = {
|
||||
"id": gid,
|
||||
"org_id": org_id,
|
||||
"creator_id": creator_id,
|
||||
"title": title or "Untitled group",
|
||||
"status": "draft", # draft -> analyzing -> ready | failed
|
||||
"status": status, # draft -> analyzing -> ready | failed
|
||||
"created_at": _now(),
|
||||
"updated_at": _now(),
|
||||
"input": {}, # form fields
|
||||
"sales_kit": None,
|
||||
"input": dict(input_data) if isinstance(input_data, dict) else {},
|
||||
"sales_kit": sales_kit if isinstance(sales_kit, dict) else None,
|
||||
"personas": [], # full persona dicts
|
||||
"report": None,
|
||||
"error": None,
|
||||
}
|
||||
if owner_user_id is not None:
|
||||
group["owner_user_id"] = owner_user_id
|
||||
return self.groups.create(group, key=gid)
|
||||
|
||||
def get(self, gid: str) -> dict[str, Any]:
|
||||
@@ -55,47 +72,158 @@ class GroupStore:
|
||||
return self.groups.update(gid, **fields)
|
||||
|
||||
def list_for_org(self, org_id: str | None = None) -> list[dict[str, Any]]:
|
||||
if not is_valid_tenant_id(org_id):
|
||||
return []
|
||||
groups = self.groups.all()
|
||||
if org_id:
|
||||
groups = [g for g in groups if g.get("org_id") == org_id]
|
||||
return groups
|
||||
return [g for g in groups if isinstance(g, dict) and g.get("org_id") == org_id]
|
||||
|
||||
def list_visible_to(
|
||||
self, *, role: str, org_id: str | None = None
|
||||
self,
|
||||
*,
|
||||
role: str,
|
||||
org_id: str | None = None,
|
||||
user_id: str | None = None,
|
||||
) -> list[dict[str, Any]]:
|
||||
"""List groups a given role/user can see. Trainees see only ready groups."""
|
||||
groups = self.groups.all()
|
||||
if org_id:
|
||||
groups = [g for g in groups if g.get("org_id") == org_id]
|
||||
"""List groups a role/user can see through a role-safe view.
|
||||
|
||||
Trainee callers receive only ready groups, only their own private groups,
|
||||
and revealable persona fields. Keeping this policy here prevents a new
|
||||
route from accidentally turning a raw group index into an IDOR/data leak.
|
||||
"""
|
||||
if role == "super_admin":
|
||||
groups = [g for g in self.groups.all() if isinstance(g, dict)]
|
||||
return groups if org_id is None else [g for g in groups if g.get("org_id") == org_id]
|
||||
if not is_valid_tenant_id(org_id):
|
||||
return []
|
||||
groups = [g for g in self.groups.all() if isinstance(g, dict)]
|
||||
groups = [g for g in groups if g.get("org_id") == org_id]
|
||||
if role == "user":
|
||||
groups = [g for g in groups if g.get("status") == "ready"]
|
||||
return groups
|
||||
if not isinstance(user_id, str) or not user_id:
|
||||
return []
|
||||
groups = [
|
||||
g
|
||||
for g in groups
|
||||
if "owner_user_id" not in g
|
||||
or (isinstance(g.get("owner_user_id"), str) and g.get("owner_user_id") == user_id)
|
||||
]
|
||||
return [self._user_visible_group(g) for g in groups]
|
||||
if role == "admin":
|
||||
# A tenant admin may inspect shared group summaries, never a trainee's
|
||||
# private group. An explicit owner marker is private even when malformed.
|
||||
return [g for g in groups if "owner_user_id" not in g]
|
||||
return []
|
||||
|
||||
@staticmethod
|
||||
def _user_visible_group(group: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Return the closed group envelope allowed to a trainee."""
|
||||
raw_input = group.get("input")
|
||||
source = raw_input if isinstance(raw_input, dict) else {}
|
||||
raw_personas = group.get("personas")
|
||||
personas = raw_personas if isinstance(raw_personas, list) else []
|
||||
status = group.get("status", "draft")
|
||||
return {
|
||||
"id": group.get("id"),
|
||||
"org_id": group.get("org_id"),
|
||||
"owner_user_id": group.get("owner_user_id"),
|
||||
"title": group.get("title", ""),
|
||||
"status": status,
|
||||
"created_at": group.get("created_at"),
|
||||
"updated_at": group.get("updated_at"),
|
||||
"input": {
|
||||
key: source[key]
|
||||
for key in ("product", "segment", "channel", "language")
|
||||
if key in source
|
||||
},
|
||||
"sales_kit": None,
|
||||
"personas": [
|
||||
revealable_view(ensure_persona_shape(persona))
|
||||
for persona in personas
|
||||
if isinstance(persona, dict)
|
||||
],
|
||||
"report": None,
|
||||
"error": "analysis_failed" if status == "failed" and group.get("error") else None,
|
||||
}
|
||||
|
||||
def get_or_create_private_group(
|
||||
self,
|
||||
*,
|
||||
org_id: str,
|
||||
owner_user_id: str,
|
||||
owner_name: str = "User",
|
||||
input_data: dict[str, Any] | None = None,
|
||||
sales_kit: dict[str, Any] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Return the one private, ready group owned by a trainee.
|
||||
|
||||
The lookup and create are one invariant. Keep both inside the
|
||||
collection lock so concurrent web workers cannot allocate duplicate
|
||||
private groups for the same ``(org_id, owner_user_id)`` pair.
|
||||
"""
|
||||
if not is_valid_tenant_id(org_id):
|
||||
raise ValueError("organization scope is invalid")
|
||||
if not isinstance(owner_user_id, str) or not owner_user_id.strip():
|
||||
raise ValueError("private owner is invalid")
|
||||
with self.groups.collection_lock():
|
||||
for group in self.list_for_org(org_id=org_id):
|
||||
if group.get("owner_user_id") == owner_user_id:
|
||||
if group.get("status") != "ready":
|
||||
raise ValueError("private group is not ready")
|
||||
return group
|
||||
group = self.create(
|
||||
org_id=org_id,
|
||||
creator_id=owner_user_id,
|
||||
title=f"{owner_name or 'User'}'s private personas",
|
||||
status="ready",
|
||||
owner_user_id=owner_user_id,
|
||||
input_data=input_data or {"channel": "social", "language": "th"},
|
||||
sales_kit=sales_kit or {
|
||||
"productName": "personal practice",
|
||||
"valueProps": [],
|
||||
"features": [],
|
||||
},
|
||||
)
|
||||
return group
|
||||
|
||||
# ── personas ────────────────────────────────────────────────────────
|
||||
def set_personas(self, gid: str, personas: list[dict[str, Any]]) -> dict[str, Any]:
|
||||
personas = [ensure_persona_shape(p) for p in personas]
|
||||
return self.groups.update(gid, personas=personas)
|
||||
with self.record_lock(gid):
|
||||
personas = [ensure_persona_shape(p) for p in personas]
|
||||
return self.groups.update(gid, personas=personas)
|
||||
|
||||
def get_persona(self, gid: str, pid: str) -> dict[str, Any] | None:
|
||||
group = self.get(gid)
|
||||
for p in group.get("personas", []):
|
||||
raw_personas = group.get("personas")
|
||||
personas = raw_personas if isinstance(raw_personas, list) else []
|
||||
for p in personas:
|
||||
if not isinstance(p, dict):
|
||||
continue
|
||||
if p.get("id") == pid:
|
||||
return p
|
||||
return None
|
||||
|
||||
def update_persona(self, gid: str, pid: str, patch: dict[str, Any]) -> dict[str, Any]:
|
||||
group = self.get(gid)
|
||||
found = False
|
||||
for i, p in enumerate(group.get("personas", [])):
|
||||
if p.get("id") == pid:
|
||||
merged = {**p, **patch, "id": pid}
|
||||
group["personas"][i] = ensure_persona_shape(merged)
|
||||
found = True
|
||||
break
|
||||
if not found:
|
||||
raise ValueError("persona not found")
|
||||
return self.groups.replace(gid, group)
|
||||
with self.record_lock(gid):
|
||||
group = self.get(gid)
|
||||
found = False
|
||||
raw_personas = group.get("personas")
|
||||
personas = raw_personas if isinstance(raw_personas, list) else []
|
||||
group["personas"] = personas
|
||||
for i, p in enumerate(personas):
|
||||
if not isinstance(p, dict):
|
||||
continue
|
||||
if p.get("id") == pid:
|
||||
merged = {**p, **patch, "id": pid}
|
||||
normalized = ensure_persona_shape(merged)
|
||||
validate_persona_traits(normalized)
|
||||
group["personas"][i] = normalized
|
||||
found = True
|
||||
break
|
||||
if not found:
|
||||
raise ValueError("persona not found")
|
||||
return self.groups.replace(gid, group)
|
||||
|
||||
def delete(self, gid: str) -> None:
|
||||
"""Hard-delete a group (personas/report included)."""
|
||||
self.groups.delete(gid)
|
||||
with self.record_lock(gid):
|
||||
self.groups.delete(gid)
|
||||
@@ -4,6 +4,7 @@ from __future__ import annotations
|
||||
from typing import Any
|
||||
|
||||
from ..llm import LLMClient
|
||||
from .store import validate_persona_traits
|
||||
|
||||
OWN_PERSONA_SYSTEM = """You generate ONE customer persona for a sales-training simulator,
|
||||
PRIVATE to a specific trainee. You produce valid JSON only: {"persona": { ... }}.
|
||||
@@ -35,8 +36,9 @@ def generate_own_persona(llm: LLMClient, *, mode: str, spec: dict[str, Any]) ->
|
||||
if not isinstance(persona, dict):
|
||||
raise ValueError("own-persona generator returned invalid data")
|
||||
persona.setdefault("tier", "B")
|
||||
persona.setdefault("channel", "social")
|
||||
persona.setdefault("channel", "facebook")
|
||||
persona.setdefault("initiation_mode", "customer")
|
||||
persona.setdefault("pains", [])
|
||||
persona.setdefault("negotiation_levers", [])
|
||||
validate_persona_traits(persona)
|
||||
return persona
|
||||
@@ -6,6 +6,7 @@ from typing import Any
|
||||
|
||||
from ..llm import LLMClient
|
||||
from .persona_prompts import PERSONA_SYSTEM
|
||||
from .store import PERSONA_CHANNELS, validate_persona_traits
|
||||
|
||||
TIERS = ["A", "B", "C"]
|
||||
PER_TIER = 5 # 5 per tier = 15 total
|
||||
@@ -21,8 +22,10 @@ class PersonaGenerator:
|
||||
*,
|
||||
sales_kit: dict[str, Any],
|
||||
language: str = "en",
|
||||
channel: str = "social",
|
||||
channel: str = "facebook",
|
||||
) -> list[dict[str, Any]]:
|
||||
if channel not in PERSONA_CHANNELS:
|
||||
raise ValueError("persona channel must be facebook or line")
|
||||
kit_json = json.dumps(sales_kit, ensure_ascii=False)[:12000]
|
||||
lang_name = "Thai" if language == "th" else "English"
|
||||
scenario = (sales_kit.get("scenarioFrame") or "").strip() or "a general product sale"
|
||||
@@ -32,61 +35,76 @@ class PersonaGenerator:
|
||||
f"Sales Kit:\n{kit_json}\n\n"
|
||||
f"Generate exactly 15 personas (5 per tier A/B/C) as JSON."
|
||||
)
|
||||
# Real LLMs sometimes return fewer than 15 (truncation / merge). Retry up to 2 extra
|
||||
# times with a nudge; the body below already ACCEPTS short results (>= 8) instead of
|
||||
# hard-failing, so these retries are just best-effort to reach a fuller set.
|
||||
attempt = 0
|
||||
while True:
|
||||
attempt += 1
|
||||
last_error: ValueError | None = None
|
||||
for attempt in range(1, 4):
|
||||
prompt = user_prompt + (
|
||||
""
|
||||
if attempt == 1
|
||||
else "\n\n(Note: you left some personas out — please output all 15, one JSON object per persona, no extra prose.)"
|
||||
else "\n\n(Note: the previous output failed the strict persona schema. Output exactly 15 valid personas: 5 A, 5 B, 5 C, unique ids, valid Facebook/LINE channel, valid customer/seller initiation, and one tier-C wrong_text persona. No extra prose.)"
|
||||
)
|
||||
result = self.llm.complete_json(
|
||||
PERSONA_SYSTEM, prompt, temperature=0.8, max_tokens=14000
|
||||
)
|
||||
personas = result.get("personas") or []
|
||||
if (isinstance(personas, list) and len(personas) >= TARGET) or attempt >= 3:
|
||||
break
|
||||
if not isinstance(personas, list) or not personas:
|
||||
raise ValueError("persona generator returned no personas")
|
||||
try:
|
||||
payload = result.get("personas") if isinstance(result, dict) else None
|
||||
return self._normalize_personas(payload, channel)
|
||||
except ValueError as exc:
|
||||
last_error = exc
|
||||
raise last_error or ValueError("persona generator returned invalid personas")
|
||||
|
||||
normalized, counts = [], {"A": 0, "B": 0, "C": 0}
|
||||
for idx, p in enumerate(personas, start=1):
|
||||
if len(normalized) >= TARGET:
|
||||
break # already reached 20 total
|
||||
if not isinstance(p, dict):
|
||||
continue
|
||||
def _normalize_personas(self, personas: object, channel: str) -> list[dict[str, Any]]:
|
||||
if not isinstance(personas, list):
|
||||
raise ValueError("persona generator returned invalid persona list")
|
||||
if len(personas) != TARGET:
|
||||
raise ValueError("persona generator must return exactly 15 personas")
|
||||
|
||||
raw_ids: set[str] = set()
|
||||
normalized: list[dict[str, Any]] = []
|
||||
counts = {tier: 0 for tier in TIERS}
|
||||
for raw in personas:
|
||||
if not isinstance(raw, dict):
|
||||
raise ValueError("persona generator returned invalid persona object")
|
||||
source_id = raw.get("id")
|
||||
if source_id is not None:
|
||||
if not isinstance(source_id, str) or not source_id.strip():
|
||||
raise ValueError("persona id is invalid")
|
||||
if source_id in raw_ids:
|
||||
raise ValueError("duplicate persona id")
|
||||
raw_ids.add(source_id)
|
||||
p = dict(raw)
|
||||
if not isinstance(p.get("name"), str) or not p["name"].strip():
|
||||
raise ValueError("persona name is invalid")
|
||||
tier = p.get("tier", p.get("intent_tier"))
|
||||
if tier not in TIERS:
|
||||
tier = "B"
|
||||
if counts[tier] >= PER_TIER:
|
||||
continue # skip overflow per tier
|
||||
counts[tier] += 1
|
||||
p["id"] = f"persona-{idx:02d}"
|
||||
p["tier"] = tier
|
||||
p["channel"] = p.get("channel", channel)
|
||||
p.setdefault("initiation_mode", "customer")
|
||||
p.setdefault("special", "")
|
||||
p.setdefault("difficulty", 1)
|
||||
p.setdefault("pains", [])
|
||||
p.setdefault("negotiation_levers", [])
|
||||
p.setdefault("objections", [])
|
||||
p.setdefault("tolerance", 3)
|
||||
raise ValueError("persona tier is invalid")
|
||||
if p.get("channel") not in PERSONA_CHANNELS:
|
||||
raise ValueError("persona channel must be facebook or line")
|
||||
if p.get("initiation_mode") not in ("customer", "seller"):
|
||||
raise ValueError("persona initiation_mode must be customer or seller")
|
||||
for field in ("pains", "objections", "negotiation_levers"):
|
||||
if not isinstance(p.get(field), list):
|
||||
raise ValueError(f"persona {field} is invalid")
|
||||
for field, low, high in (("difficulty", 1, 5), ("tolerance", 1, 5)):
|
||||
value = p.get(field, 1 if field == "difficulty" else 3)
|
||||
if isinstance(value, bool) or not isinstance(value, int) or not low <= value <= high:
|
||||
raise ValueError(f"persona {field} is invalid")
|
||||
p[field] = value
|
||||
if "recontact" in p and not isinstance(p["recontact"], bool):
|
||||
raise ValueError("persona recontact is invalid")
|
||||
p.setdefault("recontact", False)
|
||||
p.setdefault("special", "")
|
||||
counts[tier] += 1
|
||||
normalized.append(p)
|
||||
|
||||
# Wrap tier-C: ensure at least one wrong_text persona
|
||||
if "C" in counts and not any(
|
||||
p.get("special") == "wrong_text" for p in normalized
|
||||
):
|
||||
# find first tier-C and mark it
|
||||
for p in normalized:
|
||||
if p["tier"] == "C":
|
||||
p["special"] = "wrong_text"
|
||||
break
|
||||
if any(counts[tier] != PER_TIER for tier in TIERS):
|
||||
raise ValueError("persona tiers must contain exactly 5 per tier")
|
||||
if not any(p.get("tier") == "C" and p.get("special") == "wrong_text" for p in normalized):
|
||||
raise ValueError("tier C requires a wrong_text persona")
|
||||
|
||||
# Canonical ids are assigned only after all provider data is valid.
|
||||
for index, p in enumerate(normalized, start=1):
|
||||
p["id"] = f"persona-{index:02d}"
|
||||
validate_persona_traits(p)
|
||||
return normalized
|
||||
|
||||
def generate_variant(
|
||||
@@ -157,6 +175,7 @@ class PersonaGenerator:
|
||||
if locked in source:
|
||||
variant[locked] = source.get(locked)
|
||||
variant.setdefault("initiation_mode", source.get("initiation_mode", "customer"))
|
||||
variant.setdefault("channel", source.get("channel", "social"))
|
||||
variant.setdefault("channel", source.get("channel", "facebook"))
|
||||
variant.setdefault("pains", source.get("pains", []))
|
||||
validate_persona_traits(variant)
|
||||
return variant
|
||||
@@ -1,64 +1,295 @@
|
||||
"""Lightweight server-side rate limiter (no external deps).
|
||||
"""Process-safe shared rate limiter for auth endpoints.
|
||||
|
||||
Per-key (user/IP + action) sliding-window counter, persisted to disk so restarts
|
||||
don't fully reset abuse protection. In-memory fast path + on-disk snapshot.
|
||||
Each action/identity pair is stored as an atomic JSON record. The record store
|
||||
uses an OS-level lock, so forked Gunicorn workers cannot independently accept
|
||||
attempts from stale in-memory counters or overwrite one another's snapshots.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import math
|
||||
import os
|
||||
import threading
|
||||
import time
|
||||
|
||||
from ..config import Config
|
||||
from ..auth.users import AuthError, normalize_identifier
|
||||
from ..storage.store import JsonStore, StoreError, StoreNotFoundError
|
||||
|
||||
_lock = threading.Lock()
|
||||
_mem: dict[str, list[float]] = {} # key -> list of recent timestamps
|
||||
# Kept as a compatibility/debug hook for existing tests and callers. It is
|
||||
# deliberately not authoritative; the durable JsonStore is read on every
|
||||
# check, so clearing this process-local mapping cannot reset the limit.
|
||||
_mem: dict[str, list[float]] = {}
|
||||
|
||||
|
||||
def _key(action: str, ident: str) -> str:
|
||||
return f"{action}:{ident}"
|
||||
"""Encode the pair without allowing action/identity delimiter collisions."""
|
||||
return json.dumps([action, ident], ensure_ascii=False, separators=(",", ":"))
|
||||
|
||||
|
||||
def _load():
|
||||
p = Config.DATA_DIR / "ratelimit.json"
|
||||
def _record_store() -> JsonStore:
|
||||
store = JsonStore(Config.DATA_DIR / "ratelimit")
|
||||
_migrate_legacy_state(store)
|
||||
return store
|
||||
|
||||
|
||||
def _legacy_paths():
|
||||
legacy = Config.DATA_DIR / "ratelimit.json"
|
||||
return legacy, legacy.with_name(f"{legacy.name}.migrated")
|
||||
|
||||
|
||||
def _validated_timestamps(raw_timestamps: object) -> list[float]:
|
||||
if not isinstance(raw_timestamps, list):
|
||||
raise StoreError("legacy rate-limit timestamps are invalid")
|
||||
timestamps: list[float] = []
|
||||
for timestamp in raw_timestamps:
|
||||
if isinstance(timestamp, bool) or not isinstance(timestamp, (int, float)):
|
||||
raise StoreError("legacy rate-limit timestamp is invalid")
|
||||
try:
|
||||
value = float(timestamp)
|
||||
except (OverflowError, TypeError, ValueError) as exc:
|
||||
raise StoreError("legacy rate-limit timestamp is invalid") from exc
|
||||
if not math.isfinite(value):
|
||||
raise StoreError("legacy rate-limit timestamp is invalid")
|
||||
timestamps.append(value)
|
||||
return timestamps
|
||||
|
||||
|
||||
_LEGACY_ACTIONS = (
|
||||
"password-change:user",
|
||||
"password-change:ip",
|
||||
"login:user",
|
||||
"login:ip",
|
||||
"chat:user",
|
||||
)
|
||||
|
||||
|
||||
def _legacy_key_aliases(legacy_key: object) -> set[str]:
|
||||
if not isinstance(legacy_key, str) or not legacy_key:
|
||||
raise StoreError("legacy rate-limit key is invalid")
|
||||
try:
|
||||
if p.exists():
|
||||
with open(p, encoding="utf-8") as fh:
|
||||
return json.load(fh)
|
||||
except Exception:
|
||||
pass
|
||||
return {}
|
||||
structured = json.loads(legacy_key)
|
||||
except (TypeError, ValueError):
|
||||
structured = None
|
||||
if (
|
||||
isinstance(structured, list)
|
||||
and len(structured) == 2
|
||||
and all(isinstance(value, str) for value in structured)
|
||||
):
|
||||
try:
|
||||
return {
|
||||
_key(
|
||||
normalize_identifier(structured[0]),
|
||||
normalize_identifier(structured[1]),
|
||||
)
|
||||
}
|
||||
except AuthError as exc:
|
||||
raise StoreError("legacy rate-limit key is invalid") from exc
|
||||
|
||||
# The legacy format was action:identity. Only the known action prefixes
|
||||
# are accepted, so an identity containing colons remains part of the
|
||||
# identity and cannot create aliases for other action/identity pairs.
|
||||
for action in sorted(_LEGACY_ACTIONS, key=len, reverse=True):
|
||||
prefix = f"{action}:"
|
||||
if not legacy_key.startswith(prefix):
|
||||
continue
|
||||
raw_ident = legacy_key[len(prefix) :]
|
||||
try:
|
||||
return {_key(normalize_identifier(action), normalize_identifier(raw_ident))}
|
||||
except AuthError as exc:
|
||||
raise StoreError("legacy rate-limit key is invalid") from exc
|
||||
raise StoreError("legacy rate-limit key is invalid")
|
||||
|
||||
|
||||
def _save():
|
||||
def _read_migration_marker(marker_path) -> str:
|
||||
try:
|
||||
p = Config.DATA_DIR / "ratelimit.json"
|
||||
p.parent.mkdir(parents=True, exist_ok=True)
|
||||
tmp = p.with_suffix(".json.tmp")
|
||||
with open(tmp, "w", encoding="utf-8") as fh:
|
||||
json.dump(_mem, fh)
|
||||
os.replace(tmp, p)
|
||||
except Exception:
|
||||
pass
|
||||
lines = marker_path.read_text(encoding="utf-8").splitlines()
|
||||
except (OSError, UnicodeError) as exc:
|
||||
raise StoreError("rate-limit migration marker is unreadable") from exc
|
||||
if len(lines) != 2 or lines[0] != "version=2" or not lines[1].startswith("sha256="):
|
||||
raise StoreError("rate-limit migration marker is invalid")
|
||||
digest = lines[1][len("sha256=") :]
|
||||
if len(digest) != 64 or any(character not in "0123456789abcdef" for character in digest):
|
||||
raise StoreError("rate-limit migration marker is invalid")
|
||||
return digest
|
||||
|
||||
|
||||
def _write_migration_marker(marker_path, legacy_digest: str) -> None:
|
||||
temporary = marker_path.with_name(f".{marker_path.name}.{os.getpid()}.tmp")
|
||||
try:
|
||||
with temporary.open("w", encoding="utf-8") as fh:
|
||||
fh.write(f"version=2\nsha256={legacy_digest}\n")
|
||||
fh.flush()
|
||||
os.fsync(fh.fileno())
|
||||
os.replace(temporary, marker_path)
|
||||
except (OSError, UnicodeError) as exc:
|
||||
raise StoreError("could not persist rate-limit migration marker") from exc
|
||||
finally:
|
||||
if temporary.exists():
|
||||
try:
|
||||
temporary.unlink()
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
|
||||
def _migrate_legacy_state(store: JsonStore) -> None:
|
||||
"""Import the pre-S4 single-file limiter state exactly once, safely."""
|
||||
legacy_path, marker_path = _legacy_paths()
|
||||
if marker_path.exists():
|
||||
if not marker_path.is_file():
|
||||
raise StoreError("rate-limit migration marker is invalid")
|
||||
marker_digest = _read_migration_marker(marker_path)
|
||||
if not legacy_path.exists():
|
||||
return
|
||||
if not legacy_path.is_file():
|
||||
raise StoreError("legacy rate-limit state is invalid")
|
||||
try:
|
||||
legacy_bytes = legacy_path.read_bytes()
|
||||
except OSError as exc:
|
||||
raise StoreError("legacy rate-limit state is unreadable") from exc
|
||||
if hashlib.sha256(legacy_bytes).hexdigest() != marker_digest:
|
||||
raise StoreError("rate-limit migration marker does not match legacy state")
|
||||
return
|
||||
if not legacy_path.exists():
|
||||
return
|
||||
if not legacy_path.is_file():
|
||||
raise StoreError("legacy rate-limit state is invalid")
|
||||
|
||||
with store.collection_lock():
|
||||
if marker_path.exists():
|
||||
if not marker_path.is_file():
|
||||
raise StoreError("rate-limit migration marker is invalid")
|
||||
marker_digest = _read_migration_marker(marker_path)
|
||||
if not legacy_path.exists():
|
||||
return
|
||||
if not legacy_path.is_file():
|
||||
raise StoreError("legacy rate-limit state is invalid")
|
||||
try:
|
||||
legacy_bytes = legacy_path.read_bytes()
|
||||
except OSError as exc:
|
||||
raise StoreError("legacy rate-limit state is unreadable") from exc
|
||||
if hashlib.sha256(legacy_bytes).hexdigest() != marker_digest:
|
||||
raise StoreError("rate-limit migration marker does not match legacy state")
|
||||
return
|
||||
try:
|
||||
legacy_bytes = legacy_path.read_bytes()
|
||||
legacy_state = json.loads(legacy_bytes.decode("utf-8"))
|
||||
except (OSError, UnicodeError, ValueError, TypeError) as exc:
|
||||
raise StoreError("legacy rate-limit state is unreadable") from exc
|
||||
legacy_digest = hashlib.sha256(legacy_bytes).hexdigest()
|
||||
if not isinstance(legacy_state, dict):
|
||||
raise StoreError("legacy rate-limit state is invalid")
|
||||
|
||||
for legacy_key, raw_timestamps in legacy_state.items():
|
||||
timestamps = _validated_timestamps(raw_timestamps)
|
||||
entry_digest = hashlib.sha256(
|
||||
json.dumps(
|
||||
[legacy_key, timestamps],
|
||||
ensure_ascii=False,
|
||||
separators=(",", ":"),
|
||||
).encode("utf-8")
|
||||
).hexdigest()
|
||||
for key in _legacy_key_aliases(legacy_key):
|
||||
record_id = _record_id(key)
|
||||
with store.record_lock(record_id):
|
||||
current = store.get_or_none(record_id)
|
||||
imported = current.get("legacy_imports", []) if current is not None else []
|
||||
if not isinstance(imported, list) or not all(
|
||||
isinstance(value, str)
|
||||
and len(value) == 64
|
||||
and all(character in "0123456789abcdef" for character in value)
|
||||
for value in imported
|
||||
):
|
||||
raise StoreError("legacy rate-limit import metadata is invalid")
|
||||
if entry_digest in imported:
|
||||
continue
|
||||
current_timestamps = (
|
||||
_validated_timestamps(current.get("timestamps"))
|
||||
if current is not None
|
||||
else []
|
||||
)
|
||||
# A timestamp represents one attempt. Keep duplicate
|
||||
# values: collapsing them would silently lower a legacy
|
||||
# counter when several attempts shared the same clock tick.
|
||||
merged = sorted(current_timestamps + timestamps)
|
||||
imported = [*imported, entry_digest]
|
||||
if current is None:
|
||||
store.create(
|
||||
{
|
||||
"id": record_id,
|
||||
"timestamps": merged,
|
||||
"legacy_imports": imported,
|
||||
},
|
||||
key=record_id,
|
||||
)
|
||||
else:
|
||||
store.update(
|
||||
record_id,
|
||||
timestamps=merged,
|
||||
legacy_imports=imported,
|
||||
)
|
||||
|
||||
_write_migration_marker(marker_path, legacy_digest)
|
||||
|
||||
|
||||
def _record_id(key: str) -> str:
|
||||
return hashlib.sha256(key.encode("utf-8")).hexdigest()
|
||||
|
||||
|
||||
def check(action: str, ident: str, *, limit: int, window: int) -> bool:
|
||||
"""Return True if allowed; False if the limit in `window` seconds was exceeded."""
|
||||
key = _key(action, ident)
|
||||
now = time.time()
|
||||
with _lock:
|
||||
rec = list(_mem.get(key) or _load().get(key) or [])
|
||||
rec = [t for t in rec if now - t < window]
|
||||
if len(rec) >= limit:
|
||||
_mem[key] = rec
|
||||
return False
|
||||
rec.append(now)
|
||||
_mem[key] = rec
|
||||
# opportunistically persist (bounded writes)
|
||||
"""Return True when the attempt is within the configured window."""
|
||||
if type(limit) is not int or limit <= 0 or type(window) is not int or window <= 0:
|
||||
return False
|
||||
try:
|
||||
if int(now) % 5 == 0:
|
||||
_save()
|
||||
except Exception:
|
||||
pass
|
||||
return True
|
||||
normalized_action = normalize_identifier(action)
|
||||
normalized_ident = normalize_identifier(ident)
|
||||
key = _key(normalized_action, normalized_ident)
|
||||
record_id = _record_id(key)
|
||||
except (AuthError, TypeError, UnicodeError, ValueError):
|
||||
return False
|
||||
now = time.time()
|
||||
|
||||
try:
|
||||
store = _record_store()
|
||||
with store.record_lock(record_id):
|
||||
try:
|
||||
record = store.get(record_id)
|
||||
except StoreNotFoundError:
|
||||
record = None
|
||||
except (OSError, StoreError, TypeError, UnicodeError, ValueError):
|
||||
return False
|
||||
|
||||
if record is not None and not isinstance(record, dict):
|
||||
return False
|
||||
raw_timestamps = record.get("timestamps") if record is not None else []
|
||||
if not isinstance(raw_timestamps, list):
|
||||
return False
|
||||
|
||||
recent = []
|
||||
for timestamp in raw_timestamps:
|
||||
if isinstance(timestamp, bool) or not isinstance(timestamp, (int, float)):
|
||||
return False
|
||||
try:
|
||||
timestamp = float(timestamp)
|
||||
except (OverflowError, TypeError, ValueError):
|
||||
return False
|
||||
if not math.isfinite(timestamp):
|
||||
return False
|
||||
if now - timestamp < window:
|
||||
recent.append(timestamp)
|
||||
|
||||
allowed = len(recent) < limit
|
||||
if allowed:
|
||||
recent.append(now)
|
||||
|
||||
if record is None:
|
||||
store.create({"id": record_id, "timestamps": recent}, key=record_id)
|
||||
else:
|
||||
store.update(record_id, timestamps=recent)
|
||||
|
||||
# Diagnostic only; never consult this process-local mapping.
|
||||
_mem[key] = recent
|
||||
return allowed
|
||||
except (OSError, StoreError, TypeError, UnicodeError, ValueError):
|
||||
# A durable limiter must fail closed on any storage/serialization fault.
|
||||
return False
|
||||
@@ -1,8 +1,10 @@
|
||||
"""Training session store.
|
||||
|
||||
A session = one trainee's one-shot chat attempt against one persona. It records the
|
||||
full transcript + internal state + outcome + debrief. One user may have at most one
|
||||
session per persona (one-shot rule), enforced here.
|
||||
A session is identified by the full tenant scope ``(org, user, group, persona)``
|
||||
plus its mode. A trainee session is one-shot; preview sessions are deliberately
|
||||
outside that invariant so instructors can test a persona without consuming a
|
||||
trainee attempt. The scope scan and create happen under one collection lock so
|
||||
two worker threads/processes cannot create competing active sessions.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -24,38 +26,131 @@ class SessionStore:
|
||||
def create(
|
||||
self,
|
||||
*,
|
||||
org_id: str,
|
||||
user_id: str,
|
||||
group_id: str,
|
||||
persona_id: str,
|
||||
persona_name: str,
|
||||
persona_meta: dict[str, Any] | None = None,
|
||||
mode: str = "trainee",
|
||||
messages: list[dict[str, Any]] | None = None,
|
||||
internal: dict[str, Any] | None = None,
|
||||
scenario: str | None = None,
|
||||
locale: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
# One-shot: reject if the user already has a finished session on this persona
|
||||
existing = self.sessions.where(
|
||||
lambda r: r.get("user_id") == user_id
|
||||
and r.get("persona_id") == persona_id
|
||||
and r.get("outcome") in ("won", "lost")
|
||||
)
|
||||
if existing:
|
||||
raise ValueError("you have already trained on this persona (one-shot)")
|
||||
self._validate_scope(org_id, user_id, group_id, persona_id, mode)
|
||||
sid = new_id("session")
|
||||
session = {
|
||||
"id": sid,
|
||||
"org_id": org_id,
|
||||
"user_id": user_id,
|
||||
"group_id": group_id,
|
||||
"persona_id": persona_id,
|
||||
"persona_name": persona_name,
|
||||
"persona_meta": persona_meta or {},
|
||||
"scenario": scenario,
|
||||
"locale": locale,
|
||||
"mode": mode,
|
||||
"status": "active", # active | finished
|
||||
"outcome": None, # won | lost | abandoned
|
||||
"messages": [], # [{role, text, ts}]
|
||||
"internal": {"trust": 50, "pain_progress": {}, "buying_signals": [], "tier": None},
|
||||
"messages": list(messages or []), # [{role, text, ts}]
|
||||
"internal": dict(internal or {"trust": 50, "pain_progress": {}, "buying_signals": [], "tier": None}),
|
||||
"debrief": None,
|
||||
"created_at": _now(),
|
||||
"updated_at": _now(),
|
||||
}
|
||||
return self.sessions.create(session, key=sid)
|
||||
|
||||
@staticmethod
|
||||
def _validate_scope(
|
||||
org_id: str,
|
||||
user_id: str,
|
||||
group_id: str,
|
||||
persona_id: str,
|
||||
mode: str,
|
||||
) -> None:
|
||||
if not all(isinstance(value, str) and value.strip() for value in (
|
||||
org_id, user_id, group_id, persona_id,
|
||||
)):
|
||||
raise ValueError("session scope is invalid")
|
||||
if mode not in ("trainee", "preview"):
|
||||
raise ValueError("session mode is invalid")
|
||||
|
||||
@staticmethod
|
||||
def _mode(row: dict[str, Any]) -> str:
|
||||
# Records created before the explicit mode field are normal trainee attempts.
|
||||
return row.get("mode") or "trainee"
|
||||
|
||||
def _scope_rows(
|
||||
self,
|
||||
*,
|
||||
org_id: str,
|
||||
user_id: str,
|
||||
group_id: str,
|
||||
persona_id: str,
|
||||
mode: str,
|
||||
) -> list[dict[str, Any]]:
|
||||
return self.sessions.where(
|
||||
lambda row: isinstance(row, dict)
|
||||
and row.get("org_id") == org_id
|
||||
and row.get("user_id") == user_id
|
||||
and row.get("group_id") == group_id
|
||||
and row.get("persona_id") == persona_id
|
||||
and self._mode(row) == mode
|
||||
)
|
||||
|
||||
def start(
|
||||
self,
|
||||
*,
|
||||
org_id: str,
|
||||
user_id: str,
|
||||
group_id: str,
|
||||
persona_id: str,
|
||||
persona_name: str,
|
||||
persona_meta: dict[str, Any] | None = None,
|
||||
mode: str = "trainee",
|
||||
messages: list[dict[str, Any]] | None = None,
|
||||
internal: dict[str, Any] | None = None,
|
||||
scenario: str | None = None,
|
||||
locale: str | None = None,
|
||||
) -> tuple[dict[str, Any], bool]:
|
||||
"""Atomically resume or create a session in one canonical scope.
|
||||
|
||||
Returns ``(session, resumed)``. Only trainee sessions enforce the
|
||||
one-shot finished-session rule; preview sessions can be started again.
|
||||
"""
|
||||
self._validate_scope(org_id, user_id, group_id, persona_id, mode)
|
||||
with self.sessions.collection_lock():
|
||||
rows = self._scope_rows(
|
||||
org_id=org_id,
|
||||
user_id=user_id,
|
||||
group_id=group_id,
|
||||
persona_id=persona_id,
|
||||
mode=mode,
|
||||
)
|
||||
active = next((row for row in rows if row.get("status") == "active"), None)
|
||||
if active is not None:
|
||||
return active, True
|
||||
if mode == "trainee" and any(
|
||||
row.get("status") == "finished"
|
||||
or row.get("outcome") in ("won", "lost", "abandoned")
|
||||
for row in rows
|
||||
):
|
||||
raise ValueError("you have already trained on this persona (one-shot)")
|
||||
return self.create(
|
||||
org_id=org_id,
|
||||
user_id=user_id,
|
||||
group_id=group_id,
|
||||
persona_id=persona_id,
|
||||
persona_name=persona_name,
|
||||
persona_meta=persona_meta,
|
||||
mode=mode,
|
||||
messages=messages,
|
||||
internal=internal,
|
||||
scenario=scenario,
|
||||
locale=locale,
|
||||
), False
|
||||
|
||||
def get(self, sid: str) -> dict[str, Any]:
|
||||
return self.sessions.get(sid)
|
||||
|
||||
@@ -66,16 +161,74 @@ class SessionStore:
|
||||
fields.setdefault("updated_at", _now())
|
||||
return self.sessions.update(sid, **fields)
|
||||
|
||||
def active_for_persona(self, user_id: str, persona_id: str) -> dict[str, Any] | None:
|
||||
hits = self.sessions.where(
|
||||
lambda r: r.get("user_id") == user_id
|
||||
and r.get("persona_id") == persona_id
|
||||
and r.get("status") == "active"
|
||||
)
|
||||
def mutation_lock(self, sid: str):
|
||||
"""Serialize a full session transaction across threads and workers."""
|
||||
return self.sessions.record_lock(sid)
|
||||
|
||||
def active_for_scope(
|
||||
self,
|
||||
*,
|
||||
org_id: str,
|
||||
user_id: str,
|
||||
group_id: str,
|
||||
persona_id: str,
|
||||
mode: str = "trainee",
|
||||
) -> dict[str, Any] | None:
|
||||
self._validate_scope(org_id, user_id, group_id, persona_id, mode)
|
||||
hits = [
|
||||
row for row in self._scope_rows(
|
||||
org_id=org_id,
|
||||
user_id=user_id,
|
||||
group_id=group_id,
|
||||
persona_id=persona_id,
|
||||
mode=mode,
|
||||
)
|
||||
if isinstance(row, dict) and row.get("status") == "active"
|
||||
]
|
||||
return hits[0] if hits else None
|
||||
|
||||
def list_for_user(self, user_id: str) -> list[dict[str, Any]]:
|
||||
def latest_for_scope(
|
||||
self,
|
||||
*,
|
||||
org_id: str,
|
||||
user_id: str,
|
||||
group_id: str,
|
||||
persona_id: str,
|
||||
mode: str = "trainee",
|
||||
) -> dict[str, Any] | None:
|
||||
"""Return the newest record in one scope, including finished sessions."""
|
||||
self._validate_scope(org_id, user_id, group_id, persona_id, mode)
|
||||
rows = [
|
||||
row
|
||||
for row in self._scope_rows(
|
||||
org_id=org_id,
|
||||
user_id=user_id,
|
||||
group_id=group_id,
|
||||
persona_id=persona_id,
|
||||
mode=mode,
|
||||
)
|
||||
if isinstance(row, dict)
|
||||
]
|
||||
return max(rows, key=lambda row: row.get("updated_at", ""), default=None)
|
||||
|
||||
def list_for_user(
|
||||
self,
|
||||
user_id: str,
|
||||
*,
|
||||
org_id: str | None = None,
|
||||
include_preview: bool = False,
|
||||
) -> list[dict[str, Any]]:
|
||||
rows = [
|
||||
row
|
||||
for row in self.sessions.where(
|
||||
lambda row: isinstance(row, dict)
|
||||
and row.get("user_id") == user_id
|
||||
and (org_id is None or row.get("org_id") == org_id)
|
||||
and (include_preview or self._mode(row) == "trainee")
|
||||
)
|
||||
if isinstance(row, dict)
|
||||
]
|
||||
return sorted(
|
||||
self.sessions.where(lambda r: r.get("user_id") == user_id),
|
||||
rows,
|
||||
key=lambda r: r.get("created_at", ""),
|
||||
)
|
||||
@@ -11,6 +11,8 @@ from typing import Any
|
||||
|
||||
from ..llm import LLMClient, LLMError
|
||||
|
||||
MAX_PERSONA_REPLY_CHARS = 4000
|
||||
|
||||
CHAT_SYSTEM = """You are playing a REALISTIC customer named {name} in a sales-training chat.
|
||||
Stay perfectly in character at ALL times. Use {tone}.
|
||||
|
||||
@@ -24,8 +26,12 @@ naturally and it makes sense for a real customer to reveal them):
|
||||
- Your pains (some may be product-solvable, some NOT): {pains}
|
||||
- Your negotiation levers: {levers}
|
||||
- Your goal/mood: {goal}
|
||||
- Tier: {tier} (A = ready, B = researching/unsure, C = resistant but has a real pain)
|
||||
- Your tolerance (TOLERANCE): you walk away after about {tolerance} irritating/off-point/pushy
|
||||
answers. If the seller is repeatedly wrong, ignores your need, or is pushy, you feel fed up.
|
||||
- Recontact lead: {recontact}. If true, you have prior context with this seller and may be warmer
|
||||
after the time-lapse note, but still require the real pain to be handled.
|
||||
Special flag: {special}.
|
||||
Initiation mode: {init_mode}. {special_instr}
|
||||
|
||||
BEHAVIOR RULES:
|
||||
@@ -61,12 +67,10 @@ REALISM RULES:
|
||||
it's a WIN. Do NOT auto-fail on special cases — always reward genuinely skillful recovery.
|
||||
- LOST reflects the persona TYPICALLY losing (real-world >90% of such leads do not convert), but the
|
||||
trainee's skill evaluation must remain fair: a strong close beats a weak one, always.
|
||||
- If the chat drags on many turns (or turns > ~12) without the seller reaching the pain or closing,
|
||||
treat it as LOST due to failing to convert / the opportunity cooling (mirrors real leads going cold).
|
||||
- If the seller was pushy, rude, ignored the need, or mis-diagnosed the pain, mark LOST even if the
|
||||
price was acceptable.
|
||||
|
||||
Scoring (0-100): painResolution + trust + objectionHandling + efficiency (fewer turns, higher).
|
||||
Scoring (0-100): painResolution + trust + objectionHandling + quality of the close.
|
||||
Return JSON:
|
||||
{
|
||||
"outcome": "won" | "lost",
|
||||
@@ -119,9 +123,12 @@ class Simulator:
|
||||
budget=persona.get("budget", ""),
|
||||
decision_timeline=persona.get("decision_timeline", ""),
|
||||
pains=pains_txt,
|
||||
levers=", ".join(persona.get("negotiation_levers", [])) or "price, delivery time",
|
||||
levers=self._describe_levers(persona.get("negotiation_levers", [])),
|
||||
goal=persona.get("goal", ""),
|
||||
tier=persona.get("tier", "B"),
|
||||
tolerance=tolerance,
|
||||
recontact="yes" if persona.get("recontact") else "no",
|
||||
special=persona.get("special", ""),
|
||||
init_mode="you contacted the seller first (customer-initiated)"
|
||||
if persona.get("initiation_mode") == "customer"
|
||||
else "the seller opened the sale to you (you are a lead)",
|
||||
@@ -139,14 +146,17 @@ class Simulator:
|
||||
msgs.append({"role": "system", "content": f"[scene note from transcript]: {m.get('text')}"})
|
||||
else:
|
||||
msgs.append({"role": role, "content": m.get("text", "")})
|
||||
try:
|
||||
for attempt in range(2):
|
||||
if attempt:
|
||||
msgs.append({
|
||||
"role": "system",
|
||||
"content": "Contract correction: output one JSON object with a non-empty string field named reply. Do not include analysis or extra prose.",
|
||||
})
|
||||
resp = self.llm.complete_conversation(msgs, temperature=0.7, max_tokens=400)
|
||||
except LLMError as exc:
|
||||
raise
|
||||
# Return the customer's reply as plain text (no forced JSON) — a natural sentence is
|
||||
# the persona's message. Mood/decision are evaluated separately by evaluate_turn().
|
||||
reply = resp.strip() if resp and resp.strip() else "(ลูกค้ายังไม่ตอบ)"
|
||||
return reply, {"decision": "none", "mood": 0}
|
||||
parsed = self._parse_persona_reply(resp)
|
||||
if parsed is not None:
|
||||
return parsed
|
||||
raise LLMError("persona reply was empty or missing reply field")
|
||||
|
||||
def evaluate_turn(self, *, persona, messages, internal=None) -> dict[str, Any]:
|
||||
"""Per-turn state evaluation: how the persona feels + whether it has decided.
|
||||
@@ -193,6 +203,8 @@ class Simulator:
|
||||
except LLMError:
|
||||
# judge unavailable — fall back to pending (don't crash, don't misuse keywords)
|
||||
return {"mood": 0, "decision": "pending", "score_delta": 0, "reason": ""}
|
||||
if not isinstance(result, dict):
|
||||
return {"mood": 0, "decision": "pending", "score_delta": 0, "reason": ""}
|
||||
result.setdefault("mood", 0)
|
||||
result.setdefault("decision", "pending")
|
||||
result.setdefault("score_delta", 0)
|
||||
@@ -224,7 +236,7 @@ class Simulator:
|
||||
try:
|
||||
state_note = (
|
||||
"\n\nINTERNAL (hidden, for judging only): "
|
||||
f"turns={internal.get('turns', 0)}, score_trend={internal.get('score', 50)}"
|
||||
f"score_trend={internal.get('score', 50)}"
|
||||
)
|
||||
except Exception:
|
||||
state_note = ""
|
||||
@@ -235,6 +247,8 @@ class Simulator:
|
||||
)
|
||||
except LLMError as exc:
|
||||
raise
|
||||
if not isinstance(result, dict):
|
||||
result = {}
|
||||
result.setdefault("outcome", "lost")
|
||||
result.setdefault("score", 0)
|
||||
result.setdefault("pain", "")
|
||||
@@ -259,6 +273,66 @@ class Simulator:
|
||||
out.append(str(p))
|
||||
return "; ".join(out)
|
||||
|
||||
def _describe_levers(self, levers: object) -> str:
|
||||
if not isinstance(levers, list):
|
||||
return "price, delivery time"
|
||||
values = []
|
||||
for lever in levers[:20]:
|
||||
if isinstance(lever, str) and lever.strip():
|
||||
values.append(lever.strip())
|
||||
elif isinstance(lever, dict):
|
||||
label = lever.get("name") or lever.get("label") or lever.get("description")
|
||||
if isinstance(label, str) and label.strip():
|
||||
values.append(label.strip())
|
||||
return ", ".join(values) or "price, delivery time"
|
||||
|
||||
def _parse_persona_reply(self, text: object) -> tuple[str, dict[str, Any]] | None:
|
||||
"""Parse provider output without leaking protocol fields to the bubble.
|
||||
|
||||
The preferred contract is a JSON object. A plain natural-language
|
||||
response remains a safe compatibility fallback for providers that ignore
|
||||
JSON mode. Objects without a usable ``reply`` are retryable; they are
|
||||
never rendered verbatim.
|
||||
"""
|
||||
if not isinstance(text, str):
|
||||
return None
|
||||
raw = text.strip()
|
||||
if not raw:
|
||||
return None
|
||||
|
||||
payload: Any = None
|
||||
candidates = [raw]
|
||||
extracted = self._extract_json(raw)
|
||||
if extracted != raw:
|
||||
candidates.append(extracted)
|
||||
for candidate in candidates:
|
||||
try:
|
||||
payload = json.loads(candidate)
|
||||
break
|
||||
except (TypeError, json.JSONDecodeError):
|
||||
continue
|
||||
|
||||
if isinstance(payload, dict):
|
||||
reply = payload.get("reply")
|
||||
if not isinstance(reply, str) or not reply.strip():
|
||||
return None
|
||||
decision = payload.get("decision")
|
||||
if decision not in ("none", "buy", "walk"):
|
||||
decision = "none"
|
||||
try:
|
||||
mood = max(-2, min(2, int(payload.get("mood", 0))))
|
||||
except (TypeError, ValueError):
|
||||
mood = 0
|
||||
return reply.strip()[:MAX_PERSONA_REPLY_CHARS], {
|
||||
"decision": decision,
|
||||
"mood": mood,
|
||||
}
|
||||
|
||||
# A plain sentence is safe; protocol-looking malformed JSON is not.
|
||||
if raw.startswith(("{", "[", "```")):
|
||||
return None
|
||||
return raw[:MAX_PERSONA_REPLY_CHARS], {"decision": "none", "mood": 0}
|
||||
|
||||
def _special_instr(self, persona: dict[str, Any]) -> str:
|
||||
if persona.get("special") == "wrong_text":
|
||||
return (
|
||||
|
||||
@@ -13,6 +13,16 @@ from __future__ import annotations
|
||||
from typing import Any
|
||||
|
||||
DEFAULT_TIERS = ["A", "B", "C"]
|
||||
PERSONA_CHANNELS = ("facebook", "line")
|
||||
PERSONA_INITIATION_MODES = ("customer", "seller")
|
||||
|
||||
|
||||
def validate_persona_traits(p: dict[str, Any]) -> None:
|
||||
"""Reject invalid behavior traits instead of silently changing their meaning."""
|
||||
if p.get("channel") not in PERSONA_CHANNELS:
|
||||
raise ValueError("persona channel must be facebook or line")
|
||||
if p.get("initiation_mode") not in PERSONA_INITIATION_MODES:
|
||||
raise ValueError("persona initiation_mode must be customer or seller")
|
||||
|
||||
|
||||
def ensure_persona_shape(p: dict[str, Any]) -> dict[str, Any]:
|
||||
@@ -20,10 +30,11 @@ def ensure_persona_shape(p: dict[str, Any]) -> dict[str, Any]:
|
||||
pid = p.get("id") or p.get("name", "persona")
|
||||
base = {
|
||||
"id": pid,
|
||||
"source_persona_id": p.get("source_persona_id"),
|
||||
"name": p.get("name", ""),
|
||||
"tier": p.get("tier", p.get("intent_tier", "B")),
|
||||
"initiation_mode": p.get("initiation_mode", "customer"), # customer | seller
|
||||
"channel": p.get("channel", "social"), # internal tone hint (scenario overrides)
|
||||
"channel": p.get("channel", "facebook"), # facebook | line
|
||||
# revealable
|
||||
"profession": p.get("profession", ""),
|
||||
"age_group": p.get("age_group", ""),
|
||||
@@ -51,7 +62,7 @@ def ensure_persona_shape(p: dict[str, Any]) -> dict[str, Any]:
|
||||
# validation
|
||||
if base["tier"] not in DEFAULT_TIERS:
|
||||
base["tier"] = "B"
|
||||
if base["initiation_mode"] not in ("customer", "seller"):
|
||||
if base["initiation_mode"] not in PERSONA_INITIATION_MODES:
|
||||
base["initiation_mode"] = "customer"
|
||||
return base
|
||||
|
||||
@@ -63,6 +74,7 @@ def revealable_view(p: dict[str, Any]) -> dict[str, Any]:
|
||||
"name": p.get("name"),
|
||||
"tier": p.get("tier"),
|
||||
"initiation_mode": p.get("initiation_mode"),
|
||||
"channel": p.get("channel"),
|
||||
"profession": p.get("profession"),
|
||||
"age_group": p.get("age_group"),
|
||||
"location": p.get("location"),
|
||||
|
||||
+161
-32
@@ -7,6 +7,8 @@ personas are private to the user.
|
||||
from __future__ import annotations
|
||||
|
||||
import datetime
|
||||
import math
|
||||
from collections import Counter
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
@@ -40,42 +42,169 @@ class MyPersonaStore:
|
||||
]
|
||||
|
||||
|
||||
def analyze_weak_areas(sessions: list[dict[str, Any]]) -> dict[str, Any]:
|
||||
"""Summarize which persona attributes a user tends to lose against."""
|
||||
losses, wins = [], []
|
||||
for ses in sessions:
|
||||
if ses.get("outcome") == "won":
|
||||
wins.append(ses)
|
||||
elif ses.get("outcome") == "lost":
|
||||
losses.append(ses)
|
||||
_WEAK_DIMENSIONS = {
|
||||
"pain_discovery": (
|
||||
"pain discovery",
|
||||
("pain", "need", "discov", "question", "qualif", "ask deeper", "ความต้องการ", "ปัญหา"),
|
||||
),
|
||||
"objection_handling": (
|
||||
"objection handling",
|
||||
("objection", "concern", "expensive", "price objection", "ข้อโต้แย้ง", "กังวล", "แพง"),
|
||||
),
|
||||
"negotiation": (
|
||||
"negotiation",
|
||||
("negotiat", "discount", "budget", "payment", "ต่อรอง", "ส่วนลด", "งบประมาณ"),
|
||||
),
|
||||
"trust_building": (
|
||||
"trust building",
|
||||
("trust", "proof", "credib", "relationship", "ความน่าเชื่อถือ", "ความไว้ใจ"),
|
||||
),
|
||||
"close_attempt": (
|
||||
"close attempt",
|
||||
("clos", "commit", "next step", "order", "ปิดการขาย", "ตัดสินใจ", "ขั้นตอนถัดไป"),
|
||||
),
|
||||
}
|
||||
|
||||
def tally(key: str, label: str) -> list[dict[str, Any]]:
|
||||
from collections import Counter
|
||||
|
||||
c = Counter()
|
||||
for l in losses:
|
||||
meta = l.get("persona_meta") or {}
|
||||
v = meta.get(key)
|
||||
if v is not None:
|
||||
c[v] += 1
|
||||
return [{"value": k, "losses": v} for k, v in c.most_common(3)]
|
||||
def _is_finite_number(value: object) -> bool:
|
||||
if isinstance(value, bool) or not isinstance(value, (int, float)):
|
||||
return False
|
||||
try:
|
||||
return math.isfinite(value)
|
||||
except (OverflowError, TypeError, ValueError):
|
||||
return False
|
||||
|
||||
|
||||
def _finished_trainee_sessions(sessions: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
||||
"""Only completed non-preview sessions are evidence for a trainee."""
|
||||
return [
|
||||
session
|
||||
for session in sessions
|
||||
if isinstance(session, dict)
|
||||
if (session.get("mode") or "trainee") == "trainee"
|
||||
and session.get("status") == "finished"
|
||||
and session.get("outcome") in ("won", "lost")
|
||||
]
|
||||
|
||||
|
||||
def _session_evidence(session: dict[str, Any]) -> str:
|
||||
raw_debrief = session.get("debrief")
|
||||
debrief: dict[str, Any] = raw_debrief if isinstance(raw_debrief, dict) else {}
|
||||
pieces: list[str] = []
|
||||
for field in ("failurePoints", "coaching"):
|
||||
values = debrief.get(field)
|
||||
if isinstance(values, list):
|
||||
pieces.extend(str(value) for value in values if isinstance(value, str))
|
||||
why = debrief.get("why")
|
||||
if isinstance(why, str):
|
||||
pieces.append(why)
|
||||
progress = debrief.get("painProgress")
|
||||
if isinstance(progress, dict):
|
||||
pieces.extend(
|
||||
f"pain progress {key} {value}"
|
||||
for key, value in progress.items()
|
||||
if (
|
||||
isinstance(key, str)
|
||||
and _is_finite_number(value)
|
||||
and value < 100
|
||||
)
|
||||
)
|
||||
return " ".join(pieces).lower()
|
||||
|
||||
|
||||
def _dimension_evidence(sessions: list[dict[str, Any]]) -> dict[str, dict[str, Any]]:
|
||||
dimensions = {
|
||||
key: {"label": label, "evidence_count": 0, "session_ids": []}
|
||||
for key, (label, _patterns) in _WEAK_DIMENSIONS.items()
|
||||
}
|
||||
for session in sessions:
|
||||
evidence = _session_evidence(session)
|
||||
if not evidence:
|
||||
continue
|
||||
for key, (_label, patterns) in _WEAK_DIMENSIONS.items():
|
||||
if any(pattern in evidence for pattern in patterns):
|
||||
dimensions[key]["evidence_count"] += 1
|
||||
sid = session.get("id")
|
||||
if isinstance(sid, str) and sid not in dimensions[key]["session_ids"]:
|
||||
dimensions[key]["session_ids"].append(sid)
|
||||
return dimensions
|
||||
|
||||
|
||||
def analyze_weak_areas(
|
||||
sessions: list[dict[str, Any]],
|
||||
*,
|
||||
user_id: str,
|
||||
org_id: str,
|
||||
) -> dict[str, Any]:
|
||||
"""Summarize judge evidence after enforcing the caller's tenant/user scope."""
|
||||
if not isinstance(user_id, str) or not user_id.strip():
|
||||
raise ValueError("user scope is invalid")
|
||||
if not isinstance(org_id, str) or not org_id.strip() or org_id != org_id.strip():
|
||||
raise ValueError("organization scope is invalid")
|
||||
scoped_sessions = [
|
||||
session
|
||||
for session in sessions
|
||||
if isinstance(session, dict)
|
||||
and session.get("user_id") == user_id
|
||||
and session.get("org_id") == org_id
|
||||
]
|
||||
completed = _finished_trainee_sessions(scoped_sessions)
|
||||
losses = [session for session in completed if session.get("outcome") == "lost"]
|
||||
wins = [session for session in completed if session.get("outcome") == "won"]
|
||||
|
||||
def safe_score(value: object) -> int | float:
|
||||
if isinstance(value, bool) or not isinstance(value, (int, float)):
|
||||
return 0
|
||||
try:
|
||||
if not math.isfinite(value):
|
||||
return 0
|
||||
return max(0, min(100, value))
|
||||
except (OverflowError, TypeError, ValueError):
|
||||
return 0
|
||||
|
||||
def tally(key: str) -> list[dict[str, Any]]:
|
||||
counter = Counter()
|
||||
for loss in losses:
|
||||
raw_meta = loss.get("persona_meta")
|
||||
meta = raw_meta if isinstance(raw_meta, dict) else {}
|
||||
value = meta.get(key)
|
||||
if isinstance(value, str):
|
||||
value = value.strip() or None
|
||||
elif isinstance(value, bool):
|
||||
value = str(value)
|
||||
elif isinstance(value, int):
|
||||
pass
|
||||
elif isinstance(value, float):
|
||||
value = value if math.isfinite(value) else None
|
||||
else:
|
||||
value = None
|
||||
if value is not None:
|
||||
counter[value] += 1
|
||||
return [{"value": value, "losses": count} for value, count in counter.most_common(3)]
|
||||
|
||||
top_loss_personas = []
|
||||
for loss in sorted(
|
||||
losses,
|
||||
key=lambda row: (
|
||||
safe_score((row.get("debrief") or {}).get("score", 0))
|
||||
if isinstance(row.get("debrief"), dict) else 0
|
||||
),
|
||||
)[:5]:
|
||||
raw_debrief = loss.get("debrief")
|
||||
debrief: dict[str, Any] = raw_debrief if isinstance(raw_debrief, dict) else {}
|
||||
top_loss_personas.append({
|
||||
"persona_id": loss.get("persona_id"),
|
||||
"persona_name": loss.get("persona_name"),
|
||||
"score": safe_score(debrief.get("score", 0)),
|
||||
})
|
||||
|
||||
return {
|
||||
"total_sessions": len(sessions),
|
||||
"total_sessions": len(completed),
|
||||
"wins": len(wins),
|
||||
"losses": len(losses),
|
||||
"by_tier": tally("tier", "tier"),
|
||||
"by_initiation": tally("initiation_mode", "initiation"),
|
||||
"by_channel": tally("channel", "channel"),
|
||||
"top_loss_personas": [
|
||||
{
|
||||
"persona_id": l.get("persona_id"),
|
||||
"persona_name": l.get("persona_name"),
|
||||
"score": (l.get("debrief") or {}).get("score", 0),
|
||||
"why": (l.get("debrief") or {}).get("why", ""),
|
||||
}
|
||||
for l in sorted(
|
||||
losses, key=lambda x: (x.get("debrief") or {}).get("score", 0)
|
||||
)[:5]
|
||||
],
|
||||
"by_tier": tally("tier"),
|
||||
"by_initiation": tally("initiation_mode"),
|
||||
"by_channel": tally("channel"),
|
||||
"dimensions": _dimension_evidence(completed),
|
||||
"top_loss_personas": top_loss_personas,
|
||||
}
|
||||
+206
-33
@@ -5,12 +5,16 @@ atomic (temp file + os.replace + fsync). Thread-safe via a per-path lock.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from contextlib import contextmanager
|
||||
import contextvars
|
||||
import fcntl
|
||||
import json
|
||||
import os
|
||||
import threading
|
||||
import unicodedata
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
from typing import Any, Callable
|
||||
from typing import Any, Callable, Iterator
|
||||
|
||||
from ..config import Config
|
||||
|
||||
@@ -19,9 +23,14 @@ class StoreError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class StoreNotFoundError(StoreError):
|
||||
"""Raised only when a record does not exist."""
|
||||
|
||||
|
||||
class _Locks:
|
||||
def __init__(self) -> None:
|
||||
self._locks: dict[str, threading.RLock] = {}
|
||||
self._collection_locks: dict[str, threading.RLock] = {}
|
||||
self._guard = threading.Lock()
|
||||
|
||||
def get(self, key: str) -> threading.RLock:
|
||||
@@ -30,15 +39,31 @@ class _Locks:
|
||||
self._locks[key] = threading.RLock()
|
||||
return self._locks[key]
|
||||
|
||||
def get_collection(self, key: str) -> threading.RLock:
|
||||
with self._guard:
|
||||
if key not in self._collection_locks:
|
||||
self._collection_locks[key] = threading.RLock()
|
||||
return self._collection_locks[key]
|
||||
|
||||
|
||||
_locks = _Locks()
|
||||
_held_records: contextvars.ContextVar[frozenset[str]] = contextvars.ContextVar(
|
||||
"held_records", default=frozenset()
|
||||
)
|
||||
|
||||
|
||||
def new_id(prefix: str) -> str:
|
||||
return f"{prefix}-{uuid.uuid4().hex[:12]}"
|
||||
|
||||
|
||||
def contains_control_characters(value: str) -> bool:
|
||||
"""Return whether a string contains any Unicode control character."""
|
||||
return any(unicodedata.category(char) == "Cc" for char in value)
|
||||
|
||||
|
||||
def _atomic_write(path: Path, value: Any) -> None:
|
||||
if not isinstance(value, dict):
|
||||
raise StoreError("records must be JSON objects")
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
tmp = path.with_name(f".{path.name}.{uuid.uuid4().hex}.tmp")
|
||||
try:
|
||||
@@ -56,8 +81,53 @@ def _atomic_write(path: Path, value: Any) -> None:
|
||||
|
||||
|
||||
def _read_json(path: Path) -> Any:
|
||||
with path.open("r", encoding="utf-8") as fh:
|
||||
return json.load(fh)
|
||||
try:
|
||||
with path.open("r", encoding="utf-8") as fh:
|
||||
value = json.load(fh)
|
||||
except FileNotFoundError:
|
||||
raise
|
||||
except (OSError, UnicodeError, ValueError) as exc:
|
||||
raise StoreError("record is unreadable") from exc
|
||||
if not isinstance(value, dict):
|
||||
raise StoreError("record must be a JSON object")
|
||||
return value
|
||||
|
||||
|
||||
@contextmanager
|
||||
def _exclusive_file_lock(lock_path: Path) -> Iterator[None]:
|
||||
lock_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
with lock_path.open("a+", encoding="utf-8") as fh:
|
||||
fcntl.flock(fh.fileno(), fcntl.LOCK_EX)
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
fcntl.flock(fh.fileno(), fcntl.LOCK_UN)
|
||||
|
||||
|
||||
@contextmanager
|
||||
def _process_lock(path: Path) -> Iterator[None]:
|
||||
"""Coordinate writes to one record across forked worker processes."""
|
||||
with _exclusive_file_lock(path.with_name(f".{path.name}.lock")):
|
||||
yield
|
||||
|
||||
|
||||
@contextmanager
|
||||
def _maybe_process_lock(path: Path) -> Iterator[None]:
|
||||
"""Avoid reacquiring the same process lock inside record_lock()."""
|
||||
if str(path) in _held_records.get():
|
||||
yield
|
||||
else:
|
||||
with _process_lock(path):
|
||||
yield
|
||||
|
||||
|
||||
@contextmanager
|
||||
def _collection_lock(root: Path) -> Iterator[None]:
|
||||
"""Coordinate cross-record invariants across threads and worker processes."""
|
||||
lock = _locks.get_collection(str(root))
|
||||
with lock:
|
||||
with _exclusive_file_lock(root / ".collection.lock"):
|
||||
yield
|
||||
|
||||
|
||||
class JsonStore:
|
||||
@@ -69,70 +139,173 @@ class JsonStore:
|
||||
self.root.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
def _path(self, key: str) -> Path:
|
||||
if not key or "/" in key or ".." in key:
|
||||
if (
|
||||
not isinstance(key, str)
|
||||
or not key
|
||||
or len(key) > 200
|
||||
or "/" in key
|
||||
or "\\" in key
|
||||
or ".." in key
|
||||
or key == "."
|
||||
or contains_control_characters(key)
|
||||
):
|
||||
raise StoreError("invalid id")
|
||||
try:
|
||||
if len(key.encode("utf-8")) > 200:
|
||||
raise StoreError("invalid id")
|
||||
except UnicodeError as exc:
|
||||
raise StoreError("invalid id") from exc
|
||||
return self.root / f"{key}.json"
|
||||
|
||||
def create(self, value: dict[str, Any], *, key: str | None = None) -> dict[str, Any]:
|
||||
key = key or value.get(self.key_attr) or new_id(self.key_attr)
|
||||
if self.key_attr not in value:
|
||||
value = dict(value)
|
||||
value[self.key_attr] = key
|
||||
def _validate_record_key(self, key: str, value: Any) -> dict[str, Any]:
|
||||
if not isinstance(value, dict) or value.get(self.key_attr) != key:
|
||||
raise StoreError("record key does not match storage key")
|
||||
return value
|
||||
|
||||
@contextmanager
|
||||
def collection_lock(self) -> Iterator[None]:
|
||||
"""Hold an exclusive lock for invariants spanning multiple records."""
|
||||
with _collection_lock(self.root):
|
||||
yield
|
||||
|
||||
@contextmanager
|
||||
def record_lock(self, key: str) -> Iterator[None]:
|
||||
"""Hold one record lock across a read/compute/write transaction."""
|
||||
path = self._path(key)
|
||||
lock = _locks.get(str(path))
|
||||
current = _held_records.get()
|
||||
already_held = str(path) in current
|
||||
with lock:
|
||||
if path.exists():
|
||||
raise StoreError(f"already exists: {key}")
|
||||
_atomic_write(path, value)
|
||||
process_context = _maybe_process_lock(path) if already_held else _process_lock(path)
|
||||
with process_context:
|
||||
token = _held_records.set(current | {str(path)})
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
_held_records.reset(token)
|
||||
|
||||
def create(self, value: dict[str, Any], *, key: str | None = None) -> dict[str, Any]:
|
||||
if not isinstance(value, dict):
|
||||
raise StoreError("records must be JSON objects")
|
||||
requested_key = (
|
||||
key
|
||||
if key is not None
|
||||
else value[self.key_attr]
|
||||
if self.key_attr in value
|
||||
else new_id(self.key_attr)
|
||||
)
|
||||
if not isinstance(requested_key, str):
|
||||
raise StoreError("invalid id")
|
||||
path = self._path(requested_key)
|
||||
key = requested_key
|
||||
value = dict(value)
|
||||
if self.key_attr in value and value[self.key_attr] != key:
|
||||
raise StoreError("record key does not match storage key")
|
||||
value[self.key_attr] = key
|
||||
lock = _locks.get(str(path))
|
||||
with lock:
|
||||
with _maybe_process_lock(path):
|
||||
if path.exists():
|
||||
raise StoreError(f"already exists: {key}")
|
||||
_atomic_write(path, value)
|
||||
return value
|
||||
|
||||
def get(self, key: str) -> dict[str, Any]:
|
||||
path = self._path(key)
|
||||
lock = _locks.get(str(path))
|
||||
with lock:
|
||||
if not path.exists():
|
||||
raise StoreError(f"not found: {key}")
|
||||
return _read_json(path)
|
||||
with _maybe_process_lock(path):
|
||||
if not path.exists():
|
||||
raise StoreNotFoundError(f"not found: {key}")
|
||||
try:
|
||||
return self._validate_record_key(key, _read_json(path))
|
||||
except FileNotFoundError as exc:
|
||||
raise StoreNotFoundError(f"not found: {key}") from exc
|
||||
|
||||
def get_or_none(self, key: str) -> dict[str, Any] | None:
|
||||
try:
|
||||
return self.get(key)
|
||||
except StoreError:
|
||||
except StoreNotFoundError:
|
||||
return None
|
||||
|
||||
def update(self, key: str, **fields: Any) -> dict[str, Any]:
|
||||
path = self._path(key)
|
||||
lock = _locks.get(str(path))
|
||||
with lock:
|
||||
if not path.exists():
|
||||
raise StoreError(f"not found: {key}")
|
||||
cur = _read_json(path)
|
||||
cur.update(fields)
|
||||
_atomic_write(path, cur)
|
||||
return cur
|
||||
with _maybe_process_lock(path):
|
||||
if not path.exists():
|
||||
raise StoreError(f"not found: {key}")
|
||||
cur = _read_json(path)
|
||||
self._validate_record_key(key, cur)
|
||||
cur.update(fields)
|
||||
self._validate_record_key(key, cur)
|
||||
_atomic_write(path, cur)
|
||||
return cur
|
||||
|
||||
def replace(self, key: str, value: dict[str, Any]) -> dict[str, Any]:
|
||||
def update_if(
|
||||
self,
|
||||
key: str,
|
||||
predicate: Callable[[dict[str, Any]], bool],
|
||||
**fields: Any,
|
||||
) -> dict[str, Any] | None:
|
||||
"""Update one record only when ``predicate`` matches its latest value.
|
||||
|
||||
The predicate check and write share the same per-record lock, allowing
|
||||
one-time state transitions to fail closed instead of relying on a
|
||||
stale read performed by the caller.
|
||||
"""
|
||||
path = self._path(key)
|
||||
lock = _locks.get(str(path))
|
||||
with lock:
|
||||
_atomic_write(path, value)
|
||||
return value
|
||||
with _maybe_process_lock(path):
|
||||
if not path.exists():
|
||||
raise StoreError(f"not found: {key}")
|
||||
cur = _read_json(path)
|
||||
self._validate_record_key(key, cur)
|
||||
if not predicate(cur):
|
||||
return None
|
||||
cur.update(fields)
|
||||
self._validate_record_key(key, cur)
|
||||
_atomic_write(path, cur)
|
||||
return cur
|
||||
|
||||
def replace(self, key: str, value: dict[str, Any]) -> dict[str, Any]:
|
||||
path = self._path(key)
|
||||
if not isinstance(value, dict):
|
||||
raise StoreError("records must be JSON objects")
|
||||
value = self._validate_record_key(key, value)
|
||||
lock = _locks.get(str(path))
|
||||
with lock:
|
||||
with _maybe_process_lock(path):
|
||||
_atomic_write(path, value)
|
||||
return value
|
||||
|
||||
def delete(self, key: str) -> None:
|
||||
path = self._path(key)
|
||||
lock = _locks.get(str(path))
|
||||
with lock:
|
||||
if path.exists():
|
||||
path.unlink()
|
||||
with _maybe_process_lock(path):
|
||||
if path.exists():
|
||||
path.unlink()
|
||||
|
||||
def all(self) -> list[dict[str, Any]]:
|
||||
out: list[dict[str, Any]] = []
|
||||
def iter_all(self) -> Iterator[dict[str, Any]]:
|
||||
for path in sorted(self.root.glob("*.json")):
|
||||
try:
|
||||
out.append(_read_json(path))
|
||||
except (OSError, ValueError):
|
||||
key = path.name[:-5]
|
||||
self._path(key)
|
||||
yield self._validate_record_key(key, _read_json(path))
|
||||
except FileNotFoundError:
|
||||
# A concurrent delete is a normal not-found race; corruption and
|
||||
# unreadable records must propagate rather than disappear.
|
||||
continue
|
||||
return out
|
||||
|
||||
def all(self) -> list[dict[str, Any]]:
|
||||
return list(self.iter_all())
|
||||
|
||||
def iter_where(self, pred: Callable[[dict[str, Any]], bool]) -> Iterator[dict[str, Any]]:
|
||||
for row in self.iter_all():
|
||||
if pred(row):
|
||||
yield row
|
||||
|
||||
def where(self, pred: Callable[[dict[str, Any]], bool]) -> list[dict[str, Any]]:
|
||||
return [row for row in self.all() if pred(row)]
|
||||
return list(self.iter_where(pred))
|
||||
@@ -0,0 +1,58 @@
|
||||
"""Alembic environment for the S4 relational schema foundation."""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from logging.config import fileConfig
|
||||
|
||||
from alembic import context
|
||||
from sqlalchemy import pool
|
||||
|
||||
from app.db import Base, create_db_engine, validate_database_url
|
||||
import app.models # noqa: F401 - register every model on Base.metadata
|
||||
|
||||
|
||||
config = context.config
|
||||
if config.config_file_name is not None:
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
target_metadata = Base.metadata
|
||||
|
||||
|
||||
def _database_url() -> str:
|
||||
return validate_database_url(os.environ.get("DATABASE_URL") or config.get_main_option("sqlalchemy.url"))
|
||||
|
||||
|
||||
def run_migrations_offline() -> None:
|
||||
context.configure(
|
||||
url=_database_url(),
|
||||
target_metadata=target_metadata,
|
||||
literal_binds=True,
|
||||
dialect_opts={"paramstyle": "named"},
|
||||
compare_type=True,
|
||||
)
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def run_migrations_online() -> None:
|
||||
configuration = config.get_section(config.config_ini_section, {})
|
||||
configuration["sqlalchemy.url"] = _database_url()
|
||||
connectable = create_db_engine(
|
||||
configuration["sqlalchemy.url"],
|
||||
poolclass=pool.NullPool,
|
||||
)
|
||||
with connectable.connect() as connection:
|
||||
context.configure(
|
||||
connection=connection,
|
||||
target_metadata=target_metadata,
|
||||
compare_type=True,
|
||||
render_as_batch=connection.dialect.name == "sqlite",
|
||||
)
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
||||
@@ -0,0 +1,176 @@
|
||||
"""Create the tenant-safe relational schema foundation.
|
||||
|
||||
Revision ID: 0001_initial_schema
|
||||
Revises:
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
revision = "0001_initial_schema"
|
||||
down_revision = None
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.create_table(
|
||||
"organizations",
|
||||
sa.Column("id", sa.String(length=64), nullable=False),
|
||||
sa.Column("name", sa.String(length=200), nullable=False),
|
||||
sa.Column("plan", sa.String(length=64), nullable=False, server_default=sa.text("'trial'")),
|
||||
sa.Column("seats", sa.Integer(), nullable=False, server_default=sa.text("5")),
|
||||
sa.Column("active", sa.Boolean(), nullable=False, server_default=sa.true()),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
)
|
||||
op.create_table(
|
||||
"users",
|
||||
sa.Column("id", sa.String(length=64), nullable=False),
|
||||
sa.Column("org_id", sa.String(length=64), nullable=False),
|
||||
sa.Column("username", sa.String(length=160), nullable=False),
|
||||
sa.Column("password_hash", sa.String(length=512), nullable=False),
|
||||
sa.Column("email", sa.String(length=320), nullable=True),
|
||||
sa.Column("name", sa.String(length=200), nullable=False),
|
||||
sa.Column("role", sa.String(length=32), nullable=False),
|
||||
sa.Column("active", sa.Boolean(), nullable=False, server_default=sa.true()),
|
||||
sa.Column("must_setup", sa.Boolean(), nullable=False, server_default=sa.false()),
|
||||
sa.Column("accepted_terms", sa.Boolean(), nullable=False, server_default=sa.false()),
|
||||
sa.Column("accepted_terms_at", sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column("auth_version", sa.Integer(), nullable=False, server_default=sa.text("0")),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
|
||||
sa.ForeignKeyConstraint(["org_id"], ["organizations.id"], ondelete="CASCADE"),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
sa.UniqueConstraint("email", name="uq_users_email"),
|
||||
sa.UniqueConstraint("username", name="uq_users_username"),
|
||||
sa.UniqueConstraint("org_id", "id", name="uq_users_org_id_id"),
|
||||
)
|
||||
op.create_table(
|
||||
"groups",
|
||||
sa.Column("id", sa.String(length=64), nullable=False),
|
||||
sa.Column("org_id", sa.String(length=64), nullable=False),
|
||||
sa.Column("owner_user_id", sa.String(length=64), nullable=True),
|
||||
sa.Column("creator_user_id", sa.String(length=64), nullable=True),
|
||||
sa.Column("name", sa.String(length=200), nullable=False),
|
||||
sa.Column("status", sa.String(length=32), nullable=False, server_default=sa.text("'draft'")),
|
||||
sa.Column("input_json", sa.JSON(), nullable=True),
|
||||
sa.Column("sales_kit_json", sa.JSON(), nullable=True),
|
||||
sa.Column("report_text", sa.Text(), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
|
||||
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
|
||||
sa.ForeignKeyConstraint(["org_id"], ["organizations.id"], ondelete="CASCADE"),
|
||||
sa.ForeignKeyConstraint(
|
||||
["org_id", "owner_user_id"],
|
||||
["users.org_id", "users.id"],
|
||||
name="fk_groups_owner_same_org",
|
||||
ondelete="RESTRICT",
|
||||
),
|
||||
sa.ForeignKeyConstraint(
|
||||
["org_id", "creator_user_id"],
|
||||
["users.org_id", "users.id"],
|
||||
name="fk_groups_creator_same_org",
|
||||
ondelete="RESTRICT",
|
||||
),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
sa.UniqueConstraint("org_id", "id", name="uq_groups_org_id_id"),
|
||||
)
|
||||
op.create_table(
|
||||
"personas",
|
||||
sa.Column("id", sa.String(length=64), nullable=False),
|
||||
sa.Column("group_id", sa.String(length=64), nullable=False),
|
||||
sa.Column("source_persona_id", sa.String(length=64), nullable=True),
|
||||
sa.Column("tier", sa.String(length=32), nullable=False),
|
||||
sa.Column("public_json", sa.JSON(), nullable=False),
|
||||
sa.Column("latent_json", sa.JSON(), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
|
||||
sa.ForeignKeyConstraint(["group_id"], ["groups.id"], ondelete="CASCADE"),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
sa.UniqueConstraint("group_id", "id", name="uq_personas_group_id_id"),
|
||||
)
|
||||
op.create_table(
|
||||
"sessions",
|
||||
sa.Column("id", sa.String(length=64), nullable=False),
|
||||
sa.Column("org_id", sa.String(length=64), nullable=False),
|
||||
sa.Column("user_id", sa.String(length=64), nullable=False),
|
||||
sa.Column("group_id", sa.String(length=64), nullable=False),
|
||||
sa.Column("persona_id", sa.String(length=64), nullable=False),
|
||||
sa.Column("mode", sa.String(length=32), nullable=False, server_default=sa.text("'trainee'")),
|
||||
sa.Column("status", sa.String(length=32), nullable=False, server_default=sa.text("'active'")),
|
||||
sa.Column("outcome", sa.String(length=32), nullable=True),
|
||||
sa.Column("scenario_json", sa.JSON(), nullable=True),
|
||||
sa.Column("internal_json", sa.JSON(), nullable=True),
|
||||
sa.Column("debrief_json", sa.JSON(), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
|
||||
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
|
||||
sa.ForeignKeyConstraint(["org_id"], ["organizations.id"], ondelete="CASCADE"),
|
||||
sa.ForeignKeyConstraint(
|
||||
["org_id", "user_id"],
|
||||
["users.org_id", "users.id"],
|
||||
name="fk_sessions_user_same_org",
|
||||
ondelete="RESTRICT",
|
||||
),
|
||||
sa.ForeignKeyConstraint(
|
||||
["org_id", "group_id"],
|
||||
["groups.org_id", "groups.id"],
|
||||
name="fk_sessions_group_same_org",
|
||||
ondelete="RESTRICT",
|
||||
),
|
||||
sa.ForeignKeyConstraint(
|
||||
["group_id", "persona_id"],
|
||||
["personas.group_id", "personas.id"],
|
||||
name="fk_sessions_persona_in_group",
|
||||
ondelete="RESTRICT",
|
||||
),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
)
|
||||
op.create_index(
|
||||
"uq_sessions_non_preview_attempt",
|
||||
"sessions",
|
||||
["org_id", "user_id", "group_id", "persona_id"],
|
||||
unique=True,
|
||||
sqlite_where=sa.text("mode <> 'preview'"),
|
||||
postgresql_where=sa.text("mode <> 'preview'"),
|
||||
)
|
||||
op.create_table(
|
||||
"messages",
|
||||
sa.Column("id", sa.String(length=64), nullable=False),
|
||||
sa.Column("session_id", sa.String(length=64), nullable=False),
|
||||
sa.Column("sequence", sa.Integer(), nullable=False),
|
||||
sa.Column("role", sa.String(length=32), nullable=False),
|
||||
sa.Column("text", sa.Text(), nullable=False),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
|
||||
sa.ForeignKeyConstraint(["session_id"], ["sessions.id"], ondelete="CASCADE"),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
sa.UniqueConstraint("session_id", "sequence", name="uq_messages_session_sequence"),
|
||||
)
|
||||
op.create_table(
|
||||
"audit_events",
|
||||
sa.Column("id", sa.BigInteger().with_variant(sa.Integer(), "sqlite"), autoincrement=True, nullable=False),
|
||||
sa.Column("org_id", sa.String(length=64), nullable=True),
|
||||
sa.Column("actor_user_id", sa.String(length=64), nullable=True),
|
||||
sa.Column("action", sa.String(length=160), nullable=False),
|
||||
sa.Column("subject", sa.String(length=200), nullable=True),
|
||||
sa.Column("detail_json", sa.JSON(), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
|
||||
sa.ForeignKeyConstraint(["org_id"], ["organizations.id"], ondelete="SET NULL"),
|
||||
sa.ForeignKeyConstraint(
|
||||
["org_id", "actor_user_id"],
|
||||
["users.org_id", "users.id"],
|
||||
name="fk_audit_actor_same_org",
|
||||
ondelete="SET NULL",
|
||||
),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_table("audit_events")
|
||||
op.drop_table("messages")
|
||||
op.drop_index("uq_sessions_non_preview_attempt", table_name="sessions")
|
||||
op.drop_table("sessions")
|
||||
op.drop_table("personas")
|
||||
op.drop_table("groups")
|
||||
op.drop_table("users")
|
||||
op.drop_table("organizations")
|
||||
@@ -0,0 +1,74 @@
|
||||
"""Harden audit actor tenant attribution.
|
||||
|
||||
Revision ID: 0002_audit_actor_tenant_check
|
||||
Revises: 0001_initial_schema
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
revision = "0002_audit_actor_tenant_check"
|
||||
down_revision = "0001_initial_schema"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
_CONSTRAINT_NAME = "ck_audit_actor_requires_org"
|
||||
_CONSTRAINT_SQL = "actor_user_id IS NULL OR org_id IS NOT NULL"
|
||||
|
||||
|
||||
def _audit_events_table(*, include_tenant_check: bool = False) -> sa.Table:
|
||||
metadata = sa.MetaData()
|
||||
constraints: list[sa.Constraint] = [
|
||||
sa.ForeignKeyConstraint(
|
||||
["org_id"],
|
||||
["organizations.id"],
|
||||
ondelete="SET NULL",
|
||||
),
|
||||
sa.ForeignKeyConstraint(
|
||||
["org_id", "actor_user_id"],
|
||||
["users.org_id", "users.id"],
|
||||
name="fk_audit_actor_same_org",
|
||||
ondelete="SET NULL",
|
||||
),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
]
|
||||
if include_tenant_check:
|
||||
constraints.append(sa.CheckConstraint(_CONSTRAINT_SQL, name=_CONSTRAINT_NAME))
|
||||
return sa.Table(
|
||||
"audit_events",
|
||||
metadata,
|
||||
sa.Column(
|
||||
"id",
|
||||
sa.BigInteger().with_variant(sa.Integer(), "sqlite"),
|
||||
autoincrement=True,
|
||||
nullable=False,
|
||||
),
|
||||
sa.Column("org_id", sa.String(length=64), nullable=True),
|
||||
sa.Column("actor_user_id", sa.String(length=64), nullable=True),
|
||||
sa.Column("action", sa.String(length=160), nullable=False),
|
||||
sa.Column("subject", sa.String(length=200), nullable=True),
|
||||
sa.Column("detail_json", sa.JSON(), nullable=True),
|
||||
sa.Column(
|
||||
"created_at",
|
||||
sa.DateTime(timezone=True),
|
||||
nullable=False,
|
||||
server_default=sa.func.now(),
|
||||
),
|
||||
*constraints,
|
||||
)
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
with op.batch_alter_table("audit_events", copy_from=_audit_events_table()) as batch_op:
|
||||
batch_op.create_check_constraint(_CONSTRAINT_NAME, _CONSTRAINT_SQL)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
with op.batch_alter_table(
|
||||
"audit_events",
|
||||
copy_from=_audit_events_table(include_tenant_check=True),
|
||||
) as batch_op:
|
||||
batch_op.drop_constraint(_CONSTRAINT_NAME, type_="check")
|
||||
@@ -0,0 +1,4 @@
|
||||
[pytest]
|
||||
testpaths = tests
|
||||
pythonpath = .
|
||||
addopts = -ra
|
||||
@@ -0,0 +1,810 @@
|
||||
# This file was autogenerated by uv via the following command:
|
||||
# uv pip compile --generate-hashes --universal --python-version 3.11 --output-file backend/requirements.lock.txt backend/requirements.txt
|
||||
alembic==1.19.1 \
|
||||
--hash=sha256:b39018cb3d9413a19cbd54cf3c02ad33998641f0538eb77413a488a21c3e14be \
|
||||
--hash=sha256:e0fca0518118c78acc493e31bcb5402f190057aaf6df8b5b95ce94c4789cf648
|
||||
# via -r backend/requirements.txt
|
||||
annotated-types==0.8.0 \
|
||||
--hash=sha256:13b2beaad985e05e2d6407ee4c4f35590b11f8d693a258a561055cac8f64cab7 \
|
||||
--hash=sha256:f072f4d804ea359e4eaf198b1af7a8b0943881a87f31bb764f8bf219bb9419e0
|
||||
# via pydantic
|
||||
anyio==4.14.2 \
|
||||
--hash=sha256:9f505dda5ac9f0c8309b5e8bd445a8c2bf7246f3ce950121e45ea15bc41d1494 \
|
||||
--hash=sha256:cfa139f3ed1a23ee8f88a145ddb5ac7605b8bbfd8592baacd7ce3d8bb4313c7f
|
||||
# via
|
||||
# httpx
|
||||
# openai
|
||||
blinker==1.9.0 \
|
||||
--hash=sha256:b4ce2265a7abece45e7cc896e98dbebe6cead56bcf805a3d23136d145f5445bf \
|
||||
--hash=sha256:ba0efaa9080b619ff2f3459d1d500c57bddea4a6b424b60a91141db6fd2f08bc
|
||||
# via flask
|
||||
certifi==2026.7.22 \
|
||||
--hash=sha256:62f22742b58a1a33014a2b6b706588a8d7e2a88ae7bd1a6ebe8c992928483775 \
|
||||
--hash=sha256:741e2c3b351ddf169a738da9f2c048608ff7f2c5cc02f1ebc6b118bb090d5d55
|
||||
# via
|
||||
# httpcore
|
||||
# httpx
|
||||
charset-normalizer==3.4.4 \
|
||||
--hash=sha256:027f6de494925c0ab2a55eab46ae5129951638a49a34d87f4c3eda90f696b4ad \
|
||||
--hash=sha256:077fbb858e903c73f6c9db43374fd213b0b6a778106bc7032446a8e8b5b38b93 \
|
||||
--hash=sha256:0a98e6759f854bd25a58a73fa88833fba3b7c491169f86ce1180c948ab3fd394 \
|
||||
--hash=sha256:0d3d8f15c07f86e9ff82319b3d9ef6f4bf907608f53fe9d92b28ea9ae3d1fd89 \
|
||||
--hash=sha256:0f04b14ffe5fdc8c4933862d8306109a2c51e0704acfa35d51598eb45a1e89fc \
|
||||
--hash=sha256:11d694519d7f29d6cd09f6ac70028dba10f92f6cdd059096db198c283794ac86 \
|
||||
--hash=sha256:194f08cbb32dc406d6e1aea671a68be0823673db2832b38405deba2fb0d88f63 \
|
||||
--hash=sha256:1bee1e43c28aa63cb16e5c14e582580546b08e535299b8b6158a7c9c768a1f3d \
|
||||
--hash=sha256:21d142cc6c0ec30d2efee5068ca36c128a30b0f2c53c1c07bd78cb6bc1d3be5f \
|
||||
--hash=sha256:2437418e20515acec67d86e12bf70056a33abdacb5cb1655042f6538d6b085a8 \
|
||||
--hash=sha256:244bfb999c71b35de57821b8ea746b24e863398194a4014e4c76adc2bbdfeff0 \
|
||||
--hash=sha256:2677acec1a2f8ef614c6888b5b4ae4060cc184174a938ed4e8ef690e15d3e505 \
|
||||
--hash=sha256:277e970e750505ed74c832b4bf75dac7476262ee2a013f5574dd49075879e161 \
|
||||
--hash=sha256:2aaba3b0819274cc41757a1da876f810a3e4d7b6eb25699253a4effef9e8e4af \
|
||||
--hash=sha256:2b7d8f6c26245217bd2ad053761201e9f9680f8ce52f0fcd8d0755aeae5b2152 \
|
||||
--hash=sha256:2c9d3c380143a1fedbff95a312aa798578371eb29da42106a29019368a475318 \
|
||||
--hash=sha256:3162d5d8ce1bb98dd51af660f2121c55d0fa541b46dff7bb9b9f86ea1d87de72 \
|
||||
--hash=sha256:31fd66405eaf47bb62e8cd575dc621c56c668f27d46a61d975a249930dd5e2a4 \
|
||||
--hash=sha256:362d61fd13843997c1c446760ef36f240cf81d3ebf74ac62652aebaf7838561e \
|
||||
--hash=sha256:376bec83a63b8021bb5c8ea75e21c4ccb86e7e45ca4eb81146091b56599b80c3 \
|
||||
--hash=sha256:44c2a8734b333e0578090c4cd6b16f275e07aa6614ca8715e6c038e865e70576 \
|
||||
--hash=sha256:47cc91b2f4dd2833fddaedd2893006b0106129d4b94fdb6af1f4ce5a9965577c \
|
||||
--hash=sha256:4902828217069c3c5c71094537a8e623f5d097858ac6ca8252f7b4d10b7560f1 \
|
||||
--hash=sha256:4bd5d4137d500351a30687c2d3971758aac9a19208fc110ccb9d7188fbe709e8 \
|
||||
--hash=sha256:4fe7859a4e3e8457458e2ff592f15ccb02f3da787fcd31e0183879c3ad4692a1 \
|
||||
--hash=sha256:542d2cee80be6f80247095cc36c418f7bddd14f4a6de45af91dfad36d817bba2 \
|
||||
--hash=sha256:554af85e960429cf30784dd47447d5125aaa3b99a6f0683589dbd27e2f45da44 \
|
||||
--hash=sha256:5833d2c39d8896e4e19b689ffc198f08ea58116bee26dea51e362ecc7cd3ed26 \
|
||||
--hash=sha256:5947809c8a2417be3267efc979c47d76a079758166f7d43ef5ae8e9f92751f88 \
|
||||
--hash=sha256:5ae497466c7901d54b639cf42d5b8c1b6a4fead55215500d2f486d34db48d016 \
|
||||
--hash=sha256:5bd2293095d766545ec1a8f612559f6b40abc0eb18bb2f5d1171872d34036ede \
|
||||
--hash=sha256:5bfbb1b9acf3334612667b61bd3002196fe2a1eb4dd74d247e0f2a4d50ec9bbf \
|
||||
--hash=sha256:5cb4d72eea50c8868f5288b7f7f33ed276118325c1dfd3957089f6b519e1382a \
|
||||
--hash=sha256:5dbe56a36425d26d6cfb40ce79c314a2e4dd6211d51d6d2191c00bed34f354cc \
|
||||
--hash=sha256:5f819d5fe9234f9f82d75bdfa9aef3a3d72c4d24a6e57aeaebba32a704553aa0 \
|
||||
--hash=sha256:64b55f9dce520635f018f907ff1b0df1fdc31f2795a922fb49dd14fbcdf48c84 \
|
||||
--hash=sha256:6515f3182dbe4ea06ced2d9e8666d97b46ef4c75e326b79bb624110f122551db \
|
||||
--hash=sha256:65e2befcd84bc6f37095f5961e68a6f077bf44946771354a28ad434c2cce0ae1 \
|
||||
--hash=sha256:6aee717dcfead04c6eb1ce3bd29ac1e22663cdea57f943c87d1eab9a025438d7 \
|
||||
--hash=sha256:6b39f987ae8ccdf0d2642338faf2abb1862340facc796048b604ef14919e55ed \
|
||||
--hash=sha256:6e1fcf0720908f200cd21aa4e6750a48ff6ce4afe7ff5a79a90d5ed8a08296f8 \
|
||||
--hash=sha256:74018750915ee7ad843a774364e13a3db91682f26142baddf775342c3f5b1133 \
|
||||
--hash=sha256:74664978bb272435107de04e36db5a9735e78232b85b77d45cfb38f758efd33e \
|
||||
--hash=sha256:74bb723680f9f7a6234dcf67aea57e708ec1fbdf5699fb91dfd6f511b0a320ef \
|
||||
--hash=sha256:752944c7ffbfdd10c074dc58ec2d5a8a4cd9493b314d367c14d24c17684ddd14 \
|
||||
--hash=sha256:778d2e08eda00f4256d7f672ca9fef386071c9202f5e4607920b86d7803387f2 \
|
||||
--hash=sha256:780236ac706e66881f3b7f2f32dfe90507a09e67d1d454c762cf642e6e1586e0 \
|
||||
--hash=sha256:798d75d81754988d2565bff1b97ba5a44411867c0cf32b77a7e8f8d84796b10d \
|
||||
--hash=sha256:799a7a5e4fb2d5898c60b640fd4981d6a25f1c11790935a44ce38c54e985f828 \
|
||||
--hash=sha256:7a32c560861a02ff789ad905a2fe94e3f840803362c84fecf1851cb4cf3dc37f \
|
||||
--hash=sha256:7c308f7e26e4363d79df40ca5b2be1c6ba9f02bdbccfed5abddb7859a6ce72cf \
|
||||
--hash=sha256:7fa17817dc5625de8a027cb8b26d9fefa3ea28c8253929b8d6649e705d2835b6 \
|
||||
--hash=sha256:81d5eb2a312700f4ecaa977a8235b634ce853200e828fbadf3a9c50bab278328 \
|
||||
--hash=sha256:82004af6c302b5d3ab2cfc4cc5f29db16123b1a8417f2e25f9066f91d4411090 \
|
||||
--hash=sha256:837c2ce8c5a65a2035be9b3569c684358dfbf109fd3b6969630a87535495ceaa \
|
||||
--hash=sha256:840c25fb618a231545cbab0564a799f101b63b9901f2569faecd6b222ac72381 \
|
||||
--hash=sha256:8a6562c3700cce886c5be75ade4a5db4214fda19fede41d9792d100288d8f94c \
|
||||
--hash=sha256:8af65f14dc14a79b924524b1e7fffe304517b2bff5a58bf64f30b98bbc5079eb \
|
||||
--hash=sha256:8ef3c867360f88ac904fd3f5e1f902f13307af9052646963ee08ff4f131adafc \
|
||||
--hash=sha256:94537985111c35f28720e43603b8e7b43a6ecfb2ce1d3058bbe955b73404e21a \
|
||||
--hash=sha256:99ae2cffebb06e6c22bdc25801d7b30f503cc87dbd283479e7b606f70aff57ec \
|
||||
--hash=sha256:9a26f18905b8dd5d685d6d07b0cdf98a79f3c7a918906af7cc143ea2e164c8bc \
|
||||
--hash=sha256:9b35f4c90079ff2e2edc5b26c0c77925e5d2d255c42c74fdb70fb49b172726ac \
|
||||
--hash=sha256:9cd98cdc06614a2f768d2b7286d66805f94c48cde050acdbbb7db2600ab3197e \
|
||||
--hash=sha256:9d1bb833febdff5c8927f922386db610b49db6e0d4f4ee29601d71e7c2694313 \
|
||||
--hash=sha256:9f7fcd74d410a36883701fafa2482a6af2ff5ba96b9a620e9e0721e28ead5569 \
|
||||
--hash=sha256:a59cb51917aa591b1c4e6a43c132f0cdc3c76dbad6155df4e28ee626cc77a0a3 \
|
||||
--hash=sha256:a61900df84c667873b292c3de315a786dd8dac506704dea57bc957bd31e22c7d \
|
||||
--hash=sha256:a79cfe37875f822425b89a82333404539ae63dbdddf97f84dcbc3d339aae9525 \
|
||||
--hash=sha256:a8a8b89589086a25749f471e6a900d3f662d1d3b6e2e59dcecf787b1cc3a1894 \
|
||||
--hash=sha256:a8bf8d0f749c5757af2142fe7903a9df1d2e8aa3841559b2bad34b08d0e2bcf3 \
|
||||
--hash=sha256:a9768c477b9d7bd54bc0c86dbaebdec6f03306675526c9927c0e8a04e8f94af9 \
|
||||
--hash=sha256:ac1c4a689edcc530fc9d9aa11f5774b9e2f33f9a0c6a57864e90908f5208d30a \
|
||||
--hash=sha256:af2d8c67d8e573d6de5bc30cdb27e9b95e49115cd9baad5ddbd1a6207aaa82a9 \
|
||||
--hash=sha256:b435cba5f4f750aa6c0a0d92c541fb79f69a387c91e61f1795227e4ed9cece14 \
|
||||
--hash=sha256:b5b290ccc2a263e8d185130284f8501e3e36c5e02750fc6b6bdeb2e9e96f1e25 \
|
||||
--hash=sha256:b5d84d37db046c5ca74ee7bb47dd6cbc13f80665fdde3e8040bdd3fb015ecb50 \
|
||||
--hash=sha256:b7cf1017d601aa35e6bb650b6ad28652c9cd78ee6caff19f3c28d03e1c80acbf \
|
||||
--hash=sha256:bc7637e2f80d8530ee4a78e878bce464f70087ce73cf7c1caf142416923b98f1 \
|
||||
--hash=sha256:c0463276121fdee9c49b98908b3a89c39be45d86d1dbaa22957e38f6321d4ce3 \
|
||||
--hash=sha256:c4ef880e27901b6cc782f1b95f82da9313c0eb95c3af699103088fa0ac3ce9ac \
|
||||
--hash=sha256:c8ae8a0f02f57a6e61203a31428fa1d677cbe50c93622b4149d5c0f319c1d19e \
|
||||
--hash=sha256:ca5862d5b3928c4940729dacc329aa9102900382fea192fc5e52eb69d6093815 \
|
||||
--hash=sha256:cb01158d8b88ee68f15949894ccc6712278243d95f344770fa7593fa2d94410c \
|
||||
--hash=sha256:cb6254dc36b47a990e59e1068afacdcd02958bdcce30bb50cc1700a8b9d624a6 \
|
||||
--hash=sha256:cc00f04ed596e9dc0da42ed17ac5e596c6ccba999ba6bd92b0e0aef2f170f2d6 \
|
||||
--hash=sha256:cd09d08005f958f370f539f186d10aec3377d55b9eeb0d796025d4886119d76e \
|
||||
--hash=sha256:cd4b7ca9984e5e7985c12bc60a6f173f3c958eae74f3ef6624bb6b26e2abbae4 \
|
||||
--hash=sha256:ce8a0633f41a967713a59c4139d29110c07e826d131a316b50ce11b1d79b4f84 \
|
||||
--hash=sha256:cead0978fc57397645f12578bfd2d5ea9138ea0fac82b2f63f7f7c6877986a69 \
|
||||
--hash=sha256:d055ec1e26e441f6187acf818b73564e6e6282709e9bcb5b63f5b23068356a15 \
|
||||
--hash=sha256:d1f13550535ad8cff21b8d757a3257963e951d96e20ec82ab44bc64aeb62a191 \
|
||||
--hash=sha256:d9c7f57c3d666a53421049053eaacdd14bbd0a528e2186fcb2e672effd053bb0 \
|
||||
--hash=sha256:d9e45d7faa48ee908174d8fe84854479ef838fc6a705c9315372eacbc2f02897 \
|
||||
--hash=sha256:da3326d9e65ef63a817ecbcc0df6e94463713b754fe293eaa03da99befb9a5bd \
|
||||
--hash=sha256:de00632ca48df9daf77a2c65a484531649261ec9f25489917f09e455cb09ddb2 \
|
||||
--hash=sha256:e1f185f86a6f3403aa2420e815904c67b2f9ebc443f045edd0de921108345794 \
|
||||
--hash=sha256:e824f1492727fa856dd6eda4f7cee25f8518a12f3c4a56a74e8095695089cf6d \
|
||||
--hash=sha256:e912091979546adf63357d7e2ccff9b44f026c075aeaf25a52d0e95ad2281074 \
|
||||
--hash=sha256:eaabd426fe94daf8fd157c32e571c85cb12e66692f15516a83a03264b08d06c3 \
|
||||
--hash=sha256:ebf3e58c7ec8a8bed6d66a75d7fb37b55e5015b03ceae72a8e7c74495551e224 \
|
||||
--hash=sha256:ecaae4149d99b1c9e7b88bb03e3221956f68fd6d50be2ef061b2381b61d20838 \
|
||||
--hash=sha256:eecbc200c7fd5ddb9a7f16c7decb07b566c29fa2161a16cf67b8d068bd21690a \
|
||||
--hash=sha256:f155a433c2ec037d4e8df17d18922c3a0d9b3232a396690f17175d2946f0218d \
|
||||
--hash=sha256:f1e34719c6ed0b92f418c7c780480b26b5d9c50349e9a9af7d76bf757530350d \
|
||||
--hash=sha256:f34be2938726fc13801220747472850852fe6b1ea75869a048d6f896838c896f \
|
||||
--hash=sha256:f820802628d2694cb7e56db99213f930856014862f3fd943d290ea8438d07ca8 \
|
||||
--hash=sha256:f8bf04158c6b607d747e93949aa60618b61312fe647a6369f88ce2ff16043490 \
|
||||
--hash=sha256:f8e160feb2aed042cd657a72acc0b481212ed28b1b9a95c0cee1621b524e1966 \
|
||||
--hash=sha256:f9d332f8c2a2fcbffe1378594431458ddbef721c1769d78e2cbc06280d8155f9 \
|
||||
--hash=sha256:fa09f53c465e532f4d3db095e0c55b615f010ad81803d383195b6b5ca6cbf5f3 \
|
||||
--hash=sha256:faa3a41b2b66b6e50f84ae4a68c64fcd0c44355741c6374813a800cd6695db9e \
|
||||
--hash=sha256:fd44c878ea55ba351104cb93cc85e74916eb8fa440ca7903e57575e97394f608
|
||||
# via -r backend/requirements.txt
|
||||
click==8.4.2 \
|
||||
--hash=sha256:9a6cea6e60b17ebe0a44c5cc636d94f09bd66142c1cd7d8b4cd731c4917a15f6 \
|
||||
--hash=sha256:e6f9f66136c816745b9d65817da91d61d957fb16e02e4dcd0552553c5a197b76
|
||||
# via flask
|
||||
colorama==0.4.6 ; sys_platform == 'win32' \
|
||||
--hash=sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44 \
|
||||
--hash=sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6
|
||||
# via
|
||||
# click
|
||||
# pytest
|
||||
# tqdm
|
||||
distro==1.9.0 \
|
||||
--hash=sha256:2fa77c6fd8940f116ee1d6b94a2f90b13b5ea8d019b98bc8bafdcabcdd9bdbed \
|
||||
--hash=sha256:7bffd925d65168f85027d8da9af6bddab658135b840670a223589bc0c8ef02b2
|
||||
# via openai
|
||||
flask==3.1.3 \
|
||||
--hash=sha256:0ef0e52b8a9cd932855379197dd8f94047b359ca0a78695144304cb45f87c9eb \
|
||||
--hash=sha256:f4bcbefc124291925f1a26446da31a5178f9483862233b23c0c96a20701f670c
|
||||
# via
|
||||
# -r backend/requirements.txt
|
||||
# flask-cors
|
||||
flask-cors==6.0.5 \
|
||||
--hash=sha256:30c5031552cd59f620ac0c8211dac45b345d3b2df310e7721879e4f46ef9c601 \
|
||||
--hash=sha256:68fcf75693e961f3af26683b23c4b9a8fb6b64de17d20d0c37b95e8de7ab2ed8
|
||||
# via -r backend/requirements.txt
|
||||
greenlet==3.5.5 ; platform_machine == 'AMD64' or platform_machine == 'WIN32' or platform_machine == 'aarch64' or platform_machine == 'amd64' or platform_machine == 'ppc64le' or platform_machine == 'win32' or platform_machine == 'x86_64' \
|
||||
--hash=sha256:03115c2e0a371999bf8ae616aa8d653f96641d4705c457aebaa187276e9f7537 \
|
||||
--hash=sha256:03551ed792cb1b4fc0277a0c60dfd8c343894a0ba06fe60dcd22f568b433da39 \
|
||||
--hash=sha256:0e5a7de979d764aea1f5b6e95cf92b5b37741b9823702041f34b126e7f690277 \
|
||||
--hash=sha256:102817506f6090b5176c746a82603341a549b40e5c3d5b72a4c672228a918c41 \
|
||||
--hash=sha256:12e2ee66c2aba86133f10fd99d6a8856c6d351ffb7be0e4d52ef2cc5fbb705b2 \
|
||||
--hash=sha256:147b25a42e5ca5be3d42356e8f608b37af715a1c196e9bf9d1627f3341adfe1d \
|
||||
--hash=sha256:159df1942d88e8f784cbb38d6f18bdb365cd11319cfbb3e89623de2b97892d53 \
|
||||
--hash=sha256:182de51c6b572a705f2fafaab2e783bcf7d2760940229dfe73086cbae037af3e \
|
||||
--hash=sha256:19d59f068887d8c5907fc177f27683413ace3011b6ed646c0b309266e74a6502 \
|
||||
--hash=sha256:19e4e026fe20691f333b8eb1a3bc9625eceba8c3f9d62ec5a6f8581afbc6b5a5 \
|
||||
--hash=sha256:1af90aa4bc129883b340cdd6957a3bc74f60528a4993bbd1f53aaebe1d9981cc \
|
||||
--hash=sha256:1b5ed9162c0c098e0bbc2cf88a94f433c1b8926f831745252e099e5d83e17759 \
|
||||
--hash=sha256:1e8d9391fe77f15649589a907cef972dbbd6352ef7ff7dc0492f658c0c26495f \
|
||||
--hash=sha256:27493374cff1d1b7919dc8126547f2aea582737e3046147b434b1e12de56389b \
|
||||
--hash=sha256:2888a3a38bc5ee5bb6c438372197152e815837e4fab7ed7a1f86ef18ffd58ad1 \
|
||||
--hash=sha256:2b70a766135540c472ac1393d57c2e1b4a2eb85bf526a1e41e6d096173a8cee5 \
|
||||
--hash=sha256:2d57406c3efd32d7a81e17a674314e8bd00792cdab49ea3228a49aa1bfb2e769 \
|
||||
--hash=sha256:2eabb980975cba5b93a95f6f69287d05fc05ac955bfd6a320a7c083eeb52c0b0 \
|
||||
--hash=sha256:3134291427bb0f3526e9d90311988caf336eb43730e95244997a4fb15f45144f \
|
||||
--hash=sha256:35cbb8bf55ace57fbccb4fb8622c4521713acd8691e77f4696d416ea7ca527da \
|
||||
--hash=sha256:37faa97daccb6d9f4c2141ce3118d023c3c5506864a7d8bdf726f665018c1f76 \
|
||||
--hash=sha256:40239b5384f96da3963585cc6d7eaa9b56f8ae67e8d92cc82dd9e202fc847de3 \
|
||||
--hash=sha256:4441153ffba21b90d3ca89fe3d31f5c093ae6c0bf0cfdfc98f54cde22f95b62e \
|
||||
--hash=sha256:44f08341873200ba8a60a8bc14ace3d91f1754f7fa7bc66157714a8cd420a476 \
|
||||
--hash=sha256:469dbb0a78625642f4a626cfd0c6e8bccc0385b5e49189b6308bbe849ec88a8e \
|
||||
--hash=sha256:49520f0c95a48b42cf55414b8e8479beb274ea70431afc33e3f79903c71f4380 \
|
||||
--hash=sha256:499adea519f748407fc6806d20eedabac2884fd73b9f38d81236e190ba20dfef \
|
||||
--hash=sha256:49ddacd36af37735fab103846f4ee4d18a492dde72730d1699c0c8ebe30d9f18 \
|
||||
--hash=sha256:4dfc7c4470354e7b09184d1a3a985761053a2fd694ddb5b5c80242afc2c8c90b \
|
||||
--hash=sha256:5173a72310725a74afc82c164f0e52cb8ad0de62f2bb623f24f6c0cc07d80272 \
|
||||
--hash=sha256:523bb8e27614d77101ea7a8cf59f8d91219b72d5c29f6a038c92b50828bfa8d0 \
|
||||
--hash=sha256:55272212cbc5f43d1d723725ab931f1939969b7e9523882ca58b55061769d053 \
|
||||
--hash=sha256:5e2afcfc4d4305dd715809b03da5cbe437c8984f61d8917751eb5fe4aefa3e07 \
|
||||
--hash=sha256:5e9ec2e7c98e895fcea0c5cc57b2606cf86ece6d0a56578f3eb225e2af4f0387 \
|
||||
--hash=sha256:5f1b1ff4828cdc1aba4266aff814085d04a1d07959287219af021b838b265d52 \
|
||||
--hash=sha256:634cf15a233a949136879dd388e25d3296e16f3f1e217d2456797b8579ebc6ed \
|
||||
--hash=sha256:655bca754a2ef4efcb0eb48a94d3f4593536d0f3d48f8ed44343c01d16a92f95 \
|
||||
--hash=sha256:68184dfcf50ccaa8e864770fe0633a7e27250ea9329f8192ef47ee9ecfd78e1c \
|
||||
--hash=sha256:6b241c32f912ada659808d68e308c568baf577eebf757d15471472de0c18cfad \
|
||||
--hash=sha256:6ca5d6ae0739e5764f2cfcfaa562ac5a990cbdaedca93251c5e3cf07c362371f \
|
||||
--hash=sha256:6d9b454c5fc48aeaa7c4337813dbf513a6870468e426438a04d922c6d0fe63db \
|
||||
--hash=sha256:70b157cd319873e8b544ddc2de158f55bbd0a9b0218c8ce9332039801518e328 \
|
||||
--hash=sha256:712aee154f648bde84634654bb38bb78c69ac640c37a45c9effed800735049d8 \
|
||||
--hash=sha256:72507285b5caa1d17904a3f7c322ca780823a54170a0e04ec3f37bcc60d4db71 \
|
||||
--hash=sha256:740e544169527b82695ce76af2f7ad6f030904658f2f3921a1d245771fb88cfc \
|
||||
--hash=sha256:74cc6df89ec5302337adc9cf096221cbed2510fd444b0e0f1586cf0470740864 \
|
||||
--hash=sha256:7805655781fb8f28a55d05fe57ed61f5f10f1892fb587673e3bb5264f28041f0 \
|
||||
--hash=sha256:7dffc5c859fe6059974df1e37d7923d654a83e2ae18fdd616994270e001115e1 \
|
||||
--hash=sha256:7f049911ee81a16a03c33d5450d8d5867d27f596ca5fb201b86f4524e874468b \
|
||||
--hash=sha256:816230f469381ad0a43abc9fa8dda5a699e32fb78958dde32ded93213b70a667 \
|
||||
--hash=sha256:86c5113d698cb8d927b2750bb1f1d59eefe3a37e0e0217491aee29a7f84ef52c \
|
||||
--hash=sha256:8a268024ce2d7d2b04694bf1594058981a9fa663d1df4b762dee499211ed7c1c \
|
||||
--hash=sha256:8bdfd1424abcf26832961e766570cae79efdb9599d709088c9cb6ef82b194926 \
|
||||
--hash=sha256:8fec3f165dfe332e490c3247c0f6c23b0bfc45f06496ad7f00ddb00e3d35e4dc \
|
||||
--hash=sha256:95c5b1f4b3a193f8a0c2de4bfdcb48d119f7f1063941f1de1f2168051b3e52dd \
|
||||
--hash=sha256:9ab5f5b93655e77fe0d6c2dfd22b5eac751bb1f876d8ec21761b7c1fb9266007 \
|
||||
--hash=sha256:9ec0dc0e59dc9c61af5c47348365ccbbd7addfafe0a93b00336ff3da2907bdc6 \
|
||||
--hash=sha256:9ff00e12102358292087274dfb1669132387ff6e7920ebf9d85f4826ce0d3a56 \
|
||||
--hash=sha256:a1eaccf5c3a1d3e46dead602c72e6836731e8e245c9de6a27764567b6b62d4c0 \
|
||||
--hash=sha256:a5433cf291e0ef9114bd14d0d824db6e5e4a43033234bca48181a9597acca07b \
|
||||
--hash=sha256:ab3df3dffb58bf70564e93a5cec7941e4d9faa5a36cc4234a10d3131afe04f53 \
|
||||
--hash=sha256:abc8bc8d9f935cd685457545b6a53863a877fdc12c2c0f5ee9beee18d9db139c \
|
||||
--hash=sha256:adb4bae02e91a8e863e48b177e4014bdcac8a6b5e047ea1df687a61534b85e6c \
|
||||
--hash=sha256:b18007dc2473a7942fd157366b55f01da6fed7ce85318591005b419e0a439474 \
|
||||
--hash=sha256:b79fd2a5bc099b5e744f34c4c9a58954a5f4cb7529fb4b6e8446057d61b6edaa \
|
||||
--hash=sha256:be63afcbbccfad3dd95a1ba12ada84dab2ef32031973d80b5b92df67fa763a61 \
|
||||
--hash=sha256:c0db80fcd5b8aece93f66c64f78a786bbb6b96c5fe63ef5a5a4581ecf8bab206 \
|
||||
--hash=sha256:c69bed34470abfcd456984fdadaa18e62169af4480335c45f3c32d1d9c12e638 \
|
||||
--hash=sha256:c6ce25fee6cabc8bf22cb8b52e642cbb821be5b9aec8094d07ff03378141b8e9 \
|
||||
--hash=sha256:d246c0db9a2513cd45f019ba178ea4d4d4705bd210ee465e2c15d76a1ab13874 \
|
||||
--hash=sha256:d4a389a852e392a6366058651a20fa5ba40d979865aa81bea2ccbdc44805070d \
|
||||
--hash=sha256:d98ef6f92e67c6dbf299dbfd8facc1b0d2d9cedf91e325e73b3d0373fe4309d8 \
|
||||
--hash=sha256:e604f58e35833fc46ef20302bcb314dddbfd3fcf33a4f936216d51dd678d63ae \
|
||||
--hash=sha256:ef6a08349401d8eaf3cb12688ac8557de95788556b8631ef17555a4a173022c0 \
|
||||
--hash=sha256:f0e5a21bd4452a88cf032fc43c4a5b307ab1380eacb63b5988f9c0317885e773 \
|
||||
--hash=sha256:f1e2db190db51c17433eee424803818cf0670bf049d9cfe0dd07be111d1aa7c4 \
|
||||
--hash=sha256:f2e3d061b8e13aec2f0441689b3c71b244a20e5d274a52cb0f7e31bd1d139552 \
|
||||
--hash=sha256:f7278591501941bb2456af102bb9cd59aab48c6cfd6e2dd68fa1290bb0c49a42 \
|
||||
--hash=sha256:fef01bd457f11fc158b130ca0027a3c365693280e8e231b65bdaf57999f39f5b
|
||||
# via sqlalchemy
|
||||
gunicorn==26.0.0 \
|
||||
--hash=sha256:40233d26a5f0d1872916188c276e21641155111c2853f0c2cd55260aec0d24fc \
|
||||
--hash=sha256:ca9346f85e3a4aeeb64d491045c16b9a35647abd37ea15efe53080eb8b090baf
|
||||
# via -r backend/requirements.txt
|
||||
h11==0.16.0 \
|
||||
--hash=sha256:4e35b956cf45792e4caa5885e69fba00bdbc6ffafbfa020300e549b208ee5ff1 \
|
||||
--hash=sha256:63cf8bbe7522de3bf65932fda1d9c2772064ffb3dae62d55932da54b31cb6c86
|
||||
# via httpcore
|
||||
httpcore==1.0.9 \
|
||||
--hash=sha256:2d400746a40668fc9dec9810239072b40b4484b640a8c38fd654a024c7a1bf55 \
|
||||
--hash=sha256:6e34463af53fd2ab5d807f399a9b45ea31c3dfa2276f15a2c3f00afff6e176e8
|
||||
# via httpx
|
||||
httpx==0.28.1 \
|
||||
--hash=sha256:75e98c5f16b0f35b567856f597f06ff2270a374470a5c2392242528e3e3e42fc \
|
||||
--hash=sha256:d909fcccc110f8c7faf814ca82a9a4d816bc5a6dbfea25d6591d6985b8ba59ad
|
||||
# via openai
|
||||
idna==3.18 \
|
||||
--hash=sha256:7f952cbe720b688055e3f87de14f5c3e5fdaa8bc3928985c4077ca689de849a2 \
|
||||
--hash=sha256:ffb385a7e039654cef1ab9ef32c6fafe283c0c0467bba1d9029738ce4a14a848
|
||||
# via
|
||||
# anyio
|
||||
# httpx
|
||||
iniconfig==2.3.0 \
|
||||
--hash=sha256:c76315c77db068650d49c5b56314774a7804df16fee4402c1f19d6d15d8c4730 \
|
||||
--hash=sha256:f631c04d2c48c52b84d0d0549c99ff3859c98df65b3101406327ecc7d53fbf12
|
||||
# via pytest
|
||||
itsdangerous==2.2.0 \
|
||||
--hash=sha256:c6242fc49e35958c8b15141343aa660db5fc54d4f13a1db01a3f5891b98700ef \
|
||||
--hash=sha256:e0050c0b7da1eea53ffaf149c0cfbb5c6e2e2b69c4bef22c81fa6eb73e5f6173
|
||||
# via flask
|
||||
jinja2==3.1.6 \
|
||||
--hash=sha256:0137fb05990d35f1275a587e9aee6d56da821fc83491a0fb838183be43f66d6d \
|
||||
--hash=sha256:85ece4451f492d0c13c5dd7c13a64681a86afae63a5f347908daf103ce6d2f67
|
||||
# via flask
|
||||
jiter==0.16.0 \
|
||||
--hash=sha256:0542a7189c26920778658fc8fcf2af8bae05bae9924577f71804acef37996536 \
|
||||
--hash=sha256:0758ab7747a984797cf048e8eedea1d8ef39d7994b25611daf5b48fc903e8873 \
|
||||
--hash=sha256:0df91907609837f33341b8e6fe73b95991fdaa57caf1a0fbd343dffe826f386f \
|
||||
--hash=sha256:0e2e9efbe042210df657bade597f66d6d75723e3d8f45a12ea6d8167ff8bbce3 \
|
||||
--hash=sha256:0fa25b09b13075c46f5bc174f2690525a925a4fc2f7c82969a2bbabff22386ce \
|
||||
--hash=sha256:10438939205546132189c8e74a2d536a707841f3a25cd7c74ee91fe503407a26 \
|
||||
--hash=sha256:12288303c9844e61e1651d02a9a6f6633e47d39f897d6991d1427161ce6b746e \
|
||||
--hash=sha256:17ca7fae79f6d99cd9a042b75f917eaada7b895cfc7dd2ee3a16089dcaec7a85 \
|
||||
--hash=sha256:1878349266f8ee36ecb1375cc5ba2f115f35fd9f0a1a4119e725e379126647f7 \
|
||||
--hash=sha256:1a0f05e229edb29e68cdd0ccb83cea13b64263416120cf943767a6fd72e6787f \
|
||||
--hash=sha256:2577196f4474ef3fc4779a088a23b0897bbf86f9ea3679c372d45b8383b43207 \
|
||||
--hash=sha256:26798522707abb47d767db536e4148ceac1b14446bf028ee85e579a2e043cfe5 \
|
||||
--hash=sha256:26d122613ada2b708eb714695446f40fce5bdf2edb4b02116dec62faa62dfab3 \
|
||||
--hash=sha256:2a0d46ef67cc58d906a6132dd3040ca70ae4f0b0d7c9c052fe432c658a69b3f6 \
|
||||
--hash=sha256:2a8e9e39cf083016137aa5cadafe3188adc2ba6ba1fbf1e5d18889ad3e9ad056 \
|
||||
--hash=sha256:2c842cbf374a8daf50b2c04212995bee34ca2ac2cdc29a901b4cdb072c9c4131 \
|
||||
--hash=sha256:2ed5738ae4af18271a51a528b8811b0cbfa4a1858de9d83359e4169855d6a331 \
|
||||
--hash=sha256:3126a5dbad56401989ac769aca0cb56005bfb3e2366eea0ca99d1a91c3c1ee03 \
|
||||
--hash=sha256:31aaee5b80f672c1dc21272bcfb9cbdcfc1ea04ff50f00ed5af500b80c44fa93 \
|
||||
--hash=sha256:3a23f0e4f957e1be65752d2dfac9a5a06b1917af8dc85deb639c3b9d02e31290 \
|
||||
--hash=sha256:3b21b412b899fd8bd51a3046934b59a3bb068b79f70a5c6010053ac77cc53f0c \
|
||||
--hash=sha256:3c1fd2dbe1b0af19e987f03fe66c5f5bd105a2229c1aff4ab14890b24f41d21a \
|
||||
--hash=sha256:3f4d9e473a5ce7d27fef8b848df4dc16e283893d3f53b4a585e72c9595f3c284 \
|
||||
--hash=sha256:3fb7bc819187b56dc48aa5c833aaf92257da8e07efdb9306156667bd2eeb491c \
|
||||
--hash=sha256:41977aa5654023948c2dae2a81cbf9c43343954bef1cd59a154dd15a4d84c195 \
|
||||
--hash=sha256:44d019fa8cdaf89bf29c71b39e3712143fdd0ac76725c6ef954f9957a5ea8730 \
|
||||
--hash=sha256:46add52f4ad47a08bfb1219f3e673da972191489a33016edefdb5ea55bfa8c48 \
|
||||
--hash=sha256:49060fd70737fad59d33ba9dcc0d83247dc9e77187de26053a19c16c9f32bd69 \
|
||||
--hash=sha256:491e7d072a253b156fff46b78bceac4652a697aa8d7082c9c18c03d7b7917d24 \
|
||||
--hash=sha256:4a1d68ff7ca1d3b5dee20a97a3decda7d5f15003823bf6d140c81f8561d3bc5c \
|
||||
--hash=sha256:51d7b836acb0108d7c77df1742332cac2a1fa04a74d6dacec46e7091f0e91274 \
|
||||
--hash=sha256:560b2cf3fb03240cd34f27409a238547488708f05b7c3924f571a60422251ec7 \
|
||||
--hash=sha256:561926e0573ffe4a32498420a76d64b16c513e1ab413b9d28158a8764ac701e5 \
|
||||
--hash=sha256:57b37fc887a32d44798e4d8ebfa7c9683ff3da1d5bf38f08d1bb3573ccb39106 \
|
||||
--hash=sha256:5a71d0d2014c3275043e1170bf3d4e771493cb0dcf07be54c567155f4d8ee64b \
|
||||
--hash=sha256:5ab4f50ff971b611d656554ea10b75f80097392c827bc32923c6eeb6386c8b00 \
|
||||
--hash=sha256:5af7780e4a26bd7d0d989592bf9ef12ebf806b74ab709223ecca37c749872ea9 \
|
||||
--hash=sha256:5cf109d010b4b05a105afb3d43be36a21322d345ad3111e13d15f680afef0e5b \
|
||||
--hash=sha256:5ed466aee31294d7cdcd4d37dfe5c42c97bc29d9a5f00eacf24504358309cb9b \
|
||||
--hash=sha256:616e89e008a93c01104161c75b4988e58716b01d62307ebfe161e52a56d2a818 \
|
||||
--hash=sha256:62c1b7fe1f77925acf5af68b6140b8810fa87dfd4dc0a9c8568ec2fa2a10429c \
|
||||
--hash=sha256:63efadc657488f45db1c676d81e704cac2abf3fdb892def1faea61db053127e2 \
|
||||
--hash=sha256:63ffdbdae7d4499f4cda14eadc12ddcabef0fc0c081191bdc2247489cb698077 \
|
||||
--hash=sha256:64c0203212098470032aabcde9356fc168f377aade3e43def61dfe17e92f2037 \
|
||||
--hash=sha256:64d613743df53199b1aa256a7d328340da6d7078aac7705a7db9d7a791e9cfd2 \
|
||||
--hash=sha256:659039cc50b5addcc35fcc87ae2c1833b7c0a8e5326ef631a75e4478447bcf84 \
|
||||
--hash=sha256:6722bcef4ffc86c835574b1b2fac6b33b9fb4a889c781e67950e891591f3c55a \
|
||||
--hash=sha256:67c3bc1760f8c99d805dcab4e644027142a53b1d5d861f18780ebdbd5d40b72a \
|
||||
--hash=sha256:67fddeda1688f0cce2d2ae83ccf8a80f79936f2d2997d6cc2261f82fdb54a4d3 \
|
||||
--hash=sha256:6edb63a46e65a82c26800a868e49b2cac30dd5a4218b88d74bc2c848c8ad60bb \
|
||||
--hash=sha256:70a490b55634dc0d2606ce8a8e01b1d62459011beb368d15d76e1eaf62460e3d \
|
||||
--hash=sha256:710cc51d4ebdcd3c1f70b232c1db1ea1344a075770422bbd4bede5708335acbe \
|
||||
--hash=sha256:741eed508c233a76313a1c7b001f8f21b82f14327e9196ae8bd29a2cc164ae84 \
|
||||
--hash=sha256:7b24c3492c5f4f84a37946ad9cf504910cf6a782d6a4e0689b6673c5894b4a1c \
|
||||
--hash=sha256:7c9610fd25ebccb43fca584136f5c2fbb26802447eccd430dfdbab95a0fd5126 \
|
||||
--hash=sha256:850ccb1d7eedb4200f4014b1c0e8a577de114fc3cd88faad646dcc9bc4bb12ad \
|
||||
--hash=sha256:8597d23c87f59294f83bcb6229b9ed1fccee13dbba967b46930d2f1759466fee \
|
||||
--hash=sha256:8d031aabecc4f1b6276adfb42e3aabb77c89d468bf616600e8d3a11328929053 \
|
||||
--hash=sha256:8d30a4a1c87713060c8d1cc59a7b6c8fb6b8ef0a6900368014c76c87922a2929 \
|
||||
--hash=sha256:8fb8de1e23a0cb2a7f53c335049c7b72b6db41aa6227cdcc0972a1de5cb39450 \
|
||||
--hash=sha256:8fc4d94713c4697347e38faf7d6ef91547c142219bdcfc7220c4870879974244 \
|
||||
--hash=sha256:96e38eea538c8ddf853a35727c7be0741c76c13f04148ac5c116222f50ece3b3 \
|
||||
--hash=sha256:9acf1b2faec82d998811ecce7ae84d9005e53410773e9d37d61cdc424ba4581b \
|
||||
--hash=sha256:9c8a956fd72c2cf1e730d01ea080341f13aa0a97a4a33b51abebe725b7ae9ca9 \
|
||||
--hash=sha256:9ec553a99b0987efd7a3645a1a825cf29c224e494db267a83369fcc8da9aeda5 \
|
||||
--hash=sha256:a111256a7193bea0759267b10385e5870949c239ed7b6ddbaaf57573edb38734 \
|
||||
--hash=sha256:a32d2027a9fa67f109ff245a3252ece3ccc32cc56703e1deab6cc846a59e0585 \
|
||||
--hash=sha256:a5c54ef4ff776d9675837ef535b3308d6e31c208d43ebc44a0f7ab8a208c68f7 \
|
||||
--hash=sha256:adbb8edeadd431bc4477879d5d371ece7cb1334486584e0f252656dd7ffada29 \
|
||||
--hash=sha256:adff21bc78edfe086c15eb495b900306076de378dc2337c132401fc39bd79c91 \
|
||||
--hash=sha256:b1e7923093a376d93c6eb507c77045ae258d689ba577392846a1b3f10d0b09a9 \
|
||||
--hash=sha256:b2c61484666ad42726029af0c00ef4541f0f3b5cdc550221f56c2343208018ee \
|
||||
--hash=sha256:b42e9ff5376819c053da25809a8d4b6fa6e473b4856ebe42e298ac958be3d7f9 \
|
||||
--hash=sha256:b72d0b2990ca754a9102779ac98d8597b7cb31678958562214a007f909eab78e \
|
||||
--hash=sha256:b7dc85ea77d4abbae8bad0d3538678aedee75bceec4e2f6c8dfb1c74772e5aa5 \
|
||||
--hash=sha256:b8a3f9a6008048fe9def7bf465180564a6e458047d2ce499149cfbe73c3ae9db \
|
||||
--hash=sha256:baad945ed47f163ad833314f8e3288c396118934f94e7bbb9e243ce4b341a4fd \
|
||||
--hash=sha256:bae96332410f866e5900d809298b1ed82735932986c672495f9701daacd80620 \
|
||||
--hash=sha256:bc837c1b9631be10abfe0191537fe8009838204cec7e44827401ace390ddb567 \
|
||||
--hash=sha256:bf3ea07d9bc8e7d03a9fbc051295462e6dbc295b894fd72457c3136e3e43d898 \
|
||||
--hash=sha256:c22a488f7b9218e245a0025a9ba6b100e2e54700831cf4cf16833a27fba3ad01 \
|
||||
--hash=sha256:c4b4717bdb35ae456f831a6b08d01880fff399887a6bbc526a583a406e484eea \
|
||||
--hash=sha256:c5fc4f8def331036a7b8e981b4347ebe409981edbc8308a5ea842b8c3614fa6c \
|
||||
--hash=sha256:c682bea068a90b764577bdb78a60a4c1d1606daf9cd4c893832a37c7cc9d9026 \
|
||||
--hash=sha256:c90c0f63df322be920eda6ce622e3083d8906ba267f8220fe7873213b8b4430e \
|
||||
--hash=sha256:c9c53be232c2e206ef9cdbad81a48bfa74c3d3f08bcf8124630a8a748aad993e \
|
||||
--hash=sha256:cbd18dd5e2df96b580487b5745adf57ef64ad89ba2d9662fc3c19386acce7db8 \
|
||||
--hash=sha256:cf0d73f50e7b6935677854f6e8e31d499ca7064dd24734f703e060f5b237d883 \
|
||||
--hash=sha256:d284fb8d94d5855d60c44fefcab4bf966f1da6fada73992b01f6f0c9bc0c6702 \
|
||||
--hash=sha256:d28bb3c26762358dadf3e5bf0bccd29ae987d65e6988d2e6f49829c76b003c09 \
|
||||
--hash=sha256:d5bf78d0e05e45cfdd66558893938d59afe3d1b1a824a202039b20e607d25a72 \
|
||||
--hash=sha256:d5f91b1c27fc22a57993d5a5cb8a627cb8ed4b10502716fac1ffbfe1d19d84e8 \
|
||||
--hash=sha256:d8f80521644426d451e70f00c7974240cab8f6ee088aedaa9af2697153ab7805 \
|
||||
--hash=sha256:da3d7ec75dc83bb18bca888b5edfae0656a26849056c59e05a7728badd17e7af \
|
||||
--hash=sha256:dab907db06fc593645e73109acf4581ba5b548897d28b9348dc41ddc8343b2d3 \
|
||||
--hash=sha256:de5ba8763e56b793561f43bed197c9ea55776daa5e9a6b91eed68a909bc9cdbf \
|
||||
--hash=sha256:e03a5f21a5ce96a9441b8cb32719a8b88ed5388f53e0f339c5bcf54f1317f9d0 \
|
||||
--hash=sha256:e34e97bda77eb63242a410243c071e28ac7e0d8c0948c5ee658498690a4b2f2f \
|
||||
--hash=sha256:e431cfc9caf44c1d5459ff77d4e64cbf85fddb6a35dad836a15c6a9ec23087c1 \
|
||||
--hash=sha256:eab2cd170150e70153de16896a1774e3a1dca80154c56b54d7a812c479a7165e \
|
||||
--hash=sha256:ee6162b77d49a9939229df666dfa8af3e656b6701b54c4c84966d740e189264e \
|
||||
--hash=sha256:f17d61a28b4b3e0e3e2ba98490c70501403b4d196f78732439160e7fd3678127 \
|
||||
--hash=sha256:f3bd327cdfa118bc1ce69c214c2678571d5bd39b8ccd0ebf43a54db00541ba9a \
|
||||
--hash=sha256:f4444a83f946605990c98f625cdd3d2725bfb818158760c5748c653170a20e0e \
|
||||
--hash=sha256:fb08c276dd02dac3a284acdd02cacc630d2e3cd6572a4b85519f35cbd133c3de
|
||||
# via openai
|
||||
mako==1.4.1 \
|
||||
--hash=sha256:a359d9a94a541213958742b2698d0a7757bb83551767bc468a74b9905aba9617 \
|
||||
--hash=sha256:d7904710b662996425a21627710c4777c45053146942cf8a7aebf757c92b8c27
|
||||
# via alembic
|
||||
markupsafe==3.0.3 \
|
||||
--hash=sha256:0303439a41979d9e74d18ff5e2dd8c43ed6c6001fd40e5bf2e43f7bd9bbc523f \
|
||||
--hash=sha256:068f375c472b3e7acbe2d5318dea141359e6900156b5b2ba06a30b169086b91a \
|
||||
--hash=sha256:0bf2a864d67e76e5c9a34dc26ec616a66b9888e25e7b9460e1c76d3293bd9dbf \
|
||||
--hash=sha256:0db14f5dafddbb6d9208827849fad01f1a2609380add406671a26386cdf15a19 \
|
||||
--hash=sha256:0eb9ff8191e8498cca014656ae6b8d61f39da5f95b488805da4bb029cccbfbaf \
|
||||
--hash=sha256:0f4b68347f8c5eab4a13419215bdfd7f8c9b19f2b25520968adfad23eb0ce60c \
|
||||
--hash=sha256:1085e7fbddd3be5f89cc898938f42c0b3c711fdcb37d75221de2666af647c175 \
|
||||
--hash=sha256:116bb52f642a37c115f517494ea5feb03889e04df47eeff5b130b1808ce7c219 \
|
||||
--hash=sha256:12c63dfb4a98206f045aa9563db46507995f7ef6d83b2f68eda65c307c6829eb \
|
||||
--hash=sha256:133a43e73a802c5562be9bbcd03d090aa5a1fe899db609c29e8c8d815c5f6de6 \
|
||||
--hash=sha256:1353ef0c1b138e1907ae78e2f6c63ff67501122006b0f9abad68fda5f4ffc6ab \
|
||||
--hash=sha256:15d939a21d546304880945ca1ecb8a039db6b4dc49b2c5a400387cdae6a62e26 \
|
||||
--hash=sha256:177b5253b2834fe3678cb4a5f0059808258584c559193998be2601324fdeafb1 \
|
||||
--hash=sha256:1872df69a4de6aead3491198eaf13810b565bdbeec3ae2dc8780f14458ec73ce \
|
||||
--hash=sha256:1b4b79e8ebf6b55351f0d91fe80f893b4743f104bff22e90697db1590e47a218 \
|
||||
--hash=sha256:1b52b4fb9df4eb9ae465f8d0c228a00624de2334f216f178a995ccdcf82c4634 \
|
||||
--hash=sha256:1ba88449deb3de88bd40044603fafffb7bc2b055d626a330323a9ed736661695 \
|
||||
--hash=sha256:1cc7ea17a6824959616c525620e387f6dd30fec8cb44f649e31712db02123dad \
|
||||
--hash=sha256:218551f6df4868a8d527e3062d0fb968682fe92054e89978594c28e642c43a73 \
|
||||
--hash=sha256:26a5784ded40c9e318cfc2bdb30fe164bdb8665ded9cd64d500a34fb42067b1c \
|
||||
--hash=sha256:2713baf880df847f2bece4230d4d094280f4e67b1e813eec43b4c0e144a34ffe \
|
||||
--hash=sha256:2a15a08b17dd94c53a1da0438822d70ebcd13f8c3a95abe3a9ef9f11a94830aa \
|
||||
--hash=sha256:2f981d352f04553a7171b8e44369f2af4055f888dfb147d55e42d29e29e74559 \
|
||||
--hash=sha256:32001d6a8fc98c8cb5c947787c5d08b0a50663d139f1305bac5885d98d9b40fa \
|
||||
--hash=sha256:3524b778fe5cfb3452a09d31e7b5adefeea8c5be1d43c4f810ba09f2ceb29d37 \
|
||||
--hash=sha256:3537e01efc9d4dccdf77221fb1cb3b8e1a38d5428920e0657ce299b20324d758 \
|
||||
--hash=sha256:35add3b638a5d900e807944a078b51922212fb3dedb01633a8defc4b01a3c85f \
|
||||
--hash=sha256:38664109c14ffc9e7437e86b4dceb442b0096dfe3541d7864d9cbe1da4cf36c8 \
|
||||
--hash=sha256:3a7e8ae81ae39e62a41ec302f972ba6ae23a5c5396c8e60113e9066ef893da0d \
|
||||
--hash=sha256:3b562dd9e9ea93f13d53989d23a7e775fdfd1066c33494ff43f5418bc8c58a5c \
|
||||
--hash=sha256:457a69a9577064c05a97c41f4e65148652db078a3a509039e64d3467b9e7ef97 \
|
||||
--hash=sha256:4bd4cd07944443f5a265608cc6aab442e4f74dff8088b0dfc8238647b8f6ae9a \
|
||||
--hash=sha256:4e885a3d1efa2eadc93c894a21770e4bc67899e3543680313b09f139e149ab19 \
|
||||
--hash=sha256:4faffd047e07c38848ce017e8725090413cd80cbc23d86e55c587bf979e579c9 \
|
||||
--hash=sha256:509fa21c6deb7a7a273d629cf5ec029bc209d1a51178615ddf718f5918992ab9 \
|
||||
--hash=sha256:5678211cb9333a6468fb8d8be0305520aa073f50d17f089b5b4b477ea6e67fdc \
|
||||
--hash=sha256:591ae9f2a647529ca990bc681daebdd52c8791ff06c2bfa05b65163e28102ef2 \
|
||||
--hash=sha256:5a7d5dc5140555cf21a6fefbdbf8723f06fcd2f63ef108f2854de715e4422cb4 \
|
||||
--hash=sha256:69c0b73548bc525c8cb9a251cddf1931d1db4d2258e9599c28c07ef3580ef354 \
|
||||
--hash=sha256:6b5420a1d9450023228968e7e6a9ce57f65d148ab56d2313fcd589eee96a7a50 \
|
||||
--hash=sha256:722695808f4b6457b320fdc131280796bdceb04ab50fe1795cd540799ebe1698 \
|
||||
--hash=sha256:729586769a26dbceff69f7a7dbbf59ab6572b99d94576a5592625d5b411576b9 \
|
||||
--hash=sha256:77f0643abe7495da77fb436f50f8dab76dbc6e5fd25d39589a0f1fe6548bfa2b \
|
||||
--hash=sha256:795e7751525cae078558e679d646ae45574b47ed6e7771863fcc079a6171a0fc \
|
||||
--hash=sha256:7be7b61bb172e1ed687f1754f8e7484f1c8019780f6f6b0786e76bb01c2ae115 \
|
||||
--hash=sha256:7c3fb7d25180895632e5d3148dbdc29ea38ccb7fd210aa27acbd1201a1902c6e \
|
||||
--hash=sha256:7e68f88e5b8799aa49c85cd116c932a1ac15caaa3f5db09087854d218359e485 \
|
||||
--hash=sha256:83891d0e9fb81a825d9a6d61e3f07550ca70a076484292a70fde82c4b807286f \
|
||||
--hash=sha256:8485f406a96febb5140bfeca44a73e3ce5116b2501ac54fe953e488fb1d03b12 \
|
||||
--hash=sha256:8709b08f4a89aa7586de0aadc8da56180242ee0ada3999749b183aa23df95025 \
|
||||
--hash=sha256:8f71bc33915be5186016f675cd83a1e08523649b0e33efdb898db577ef5bb009 \
|
||||
--hash=sha256:915c04ba3851909ce68ccc2b8e2cd691618c4dc4c4232fb7982bca3f41fd8c3d \
|
||||
--hash=sha256:949b8d66bc381ee8b007cd945914c721d9aba8e27f71959d750a46f7c282b20b \
|
||||
--hash=sha256:94c6f0bb423f739146aec64595853541634bde58b2135f27f61c1ffd1cd4d16a \
|
||||
--hash=sha256:9a1abfdc021a164803f4d485104931fb8f8c1efd55bc6b748d2f5774e78b62c5 \
|
||||
--hash=sha256:9b79b7a16f7fedff2495d684f2b59b0457c3b493778c9eed31111be64d58279f \
|
||||
--hash=sha256:a320721ab5a1aba0a233739394eb907f8c8da5c98c9181d1161e77a0c8e36f2d \
|
||||
--hash=sha256:a4afe79fb3de0b7097d81da19090f4df4f8d3a2b3adaa8764138aac2e44f3af1 \
|
||||
--hash=sha256:ad2cf8aa28b8c020ab2fc8287b0f823d0a7d8630784c31e9ee5edea20f406287 \
|
||||
--hash=sha256:b8512a91625c9b3da6f127803b166b629725e68af71f8184ae7e7d54686a56d6 \
|
||||
--hash=sha256:bc51efed119bc9cfdf792cdeaa4d67e8f6fcccab66ed4bfdd6bde3e59bfcbb2f \
|
||||
--hash=sha256:bdc919ead48f234740ad807933cdf545180bfbe9342c2bb451556db2ed958581 \
|
||||
--hash=sha256:bdd37121970bfd8be76c5fb069c7751683bdf373db1ed6c010162b2a130248ed \
|
||||
--hash=sha256:be8813b57049a7dc738189df53d69395eba14fb99345e0a5994914a3864c8a4b \
|
||||
--hash=sha256:c0c0b3ade1c0b13b936d7970b1d37a57acde9199dc2aecc4c336773e1d86049c \
|
||||
--hash=sha256:c47a551199eb8eb2121d4f0f15ae0f923d31350ab9280078d1e5f12b249e0026 \
|
||||
--hash=sha256:c4ffb7ebf07cfe8931028e3e4c85f0357459a3f9f9490886198848f4fa002ec8 \
|
||||
--hash=sha256:ccfcd093f13f0f0b7fdd0f198b90053bf7b2f02a3927a30e63f3ccc9df56b676 \
|
||||
--hash=sha256:d2ee202e79d8ed691ceebae8e0486bd9a2cd4794cec4824e1c99b6f5009502f6 \
|
||||
--hash=sha256:d53197da72cc091b024dd97249dfc7794d6a56530370992a5e1a08983ad9230e \
|
||||
--hash=sha256:d6dd0be5b5b189d31db7cda48b91d7e0a9795f31430b7f271219ab30f1d3ac9d \
|
||||
--hash=sha256:d88b440e37a16e651bda4c7c2b930eb586fd15ca7406cb39e211fcff3bf3017d \
|
||||
--hash=sha256:de8a88e63464af587c950061a5e6a67d3632e36df62b986892331d4620a35c01 \
|
||||
--hash=sha256:df2449253ef108a379b8b5d6b43f4b1a8e81a061d6537becd5582fba5f9196d7 \
|
||||
--hash=sha256:e1c1493fb6e50ab01d20a22826e57520f1284df32f2d8601fdd90b6304601419 \
|
||||
--hash=sha256:e1cf1972137e83c5d4c136c43ced9ac51d0e124706ee1c8aa8532c1287fa8795 \
|
||||
--hash=sha256:e2103a929dfa2fcaf9bb4e7c091983a49c9ac3b19c9061b6d5427dd7d14d81a1 \
|
||||
--hash=sha256:e56b7d45a839a697b5eb268c82a71bd8c7f6c94d6fd50c3d577fa39a9f1409f5 \
|
||||
--hash=sha256:e8afc3f2ccfa24215f8cb28dcf43f0113ac3c37c2f0f0806d8c70e4228c5cf4d \
|
||||
--hash=sha256:e8fc20152abba6b83724d7ff268c249fa196d8259ff481f3b1476383f8f24e42 \
|
||||
--hash=sha256:eaa9599de571d72e2daf60164784109f19978b327a3910d3e9de8c97b5b70cfe \
|
||||
--hash=sha256:ec15a59cf5af7be74194f7ab02d0f59a62bdcf1a537677ce67a2537c9b87fcda \
|
||||
--hash=sha256:f190daf01f13c72eac4efd5c430a8de82489d9cff23c364c3ea822545032993e \
|
||||
--hash=sha256:f34c41761022dd093b4b6896d4810782ffbabe30f2d443ff5f083e0cbbb8c737 \
|
||||
--hash=sha256:f3e98bb3798ead92273dc0e5fd0f31ade220f59a266ffd8a4f6065e0a3ce0523 \
|
||||
--hash=sha256:f42d0984e947b8adf7dd6dde396e720934d12c506ce84eea8476409563607591 \
|
||||
--hash=sha256:f71a396b3bf33ecaa1626c255855702aca4d3d9fea5e051b41ac59a9c1c41edc \
|
||||
--hash=sha256:f9e130248f4462aaa8e2552d547f36ddadbeaa573879158d721bbd33dfe4743a \
|
||||
--hash=sha256:fed51ac40f757d41b7c48425901843666a6677e3e8eb0abcff09e4ba6e664f50
|
||||
# via
|
||||
# flask
|
||||
# jinja2
|
||||
# mako
|
||||
# werkzeug
|
||||
openai==2.24.0 \
|
||||
--hash=sha256:1e5769f540dbd01cb33bc4716a23e67b9d695161a734aff9c5f925e2bf99a673 \
|
||||
--hash=sha256:fed30480d7d6c884303287bde864980a4b137b60553ffbcf9ab4a233b7a73d94
|
||||
# via -r backend/requirements.txt
|
||||
packaging==26.3 \
|
||||
--hash=sha256:94edc256424af38762eb31306eed28beb9f0efc50a8837492c9d6fd6004aed79 \
|
||||
--hash=sha256:d7193f7c8e4e93f444fde0262bf90af30e16fa0ad0ad44cb553c87339b23cd1c
|
||||
# via
|
||||
# gunicorn
|
||||
# pytest
|
||||
pluggy==1.6.0 \
|
||||
--hash=sha256:7dcc130b76258d33b90f61b658791dede3486c3e6bfb003ee5c9bfb396dd22f3 \
|
||||
--hash=sha256:e920276dd6813095e9377c0bc5566d94c932c33b27a3e3945d8389c374dd4746
|
||||
# via pytest
|
||||
psycopg==3.3.4 \
|
||||
--hash=sha256:b6bbc25ccf05c8fad3b061d9db2ef0909a555171b84b07f29458a447253d679a \
|
||||
--hash=sha256:e21207764952cff81b6b8bdacad9a3939f2793367fdac2987b3aac36a651b5bc
|
||||
# via -r backend/requirements.txt
|
||||
psycopg-binary==3.3.4 ; implementation_name != 'pypy' \
|
||||
--hash=sha256:018fbed325936da502feb546642c982dcc4b9ffdea32dfef78dbf3b7f7ad4070 \
|
||||
--hash=sha256:0579252a1202cd73e4da137a1426e2dae993ae44e757605344282af3a082848c \
|
||||
--hash=sha256:136f199a407b5348b9b857c504aff60c77622a28482e7195839ce1b51238c4cc \
|
||||
--hash=sha256:13a7f380824c35896dcac7fe0f61440f7ca49d6dc73f3c13a9a4471e6a3b302e \
|
||||
--hash=sha256:17a21953a9e5ff3a16dab692625a3676e2f101db5e40072f39dbee2250194d68 \
|
||||
--hash=sha256:1dc1f79fd16bb1f3f4421417a514607539f17804d95c7ed617265369d1981cae \
|
||||
--hash=sha256:1fbaa292a3c8bb61b45df1ad3da1908ccee7cb889db9425e3557d9e34e2a4829 \
|
||||
--hash=sha256:22cdbf5f91ef7bb91fe0c5757e1962d3127a8010256eefd9c61fcaf441802097 \
|
||||
--hash=sha256:26df2717e59c0473e4465a97dfb1b7afebaa479277870fd5784d1436470db47c \
|
||||
--hash=sha256:276904e3452d6a23d474ef9a21eee19f20eed3d53ddd2576af033827e0ba0992 \
|
||||
--hash=sha256:28b7398fdd19db3232c884fb24550bdfe951221f510e195e233299e4c9b78f97 \
|
||||
--hash=sha256:2c09aad7051326e7603c14e50636db9c01f78272dc54b3accff03d46370461e6 \
|
||||
--hash=sha256:32a6fbf8481e3a370d0d72b860d35948a693cb01281da217f7b2f307636e591a \
|
||||
--hash=sha256:41f2ec0fea529832982bcb6c9415de3c86264ebe562b77a467c0fbcd7efbba8d \
|
||||
--hash=sha256:46893c26858be12cc49ca4226ed6a60b4bfccadd946b3bebb783a60b38788228 \
|
||||
--hash=sha256:47c656a8a7ba6eb0cff1801a4caaa9c8bdc12d03080e273aff1c8ac39971a77e \
|
||||
--hash=sha256:494ca54901be8cf9eb7e02c25b731f2317c378efa44f43e8f9bd0e1184ae7be4 \
|
||||
--hash=sha256:514404ed543efd620c85602b747df2a23cf1241b4067199e1a66f2d2757aaa41 \
|
||||
--hash=sha256:574ea21a9651958f1535c5a1c649c7409e9168bcbffa29a3f2f961f58b322949 \
|
||||
--hash=sha256:580ae30a5f95ccd90008ec697d3ed6a4a2047a516407ad904283fa42086936e9 \
|
||||
--hash=sha256:5ab28a2a7649df3b72e6b674b4c190e448e8e77cf496a65bd846472048de2089 \
|
||||
--hash=sha256:5c4ab71be17bdca30cb34c34c4e1496e2f5d6f20c199c12bad226070b22ef9bf \
|
||||
--hash=sha256:612a627d733f695b1de1f9b4bd511c15f999a5d8b915d444bbd7dd71cf3370da \
|
||||
--hash=sha256:6402a9d8146cf4b3974ded3fd28a971e83dc6a0333eb7822524a3aa20b546578 \
|
||||
--hash=sha256:6b9016b1714da4dd5ecaaa75b82098aa5a0b87854ce9b092e21c27c4ae23e014 \
|
||||
--hash=sha256:71e55ccbdfae79a2ed9c6369c3008a3025817ff9d7e27b32a2d84e2a4267e66e \
|
||||
--hash=sha256:7465bfe6087d2d5b42d4c53b9b11ca9f218e477317a4a162a10e3c19e984ba8e \
|
||||
--hash=sha256:75a9067e236f9b9ae3535b66fe99bddb33d39c0de10112e49b9ab11eee53dc31 \
|
||||
--hash=sha256:773d573e11f437ce0bdb95b7c18dc58390494f96d43f8b45b9760436114f7652 \
|
||||
--hash=sha256:77df19583501ea288eaf15ac0fe7ad01e6d8091a91d5c41df5c718f307d8e31b \
|
||||
--hash=sha256:7f7668f30b9dd5163197e5cbf4e0efd54e00f0a859cc566ce56cfc31f4054839 \
|
||||
--hash=sha256:8c0056529e68dbe9184cd4019a1f3d8f3a4ead2f6fc7a5afcf27d3314edd1277 \
|
||||
--hash=sha256:94596f9e7633ee3f6440711d43bb70aa31cc0a46a900ab8b4201a366ace5c9e7 \
|
||||
--hash=sha256:ab8cca8ef8fb1ccf5b048ae5bd78ba55b9e4b5d472e3ce5ca39ff4d2a9c249e4 \
|
||||
--hash=sha256:ad3bc94054876155549fdaedf4a46d1ec69d39a5bcee377148afe498e84c4b8e \
|
||||
--hash=sha256:b56b603ebcea8aa10b46228b8410ba7f13e7c2ee54389d4d9be0927fd8ce2a70 \
|
||||
--hash=sha256:b6f5a29e9c775b9f12a1a717aa7a2c80f9e1db6f27ba44a5b59c80ac61d2ffcf \
|
||||
--hash=sha256:b7bfff1ca23732b488cbca3076fc11bc98d520ee122514fdb17a8e20d3338f5a \
|
||||
--hash=sha256:bdef84570ebbce1d42b4e7ea952d21c414c5f118ad02fee00c5625f35e134429 \
|
||||
--hash=sha256:c37e024c07308cd06cf3ec51bfd0e7f6157585a4d84d1bce4a7f5f7913719bf8 \
|
||||
--hash=sha256:c677c4ad433cb7150c8cd304a0769ae3bcfbe5ea0676eb53faa7b1443b16d0d3 \
|
||||
--hash=sha256:cf7f73a4a792bc5db58a4b385d8a1467e8d468f7548702fb0ed1e9b7501b1c13 \
|
||||
--hash=sha256:cffc3408d77a27973f33e5d909b624cce683db5fc25964b02fe0aae7886c1007 \
|
||||
--hash=sha256:d7b4d40c153fa352ab3cca530f3a0baedf7621b2ebcbd7f084009522c21788fc \
|
||||
--hash=sha256:dbfdb9b6cc79f31104a7b162a2b921b765fcc62af6c00540a167a8de47e4ed38 \
|
||||
--hash=sha256:df1d567fc430f6df15c9fcf67d87685fc49bdb325adc0db5af1adfb2f44eb5c9 \
|
||||
--hash=sha256:e2631da29253a98bd496e6c4813b24e09a4fe3fb2a9e88513305d6f8747cce95 \
|
||||
--hash=sha256:e7510c37550f91a187e3660a8cc50d4b760f8c3b8b2f89ebc5698cd2c7f2c85d \
|
||||
--hash=sha256:eb05ee1c2b817d27c537333224c9e83c7afb86fe7296ba970990068baf819b16 \
|
||||
--hash=sha256:eb4eed2079c01a4850bf467deacfab56d356d4225040170af03dc9958321242d \
|
||||
--hash=sha256:ee17a2cf4943cde261adfad1bbc5bf38d6b3776d7afff74c7cabcbeaeb08c260 \
|
||||
--hash=sha256:f80e3f2b5331dbbf0901bcb658056c03eeb2c1ef31d774afb0d61598b242e744 \
|
||||
--hash=sha256:f9b1c2533af01cd7648378599f82b0b8ae32f293296e6eec5753a625bc97ef28 \
|
||||
--hash=sha256:fa1cbc10768a796c96d3243656016bf4e337c81c71097270bb7b0ad6210d9765 \
|
||||
--hash=sha256:fbd1d4ed566895ad2d3bf4ddfd8bae90026930ddf29df3b9d91d32c8c47866a7
|
||||
# via psycopg
|
||||
pydantic==2.13.4 \
|
||||
--hash=sha256:45a282cde31d808236fd7ea9d919b128653c8b38b393d1c4ab335c62924d9aba \
|
||||
--hash=sha256:c40756b57adaa8b1efeeced5c196f3f3b7c435f90e84ea7f443901bec8099ef6
|
||||
# via
|
||||
# -r backend/requirements.txt
|
||||
# openai
|
||||
pydantic-core==2.46.4 \
|
||||
--hash=sha256:00c603d540afdd6b80eb39f078f33ebd46211f02f33e34a32d9f053bba711de0 \
|
||||
--hash=sha256:0186750b482eefa11d7f435892b09c5c606193ef3375bcf94aa00ae6bfb66262 \
|
||||
--hash=sha256:041bde0a48fd37cf71cab1c9d56d3e8625a3793fef1f7dd232b3ff37e978ecda \
|
||||
--hash=sha256:0c563b08bca408dc7f65f700633d8442fffb2421fc47b8101377e9fd65051ff0 \
|
||||
--hash=sha256:0cbe8b01f948de4286c74cdd6c667aceb38f5c1e26f0693b3983d9d74887c65e \
|
||||
--hash=sha256:0ce40cd7b21210e99342afafbd4d0f76d784eb5b1d60f3bdc566be4983c6c73b \
|
||||
--hash=sha256:0e96592440881c74a213e5ad528e2b24d3d4f940de2766bed9010ab1d9e51594 \
|
||||
--hash=sha256:10e17cbb10a330363733efc4d7c4d0dd827ac0909b8f6a6542298fed1ea62f29 \
|
||||
--hash=sha256:133878133d271ade3d41d1bfb2a45ec38dbdbda40bc065921c6b04e4630127e2 \
|
||||
--hash=sha256:14d4edf427bdcf950a8a02d7cb44a08614388dd6e1bdcbf4f67504fa7887da9c \
|
||||
--hash=sha256:14f4c5d6db102bd796a627bbb3a17b4cf4574b9ae861d8b7c9a9661c6dd3362d \
|
||||
--hash=sha256:17299feefe090f2caa5b8e37222bb5f663e4935a8bfa6931d4102e5df1a9f398 \
|
||||
--hash=sha256:184c081504d17f1c1066e430e117142b2c77d9448a97f7b65c6ac9fd9aee238d \
|
||||
--hash=sha256:18e5ceec2ab67e6d5f1a9085e5a24c9c4e2ac4545730bfe668680bca05e555f3 \
|
||||
--hash=sha256:19e51f073cd3df251856a8a4189fbdf1de4012c3ebacfb1884f94f1eb406079f \
|
||||
--hash=sha256:1a7dd0b3ee80d90150e3495a3a13ac34dbcbfd4f012996a6a1d8900e91b5c0fb \
|
||||
--hash=sha256:1d8ba486450b14f3b1d63bc521d410ec7565e52f887b9fb671791886436a42f7 \
|
||||
--hash=sha256:2108ba5c1c1eca18030634489dc544844144ee36357f2f9f780b93e7ddbb44b5 \
|
||||
--hash=sha256:228ee9bae8bef5b1e97ec58302f80357c37199e0d0a99174e138d28e6957b9d9 \
|
||||
--hash=sha256:23ace664830ee0bfe014a0c7bc248b1f7f25ed7ad103852c317624a1083af462 \
|
||||
--hash=sha256:2412e734dcb48da14d4e4006b82b46b74f2518b8a26ee7e58c6844a6cd6d03c4 \
|
||||
--hash=sha256:29c61fc04a3d840155ff08e475a04809278972fe6aef51e2720554e96367e34b \
|
||||
--hash=sha256:2f84c03c8607173d16b5a854ec68a2f9079ae03237a54fb506d13af47e1d018d \
|
||||
--hash=sha256:3009f12e4e90b7f88b4f9adb1b0c4a3d58fe7820f3238c190047209d148026df \
|
||||
--hash=sha256:3245406455a5d98187ec35530fd772b1d799b26667980872c8d4614991e2c4a2 \
|
||||
--hash=sha256:3447661d99f75a3683a4cf5c87da72f2161964611864dbbeac7fbb118bb4bfc0 \
|
||||
--hash=sha256:372429a130e469c9cd698925ce5fc50940b7a1336b0d82038e63d5bbc4edc519 \
|
||||
--hash=sha256:395aebd9183f9d112f569aeb5b2214d1a10a33bec8456447f7fbdfa51d38d4cd \
|
||||
--hash=sha256:3a233125ac121aa3ffba9a2b59edfc4a985a76092dc8279586ab4b71390875e7 \
|
||||
--hash=sha256:3be77f45df024d789a672ae34f8b06fb346c4f9f46ea714956660ea4862e89ac \
|
||||
--hash=sha256:3bf92c5d0e00fefaab325a4d27828fe6b6e2a21848686b5b60d2d9eeb09d76c6 \
|
||||
--hash=sha256:3ecbc122d18468d06ca279dc26a8c2e2d5acb10943bb35e36ae92096dc3b5565 \
|
||||
--hash=sha256:3fb702cd90b0446a3a1c5e470bfa0dd23c0233b676a9099ddcc964fa6ca13898 \
|
||||
--hash=sha256:428e04521a40150c85216fc8b85e8d39fece235a9cf5e383761238c7fa9b96fb \
|
||||
--hash=sha256:432c179df7874eeb73307aad2df0755e1ae0efa61ff0ea89b93e194411ae3928 \
|
||||
--hash=sha256:4a05d69cba51d852c5c3e92758653245a50c0b646ced0cf05bd793ed592839d6 \
|
||||
--hash=sha256:4c63ebc82684aa89d9a3bcbd13d515b3be44250dc68dd3bd81526c1cb31286c3 \
|
||||
--hash=sha256:4fc73cb559bdb54b1134a706a2802a4cddd27a0633f5abb7e53056268751ac6a \
|
||||
--hash=sha256:4fcbe087dbc2068af7eda3aa87634eba216dbda64d1ae73c8684b621d33f6596 \
|
||||
--hash=sha256:56cb4851bcaf3d117eddcef4fe66afd750a50274b0da8e22be256d10e5611987 \
|
||||
--hash=sha256:5855698a4856556d86e8e6cd8434bc3ac0314ee8e12089ae0e143f64c6256e4e \
|
||||
--hash=sha256:5a4330cdbc57162e4b3aa303f588ba752257694c9c9be3e7ebb11b4aca659b5d \
|
||||
--hash=sha256:5b712b53160b79a5850310b912a5ef8e57e56947c8ad690c227f5c9d7e561712 \
|
||||
--hash=sha256:5d5902252db0d3cedf8d4a1bc68f70eeb430f7e4c7104c8c476753519b423008 \
|
||||
--hash=sha256:617d7e2ca7dcb8c5cf6bcb8c59b8832c94b36196bbf1cbd1bfb56ed341905edd \
|
||||
--hash=sha256:62f875393d7f270851f20523dd2e29f082bcc82292d66db2b64ea71f64b6e1c1 \
|
||||
--hash=sha256:633147d34cf4550417f12e2b1a0383973bdf5cdfde212cb09e9a581cf10820be \
|
||||
--hash=sha256:66ce7632c22d837c95301830e111ad0128a32b8207533b60896a96c4915192ea \
|
||||
--hash=sha256:6b3ace8194b0e5204818c92802dcdca7fc6d88aabbb799d7c795540d9cd6d292 \
|
||||
--hash=sha256:6f2eeda33a839975441c86a4119e1383c50b47faf0cbb5176985565c6bb02c33 \
|
||||
--hash=sha256:7027560ee92211647d0d34e3f7cd6f50da56399d26a9c8ad0da286d3869a53f3 \
|
||||
--hash=sha256:7283d57845ecf5a163403eb0702dfc220cc4fbdd18919cb5ccea4f95ee1cdab4 \
|
||||
--hash=sha256:7a5f930472650a82629163023e630d160863fce524c616f4e5186e5de9d9a49b \
|
||||
--hash=sha256:7bfb192b3f4b9e8a89b6277b6ce787564f62cfd272055f6e685726b111dc7826 \
|
||||
--hash=sha256:811ff8e9c313ab425368bcbb36e5c4ebd7108c2bbf4e4089cfbb0b01eff63fac \
|
||||
--hash=sha256:8233f2947cf85404441fd7e0085f53b10c93e0ee78611099b5c7237e36aacbf7 \
|
||||
--hash=sha256:82cf5301172168103724d49a1444d3378cb20cdee30b116a1bd6031236298a5d \
|
||||
--hash=sha256:8358a950c8909158e3df31538a7e4edc2d7265a7c54b47f0864d9e5bae9dcebf \
|
||||
--hash=sha256:85bb3611ff1802f3ee7fdd7dbff26b56f343fb432d57a4728fdd49b6ef35e2f4 \
|
||||
--hash=sha256:86e1a4418c6cd97d60c95c71164158eaf7324fae7b0923264016baa993eba6fc \
|
||||
--hash=sha256:8b9bab013d1c7a79d3501ff86d0bc9c31bf587db4551677b96bec07df78c6b15 \
|
||||
--hash=sha256:8c5dac79fa1614d1e06ca695109c6105923bd9c7d1d6c918d4e637b7e6b32fd3 \
|
||||
--hash=sha256:8d0820e8192167f80d88d64038e609c31452eeca865b4e1d9950a27a4609b00b \
|
||||
--hash=sha256:8daafc69c93ee8a0204506a3b6b30f586ef54028f52aeeeb5c4cfc5184fd5914 \
|
||||
--hash=sha256:9037063db01f09b09e237c282b6792bd4da634b5402c4e7f0c61effed7701a04 \
|
||||
--hash=sha256:905a0ed8ea6f2d61c1738835f99b699348d7857379083e5fc497fa0c967a407c \
|
||||
--hash=sha256:90884113d8b48f760e9587002789ddd741e76ab9f89518cd1e43b1f1a52ec44b \
|
||||
--hash=sha256:91a06d2e259ecfbd8c901d70c3c507900458498142b3026a296b7de4d1322cc9 \
|
||||
--hash=sha256:926c9541b14b12b1681dca8a0b75feb510b06c6341b70a8e500c2fdcff837cce \
|
||||
--hash=sha256:9401557acd873c3a7f3eb9383edef8ac4968f9510e340f4808d427e75667e7b4 \
|
||||
--hash=sha256:9551187363ffc0de2a00b2e47c25aeaeb1020b69b668762966df15fc5659dd5a \
|
||||
--hash=sha256:962ccbab7b642487b1d8b7df90ef677e03134cf1fd8880bf698649b22a69371f \
|
||||
--hash=sha256:97e7cf2be5c77b7d1a9713a05605d49460d02c6078d38d8bef3cbe323c548424 \
|
||||
--hash=sha256:9aa768456404a8bf48a4406685ac2bec8e72b62c69313734fa3b73cf33b3a894 \
|
||||
--hash=sha256:9bc519fbf2b7578398853d815009ae5e4d4603d12f4e3f91da8c06852d3da3e9 \
|
||||
--hash=sha256:9d56801be94b86a9da183e5f3766e6310752b99ff647e38b09a9500d88e46e76 \
|
||||
--hash=sha256:9f444c499b3eefd3a92e348059471ea0c3a6e303d9c1cec09fa748fd9f895201 \
|
||||
--hash=sha256:9fa8ae11da9e2b3126c6426f147e0fba88d96d65921799bb30c6abd1cb2c97fb \
|
||||
--hash=sha256:a0f62d0a58f4e7da165457e995725421e0064f2255d8eccebc49f41bbc23b109 \
|
||||
--hash=sha256:a396dcc17e5a0b164dbe026896245a4fa9ff402edca1dff0be3d53a517f74de4 \
|
||||
--hash=sha256:aaa2a54443eff1950ba5ddc6b6ccda0d9c84a364276a62f969bdf2a390650848 \
|
||||
--hash=sha256:ad785e92e6dc634c21555edc8bd6b64957ab844541bcb96a1366c202951ae526 \
|
||||
--hash=sha256:af8244b2bef6aaad6d92cda81372de7f8c8d36c9f0c3ea36e827c60e7d9467a0 \
|
||||
--hash=sha256:b078afbc25f3a1436c7a1d2cd3e322497ee99615ba97c563566fdf46aff1ee01 \
|
||||
--hash=sha256:b2f69dec1725e79a012d920df1707de5caf7ed5e08f3be4435e25803efc47458 \
|
||||
--hash=sha256:b8458003118a712e66286df6a707db01c52c0f52f7db8e4a38f0da1d3b94fc4e \
|
||||
--hash=sha256:bb63e0198ca18aad131c089b9204c23079c3afa95487e561f4c522d519e55aba \
|
||||
--hash=sha256:bfec22eab3c8cc2ceec0248aec886624116dc079afa027ecc8ad4a7e62010f8a \
|
||||
--hash=sha256:c1747f85cee84c26985853c6f3d9bd3e75da5212912443fa111c113b9c246f39 \
|
||||
--hash=sha256:c1b3f518abeca3aa13c712fd202306e145abf59a18b094a6bafb2d2bbf59192c \
|
||||
--hash=sha256:c50f2528cf200c5eed56faf3f4e22fcd5f38c157a8b78576e6ba3168ec35f000 \
|
||||
--hash=sha256:c68fcd102d71ea85c5b2dfac3f4f8476eff42a9e078fd5faefff6d145063536b \
|
||||
--hash=sha256:c7a7bd4e39e8e4c12c39cd480356842b6a8a06e41b23a55a5e3e191718838ddf \
|
||||
--hash=sha256:c94f0688e7b8d0a67abf40e57a7eaaecd17cc9586706a31b76c031f63df052b4 \
|
||||
--hash=sha256:cbaf13819775b7f769bf4a1f066cb6df7a28d4480081a589828ef190226881cd \
|
||||
--hash=sha256:cd2213145bcc2ba85884d0ac63d222fece9209678f77b9b4d76f054c561adb28 \
|
||||
--hash=sha256:ce5c1d2a8b27468f433ca974829c44060b8097eedc39933e3c206a90ee49c4a9 \
|
||||
--hash=sha256:d396ec2b979760aaf3218e76c24e65bd0aca24983298653b3a9d7a45f9e47b30 \
|
||||
--hash=sha256:d51026d73fcfd93610abc7b27789c26b313920fcfb20e27462d74a7f8b06e983 \
|
||||
--hash=sha256:d80ee3d731373b24cebbc10d689ca4ee1875caf0d5703a245db18efd4dd37fc1 \
|
||||
--hash=sha256:d995260fdf4e1db774581b4900e0f832abe3c7c84996726bbc161b19c8f29e76 \
|
||||
--hash=sha256:da4b951fe36dc7c3a1ccb4e3cd1747c3542b8c9ceede8fc86cae054e764485f5 \
|
||||
--hash=sha256:daa27d92c36f24388fe3ad306b174781c747627f134452e4f128ea00ce1fe8c4 \
|
||||
--hash=sha256:db06ffe51636ffe9ca531fe9023dd64bdd794be8754cb5df57c5498ae5b518a7 \
|
||||
--hash=sha256:e0d65b8c354be7fb5f720c3caa8bc940bc2d20ce749c8e06135f07f8ed95dd7c \
|
||||
--hash=sha256:e68b7a074f65a2fd746c52a7ce6142ab7006074ac269ace0c25cd8ba171f8066 \
|
||||
--hash=sha256:e739fee756ba1010f8bcccb534252e85a35fe45ae92c295a06059ce58b74ccd3 \
|
||||
--hash=sha256:e846ae7835bf0703ae43f534ab79a867146dadd59dc9ca5c8b53d5c8f7c9ef02 \
|
||||
--hash=sha256:e9c26f834c65f5752f3f06cb08cb86a913ceb7274d0db6e267808a708b46bc89 \
|
||||
--hash=sha256:ea793e075b70290d89d8142074262885d3f7da19634845135751bd6344f73b50 \
|
||||
--hash=sha256:f027324c56cd5406ca49c124b0db10e56c69064fec039acc571c29020cc87c76 \
|
||||
--hash=sha256:f13a646d65d09fbf1bc6b3a9635d30095c8e7e5cc419ff35ecc563c5fd04cd49 \
|
||||
--hash=sha256:f47286a97f0bc9b8859519809077b91b2cefe4ae47fcbf5e466a009c1c5d742b \
|
||||
--hash=sha256:f747929cf940cddb5b3668a390056ddd5ba2e5010615ea2dcf4f9c4f3ab8791d \
|
||||
--hash=sha256:f99626688942fb746e545232e7726926f3be91b5975f8b55327665fafda991c7 \
|
||||
--hash=sha256:f9fa868638bf362d3d138ea55829cefb3d5f4b0d7f142234382a15e2485dbec4 \
|
||||
--hash=sha256:fbdb89b3e1c94a30cc5edfce477c6e6a5dc4d8f84665b455c27582f211a1c72c \
|
||||
--hash=sha256:fc010ab034c8c7452522748bf937df58020d256ccae0874463d1f4d01758af8e \
|
||||
--hash=sha256:fc3e9034a63de20e15e8ade85358bc6efc614008cab72898b4b4952bea0509ff \
|
||||
--hash=sha256:fd8b3d9fd264be37976686c7f65cd52a83f5e84f4bfd2adf9c1d469676bbb6ae
|
||||
# via pydantic
|
||||
pygments==2.20.0 \
|
||||
--hash=sha256:6757cd03768053ff99f3039c1a36d6c0aa0b263438fcab17520b30a303a82b5f \
|
||||
--hash=sha256:81a9e26dd42fd28a23a2d169d86d7ac03b46e2f8b59ed4698fb4785f946d0176
|
||||
# via pytest
|
||||
pyjwt==2.13.0 \
|
||||
--hash=sha256:41571c89ca91598c79e8ef18a2d07367d4810fbbd6f637794879baf1b7703423 \
|
||||
--hash=sha256:66adcc2aff09b3f1bbd95fc1e1577df8ac8723c978552fd43304c8a290ac5728
|
||||
# via -r backend/requirements.txt
|
||||
pymupdf==1.28.2 \
|
||||
--hash=sha256:2e1b574c0fd2cb238021033fd3c0f9c4388816638df064e4bfb56d9d81736dc8 \
|
||||
--hash=sha256:3050a233dde1211efe89ada74e2add6238436434159f46097a1423aad2842545 \
|
||||
--hash=sha256:397d6715c1f0df7548a92d0afd8ce370fc48fa47aeefac16be2bc04a16a8227f \
|
||||
--hash=sha256:530ef543a3885b3b81cb72a854e7c5a625a9233201221132bb6c31698c6a2bdb \
|
||||
--hash=sha256:5e0be7908a715aa20333caddd73f1d6f01e4cd0c26e869fa2dd0b7f344da2249 \
|
||||
--hash=sha256:5fc315b425ff1f7afdd1ea2f348205cb19b806767daae7ce4d64115799c2bae1 \
|
||||
--hash=sha256:7113846b35dbf0a033f088e4f4fb543dabeb4b0b12c112966a1ca1ee2d5eacae \
|
||||
--hash=sha256:ebd244918798502d7b4504c90410d1711a4d7675a32584ca30f1bab419ecbffe \
|
||||
--hash=sha256:f89fb2d86d07d643a269f17a093105057e20c79c1d06c103b53600067b6d2b01 \
|
||||
--hash=sha256:fd481ed48bef56305c41fb7e05a055c03345c899c7b101dad086258b438f8168 \
|
||||
--hash=sha256:ffe91a24edc75c80da2a4b62f50fc0f54632d34fc8fe4cbc48e5c7ff07cf8fb4
|
||||
# via -r backend/requirements.txt
|
||||
pytest==8.4.2 \
|
||||
--hash=sha256:86c0d0b93306b961d58d62a4db4879f27fe25513d4b969df351abdddb3c30e01 \
|
||||
--hash=sha256:872f880de3fc3a5bdc88a11b39c9710c3497a547cfa9320bc3c5e62fbf272e79
|
||||
# via -r backend/requirements.txt
|
||||
python-dotenv==1.2.2 \
|
||||
--hash=sha256:1d8214789a24de455a8b8bd8ae6fe3c6b69a5e3d64aa8a8e5d68e694bbcb285a \
|
||||
--hash=sha256:2c371a91fbd7ba082c2c1dc1f8bf89ca22564a087c2c287cd9b662adde799cf3
|
||||
# via -r backend/requirements.txt
|
||||
sniffio==1.3.1 \
|
||||
--hash=sha256:2f6da418d1f1e0fddd844478f41680e794e6051915791a034ff65e5f100525a2 \
|
||||
--hash=sha256:f4324edc670a0f49750a81b895f35c3adb843cca46f0530f79fc1babb23789dc
|
||||
# via openai
|
||||
sqlalchemy==2.0.52 \
|
||||
--hash=sha256:11560064cc4696e772298b6221ede59e646386d9f2a85d549365473b972f7850 \
|
||||
--hash=sha256:1b2d9e507a458832adcfbd8af6e2036ddf069b7710b799448542ebccae2dceee \
|
||||
--hash=sha256:1b92a1e23ed40022081217b40d2d1feba4f77064e69ef4f39f68bcbbd148452a \
|
||||
--hash=sha256:2d5e53e36e37129fe0be8b9d08b6e4052c10a963ee6cda56c8c10dcc194b99ca \
|
||||
--hash=sha256:2e15b1d1116a64fc399b8c2694a83f3e792fdc58df28514a81e1dc4f8cf22729 \
|
||||
--hash=sha256:2eb3c6a64b1bfe6704777cfd504e7b8ad093a5f3e03ce67663a5e6742f294e43 \
|
||||
--hash=sha256:2f5fa2b2aca75d2c7f36db3a8dd04717b6fbfd1a964fb32bdeae16698e475ab3 \
|
||||
--hash=sha256:2f9eccf8793c8c3f8dd2dfd11b9e400cb27d1d19370ef732b66017e212107822 \
|
||||
--hash=sha256:309cc8ba50fc5d2174189dfcd49cdf7aa711f8346afcff19f2642ae4fc449c14 \
|
||||
--hash=sha256:37a4d548327b6cab9c7d8cdb4e0e82feabee0110c4d150059068e2d1cfbd99ee \
|
||||
--hash=sha256:3b81b8363a919ce53453591cdb93702e6bd54ade6c4fa2f468fc053baee5ed89 \
|
||||
--hash=sha256:3c95c3044edddb65e4a2f7194ec52ca5a9736f72d33ca3a6fa4196aedcc689fd \
|
||||
--hash=sha256:410d52be41d17f1a236d19520fbe776257dc16516ed06bd16d433311842aefd9 \
|
||||
--hash=sha256:4699dbb8d396d199e7e78fd4d525e3ad3d6008a9c8c0160b87e74c606c2c3736 \
|
||||
--hash=sha256:46f0c46f0d360d727b84660b26c62b295d82306ec2c82b701e97747d2c6dcbe1 \
|
||||
--hash=sha256:49565daf5af554f538e23aef1fc81a95a4e49658f152285e45c02f5fc44f04cd \
|
||||
--hash=sha256:4b89e93bb89eabdbea9d5d3fa2d6cc6544e733c33064339f91e5292480cf130e \
|
||||
--hash=sha256:50bff43b632a56fbf5ed9afdd76307e1512b62051bcd5afb341ae67205bbb6c8 \
|
||||
--hash=sha256:5e2d46356ac2ccb7d268ab6c2319ac6a2b42f1b8d5fd8bd3d46855cd82abee97 \
|
||||
--hash=sha256:5f8438a98d49424acf69d0d53c0a522951dfe49a6f2d86417fbb37ad3066ab43 \
|
||||
--hash=sha256:651d6d8782e80679e6151707c7b490834d46ada526328895abf567f25e63d29c \
|
||||
--hash=sha256:6c1b7ed45bf87b214e0a9def9c2313949067efe6269db5ef18d542ee13250af7 \
|
||||
--hash=sha256:765f439da5bc8696973bc0c8a31fae0912ac3ff1cb9d66246a6b2728ee4fbbc8 \
|
||||
--hash=sha256:77a247d3fd179f6583171e7e0e98f40dc6642ed4f655557515a5a7e25923e9a4 \
|
||||
--hash=sha256:7a0d48c4b80717c61385b4e966e087c839a66cfd7b780641dcb428f4dba65608 \
|
||||
--hash=sha256:812bae5138bfc0aa46fb0686da0fc7f581f68e2bbb05bc24c3713bebaedd1437 \
|
||||
--hash=sha256:8738008376d22f30f411ea3efecf39b51110b6996d80bb73786f30bcfdd5fd3b \
|
||||
--hash=sha256:8cf993f065bc04caa5000b339e8d9d6f3d9d00251511f850147c516c9e07115f \
|
||||
--hash=sha256:923bb183c1dc64fdf7b717965e3d59938ec4f8b8710b419a21ce403e5da9a9e1 \
|
||||
--hash=sha256:9255ceb65a80c1b001129060b63ee776a2e9c288be3b662be36dfbb888fffdcd \
|
||||
--hash=sha256:938325a5373267afc53bfbe72983b20fbd64ca47842aac62433c3da1137ecff1 \
|
||||
--hash=sha256:9876b09b9f1ce7398b0ffece585c0a911244c53191187341f6bcae640e133751 \
|
||||
--hash=sha256:a593db51b3bae75db17a5738ad5f992244b3a03863f83c28117ee482c6a3f76d \
|
||||
--hash=sha256:a7438774e1091192fc50a2bd8ceff5c596912d00ecd46587e88effdea7826101 \
|
||||
--hash=sha256:ab66fa9618269390d4dfa222f2f2f88f7bc4bf5da13905131b818217db7e8057 \
|
||||
--hash=sha256:ab9da41e61b9979b910499d633b241df20c51ee5037e5405b11c2faac3cbe1a2 \
|
||||
--hash=sha256:afda3ec521d0517d0de783fc70030775841900896d832de5bbd066549290470e \
|
||||
--hash=sha256:b08cddb8989775e3c88799d86704bdfc3ee6e9846118201aa5997f16f27e3a15 \
|
||||
--hash=sha256:be8c49131665dfe2cc74c498aa1240ffb548d0fd901325dd11c2c7a18956f727 \
|
||||
--hash=sha256:c1e61d08bdf4ee2f41024569e3400de7d6734ba498144766b11260936ccfa582 \
|
||||
--hash=sha256:c63bda077685c85ca513286547a531ba57e7a68cf0a7ed3bafcc2bbd18896f4d \
|
||||
--hash=sha256:cce4922535db73f9dbb91e3db2b3e851ac629467fd1ebd8e354a60e369521c63 \
|
||||
--hash=sha256:cd9206024b8602e7518bbaf44016c29e0045722f09328d8e654941023920d0b3 \
|
||||
--hash=sha256:cef328349452ae152637df4d11ce5a0919ecdf0a363e16c830c3518ee33bde72 \
|
||||
--hash=sha256:de89de5b5798cafdd7ef7b7b804acec246d6152922128fd9d156cd1701271aff \
|
||||
--hash=sha256:df8f213ceb485d8227b74935eb87ba0d80169a8401eba7835da6e30d6727dac4 \
|
||||
--hash=sha256:dfe9ce533dbe4d0a2ae1486546619bd30b76bcd670539a44d910361376175f5e \
|
||||
--hash=sha256:e0c3ce43907374889f3352bdcc6195c970148a2cb71574cd0237a5071a37fb6c \
|
||||
--hash=sha256:e49f51a5d59857a7a0dcaf9469febf7197d9394bd88f00d69c2c4e848112cdbf \
|
||||
--hash=sha256:f1c850792a3b25a3ad74dade3f05e4f402cdebfea27438bcadafaa1617f77bcc \
|
||||
--hash=sha256:f2b09029ef6f260409eefa5dc2b8276f6c3d7b892bfb50d50e8f852257d4a6b4 \
|
||||
--hash=sha256:f4d4f7afc682961dc567db70e00a7b5bd81ccd3743c46199b0257f0744902dde
|
||||
# via
|
||||
# -r backend/requirements.txt
|
||||
# alembic
|
||||
tqdm==4.70.0 \
|
||||
--hash=sha256:55b0b0dbd97462d06ebee91e4dac24ed4d4702be82b24f07e6c1d27e08cea220 \
|
||||
--hash=sha256:7f585706bfddbdebf89daac705b2dfcc16890130727d3197ca62c732b4310953
|
||||
# via openai
|
||||
typing-extensions==4.16.0 \
|
||||
--hash=sha256:481caa481374e813c1b176ada14e97f1f67a4539ce9cfeb3f350d78d6370c2e8 \
|
||||
--hash=sha256:dc983d19a509c94dba722ee6abd33940f7c05a89e243c47e907eb4db6f1a43e5
|
||||
# via
|
||||
# alembic
|
||||
# anyio
|
||||
# openai
|
||||
# psycopg
|
||||
# pydantic
|
||||
# pydantic-core
|
||||
# sqlalchemy
|
||||
# typing-inspection
|
||||
typing-inspection==0.4.4 \
|
||||
--hash=sha256:547274fa6b0a561ccf549cc9524b999a578e737d015d8709d021f9d0d13bea47 \
|
||||
--hash=sha256:65b8397ba37ccbce054456aaccddfc91e6e3083c92824df348d96ca832f3f147
|
||||
# via pydantic
|
||||
tzdata==2026.3 ; sys_platform == 'win32' \
|
||||
--hash=sha256:4a1518b8993086a7982523e071643f3c0e5f213e75b21318e78bcabfff9d1415 \
|
||||
--hash=sha256:dc096730c87af6cab1b171c9d532be840741ff5d459015e7f6947bd7d7e54931
|
||||
# via psycopg
|
||||
werkzeug==3.1.8 \
|
||||
--hash=sha256:63a77fb8892bf28ebc3178683445222aa500e48ebad5ec77b0ad80f8726b1f50 \
|
||||
--hash=sha256:9bad61a4268dac112f1c5cd4630a56ede601b6ed420300677a869083d70a4c44
|
||||
# via
|
||||
# -r backend/requirements.txt
|
||||
# flask
|
||||
# flask-cors
|
||||
@@ -1,9 +1,14 @@
|
||||
flask>=3.0.0
|
||||
flask-cors>=6.0.0
|
||||
PyJWT>=2.8.0
|
||||
python-dotenv>=1.0.0
|
||||
openai>=1.0.0
|
||||
PyMuPDF>=1.24.0
|
||||
charset-normalizer>=3.0.0
|
||||
pydantic>=2.0.0
|
||||
werkzeug>=3.0.0
|
||||
flask==3.1.3
|
||||
flask-cors==6.0.5
|
||||
PyJWT==2.13.0
|
||||
python-dotenv==1.2.2
|
||||
openai==2.24.0
|
||||
PyMuPDF==1.28.2
|
||||
charset-normalizer==3.4.4
|
||||
pydantic==2.13.4
|
||||
werkzeug==3.1.8
|
||||
gunicorn==26.0.0
|
||||
pytest==8.4.2
|
||||
SQLAlchemy==2.0.52
|
||||
alembic==1.19.1
|
||||
psycopg[binary]==3.3.4
|
||||
+2
-1
@@ -9,9 +9,10 @@ sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
from app import create_app # noqa: E402
|
||||
from app.config import Config # noqa: E402
|
||||
|
||||
app = create_app()
|
||||
|
||||
|
||||
def main() -> None:
|
||||
app = create_app()
|
||||
app.run(
|
||||
host=Config.FLASK_HOST,
|
||||
port=Config.FLASK_PORT,
|
||||
|
||||
@@ -0,0 +1,571 @@
|
||||
"""Fail-closed, idempotent JSON-store to relational importer.
|
||||
|
||||
Dry-run is the default. Applying data requires an explicit target URL and a
|
||||
fresh backup directory. The importer validates the complete reference graph
|
||||
before opening a target transaction and never overwrites conflicting rows.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import sys
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
# Allow the documented ``python scripts/migrate_json_to_postgres.py`` form to
|
||||
# resolve the backend's ``app`` package when this file is executed directly.
|
||||
if __package__ in (None, ""):
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
from app.db import create_db_engine, create_session_factory
|
||||
from app.models import Group, Message, Organization, Persona, TrainingSession, User
|
||||
from app.services.store import revealable_view
|
||||
|
||||
MAX_SOURCE_FILE_BYTES = 10 * 1024 * 1024
|
||||
_ID_RE = re.compile(r"^[A-Za-z0-9_.:-]{1,64}$")
|
||||
_COLLECTIONS = ("orgs", "users", "groups", "sessions", "my_personas")
|
||||
|
||||
|
||||
class ImportValidationError(ValueError):
|
||||
"""Raised when source data or a target conflict fails closed."""
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class SourceSnapshot:
|
||||
orgs: list[dict[str, Any]]
|
||||
users: list[dict[str, Any]]
|
||||
groups: list[dict[str, Any]]
|
||||
sessions: list[dict[str, Any]]
|
||||
my_personas: list[dict[str, Any]]
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ImportRow:
|
||||
table: str
|
||||
key: str
|
||||
model: type[Any]
|
||||
values: dict[str, Any]
|
||||
|
||||
|
||||
def _read_collection(source_root: Path, collection: str) -> list[dict[str, Any]]:
|
||||
directory = source_root / collection
|
||||
if not directory.exists():
|
||||
return []
|
||||
if directory.is_symlink() or not directory.is_dir():
|
||||
raise ImportValidationError(f"source collection is not a directory: {collection}")
|
||||
|
||||
rows: list[dict[str, Any]] = []
|
||||
for path in sorted(directory.glob("*.json")):
|
||||
if path.is_symlink():
|
||||
raise ImportValidationError(f"symlink source file rejected: {collection}")
|
||||
try:
|
||||
if path.stat().st_size > MAX_SOURCE_FILE_BYTES:
|
||||
raise ImportValidationError(f"source file exceeds bounded size: {collection}")
|
||||
value = json.loads(path.read_text(encoding="utf-8"))
|
||||
except ImportValidationError:
|
||||
raise
|
||||
except (OSError, UnicodeError, json.JSONDecodeError) as exc:
|
||||
raise ImportValidationError(f"invalid source JSON in {collection}") from exc
|
||||
if not isinstance(value, dict):
|
||||
raise ImportValidationError(f"source record must be an object: {collection}")
|
||||
rows.append(value)
|
||||
return rows
|
||||
|
||||
|
||||
def _reject_nonempty_audit(source_root: Path) -> None:
|
||||
path = source_root / "audit" / "audit.jsonl"
|
||||
if not path.exists():
|
||||
return
|
||||
if path.is_symlink():
|
||||
raise ImportValidationError("symlink audit source rejected")
|
||||
try:
|
||||
if path.stat().st_size > MAX_SOURCE_FILE_BYTES:
|
||||
raise ImportValidationError("audit source exceeds bounded size")
|
||||
has_entries = any(line.strip() for line in path.read_text(encoding="utf-8").splitlines())
|
||||
except ImportValidationError:
|
||||
raise
|
||||
except (OSError, UnicodeError) as exc:
|
||||
raise ImportValidationError("audit source cannot be read") from exc
|
||||
if has_entries:
|
||||
raise ImportValidationError(
|
||||
"audit JSONL contains entries; import audit events through the S4.6 PostgreSQL audit gate"
|
||||
)
|
||||
|
||||
|
||||
def load_source(source_root: Path) -> SourceSnapshot:
|
||||
"""Read only supported JSON collections; credentials/config files are ignored."""
|
||||
root = source_root.expanduser().resolve()
|
||||
if not root.exists() or not root.is_dir():
|
||||
raise ImportValidationError("source data directory is missing")
|
||||
_reject_nonempty_audit(root)
|
||||
values = {collection: _read_collection(root, collection) for collection in _COLLECTIONS}
|
||||
return SourceSnapshot(**values)
|
||||
|
||||
|
||||
def _canonical_checksum(snapshot: SourceSnapshot) -> str:
|
||||
payload = {collection: getattr(snapshot, collection) for collection in _COLLECTIONS}
|
||||
encoded = json.dumps(
|
||||
payload,
|
||||
ensure_ascii=False,
|
||||
sort_keys=True,
|
||||
separators=(",", ":"),
|
||||
).encode("utf-8")
|
||||
return hashlib.sha256(encoded).hexdigest()
|
||||
|
||||
|
||||
def _record_id(record: dict[str, Any], collection: str) -> str:
|
||||
value = record.get("id")
|
||||
if not isinstance(value, str) or not _ID_RE.fullmatch(value):
|
||||
raise ImportValidationError(f"invalid id in {collection} collection")
|
||||
return value
|
||||
|
||||
|
||||
def _required_string(value: object, field: str, collection: str) -> str:
|
||||
if not isinstance(value, str) or not value.strip():
|
||||
raise ImportValidationError(f"invalid {field} in {collection} collection")
|
||||
return value.strip()
|
||||
|
||||
|
||||
def _optional_string(value: object, field: str, collection: str) -> str | None:
|
||||
if value is None or value == "":
|
||||
return None
|
||||
return _required_string(value, field, collection)
|
||||
|
||||
|
||||
def _timestamp(value: object, field: str, collection: str) -> datetime | None:
|
||||
if value is None or value == "":
|
||||
return None
|
||||
if not isinstance(value, str):
|
||||
raise ImportValidationError(f"invalid {field} timestamp in {collection} collection")
|
||||
try:
|
||||
parsed = datetime.fromisoformat(value.replace("Z", "+00:00"))
|
||||
except ValueError as exc:
|
||||
raise ImportValidationError(f"invalid {field} timestamp in {collection} collection") from exc
|
||||
if parsed.tzinfo is None:
|
||||
parsed = parsed.replace(tzinfo=timezone.utc)
|
||||
return parsed.astimezone(timezone.utc)
|
||||
|
||||
|
||||
def _json_object(value: object, field: str, collection: str) -> dict[str, Any] | None:
|
||||
if value is None:
|
||||
return None
|
||||
if not isinstance(value, dict):
|
||||
raise ImportValidationError(f"invalid {field} object in {collection} collection")
|
||||
return value
|
||||
|
||||
|
||||
def _bool_value(value: object, field: str, collection: str, *, default: bool) -> bool:
|
||||
if value is None:
|
||||
return default
|
||||
if not isinstance(value, bool):
|
||||
raise ImportValidationError(f"invalid {field} in {collection} collection")
|
||||
return value
|
||||
|
||||
|
||||
def _int_value(value: object, field: str, collection: str, *, default: int, minimum: int = 0) -> int:
|
||||
if value is None:
|
||||
return default
|
||||
if isinstance(value, bool) or not isinstance(value, int) or value < minimum:
|
||||
raise ImportValidationError(f"invalid {field} in {collection} collection")
|
||||
return value
|
||||
|
||||
|
||||
def _validate_unique(values: list[str], label: str) -> None:
|
||||
if len(values) != len(set(values)):
|
||||
raise ImportValidationError(f"duplicate {label} in source")
|
||||
|
||||
|
||||
def _scenario_payload(row: dict[str, Any]) -> dict[str, Any] | None:
|
||||
raw = row.get("scenario")
|
||||
if isinstance(raw, dict):
|
||||
payload = dict(raw)
|
||||
if row.get("locale") is not None:
|
||||
payload.setdefault("locale", row.get("locale"))
|
||||
if row.get("persona_name") is not None:
|
||||
payload.setdefault("persona_name", row.get("persona_name"))
|
||||
if row.get("persona_meta") is not None:
|
||||
payload.setdefault("persona_meta", row.get("persona_meta"))
|
||||
return payload
|
||||
if raw is None and not any(row.get(key) is not None for key in ("locale", "persona_name", "persona_meta")):
|
||||
return None
|
||||
return {
|
||||
"scenario": raw,
|
||||
"locale": row.get("locale"),
|
||||
"persona_name": row.get("persona_name"),
|
||||
"persona_meta": row.get("persona_meta") or {},
|
||||
}
|
||||
|
||||
|
||||
def _report_text(value: object) -> str | None:
|
||||
if value is None:
|
||||
return None
|
||||
if isinstance(value, str):
|
||||
return value
|
||||
return json.dumps(value, ensure_ascii=False, sort_keys=True)
|
||||
|
||||
|
||||
def _derived_message_id(session_id: str, sequence: int) -> str:
|
||||
digest = hashlib.sha256(f"{session_id}\0{sequence}".encode("utf-8")).hexdigest()[:32]
|
||||
return f"msg-{digest}"
|
||||
|
||||
|
||||
def _build_rows(snapshot: SourceSnapshot) -> list[ImportRow]:
|
||||
orgs_by_id: dict[str, dict[str, Any]] = {}
|
||||
users_by_id: dict[str, dict[str, Any]] = {}
|
||||
groups_by_id: dict[str, dict[str, Any]] = {}
|
||||
personas_by_id: dict[str, tuple[str, dict[str, Any]]] = {}
|
||||
rows: list[ImportRow] = []
|
||||
|
||||
for record in snapshot.orgs:
|
||||
org_id = _record_id(record, "orgs")
|
||||
if org_id in orgs_by_id:
|
||||
raise ImportValidationError("duplicate organization id in source")
|
||||
orgs_by_id[org_id] = record
|
||||
values = {
|
||||
"id": org_id,
|
||||
"name": _required_string(record.get("name"), "name", "orgs"),
|
||||
"plan": _required_string(record.get("plan", "trial"), "plan", "orgs"),
|
||||
"seats": _int_value(record.get("seats"), "seats", "orgs", default=5, minimum=1),
|
||||
"active": _bool_value(record.get("active"), "active", "orgs", default=True),
|
||||
}
|
||||
created_at = _timestamp(record.get("created_at"), "created_at", "orgs")
|
||||
if created_at is not None:
|
||||
values["created_at"] = created_at
|
||||
rows.append(ImportRow("organizations", org_id, Organization, values))
|
||||
|
||||
usernames: list[str] = []
|
||||
emails: list[str] = []
|
||||
for record in snapshot.users:
|
||||
user_id = _record_id(record, "users")
|
||||
if user_id in users_by_id:
|
||||
raise ImportValidationError("duplicate user id in source")
|
||||
org_id = _required_string(record.get("org_id"), "org_id", "users")
|
||||
if org_id not in orgs_by_id:
|
||||
raise ImportValidationError("user references missing organization")
|
||||
username = _required_string(record.get("username"), "username", "users").lower()
|
||||
password_hash = _required_string(record.get("password_hash"), "password_hash", "users")
|
||||
email = _optional_string(record.get("email"), "email", "users")
|
||||
usernames.append(username)
|
||||
if email:
|
||||
emails.append(email.lower())
|
||||
users_by_id[user_id] = record
|
||||
values = {
|
||||
"id": user_id,
|
||||
"org_id": org_id,
|
||||
"username": username,
|
||||
"password_hash": password_hash,
|
||||
"email": email.lower() if email else None,
|
||||
"name": _required_string(record.get("name") or username, "name", "users"),
|
||||
"role": _required_string(record.get("role", "user"), "role", "users"),
|
||||
"active": _bool_value(record.get("active"), "active", "users", default=True),
|
||||
"must_setup": _bool_value(record.get("must_setup"), "must_setup", "users", default=False),
|
||||
"accepted_terms": _bool_value(record.get("accepted_terms"), "accepted_terms", "users", default=False),
|
||||
"auth_version": _int_value(record.get("auth_version"), "auth_version", "users", default=0),
|
||||
}
|
||||
accepted_at = _timestamp(record.get("accepted_terms_at"), "accepted_terms_at", "users")
|
||||
created_at = _timestamp(record.get("created_at"), "created_at", "users")
|
||||
if accepted_at is not None:
|
||||
values["accepted_terms_at"] = accepted_at
|
||||
if created_at is not None:
|
||||
values["created_at"] = created_at
|
||||
rows.append(ImportRow("users", user_id, User, values))
|
||||
_validate_unique(usernames, "username")
|
||||
_validate_unique(emails, "email")
|
||||
|
||||
persona_records: list[tuple[str, str, dict[str, Any], str]] = []
|
||||
for record in snapshot.groups:
|
||||
group_id = _record_id(record, "groups")
|
||||
if group_id in groups_by_id:
|
||||
raise ImportValidationError("duplicate group id in source")
|
||||
org_id = _required_string(record.get("org_id"), "org_id", "groups")
|
||||
if org_id not in orgs_by_id:
|
||||
raise ImportValidationError("cross-tenant group reference: missing organization")
|
||||
creator_id = _optional_string(
|
||||
record.get("creator_user_id") or record.get("creator_id"),
|
||||
"creator_user_id",
|
||||
"groups",
|
||||
)
|
||||
owner_id = _optional_string(record.get("owner_user_id"), "owner_user_id", "groups")
|
||||
for label, user_id in (("creator", creator_id), ("owner", owner_id)):
|
||||
if user_id is None:
|
||||
continue
|
||||
user = users_by_id.get(user_id)
|
||||
if user is None or user.get("org_id") != org_id:
|
||||
raise ImportValidationError(f"cross-tenant group reference: {label}")
|
||||
groups_by_id[group_id] = record
|
||||
values = {
|
||||
"id": group_id,
|
||||
"org_id": org_id,
|
||||
"owner_user_id": owner_id,
|
||||
"creator_user_id": creator_id,
|
||||
"name": _required_string(record.get("name") or record.get("title"), "name", "groups"),
|
||||
"status": _required_string(record.get("status", "draft"), "status", "groups"),
|
||||
"input_json": _json_object(record.get("input"), "input", "groups"),
|
||||
"sales_kit_json": _json_object(record.get("sales_kit"), "sales_kit", "groups"),
|
||||
"report_text": _report_text(record.get("report")),
|
||||
}
|
||||
created_at = _timestamp(record.get("created_at"), "created_at", "groups")
|
||||
updated_at = _timestamp(record.get("updated_at"), "updated_at", "groups")
|
||||
if created_at is not None:
|
||||
values["created_at"] = created_at
|
||||
if updated_at is not None:
|
||||
values["updated_at"] = updated_at
|
||||
rows.append(ImportRow("groups", group_id, Group, values))
|
||||
personas = record.get("personas", [])
|
||||
if not isinstance(personas, list):
|
||||
raise ImportValidationError("group personas must be a list")
|
||||
for persona in personas:
|
||||
if not isinstance(persona, dict):
|
||||
raise ImportValidationError("persona record must be an object")
|
||||
persona_records.append((group_id, org_id, persona, "groups"))
|
||||
|
||||
for record in snapshot.my_personas:
|
||||
user_id = _required_string(record.get("user_id"), "user_id", "my_personas")
|
||||
user = users_by_id.get(user_id)
|
||||
if user is None:
|
||||
raise ImportValidationError("my_personas references missing user")
|
||||
persona = record.get("persona")
|
||||
if not isinstance(persona, dict):
|
||||
raise ImportValidationError("my_personas persona must be an object")
|
||||
private_group_id = "private-" + hashlib.sha256(user_id.encode("utf-8")).hexdigest()[:24]
|
||||
if private_group_id not in groups_by_id:
|
||||
groups_by_id[private_group_id] = {"id": private_group_id}
|
||||
rows.append(
|
||||
ImportRow(
|
||||
"groups",
|
||||
private_group_id,
|
||||
Group,
|
||||
{
|
||||
"id": private_group_id,
|
||||
"org_id": user["org_id"],
|
||||
"owner_user_id": user_id,
|
||||
"creator_user_id": user_id,
|
||||
"name": f"{user.get('name') or user_id}'s private personas",
|
||||
"status": "ready",
|
||||
"input_json": {"source": "my_personas"},
|
||||
"sales_kit_json": None,
|
||||
"report_text": None,
|
||||
},
|
||||
)
|
||||
)
|
||||
persona_records.append((private_group_id, user["org_id"], persona, "my_personas"))
|
||||
|
||||
for group_id, _org_id, persona, collection in persona_records:
|
||||
persona_id = _record_id(persona, collection)
|
||||
if persona_id in personas_by_id:
|
||||
raise ImportValidationError("duplicate persona id in source")
|
||||
personas_by_id[persona_id] = (group_id, persona)
|
||||
full_persona = dict(persona)
|
||||
values = {
|
||||
"id": persona_id,
|
||||
"group_id": group_id,
|
||||
"source_persona_id": _optional_string(persona.get("source_persona_id"), "source_persona_id", collection),
|
||||
"tier": _required_string(persona.get("tier") or persona.get("intent_tier") or "B", "tier", collection),
|
||||
"public_json": revealable_view(full_persona),
|
||||
"latent_json": full_persona,
|
||||
}
|
||||
rows.append(ImportRow("personas", persona_id, Persona, values))
|
||||
|
||||
session_ids: set[str] = set()
|
||||
message_keys: set[tuple[str, int]] = set()
|
||||
for record in snapshot.sessions:
|
||||
session_id = _record_id(record, "sessions")
|
||||
if session_id in session_ids:
|
||||
raise ImportValidationError("duplicate session id in source")
|
||||
session_ids.add(session_id)
|
||||
org_id = _required_string(record.get("org_id"), "org_id", "sessions")
|
||||
user_id = _required_string(record.get("user_id"), "user_id", "sessions")
|
||||
group_id = _required_string(record.get("group_id"), "group_id", "sessions")
|
||||
persona_id = _required_string(record.get("persona_id"), "persona_id", "sessions")
|
||||
user = users_by_id.get(user_id)
|
||||
group = groups_by_id.get(group_id)
|
||||
persona = personas_by_id.get(persona_id)
|
||||
if (
|
||||
user is None
|
||||
or group is None
|
||||
or persona is None
|
||||
or user.get("org_id") != org_id
|
||||
or group.get("org_id", org_id) != org_id
|
||||
or persona[0] != group_id
|
||||
):
|
||||
raise ImportValidationError("cross-tenant session reference")
|
||||
mode = _required_string(record.get("mode") or "trainee", "mode", "sessions")
|
||||
if mode not in ("trainee", "preview"):
|
||||
raise ImportValidationError("invalid session mode")
|
||||
values = {
|
||||
"id": session_id,
|
||||
"org_id": org_id,
|
||||
"user_id": user_id,
|
||||
"group_id": group_id,
|
||||
"persona_id": persona_id,
|
||||
"mode": mode,
|
||||
"status": _required_string(record.get("status", "active"), "status", "sessions"),
|
||||
"outcome": _optional_string(record.get("outcome"), "outcome", "sessions"),
|
||||
"scenario_json": _scenario_payload(record),
|
||||
"internal_json": _json_object(record.get("internal"), "internal", "sessions"),
|
||||
"debrief_json": _json_object(record.get("debrief"), "debrief", "sessions"),
|
||||
}
|
||||
created_at = _timestamp(record.get("created_at"), "created_at", "sessions")
|
||||
updated_at = _timestamp(record.get("updated_at"), "updated_at", "sessions")
|
||||
if created_at is not None:
|
||||
values["created_at"] = created_at
|
||||
if updated_at is not None:
|
||||
values["updated_at"] = updated_at
|
||||
rows.append(ImportRow("sessions", session_id, TrainingSession, values))
|
||||
|
||||
messages = record.get("messages", [])
|
||||
if not isinstance(messages, list):
|
||||
raise ImportValidationError("session messages must be a list")
|
||||
for index, message in enumerate(messages, start=1):
|
||||
if not isinstance(message, dict):
|
||||
raise ImportValidationError("message record must be an object")
|
||||
sequence = message.get("sequence", index)
|
||||
if isinstance(sequence, bool) or not isinstance(sequence, int) or sequence < 1:
|
||||
raise ImportValidationError("message sequence must be a positive integer")
|
||||
if (session_id, sequence) in message_keys:
|
||||
raise ImportValidationError("duplicate message sequence in source")
|
||||
message_keys.add((session_id, sequence))
|
||||
role = _required_string(message.get("role"), "role", "sessions")
|
||||
text = message.get("text")
|
||||
if not isinstance(text, str):
|
||||
raise ImportValidationError("message text must be a string")
|
||||
message_values = {
|
||||
"id": _derived_message_id(session_id, sequence),
|
||||
"session_id": session_id,
|
||||
"sequence": sequence,
|
||||
"role": role,
|
||||
"text": text,
|
||||
}
|
||||
message_time = _timestamp(message.get("created_at") or message.get("ts"), "message time", "sessions")
|
||||
if message_time is not None:
|
||||
message_values["created_at"] = message_time
|
||||
rows.append(ImportRow("messages", message_values["id"], Message, message_values))
|
||||
|
||||
return rows
|
||||
|
||||
|
||||
def _values_equal(actual: object, expected: object) -> bool:
|
||||
if isinstance(actual, datetime) and isinstance(expected, datetime):
|
||||
actual_utc = actual.replace(tzinfo=timezone.utc) if actual.tzinfo is None else actual.astimezone(timezone.utc)
|
||||
expected_utc = expected.replace(tzinfo=timezone.utc) if expected.tzinfo is None else expected.astimezone(timezone.utc)
|
||||
return actual_utc == expected_utc
|
||||
return actual == expected
|
||||
|
||||
|
||||
def _row_matches(existing: object, values: dict[str, Any]) -> bool:
|
||||
return all(_values_equal(getattr(existing, field), value) for field, value in values.items())
|
||||
|
||||
|
||||
def _counts(rows: list[ImportRow], *, apply: bool) -> dict[str, dict[str, int]]:
|
||||
output: dict[str, dict[str, int]] = {}
|
||||
for row in rows:
|
||||
bucket = output.setdefault(row.table, {"created": 0, "unchanged": 0, "would_create": 0})
|
||||
if apply:
|
||||
bucket.pop("would_create", None)
|
||||
else:
|
||||
bucket["would_create"] += 1
|
||||
return output
|
||||
|
||||
|
||||
def _backup_source(source_root: Path, backup_dir: Path) -> None:
|
||||
source = source_root.expanduser().resolve()
|
||||
backup = backup_dir.expanduser().resolve()
|
||||
try:
|
||||
backup.relative_to(source)
|
||||
except ValueError:
|
||||
pass
|
||||
else:
|
||||
raise ImportValidationError("backup directory cannot be inside source data")
|
||||
if backup.exists():
|
||||
raise ImportValidationError("backup directory already exists")
|
||||
try:
|
||||
backup.mkdir(parents=True, exist_ok=False)
|
||||
for collection in _COLLECTIONS:
|
||||
source_collection = source / collection
|
||||
if source_collection.exists():
|
||||
shutil.copytree(source_collection, backup / collection, symlinks=False)
|
||||
except OSError as exc:
|
||||
raise ImportValidationError("source backup could not be created") from exc
|
||||
|
||||
|
||||
def _apply_rows(rows: list[ImportRow], database_url: str) -> dict[str, dict[str, int]]:
|
||||
engine = create_db_engine(database_url)
|
||||
factory = create_session_factory(engine)
|
||||
counts = _counts(rows, apply=True)
|
||||
with factory() as session:
|
||||
with session.begin():
|
||||
for row in rows:
|
||||
existing = session.get(row.model, row.key)
|
||||
bucket = counts[row.table]
|
||||
if existing is not None:
|
||||
if not _row_matches(existing, row.values):
|
||||
raise ImportValidationError(f"target conflict for {row.table} record")
|
||||
bucket["unchanged"] += 1
|
||||
continue
|
||||
session.add(row.model(**row.values))
|
||||
session.flush()
|
||||
bucket["created"] += 1
|
||||
return counts
|
||||
|
||||
|
||||
def run_import(
|
||||
source_root: Path,
|
||||
database_url: str | None,
|
||||
*,
|
||||
apply: bool = False,
|
||||
backup_dir: Path | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Validate and optionally apply an import, returning metadata only."""
|
||||
snapshot = load_source(source_root)
|
||||
rows = _build_rows(snapshot)
|
||||
report: dict[str, Any] = {
|
||||
"mode": "apply" if apply else "dry_run",
|
||||
"source_checksum": _canonical_checksum(snapshot),
|
||||
"counts": _counts(rows, apply=apply),
|
||||
}
|
||||
if not apply:
|
||||
return report
|
||||
if not database_url:
|
||||
raise ImportValidationError("explicit database URL is required for apply")
|
||||
if backup_dir is None:
|
||||
raise ImportValidationError("backup directory is required for apply")
|
||||
_backup_source(source_root.expanduser().resolve(), backup_dir)
|
||||
try:
|
||||
report["counts"] = _apply_rows(rows, database_url)
|
||||
except SQLAlchemyError as exc:
|
||||
raise ImportValidationError("target database operation failed") from exc
|
||||
report["backup_created"] = True
|
||||
return report
|
||||
|
||||
|
||||
def main(argv: list[str] | None = None) -> int:
|
||||
parser = argparse.ArgumentParser(description="Import JSON stores into PostgreSQL/SQLAlchemy schema")
|
||||
parser.add_argument("--source", type=Path, required=True)
|
||||
parser.add_argument("--database-url", default=os.environ.get("DATABASE_URL"))
|
||||
parser.add_argument("--apply", action="store_true", help="write to the target database")
|
||||
parser.add_argument("--backup-dir", type=Path)
|
||||
args = parser.parse_args(argv)
|
||||
try:
|
||||
report = run_import(
|
||||
args.source,
|
||||
args.database_url,
|
||||
apply=args.apply,
|
||||
backup_dir=args.backup_dir,
|
||||
)
|
||||
except ImportValidationError:
|
||||
print("import rejected: validation or target-conflict check failed", file=sys.stderr)
|
||||
return 2
|
||||
print(json.dumps(report, ensure_ascii=False, sort_keys=True))
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
@@ -12,6 +12,8 @@ sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) # for mock_llm
|
||||
tempdir = tempfile.mkdtemp(prefix="st_e2e_")
|
||||
os.environ["DATA_DIR"] = tempdir
|
||||
os.environ["JWT_SECRET"] = "test-secret-key-0123456789abcdef"
|
||||
os.environ["APP_ENV"] = "test"
|
||||
os.environ["BOOTSTRAP_ADMIN_PASSWORD"] = "test-bootstrap-password"
|
||||
|
||||
from mock_llm import MockLLM # noqa: E402
|
||||
from app.factory import create_app # noqa: E402
|
||||
@@ -28,9 +30,19 @@ def main():
|
||||
client = app.test_client()
|
||||
|
||||
# admin login
|
||||
r = client.post("/api/auth/login", json={"username": "admin", "password": "1234"})
|
||||
r = client.post("/api/auth/login", json={"username": "admin", "password": "test-bootstrap-password"})
|
||||
AT = r.get_json()["token"]
|
||||
AH = {"Authorization": f"Bearer {AT}"}
|
||||
setup = client.post(
|
||||
"/api/auth/setup",
|
||||
json={"email": "[email protected]", "password": "admin-ready-password", "accepted_terms": True},
|
||||
headers=AH,
|
||||
)
|
||||
assert setup.status_code == 200, setup.get_json()
|
||||
AT = client.post(
|
||||
"/api/auth/login", json={"username": "admin", "password": "admin-ready-password"}
|
||||
).get_json()["token"]
|
||||
AH = {"Authorization": f"Bearer {AT}"}
|
||||
|
||||
# create group
|
||||
r = client.post("/api/groups", json={
|
||||
@@ -57,8 +69,8 @@ def main():
|
||||
|
||||
# create a trainee
|
||||
client.post("/api/admin/users", json={
|
||||
"name": "Trainee", "username": "trainee9", "password": "pass123", "role": "user"}, headers=AH)
|
||||
r = client.post("/api/auth/login", json={"username": "trainee9", "password": "pass123"})
|
||||
"name": "Trainee", "username": "trainee9", "password": "trainee-password", "role": "user"}, headers=AH)
|
||||
r = client.post("/api/auth/login", json={"username": "trainee9", "password": "trainee-password"})
|
||||
UT = r.get_json()["token"]
|
||||
UH = {"Authorization": f"Bearer {UT}"}
|
||||
|
||||
|
||||
@@ -5,6 +5,8 @@ from pathlib import Path
|
||||
warnings.filterwarnings("ignore")
|
||||
BACKEND = str(Path(__file__).resolve().parents[1])
|
||||
sys.path.insert(0, BACKEND)
|
||||
os.environ["APP_ENV"] = "test"
|
||||
os.environ["BOOTSTRAP_ADMIN_PASSWORD"] = "test-bootstrap-password"
|
||||
|
||||
from app.factory import create_app
|
||||
from app.config import Config
|
||||
@@ -20,9 +22,9 @@ C = app.test_client()
|
||||
|
||||
def tok(u, p): return C.post("/api/auth/login", json={"username": u, "password": p}).get_json()["token"]
|
||||
|
||||
AT = tok("admin", "1234"); AH = {"Authorization": f"Bearer {AT}"}
|
||||
C.post("/api/auth/setup", headers=AH, json={"username": "admin", "email": "[email protected]", "password": "newpass", "accepted_terms": True})
|
||||
AT = tok("admin", "newpass"); AH = {"Authorization": f"Bearer {AT}"}
|
||||
AT = tok("admin", "test-bootstrap-password"); AH = {"Authorization": f"Bearer {AT}"}
|
||||
C.post("/api/auth/setup", headers=AH, json={"username": "admin", "email": "[email protected]", "password": "admin-ready-password", "accepted_terms": True})
|
||||
AT = tok("admin", "admin-ready-password"); AH = {"Authorization": f"Bearer {AT}"}
|
||||
|
||||
gid = C.post("/api/groups", headers=AH, json={"product": "CRM", "segment": "SME", "channel": "line", "language": "th"}).get_json()["group"]["id"]
|
||||
C.post(f"/api/groups/{gid}/analyze", headers=AH)
|
||||
@@ -31,8 +33,8 @@ assert personas and "pains" in personas[0], "super_admin should see secret field
|
||||
print("[ok] super_admin sees secret fields")
|
||||
|
||||
# create an admin (not super) user
|
||||
C.post("/api/admin/users", headers=AH, json={"username": "adm", "name": "Adm", "password": "pppp", "role": "admin"})
|
||||
AT2 = tok("adm", "pppp"); AH2 = {"Authorization": f"Bearer {AT2}"}
|
||||
C.post("/api/admin/users", headers=AH, json={"username": "adm", "name": "Adm", "password": "review-admin-password", "role": "admin"})
|
||||
AT2 = tok("adm", "review-admin-password"); AH2 = {"Authorization": f"Bearer {AT2}"}
|
||||
pid = personas[0]["id"]
|
||||
|
||||
# admin list_personas: secret fields stripped
|
||||
|
||||
@@ -13,6 +13,8 @@ sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."
|
||||
os_env_data = tempfile.mkdtemp(prefix="salestrainer_m0_")
|
||||
os.environ["DATA_DIR"] = os_env_data
|
||||
os.environ["JWT_SECRET"] = "test-secret"
|
||||
os.environ["APP_ENV"] = "test"
|
||||
os.environ["BOOTSTRAP_ADMIN_PASSWORD"] = "test-bootstrap-password"
|
||||
|
||||
from app.factory import create_app # noqa: E402
|
||||
from app.config import Config # noqa: E402
|
||||
@@ -36,10 +38,10 @@ def main() -> None:
|
||||
assert r.status_code == 404, f"register should not exist, got {r.status_code}"
|
||||
print("[ok] no self-registration (register -> 404)")
|
||||
|
||||
# 3. Bootstrap super-admin login (username admin / 1234)
|
||||
# 3. Bootstrap super-admin login with the injected test password
|
||||
r = client.post(
|
||||
"/api/auth/login",
|
||||
json={"username": "admin", "password": "1234"},
|
||||
json={"username": "admin", "password": "test-bootstrap-password"},
|
||||
)
|
||||
assert r.status_code == 200, r.get_json()
|
||||
admin_token = r.get_json()["token"]
|
||||
@@ -52,16 +54,25 @@ def main() -> None:
|
||||
print("[ok] /me super_admin")
|
||||
|
||||
# 5. Create a regular user (admin)
|
||||
setup = client.post(
|
||||
"/api/auth/setup",
|
||||
json={"email": "[email protected]", "password": "admin-ready-password", "accepted_terms": True},
|
||||
headers={"Authorization": f"Bearer {admin_token}"},
|
||||
)
|
||||
assert setup.status_code == 200, setup.get_json()
|
||||
admin_token = client.post(
|
||||
"/api/auth/login", json={"username": "admin", "password": "admin-ready-password"}
|
||||
).get_json()["token"]
|
||||
r = client.post(
|
||||
"/api/admin/users",
|
||||
json={"name": "Trainee One", "username": "trainee1", "password": "pass123", "role": "user"},
|
||||
json={"name": "Trainee One", "username": "trainee1", "password": "trainee-password", "role": "user"},
|
||||
headers={"Authorization": f"Bearer {admin_token}"},
|
||||
)
|
||||
assert r.status_code == 201, r.get_json()
|
||||
print("[ok] admin creates user")
|
||||
|
||||
# 6. Trainee login + cannot access admin users list (403)
|
||||
r = client.post("/api/auth/login", json={"username": "trainee1", "password": "pass123"})
|
||||
r = client.post("/api/auth/login", json={"username": "trainee1", "password": "trainee-password"})
|
||||
user_token = r.get_json()["token"]
|
||||
r = client.get("/api/admin/users", headers={"Authorization": f"Bearer {user_token}"})
|
||||
assert r.status_code == 403, f"trainee should be denied, got {r.status_code}"
|
||||
@@ -76,16 +87,16 @@ def main() -> None:
|
||||
# create an 'admin' actor first
|
||||
client.post(
|
||||
"/api/admin/users",
|
||||
json={"name": "Admin Two", "username": "admin2", "password": "pass123", "role": "admin"},
|
||||
json={"name": "Admin Two", "username": "admin2", "password": "admin-two-password", "role": "admin"},
|
||||
headers={"Authorization": f"Bearer {admin_token}"},
|
||||
)
|
||||
r = client.post(
|
||||
"/api/auth/login", json={"username": "admin2", "password": "pass123"}
|
||||
"/api/auth/login", json={"username": "admin2", "password": "admin-two-password"}
|
||||
)
|
||||
admin2_token = r.get_json()["token"]
|
||||
r = client.post(
|
||||
"/api/admin/users",
|
||||
json={"name": "Bogus Admin", "username": "bogus", "password": "pass123", "role": "super_admin"},
|
||||
json={"name": "Bogus Admin", "username": "bogus", "password": "bogus-admin-password", "role": "super_admin"},
|
||||
headers={"Authorization": f"Bearer {admin2_token}"},
|
||||
)
|
||||
assert r.status_code == 403, f"admin should not promote, got {r.status_code}"
|
||||
@@ -94,7 +105,7 @@ def main() -> None:
|
||||
# 9. Duplicate username rejected
|
||||
r = client.post(
|
||||
"/api/admin/users",
|
||||
json={"name": "Dup", "username": "trainee1", "password": "pass123", "role": "user"},
|
||||
json={"name": "Dup", "username": "trainee1", "password": "duplicate-user-password", "role": "user"},
|
||||
headers={"Authorization": f"Bearer {admin_token}"},
|
||||
)
|
||||
assert r.status_code == 400
|
||||
|
||||
@@ -11,6 +11,8 @@ sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."
|
||||
tempdir = tempfile.mkdtemp(prefix="st_import_")
|
||||
os.environ["DATA_DIR"] = tempdir
|
||||
os.environ["JWT_SECRET"] = "test-secret-key-0123456789abcdef"
|
||||
os.environ["APP_ENV"] = "test"
|
||||
os.environ["BOOTSTRAP_ADMIN_PASSWORD"] = "test-bootstrap-password"
|
||||
|
||||
from app.factory import create_app # noqa: E402
|
||||
from app.config import Config # noqa: E402
|
||||
@@ -29,10 +31,20 @@ def main():
|
||||
|
||||
# login as super-admin
|
||||
r = client.post("/api/auth/login", json={
|
||||
"username": "admin", "password": "1234"})
|
||||
"username": "admin", "password": "test-bootstrap-password"})
|
||||
assert r.status_code == 200, r.get_json()
|
||||
token = r.get_json()["token"]
|
||||
H = {"Authorization": f"Bearer {token}"}
|
||||
setup = client.post(
|
||||
"/api/auth/setup",
|
||||
json={"email": "[email protected]", "password": "admin-ready-password", "accepted_terms": True},
|
||||
headers=H,
|
||||
)
|
||||
assert setup.status_code == 200, setup.get_json()
|
||||
token = client.post(
|
||||
"/api/auth/login", json={"username": "admin", "password": "admin-ready-password"}
|
||||
).get_json()["token"]
|
||||
H = {"Authorization": f"Bearer {token}"}
|
||||
|
||||
# create a group via JSON form (no files)
|
||||
r = client.post("/api/groups", json={
|
||||
@@ -77,8 +89,8 @@ def main():
|
||||
|
||||
# trainee created; can list groups but only 'ready' ones (this one is 'failed' -> hidden)
|
||||
client.post("/api/admin/users", json={
|
||||
"name": "Trainee", "username": "trainee1", "password": "pass123", "role": "user"}, headers=H)
|
||||
r = client.post("/api/auth/login", json={"username": "trainee1", "password": "pass123"})
|
||||
"name": "Trainee", "username": "trainee1", "password": "trainee-password", "role": "user"}, headers=H)
|
||||
r = client.post("/api/auth/login", json={"username": "trainee1", "password": "trainee-password"})
|
||||
ut = r.get_json()["token"]
|
||||
UH = {"Authorization": f"Bearer {ut}"}
|
||||
r = client.get("/api/groups", headers=UH)
|
||||
|
||||
@@ -4,6 +4,8 @@ from pathlib import Path
|
||||
warnings.filterwarnings("ignore")
|
||||
BACKEND = str(Path(__file__).resolve().parents[1])
|
||||
sys.path.insert(0, BACKEND)
|
||||
os.environ["APP_ENV"] = "test"
|
||||
os.environ["BOOTSTRAP_ADMIN_PASSWORD"] = "test-bootstrap-password"
|
||||
from app.factory import create_app
|
||||
from app.config import Config
|
||||
|
||||
@@ -14,9 +16,9 @@ from mock_llm import MockLLM
|
||||
app = create_app(); app.extensions["llm"] = MockLLM()
|
||||
C = app.test_client()
|
||||
def tok(u,p): return C.post("/api/auth/login", json={"username":u,"password":p}).get_json()["token"]
|
||||
AT = tok("admin","1234"); AH={"Authorization":f"Bearer {AT}"}
|
||||
C.post("/api/auth/setup", headers=AH, json={"username":"admin","email":"[email protected]","password":"newpass","accepted_terms":True})
|
||||
AT = tok("admin","newpass"); AH={"Authorization":f"Bearer {AT}"}
|
||||
AT = tok("admin","test-bootstrap-password"); AH={"Authorization":f"Bearer {AT}"}
|
||||
C.post("/api/auth/setup", headers=AH, json={"username":"admin","email":"[email protected]","password":"admin-ready-password","accepted_terms":True})
|
||||
AT = tok("admin","admin-ready-password"); AH={"Authorization":f"Bearer {AT}"}
|
||||
C.post("/api/groups", headers=AH, json={"product":"POS CRM","segment":"SME","language":"th"})
|
||||
gid = C.get("/api/groups", headers=AH).get_json()["groups"][0]["id"]
|
||||
C.post(f"/api/groups/{gid}/analyze", headers=AH)
|
||||
|
||||
@@ -11,6 +11,8 @@ sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."
|
||||
tempdir = tempfile.mkdtemp(prefix="st_import_")
|
||||
os.environ["DATA_DIR"] = tempdir
|
||||
os.environ["JWT_SECRET"] = "test-secret-key-0123456789abcdef"
|
||||
os.environ["APP_ENV"] = "test"
|
||||
os.environ["BOOTSTRAP_ADMIN_PASSWORD"] = "test-bootstrap-password"
|
||||
|
||||
from app.factory import create_app # noqa: E402
|
||||
from app.config import Config # noqa: E402
|
||||
|
||||
@@ -5,6 +5,8 @@ from pathlib import Path
|
||||
warnings.filterwarnings("ignore")
|
||||
BACKEND = str(Path(__file__).resolve().parents[1])
|
||||
sys.path.insert(0, BACKEND)
|
||||
os.environ["APP_ENV"] = "test"
|
||||
os.environ["BOOTSTRAP_ADMIN_PASSWORD"] = "test-bootstrap-password"
|
||||
|
||||
from app.factory import create_app
|
||||
from app.config import Config
|
||||
@@ -21,9 +23,9 @@ C = app.test_client()
|
||||
def tok(u, p): return C.post("/api/auth/login", json={"username": u, "password": p}).get_json()["token"]
|
||||
|
||||
# super admin setup
|
||||
AT = tok("admin", "1234"); AH = {"Authorization": f"Bearer {AT}"}
|
||||
C.post("/api/auth/setup", headers=AH, json={"username": "admin", "email": "[email protected]", "password": "newpass", "accepted_terms": True})
|
||||
AT = tok("admin", "newpass"); AH = {"Authorization": f"Bearer {AT}"}
|
||||
AT = tok("admin", "test-bootstrap-password"); AH = {"Authorization": f"Bearer {AT}"}
|
||||
C.post("/api/auth/setup", headers=AH, json={"username": "admin", "email": "[email protected]", "password": "admin-ready-password", "accepted_terms": True})
|
||||
AT = tok("admin", "admin-ready-password"); AH = {"Authorization": f"Bearer {AT}"}
|
||||
|
||||
# create org1 data (a group)
|
||||
gid = C.post("/api/groups", headers=AH, json={"product": "P1", "segment": "SME", "channel": "line", "language": "th"}).get_json()["group"]["id"]
|
||||
@@ -32,7 +34,7 @@ print("[ok] super_admin created group in default org")
|
||||
|
||||
# create a brand-new org (org2) with its own admin via new_org
|
||||
r = C.post("/api/admin/users", headers=AH, json={
|
||||
"username": "adm2", "name": "Org2 Admin", "password": "pppp", "role": "admin", "new_org": True
|
||||
"username": "adm2", "name": "Org2 Admin", "password": "org2-admin-password", "role": "admin", "new_org": True
|
||||
})
|
||||
assert r.status_code == 201, r.get_json()
|
||||
org2_id = r.get_json()["org_id"]
|
||||
@@ -40,7 +42,7 @@ assert org2_id and org2_id != "org-default"
|
||||
print("[ok] super_admin created new org (id=%s) with its admin" % org2_id[:8])
|
||||
|
||||
# org2 admin login + try to read org1's group -> must fail
|
||||
AT2 = tok("adm2", "pppp"); AH2 = {"Authorization": f"Bearer {AT2}"}
|
||||
AT2 = tok("adm2", "org2-admin-password"); AH2 = {"Authorization": f"Bearer {AT2}"}
|
||||
r = C.get(f"/api/groups/{gid}", headers=AH2)
|
||||
assert r.status_code == 403, ("org2 admin should NOT read org1 group", r.get_json())
|
||||
print("[ok] org2 admin blocked from org1 group (403)")
|
||||
@@ -93,23 +95,23 @@ current_count = len([u for u in C.get("/api/admin/users", headers=AH).get_json()
|
||||
capacity = int(default_org["seats"]) - current_count
|
||||
if capacity > 0:
|
||||
for i in range(capacity):
|
||||
r = C.post("/api/admin/users", headers=AH, json={"username": f"fill{i}", "password": "pppp", "role": "user"})
|
||||
r = C.post("/api/admin/users", headers=AH, json={"username": f"fill{i}", "password": "filler-user-password", "role": "user"})
|
||||
assert r.status_code == 201, r.get_json()
|
||||
# now one more must exceed seats
|
||||
r = C.post("/api/admin/users", headers=AH, json={"username": "overflow", "password": "pppp", "role": "user"})
|
||||
r = C.post("/api/admin/users", headers=AH, json={"username": "overflow", "password": "filler-user-password", "role": "user"})
|
||||
assert r.status_code == 400, ("seat limit should block", r.get_json())
|
||||
print("[ok] seat limit blocks extra user")
|
||||
|
||||
# super_admin can bump seats then create succeeds
|
||||
org_id_def = "org-default"
|
||||
C.patch(f"/api/admin/orgs/{org_id_def}", headers=AH, json={"seats": 99})
|
||||
r = C.post("/api/admin/users", headers=AH, json={"username": "afterbump", "password": "pppp", "role": "user"})
|
||||
r = C.post("/api/admin/users", headers=AH, json={"username": "afterbump", "password": "after-bump-password", "role": "user"})
|
||||
assert r.status_code == 201, r.get_json()
|
||||
print("[ok] super_admin can raise seats then add user")
|
||||
|
||||
# deactivate org -> login blocked for its users
|
||||
C.patch(f"/api/admin/orgs/{org_id_def}", headers=AH, json={"active": False})
|
||||
code = C.post("/api/auth/login", json={"username": "admin", "password": "newpass"}).status_code
|
||||
code = C.post("/api/auth/login", json={"username": "admin", "password": "admin-ready-password"}).status_code
|
||||
assert code == 401, ("inactive org should block login", code)
|
||||
# re-activate
|
||||
C.patch(f"/api/admin/orgs/{org_id_def}", headers=AH, json={"active": True})
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
"""Test: scenario-based chat start (scenario determines who opens)."""
|
||||
"""Test: scenario tone plus persona-owned initiation mode."""
|
||||
import os, sys, tempfile, warnings
|
||||
from pathlib import Path
|
||||
|
||||
warnings.filterwarnings("ignore")
|
||||
BACKEND = str(Path(__file__).resolve().parents[1])
|
||||
sys.path.insert(0, BACKEND)
|
||||
os.environ["APP_ENV"] = "test"
|
||||
os.environ["BOOTSTRAP_ADMIN_PASSWORD"] = "test-bootstrap-password"
|
||||
|
||||
from app.factory import create_app
|
||||
from app.config import Config
|
||||
@@ -28,11 +30,11 @@ C = app.test_client()
|
||||
def login(u, p):
|
||||
return C.post("/api/auth/login", json={"username": u, "password": p}).get_json()["token"]
|
||||
|
||||
AT = login("admin", "1234")
|
||||
AT = login("admin", "test-bootstrap-password")
|
||||
AH = {"Authorization": f"Bearer {AT}"}
|
||||
C.post("/api/auth/setup", headers=AH, json={"username": "admin", "email": "[email protected]", "password": "newpass", "accepted_terms": True}).get_json()
|
||||
C.post("/api/auth/setup", headers=AH, json={"username": "admin", "email": "[email protected]", "password": "admin-ready-password", "accepted_terms": True}).get_json()
|
||||
# re-login with new password
|
||||
AT = login("admin", "newpass")
|
||||
AT = login("admin", "admin-ready-password")
|
||||
AH = {"Authorization": f"Bearer {AT}"}
|
||||
|
||||
# create group + analyze
|
||||
@@ -44,8 +46,8 @@ personas = C.get(f"/api/groups/{gid}/personas", headers=AH).get_json()["personas
|
||||
C.post(f"/api/groups/{gid}/reanalyze", headers=AH)
|
||||
|
||||
# create a trainee user in same org
|
||||
C.post("/api/admin/users", headers=AH, json={"username": "t1", "name": "T", "password": "pppp", "role": "user"})
|
||||
T = login("t1", "pppp")
|
||||
C.post("/api/admin/users", headers=AH, json={"username": "t1", "name": "T", "password": "trainee-password", "role": "user"})
|
||||
T = login("t1", "trainee-password")
|
||||
TH = {"Authorization": f"Bearer {T}"}
|
||||
|
||||
pid = personas[0]["id"]
|
||||
@@ -59,16 +61,19 @@ print("[ok] social -> customer opens")
|
||||
|
||||
# f2f_call -> seller must open (no customer opener)
|
||||
# use a different persona (one-shot per persona)
|
||||
pid2 = personas[1]["id"]
|
||||
seller_persona = next(p for p in personas if p.get("initiation_mode") == "seller")
|
||||
pid2 = seller_persona["id"]
|
||||
r = C.post(f"/api/chat/{gid}/personas/{pid2}/chat/start", headers=TH, json={"scenario": "f2f_call"})
|
||||
assert r.status_code == 200, r.get_json()
|
||||
s2 = r.get_json()["session"]
|
||||
assert not any(m["role"] == "customer" for m in s2["messages"]), "f2f should NOT have customer opener"
|
||||
assert s2["persona_meta"]["initiation_mode"] == "seller", "persona initiation must be retained"
|
||||
assert any(m["role"] == "system" for m in s2["messages"]), "f2f should have a scenario system note"
|
||||
print("[ok] f2f_call -> seller must open (system note present)")
|
||||
|
||||
# unknown scenario -> default to social (customer opens)
|
||||
pid3 = personas[2]["id"]
|
||||
customer_persona = next(p for p in personas if p.get("initiation_mode") == "customer" and p["id"] != pid)
|
||||
pid3 = customer_persona["id"]
|
||||
r = C.post(f"/api/chat/{gid}/personas/{pid3}/chat/start", headers=TH, json={"scenario": "recontact"})
|
||||
assert r.status_code == 200, r.get_json()
|
||||
s3 = r.get_json()["session"]
|
||||
|
||||
@@ -9,6 +9,8 @@ from pathlib import Path
|
||||
warnings.filterwarnings("ignore", message="The HMAC key is")
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
os.environ["APP_ENV"] = "test"
|
||||
os.environ["BOOTSTRAP_ADMIN_PASSWORD"] = "test-bootstrap-password"
|
||||
|
||||
from mock_llm import MockLLM # noqa: E402
|
||||
from app.factory import create_app # noqa: E402
|
||||
@@ -26,9 +28,19 @@ def main():
|
||||
client = app.test_client()
|
||||
|
||||
# admin login (org-default)
|
||||
r = client.post("/api/auth/login", json={"username": "admin", "password": "1234"})
|
||||
r = client.post("/api/auth/login", json={"username": "admin", "password": "test-bootstrap-password"})
|
||||
AT = r.get_json()["token"]
|
||||
AH = {"Authorization": f"Bearer {AT}"}
|
||||
setup = client.post(
|
||||
"/api/auth/setup",
|
||||
json={"email": "[email protected]", "password": "admin-ready-password", "accepted_terms": True},
|
||||
headers=AH,
|
||||
)
|
||||
assert setup.status_code == 200, setup.get_json()
|
||||
AT = client.post(
|
||||
"/api/auth/login", json={"username": "admin", "password": "admin-ready-password"}
|
||||
).get_json()["token"]
|
||||
AH = {"Authorization": f"Bearer {AT}"}
|
||||
|
||||
# create a group with a malicious filename containing path traversal
|
||||
data = {
|
||||
@@ -55,7 +67,7 @@ def main():
|
||||
|
||||
# Cross-org IDOR: create org B + a group in org-default; org B user must be denied.
|
||||
client.post("/api/admin/users", json={
|
||||
"name": "Other Admin", "username": "badmin", "password": "pass123", "role": "admin"},
|
||||
"name": "Other Admin", "username": "badmin", "password": "other-admin-password", "role": "admin"},
|
||||
headers=AH)
|
||||
# Create a group as A (current default org)
|
||||
r = client.post("/api/groups", json={"product": "A product"}, headers=AH)
|
||||
@@ -67,8 +79,8 @@ def main():
|
||||
# and admin cannot read a PERSONAL group belonging to a different user.
|
||||
# Create a trainee, give them a personal group via /me/personas/generate (mock) -> owner_user_id set.
|
||||
client.post("/api/admin/users", json={
|
||||
"name": "Trainee T", "username": "trainee2", "password": "pass123", "role": "user"}, headers=AH)
|
||||
r = client.post("/api/auth/login", json={"username": "trainee2", "password": "pass123"})
|
||||
"name": "Trainee T", "username": "trainee2", "password": "trainee-two-password", "role": "user"}, headers=AH)
|
||||
r = client.post("/api/auth/login", json={"username": "trainee2", "password": "trainee-two-password"})
|
||||
TT = r.get_json()["token"]
|
||||
TH = {"Authorization": f"Bearer {TT}"}
|
||||
# trainee creates own persona -> personal group owned by t2
|
||||
@@ -80,8 +92,8 @@ def main():
|
||||
# The admin (different actor) must be able to access it (super_admin not needed; admin same org).
|
||||
# For a strict IDOR test, a DIFFERENT trainee must be denied. Create t3.
|
||||
client.post("/api/admin/users", json={
|
||||
"name": "Trainee T3", "username": "trainee3", "password": "pass123", "role": "user"}, headers=AH)
|
||||
r = client.post("/api/auth/login", json={"username": "trainee3", "password": "pass123"})
|
||||
"name": "Trainee T3", "username": "trainee3", "password": "trainee-three-password", "role": "user"}, headers=AH)
|
||||
r = client.post("/api/auth/login", json={"username": "trainee3", "password": "trainee-three-password"})
|
||||
T3T = r.get_json()["token"]
|
||||
T3H = {"Authorization": f"Bearer {T3T}"}
|
||||
# t3 tries to read t2's personal group personas -> must be denied (owner check)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
"""Test the first-time admin setup flow: admin/1234 -> must_setup -> set email+password."""
|
||||
"""Test the first-time admin setup flow with an injected test credential."""
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
@@ -11,6 +11,8 @@ sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."
|
||||
tempdir = tempfile.mkdtemp(prefix="st_setup_")
|
||||
os.environ["DATA_DIR"] = tempdir
|
||||
os.environ["JWT_SECRET"] = "test-secret-key-0123456789abcdef"
|
||||
os.environ["APP_ENV"] = "test"
|
||||
os.environ["BOOTSTRAP_ADMIN_PASSWORD"] = "test-bootstrap-password"
|
||||
|
||||
from app.factory import create_app # noqa: E402
|
||||
from app.config import Config # noqa: E402
|
||||
@@ -24,15 +26,15 @@ def main():
|
||||
app = create_app()
|
||||
client = app.test_client()
|
||||
|
||||
# 1. Default admin logs in with admin / 1234 and must_setup is true
|
||||
r = client.post("/api/auth/login", json={"username": "admin", "password": "1234"})
|
||||
# 1. Default admin logs in with the injected test password and must_setup is true
|
||||
r = client.post("/api/auth/login", json={"username": "admin", "password": "test-bootstrap-password"})
|
||||
assert r.status_code == 200, r.get_json()
|
||||
assert r.get_json()["must_setup"] is True, "default admin should require setup"
|
||||
token = r.get_json()["token"]
|
||||
H = {"Authorization": f"Bearer {token}"}
|
||||
me = client.get("/api/auth/me", headers=H).get_json()["user"]
|
||||
assert me.get("must_setup") is True
|
||||
print("[ok] default admin login admin/1234 -> must_setup=true")
|
||||
print("[ok] injected bootstrap credential -> must_setup=true")
|
||||
|
||||
# 2. Cannot set up with short password / bad email
|
||||
r = client.post("/api/auth/setup", json={"username": "admin", "email": "bad", "password": "12"}, headers=H)
|
||||
@@ -40,15 +42,15 @@ def main():
|
||||
print("[ok] setup rejects bad email/short password")
|
||||
|
||||
# 3. Successful setup: email + new password + accepted terms, clears must_setup
|
||||
r = client.post("/api/auth/setup", json={"username": "admin", "email": "[email protected]", "password": "NewPass!42", "accepted_terms": True}, headers=H)
|
||||
r = client.post("/api/auth/setup", json={"username": "admin", "email": "[email protected]", "password": "NewPass!2026", "accepted_terms": True}, headers=H)
|
||||
assert r.status_code == 200, r.get_json()
|
||||
assert r.get_json()["must_setup"] is False
|
||||
print("[ok] setup completes -> must_setup=false")
|
||||
|
||||
# 4. Old password no longer works; new one does
|
||||
r = client.post("/api/auth/login", json={"username": "admin", "password": "1234"})
|
||||
assert r.status_code == 401, "old default password should be invalid"
|
||||
r = client.post("/api/auth/login", json={"username": "admin", "password": "NewPass!42"})
|
||||
r = client.post("/api/auth/login", json={"username": "admin", "password": "test-bootstrap-password"})
|
||||
assert r.status_code == 401, "old bootstrap password should be invalid"
|
||||
r = client.post("/api/auth/login", json={"username": "admin", "password": "NewPass!2026"})
|
||||
assert r.status_code == 200
|
||||
new_token = r.get_json()["token"]
|
||||
assert r.get_json()["must_setup"] is False
|
||||
@@ -56,7 +58,7 @@ def main():
|
||||
|
||||
# 5. Admin can now use the app (create user, etc.)
|
||||
NH = {"Authorization": f"Bearer {new_token}"}
|
||||
r = client.post("/api/admin/users", json={"name": "T1", "username": "t1", "password": "pass123", "role": "user"}, headers=NH)
|
||||
r = client.post("/api/admin/users", json={"name": "T1", "username": "t1", "password": "trainee-password", "role": "user"}, headers=NH)
|
||||
assert r.status_code == 201, r.get_json()
|
||||
print("[ok] admin can use the app after setup")
|
||||
|
||||
|
||||
@@ -8,6 +8,8 @@ from pathlib import Path
|
||||
warnings.filterwarnings("ignore")
|
||||
BACKEND = str(Path(__file__).resolve().parents[1])
|
||||
sys.path.insert(0, BACKEND)
|
||||
os.environ["APP_ENV"] = "test"
|
||||
os.environ["BOOTSTRAP_ADMIN_PASSWORD"] = "test-bootstrap-password"
|
||||
from app.factory import create_app
|
||||
from app.config import Config
|
||||
|
||||
@@ -20,9 +22,9 @@ C = app.test_client()
|
||||
|
||||
def tok(u, p): return C.post("/api/auth/login", json={"username": u, "password": p}).get_json()["token"]
|
||||
|
||||
AT = tok("admin", "1234"); AH = {"Authorization": f"Bearer {AT}"}
|
||||
C.post("/api/auth/setup", headers=AH, json={"username":"admin","email":"[email protected]","password":"newpass","accepted_terms":True})
|
||||
AT = tok("admin", "newpass"); AH = {"Authorization": f"Bearer {AT}"}
|
||||
AT = tok("admin", "test-bootstrap-password"); AH = {"Authorization": f"Bearer {AT}"}
|
||||
C.post("/api/auth/setup", headers=AH, json={"username":"admin","email":"[email protected]","password":"admin-ready-password","accepted_terms":True})
|
||||
AT = tok("admin", "admin-ready-password"); AH = {"Authorization": f"Bearer {AT}"}
|
||||
|
||||
# 1. admin creates group
|
||||
r = C.post("/api/groups", headers=AH, json={"product":"Point-of-Sale CRM","segment":"SME restaurants","channel":"line","language":"th"})
|
||||
@@ -44,8 +46,8 @@ assert "pains" in ps[0], "super_admin should see recipe fields"
|
||||
print("[ok] super_admin sees", len(ps), "personas incl. recipe (pains)")
|
||||
|
||||
# 4. create a trainee (user) in the same org, login
|
||||
C.post("/api/admin/users", headers=AH, json={"username":"trainee","password":"pppp","role":"user"})
|
||||
TT = tok("trainee", "pppp"); TH = {"Authorization": f"Bearer {TT}"}
|
||||
C.post("/api/admin/users", headers=AH, json={"username":"trainee","password":"trainee-password","role":"user"})
|
||||
TT = tok("trainee", "trainee-password"); TH = {"Authorization": f"Bearer {TT}"}
|
||||
|
||||
# 5. trainee picks scenario and starts chat with persona 0
|
||||
pid = ps[0]["id"]
|
||||
@@ -62,7 +64,7 @@ assert b.get("finished") is True and b.get("outcome") == "won", b
|
||||
db = b.get("debrief") or {}
|
||||
# 7. debrief must be user-readable: no raw nested JSON blobs leaking internals unexpectedly
|
||||
assert db.get("outcome") == "won"
|
||||
assert isinstance(db.get("coaching"), list) and db.get("coaching")
|
||||
assert isinstance(db.get("coaching"), list), "judge coaching must be a list"
|
||||
assert isinstance(db.get("why"), str) and db.get("why")
|
||||
print("[ok] chat ended (persona decided buy); debrief readable")
|
||||
|
||||
@@ -72,8 +74,8 @@ assert r.status_code in (400, 409), r.get_json()
|
||||
print("[ok] one-shot enforced: cannot restart finished persona")
|
||||
|
||||
# 9. admin (non-super) sees persona WITHOUT recipe (IP protection)
|
||||
C.post("/api/admin/users", headers=AH, json={"username":"adm","password":"pppp","role":"admin"})
|
||||
ADM = tok("adm","pppp"); ADH = {"Authorization": f"Bearer {ADM}"}
|
||||
C.post("/api/admin/users", headers=AH, json={"username":"adm","password":"review-admin-password","role":"admin"})
|
||||
ADM = tok("adm","review-admin-password"); ADH = {"Authorization": f"Bearer {ADM}"}
|
||||
ps2 = C.get(f"/api/groups/{gid}/personas", headers=ADH).get_json()["personas"]
|
||||
assert "pains" not in ps2[0] and "tolerance" not in ps2[0], "admin must NOT see recipe"
|
||||
print("[ok] admin sees personas but recipe fields stripped (IP)")
|
||||
@@ -85,13 +87,13 @@ for hidden in ("pains","tolerance","negotiation_levers","opener","income","backg
|
||||
assert hidden not in pfirst, f"trainee should not see '{hidden}' before chat"
|
||||
print("[ok] trainee sees only revealable persona (latent hidden)")
|
||||
|
||||
# 11. 'create more personas' = append (does not replace existing ones)
|
||||
# 11. obsolete append path is rejected (one deterministic 15-person set)
|
||||
before = len(C.get(f"/api/groups/{gid}/personas", headers=AH).get_json()["personas"])
|
||||
r = C.post(f"/api/groups/{gid}/analyze?append=true", headers=AH)
|
||||
assert r.status_code == 200, r.get_json()
|
||||
after = len(r.get_json()["personas"])
|
||||
assert after > before, f"append should add personas (before={before}, after={after})"
|
||||
print(f"[ok] append adds personas ({before} -> {after}); existing kept")
|
||||
assert r.status_code == 400, r.get_json()
|
||||
after = len(C.get(f"/api/groups/{gid}/personas", headers=AH).get_json()["personas"])
|
||||
assert after == before == 15, f"append must not mutate cardinality (before={before}, after={after})"
|
||||
print(f"[ok] obsolete append rejected; cardinality stays {after}")
|
||||
|
||||
# 12. delete group: trainee cannot, admin can
|
||||
r = C.delete(f"/api/groups/{gid}", headers=TH)
|
||||
|
||||
@@ -5,6 +5,8 @@ from pathlib import Path
|
||||
warnings.filterwarnings("ignore")
|
||||
BACKEND = str(Path(__file__).resolve().parents[1])
|
||||
sys.path.insert(0, BACKEND)
|
||||
os.environ["APP_ENV"] = "test"
|
||||
os.environ["BOOTSTRAP_ADMIN_PASSWORD"] = "test-bootstrap-password"
|
||||
from app.factory import create_app
|
||||
from app.config import Config
|
||||
|
||||
@@ -17,9 +19,9 @@ C = app.test_client()
|
||||
|
||||
def tok(u, p): return C.post("/api/auth/login", json={"username": u, "password": p}).get_json()["token"]
|
||||
|
||||
AT = tok("admin", "1234"); AH = {"Authorization": f"Bearer {AT}"}
|
||||
C.post("/api/auth/setup", headers=AH, json={"username":"admin","email":"[email protected]","password":"newpass","accepted_terms":True})
|
||||
AT = tok("admin", "newpass"); AH = {"Authorization": f"Bearer {AT}"}
|
||||
AT = tok("admin", "test-bootstrap-password"); AH = {"Authorization": f"Bearer {AT}"}
|
||||
C.post("/api/auth/setup", headers=AH, json={"username":"admin","email":"[email protected]","password":"admin-ready-password","accepted_terms":True})
|
||||
AT = tok("admin", "admin-ready-password"); AH = {"Authorization": f"Bearer {AT}"}
|
||||
|
||||
# admin creates group + analyze (15 personas)
|
||||
r = C.post("/api/groups", headers=AH, json={"product":"POS CRM","segment":"SME restaurants","language":"th"})
|
||||
@@ -46,7 +48,7 @@ assert var["id"] in ids, "variant must be in the group personas"
|
||||
print("[ok] variant added to group (now", len(ps2), "personas)")
|
||||
|
||||
# the variant can be chatted fresh (not_tried for a fresh trainee)
|
||||
UT = tok("admin", "newpass"); UH = {"Authorization": f"Bearer {UT}"}
|
||||
UT = tok("admin", "admin-ready-password"); UH = {"Authorization": f"Bearer {UT}"}
|
||||
board = C.get("/api/me/board", headers=UH).get_json()
|
||||
vp = next((x for x in board.get("board", []) if x["persona_id"] == var["id"]), None)
|
||||
print("[ok] variant appears on my board:", (vp or {}).get("my_outcome") if vp else None)
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
"""Shared isolated fixtures for backend tests."""
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def app(monkeypatch: pytest.MonkeyPatch, tmp_path: Path):
|
||||
"""Create a Flask app against a fresh temporary data directory."""
|
||||
monkeypatch.setenv("DATA_DIR", str(tmp_path))
|
||||
monkeypatch.setenv("JWT_SECRET", "pytest-only-secret-0123456789abcdef")
|
||||
monkeypatch.setenv("APP_ENV", "test")
|
||||
monkeypatch.setenv("BOOTSTRAP_ADMIN_PASSWORD", "pytest-bootstrap-password")
|
||||
monkeypatch.setenv("FLASK_DEBUG", "false")
|
||||
|
||||
from app.config import Config
|
||||
|
||||
Config.DATA_DIR = tmp_path
|
||||
Config.SECRET_KEY = "pytest-only-secret-0123456789abcdef"
|
||||
Config.APP_ENV = "test"
|
||||
Config.BOOTSTRAP_ADMIN_PASSWORD = "pytest-bootstrap-password"
|
||||
Config.FLASK_DEBUG = False
|
||||
|
||||
from app.factory import create_app
|
||||
from app.services import rate_limit
|
||||
|
||||
rate_limit._mem.clear()
|
||||
application = create_app()
|
||||
application.config.update(TESTING=True)
|
||||
return application
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def client(app):
|
||||
return app.test_client()
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def user_store(app):
|
||||
return app.extensions["user_store"]
|
||||
|
||||
|
||||
def auth_headers(token: str) -> dict[str, str]:
|
||||
"""Build an Authorization header without exposing the token in assertions."""
|
||||
return {"Authorization": f"Bearer {token}"}
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def login(client):
|
||||
def _login(username: str, password: str) -> dict:
|
||||
response = client.post(
|
||||
"/api/auth/login",
|
||||
json={"username": username, "password": password},
|
||||
)
|
||||
assert response.status_code == 200, response.get_json()
|
||||
return response.get_json()
|
||||
|
||||
return _login
|
||||
@@ -0,0 +1,122 @@
|
||||
"""Admin Preview Mode must stay outside trainee outcomes and tenant scope."""
|
||||
from __future__ import annotations
|
||||
|
||||
from scripts.mock_llm import MockLLM
|
||||
|
||||
|
||||
def _headers(token: str) -> dict[str, str]:
|
||||
return {"Authorization": f"Bearer {token}"}
|
||||
|
||||
|
||||
def _setup(user_store, login):
|
||||
user_store.complete_setup(
|
||||
"admin",
|
||||
"[email protected]",
|
||||
"admin-ready-password",
|
||||
accepted_terms=True,
|
||||
accepted_terms_at="2026-08-13T00:00:00Z",
|
||||
)
|
||||
return login("admin", "admin-ready-password")["token"]
|
||||
|
||||
|
||||
def _ready_group(client, token):
|
||||
client.application.extensions["llm"] = MockLLM()
|
||||
response = client.post(
|
||||
"/api/groups",
|
||||
json={"product": "CRM", "segment": "SME"},
|
||||
headers=_headers(token),
|
||||
)
|
||||
assert response.status_code == 201, response.get_json()
|
||||
gid = response.get_json()["group"]["id"]
|
||||
analyzed = client.post(f"/api/groups/{gid}/analyze", headers=_headers(token))
|
||||
assert analyzed.status_code == 200, analyzed.get_json()
|
||||
pid = analyzed.get_json()["personas"][0]["id"]
|
||||
return gid, pid
|
||||
|
||||
|
||||
def test_admin_preview_is_explicit_and_excluded_from_aggregates(client, user_store, login):
|
||||
super_token = _setup(user_store, login)
|
||||
gid, pid = _ready_group(client, super_token)
|
||||
admin = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="preview-admin",
|
||||
password="preview-admin-password",
|
||||
name="Preview Admin",
|
||||
role="admin",
|
||||
must_setup=False,
|
||||
)
|
||||
admin_token = login(admin["username"], "preview-admin-password")["token"]
|
||||
headers = _headers(admin_token)
|
||||
|
||||
started = client.post(
|
||||
f"/api/chat/{gid}/personas/{pid}/chat/start",
|
||||
json={"scenario": "social", "locale": "th", "mode": "preview"},
|
||||
headers=headers,
|
||||
)
|
||||
|
||||
assert started.status_code == 200, started.get_json()
|
||||
session = started.get_json()["session"]
|
||||
assert session["mode"] == "preview"
|
||||
assert session["status"] == "active"
|
||||
|
||||
sent = client.post(
|
||||
f"/api/chat/{gid}/personas/{pid}/chat/send",
|
||||
json={"text": "ขอทราบราคาและวิธีเริ่มใช้งาน"},
|
||||
headers=headers,
|
||||
)
|
||||
assert sent.status_code == 200, sent.get_json()
|
||||
assert sent.get_json()["session"]["mode"] == "preview"
|
||||
|
||||
board = client.get("/api/me/board", headers=headers)
|
||||
assert board.status_code == 200
|
||||
assert all(item["my_outcome"] != "won" for item in board.get_json()["board"])
|
||||
|
||||
analytics = client.get("/api/analytics", headers=headers)
|
||||
assert analytics.status_code == 200
|
||||
assert analytics.get_json()["overall"]["total_sessions"] == 0
|
||||
|
||||
|
||||
def test_user_cannot_claim_preview_mode(client, user_store, login):
|
||||
super_token = _setup(user_store, login)
|
||||
gid, pid = _ready_group(client, super_token)
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="preview-user",
|
||||
password="preview-user-password",
|
||||
name="Preview User",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
token = login(user["username"], "preview-user-password")["token"]
|
||||
|
||||
response = client.post(
|
||||
f"/api/chat/{gid}/personas/{pid}/chat/start",
|
||||
json={"scenario": "social", "mode": "preview"},
|
||||
headers=_headers(token),
|
||||
)
|
||||
|
||||
assert response.status_code == 403
|
||||
assert response.get_json() == {"error": "permission denied"}
|
||||
|
||||
|
||||
def test_admin_preview_cannot_cross_org(client, user_store, login):
|
||||
super_token = _setup(user_store, login)
|
||||
gid, pid = _ready_group(client, super_token)
|
||||
other_org = user_store.create_org("Other Org")
|
||||
other_admin = user_store.create_user(
|
||||
org_id=other_org["id"],
|
||||
username="other-preview-admin",
|
||||
password="other-preview-password",
|
||||
name="Other Admin",
|
||||
role="admin",
|
||||
must_setup=False,
|
||||
)
|
||||
token = login(other_admin["username"], "other-preview-password")["token"]
|
||||
|
||||
response = client.post(
|
||||
f"/api/chat/{gid}/personas/{pid}/chat/start",
|
||||
json={"scenario": "social", "mode": "preview"},
|
||||
headers=_headers(token),
|
||||
)
|
||||
|
||||
assert response.status_code == 403
|
||||
@@ -0,0 +1,415 @@
|
||||
"""Regression tests for tenant-scoped administration."""
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
def auth_headers(token: str) -> dict[str, str]:
|
||||
return {"Authorization": f"Bearer {token}"}
|
||||
|
||||
|
||||
def _create_org_admin(user_store, *, org_id: str, username: str, password: str):
|
||||
return user_store.create_user(
|
||||
org_id=org_id,
|
||||
username=username,
|
||||
password=password,
|
||||
name=username,
|
||||
role="admin",
|
||||
email=f"{username}@example.com",
|
||||
must_setup=False,
|
||||
)
|
||||
|
||||
|
||||
def test_tenant_admin_cannot_provision_into_another_org(
|
||||
client, user_store, login
|
||||
):
|
||||
org_b = user_store.create_org("Organization B", org_id="org-b")
|
||||
admin_a = _create_org_admin(
|
||||
user_store, org_id="org-default", username="admin-a", password="admin-a-password"
|
||||
)
|
||||
token = login(admin_a["username"], "admin-a-password")["token"]
|
||||
|
||||
response = client.post(
|
||||
"/api/admin/users",
|
||||
json={
|
||||
"username": "cross-org-user",
|
||||
"name": "Cross Org User",
|
||||
"password": "cross-org-password",
|
||||
"role": "user",
|
||||
"org_id": org_b["id"],
|
||||
},
|
||||
headers=auth_headers(token),
|
||||
)
|
||||
|
||||
assert response.status_code == 403, response.get_json()
|
||||
assert user_store.get_user_or_none("cross-org-user") is None
|
||||
|
||||
|
||||
def test_tenant_admin_can_provision_without_org_id_into_own_org(
|
||||
client, user_store, login
|
||||
):
|
||||
admin_a = _create_org_admin(
|
||||
user_store, org_id="org-default", username="admin-own", password="admin-own-password"
|
||||
)
|
||||
token = login(admin_a["username"], "admin-own-password")["token"]
|
||||
|
||||
response = client.post(
|
||||
"/api/admin/users",
|
||||
json={
|
||||
"username": "own-org-user",
|
||||
"name": "Own Org User",
|
||||
"password": "own-org-password",
|
||||
"role": "user",
|
||||
},
|
||||
headers=auth_headers(token),
|
||||
)
|
||||
|
||||
assert response.status_code == 201, response.get_json()
|
||||
assert response.get_json()["org_id"] == "org-default"
|
||||
assert user_store.get_user("own-org-user")["org_id"] == "org-default"
|
||||
|
||||
|
||||
def test_tenant_admin_cannot_update_user_in_another_org(
|
||||
client, user_store, login
|
||||
):
|
||||
org_b = user_store.create_org("Organization B", org_id="org-b")
|
||||
admin_a = _create_org_admin(
|
||||
user_store, org_id="org-default", username="admin-update", password="admin-update-password"
|
||||
)
|
||||
victim = user_store.create_user(
|
||||
org_id=org_b["id"],
|
||||
username="org-b-victim",
|
||||
password="victim-password",
|
||||
name="Org B Victim",
|
||||
role="user",
|
||||
email="[email protected]",
|
||||
must_setup=False,
|
||||
)
|
||||
before = dict(victim)
|
||||
token = login(admin_a["username"], "admin-update-password")["token"]
|
||||
|
||||
response = client.put(
|
||||
"/api/admin/users/org-b-victim",
|
||||
json={"password": "attacker-password", "email": "[email protected]"},
|
||||
headers=auth_headers(token),
|
||||
)
|
||||
|
||||
assert response.status_code in (403, 404), response.get_json()
|
||||
assert user_store.get_user("org-b-victim") == before
|
||||
|
||||
|
||||
def test_tenant_admin_cannot_update_super_admin(
|
||||
client, user_store, login
|
||||
):
|
||||
admin_a = _create_org_admin(
|
||||
user_store, org_id="org-default", username="admin-no-root", password="admin-no-root-password"
|
||||
)
|
||||
super_admin_before = dict(user_store.get_user("admin"))
|
||||
token = login(admin_a["username"], "admin-no-root-password")["token"]
|
||||
|
||||
response = client.put(
|
||||
"/api/admin/users/admin",
|
||||
json={"password": "attacker-password", "email": "[email protected]"},
|
||||
headers=auth_headers(token),
|
||||
)
|
||||
|
||||
assert response.status_code in (403, 404), response.get_json()
|
||||
assert user_store.get_user("admin") == super_admin_before
|
||||
|
||||
|
||||
def test_tenant_admin_cannot_change_role_or_active_state(
|
||||
client, user_store, login
|
||||
):
|
||||
admin_a = _create_org_admin(
|
||||
user_store, org_id="org-default", username="admin-role", password="admin-role-password"
|
||||
)
|
||||
target = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="same-org-user",
|
||||
password="same-org-password",
|
||||
name="Same Org User",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
before = dict(target)
|
||||
token = login(admin_a["username"], "admin-role-password")["token"]
|
||||
|
||||
role_response = client.put(
|
||||
"/api/admin/users/same-org-user",
|
||||
json={"role": "admin"},
|
||||
headers=auth_headers(token),
|
||||
)
|
||||
active_response = client.put(
|
||||
"/api/admin/users/same-org-user",
|
||||
json={"active": False},
|
||||
headers=auth_headers(token),
|
||||
)
|
||||
|
||||
assert role_response.status_code == 403, role_response.get_json()
|
||||
assert active_response.status_code == 403, active_response.get_json()
|
||||
assert user_store.get_user("same-org-user") == before
|
||||
|
||||
|
||||
def test_admin_password_update_rejects_weak_password_without_mutation(
|
||||
client, user_store, login
|
||||
):
|
||||
admin = _create_org_admin(
|
||||
user_store, org_id="org-default", username="admin-password-policy", password="admin-password-policy"
|
||||
)
|
||||
target = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="password-policy-target",
|
||||
password="target-original-password",
|
||||
name="Password Policy Target",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
before = user_store.get_user(target["username"])
|
||||
token = login(admin["username"], "admin-password-policy")["token"]
|
||||
|
||||
response = client.put(
|
||||
"/api/admin/users/password-policy-target",
|
||||
json={"password": "12345678901"},
|
||||
headers=auth_headers(token),
|
||||
)
|
||||
|
||||
assert response.status_code == 400, response.get_json()
|
||||
assert user_store.get_user(target["username"]) == before
|
||||
|
||||
|
||||
def test_admin_profile_update_invalid_email_does_not_partially_change_password(
|
||||
client, user_store, login
|
||||
):
|
||||
admin = _create_org_admin(
|
||||
user_store, org_id="org-default", username="admin-profile-policy", password="admin-profile-policy"
|
||||
)
|
||||
target = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="profile-policy-target",
|
||||
password="target-original-password",
|
||||
name="Profile Policy Target",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
before = user_store.get_user(target["username"])
|
||||
token = login(admin["username"], "admin-profile-policy")["token"]
|
||||
|
||||
response = client.put(
|
||||
"/api/admin/users/profile-policy-target",
|
||||
json={"password": "target-updated-password", "email": "invalid-email"},
|
||||
headers=auth_headers(token),
|
||||
)
|
||||
|
||||
assert response.status_code == 400, response.get_json()
|
||||
assert user_store.get_user(target["username"]) == before
|
||||
|
||||
|
||||
def test_new_org_weak_password_does_not_create_orphan_org(
|
||||
client, user_store, login
|
||||
):
|
||||
user_store.complete_setup(
|
||||
"admin",
|
||||
"[email protected]",
|
||||
"bootstrap-admin-password",
|
||||
accepted_terms=True,
|
||||
accepted_terms_at="2026-08-13T00:00:00Z",
|
||||
)
|
||||
token = login("admin", "bootstrap-admin-password")["token"]
|
||||
org_ids_before = {org["id"] for org in user_store.orgs.all()}
|
||||
|
||||
response = client.post(
|
||||
"/api/admin/users",
|
||||
json={
|
||||
"username": "weak-new-org-admin",
|
||||
"password": "12345678901",
|
||||
"role": "admin",
|
||||
"new_org": True,
|
||||
},
|
||||
headers=auth_headers(token),
|
||||
)
|
||||
|
||||
assert response.status_code == 400, response.get_json()
|
||||
assert user_store.get_user_or_none("weak-new-org-admin") is None
|
||||
assert {org["id"] for org in user_store.orgs.all()} == org_ids_before
|
||||
|
||||
|
||||
def test_new_org_invalid_email_does_not_create_orphan_org(
|
||||
client, user_store, login
|
||||
):
|
||||
user_store.complete_setup(
|
||||
"admin",
|
||||
"[email protected]",
|
||||
"bootstrap-admin-password",
|
||||
accepted_terms=True,
|
||||
accepted_terms_at="2026-08-13T00:00:00Z",
|
||||
)
|
||||
token = login("admin", "bootstrap-admin-password")["token"]
|
||||
org_ids_before = {org["id"] for org in user_store.orgs.all()}
|
||||
|
||||
response = client.post(
|
||||
"/api/admin/users",
|
||||
json={
|
||||
"username": "invalid-email-new-org-admin",
|
||||
"email": "not-an-email",
|
||||
"password": "valid-new-org-password",
|
||||
"role": "admin",
|
||||
"new_org": True,
|
||||
},
|
||||
headers=auth_headers(token),
|
||||
)
|
||||
|
||||
assert response.status_code == 400, response.get_json()
|
||||
assert user_store.get_user_or_none("invalid-email-new-org-admin") is None
|
||||
assert {org["id"] for org in user_store.orgs.all()} == org_ids_before
|
||||
|
||||
|
||||
def test_new_org_non_string_email_does_not_create_orphan_org(
|
||||
client, user_store, login
|
||||
):
|
||||
user_store.complete_setup(
|
||||
"admin",
|
||||
"[email protected]",
|
||||
"bootstrap-admin-password",
|
||||
accepted_terms=True,
|
||||
accepted_terms_at="2026-08-13T00:00:00Z",
|
||||
)
|
||||
token = login("admin", "bootstrap-admin-password")["token"]
|
||||
org_ids_before = {org["id"] for org in user_store.orgs.all()}
|
||||
|
||||
response = client.post(
|
||||
"/api/admin/users",
|
||||
json={
|
||||
"username": "type-email-new-org-admin",
|
||||
"email": 123,
|
||||
"password": "valid-new-org-password",
|
||||
"role": "admin",
|
||||
"new_org": True,
|
||||
},
|
||||
headers=auth_headers(token),
|
||||
)
|
||||
|
||||
assert response.status_code == 400, response.get_json()
|
||||
assert user_store.get_user_or_none("type-email-new-org-admin") is None
|
||||
assert {org["id"] for org in user_store.orgs.all()} == org_ids_before
|
||||
|
||||
|
||||
def test_admin_create_rejects_non_string_name_without_mutation(
|
||||
client, user_store, login
|
||||
):
|
||||
user_store.complete_setup(
|
||||
"admin",
|
||||
"[email protected]",
|
||||
"bootstrap-admin-password",
|
||||
accepted_terms=True,
|
||||
accepted_terms_at="2026-08-13T00:00:00Z",
|
||||
)
|
||||
token = login("admin", "bootstrap-admin-password")["token"]
|
||||
|
||||
response = client.post(
|
||||
"/api/admin/users",
|
||||
json={
|
||||
"username": "non-string-name-user",
|
||||
"name": 123,
|
||||
"password": "valid-created-password",
|
||||
"role": "user",
|
||||
},
|
||||
headers=auth_headers(token),
|
||||
)
|
||||
|
||||
assert response.status_code == 400, response.get_json()
|
||||
assert user_store.get_user_or_none("non-string-name-user") is None
|
||||
|
||||
|
||||
def test_new_org_unexpected_user_write_failure_rolls_back_org(
|
||||
client, user_store, login, monkeypatch
|
||||
):
|
||||
user_store.complete_setup(
|
||||
"admin",
|
||||
"[email protected]",
|
||||
"bootstrap-admin-password",
|
||||
accepted_terms=True,
|
||||
accepted_terms_at="2026-08-13T00:00:00Z",
|
||||
)
|
||||
token = login("admin", "bootstrap-admin-password")["token"]
|
||||
org_ids_before = {org["id"] for org in user_store.orgs.all()}
|
||||
|
||||
def fail_user_write(**_kwargs):
|
||||
raise RuntimeError("simulated persistence failure")
|
||||
|
||||
monkeypatch.setattr(user_store, "create_user", fail_user_write)
|
||||
response = client.post(
|
||||
"/api/admin/users",
|
||||
json={
|
||||
"username": "failed-new-org-admin",
|
||||
"password": "valid-new-org-password",
|
||||
"role": "admin",
|
||||
"new_org": True,
|
||||
},
|
||||
headers=auth_headers(token),
|
||||
)
|
||||
|
||||
assert response.status_code == 500, response.get_json()
|
||||
assert {org["id"] for org in user_store.orgs.all()} == org_ids_before
|
||||
|
||||
|
||||
def test_super_admin_can_provision_into_existing_org(
|
||||
client, user_store, login
|
||||
):
|
||||
org_b = user_store.create_org("Organization B", org_id="org-b")
|
||||
user_store.complete_setup(
|
||||
"admin",
|
||||
"[email protected]",
|
||||
"bootstrap-password",
|
||||
accepted_terms=True,
|
||||
accepted_terms_at="2026-08-13T00:00:00Z",
|
||||
)
|
||||
token = login("admin", "bootstrap-password")["token"]
|
||||
|
||||
response = client.post(
|
||||
"/api/admin/users",
|
||||
json={
|
||||
"username": "platform-created",
|
||||
"name": "Platform Created",
|
||||
"password": "platform-password",
|
||||
"role": "user",
|
||||
"org_id": org_b["id"],
|
||||
},
|
||||
headers=auth_headers(token),
|
||||
)
|
||||
|
||||
assert response.status_code == 201, response.get_json()
|
||||
assert user_store.get_user("platform-created")["org_id"] == org_b["id"]
|
||||
|
||||
|
||||
def test_super_admin_group_listing_spans_all_orgs(client, user_store, login):
|
||||
user_store.complete_setup(
|
||||
"admin",
|
||||
"[email protected]",
|
||||
"platform-admin-password",
|
||||
accepted_terms=True,
|
||||
accepted_terms_at="2026-08-13T00:00:00Z",
|
||||
)
|
||||
super_token = login("admin", "platform-admin-password")["token"]
|
||||
org_b = user_store.create_org("Organization B", org_id="org-b")
|
||||
org_b_admin = user_store.create_user(
|
||||
org_id=org_b["id"],
|
||||
username="org-b-group-admin",
|
||||
password="org-b-group-password",
|
||||
name="Org B Group Admin",
|
||||
role="admin",
|
||||
must_setup=False,
|
||||
)
|
||||
org_b_token = login(
|
||||
org_b_admin["username"], "org-b-group-password"
|
||||
)["token"]
|
||||
|
||||
created = client.post(
|
||||
"/api/groups",
|
||||
json={"product": "Org B product", "segment": "Org B segment"},
|
||||
headers=auth_headers(org_b_token),
|
||||
)
|
||||
assert created.status_code == 201, created.get_json()
|
||||
gid = created.get_json()["group"]["id"]
|
||||
|
||||
response = client.get("/api/groups", headers=auth_headers(super_token))
|
||||
|
||||
assert response.status_code == 200, response.get_json()
|
||||
assert gid in {group["id"] for group in response.get_json()["groups"]}
|
||||
@@ -0,0 +1,830 @@
|
||||
"""Regression tests for first-time setup authorization."""
|
||||
from __future__ import annotations
|
||||
|
||||
import multiprocessing as mp
|
||||
from pathlib import Path
|
||||
import threading
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
import pytest
|
||||
|
||||
from app.auth.users import AuthError
|
||||
|
||||
|
||||
def _complete_setup_in_process(
|
||||
data_dir: str,
|
||||
email: str,
|
||||
barrier,
|
||||
result_queue,
|
||||
) -> None:
|
||||
"""Attempt setup in a forked worker after synchronizing before the write."""
|
||||
import app.auth.users as users_module
|
||||
from app.auth.users import UserStore
|
||||
|
||||
real_hash = users_module.generate_password_hash
|
||||
|
||||
def synchronized_hash(password: str) -> str:
|
||||
barrier.wait(timeout=5)
|
||||
return real_hash(password)
|
||||
|
||||
users_module.generate_password_hash = synchronized_hash
|
||||
try:
|
||||
updated = UserStore(Path(data_dir)).complete_setup(
|
||||
"setup-process-race",
|
||||
email,
|
||||
"new-password",
|
||||
accepted_terms=True,
|
||||
accepted_terms_at="2026-08-13T00:00:00Z",
|
||||
)
|
||||
except AuthError as exc:
|
||||
result_queue.put(("error", str(exc)))
|
||||
except BaseException as exc: # pragma: no cover - diagnostic for child failures
|
||||
result_queue.put(("exception", type(exc).__name__))
|
||||
else:
|
||||
result_queue.put(("ok", updated["email"]))
|
||||
|
||||
|
||||
def _complete_setup_holding_email_lock(
|
||||
data_dir: str,
|
||||
email: str,
|
||||
email_checked,
|
||||
release_email_check,
|
||||
result_queue,
|
||||
) -> None:
|
||||
"""Hold the collection lock after setup's email lookup for a cross-process race test."""
|
||||
from app.auth.users import AuthError, UserStore
|
||||
|
||||
original_by_email = UserStore.by_email
|
||||
|
||||
def paused_by_email(self, email: str):
|
||||
found = original_by_email(self, email)
|
||||
email_checked.set()
|
||||
release_email_check.wait(timeout=10)
|
||||
return found
|
||||
|
||||
UserStore.by_email = paused_by_email
|
||||
try:
|
||||
updated = UserStore(Path(data_dir)).complete_setup(
|
||||
"setup-create-process-race",
|
||||
email,
|
||||
"new-password",
|
||||
accepted_terms=True,
|
||||
accepted_terms_at="2026-08-13T00:00:00Z",
|
||||
)
|
||||
except AuthError as exc:
|
||||
result_queue.put(("error", str(exc)))
|
||||
except BaseException as exc: # pragma: no cover - diagnostic for child failures
|
||||
result_queue.put(("exception", type(exc).__name__))
|
||||
else:
|
||||
result_queue.put(("ok", updated["username"]))
|
||||
|
||||
|
||||
def _create_user_in_process(
|
||||
data_dir: str,
|
||||
email: str,
|
||||
started,
|
||||
result_queue,
|
||||
) -> None:
|
||||
"""Attempt an invited-user create in a separate worker process."""
|
||||
from app.auth.users import AuthError, UserStore
|
||||
|
||||
started.set()
|
||||
try:
|
||||
created = UserStore(Path(data_dir)).create_user(
|
||||
org_id="org-default",
|
||||
username="invited-during-setup-process",
|
||||
password="invite-password",
|
||||
name="Invited During Setup Process",
|
||||
role="user",
|
||||
email=email,
|
||||
)
|
||||
except AuthError as exc:
|
||||
result_queue.put(("error", str(exc)))
|
||||
except BaseException as exc: # pragma: no cover - diagnostic for child failures
|
||||
result_queue.put(("exception", type(exc).__name__))
|
||||
else:
|
||||
result_queue.put(("ok", created["username"]))
|
||||
|
||||
|
||||
def test_setup_cannot_change_another_account(client, user_store, login):
|
||||
"""A setup caller must not select an arbitrary username as mutation target."""
|
||||
user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="victim",
|
||||
password="victim-password",
|
||||
name="Victim",
|
||||
role="user",
|
||||
email="[email protected]",
|
||||
must_setup=False,
|
||||
)
|
||||
caller = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="caller",
|
||||
password="caller-password",
|
||||
name="Caller",
|
||||
role="user",
|
||||
must_setup=True,
|
||||
)
|
||||
|
||||
before = user_store.get_user("victim")
|
||||
token = login(caller["username"], "caller-password")["token"]
|
||||
|
||||
response = client.post(
|
||||
"/api/auth/setup",
|
||||
json={
|
||||
"username": "victim",
|
||||
"email": "[email protected]",
|
||||
"password": "attacker-password",
|
||||
"accepted_terms": True,
|
||||
},
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
)
|
||||
|
||||
assert response.status_code in (401, 403), response.get_json()
|
||||
after = user_store.get_user("victim")
|
||||
assert after["email"] == before["email"]
|
||||
assert after["password_hash"] == before["password_hash"]
|
||||
assert after["must_setup"] is False
|
||||
|
||||
|
||||
def test_setup_updates_only_the_authenticated_user(client, user_store, login):
|
||||
caller = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="setup-user",
|
||||
password="old-password",
|
||||
name="Setup User",
|
||||
role="user",
|
||||
must_setup=True,
|
||||
)
|
||||
token = login(caller["username"], "old-password")["token"]
|
||||
|
||||
response = client.post(
|
||||
"/api/auth/setup",
|
||||
json={
|
||||
"email": "[email protected]",
|
||||
"password": "new-password",
|
||||
"accepted_terms": True,
|
||||
},
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
)
|
||||
|
||||
assert response.status_code == 200, response.get_json()
|
||||
body = response.get_json()
|
||||
assert body["must_setup"] is False
|
||||
assert "password_hash" not in body["user"]
|
||||
updated = user_store.get_user("setup-user")
|
||||
assert updated["email"] == "[email protected]"
|
||||
assert updated["must_setup"] is False
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"terms_payload",
|
||||
[{}, {"accepted_terms": False}, {"accepted_terms": "true"}],
|
||||
ids=["omitted", "false", "non_boolean"],
|
||||
)
|
||||
def test_setup_requires_literal_true_terms_and_preserves_state(
|
||||
client, user_store, login, terms_payload
|
||||
):
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username=f"terms-{terms_payload.get('accepted_terms', 'omitted')}",
|
||||
password="terms-password",
|
||||
name="Terms User",
|
||||
role="user",
|
||||
must_setup=True,
|
||||
)
|
||||
token = login(user["username"], "terms-password")["token"]
|
||||
before = user_store.get_user(user["username"])
|
||||
payload = {
|
||||
"email": "[email protected]",
|
||||
"password": "terms-new-password",
|
||||
**terms_payload,
|
||||
}
|
||||
|
||||
response = client.post(
|
||||
"/api/auth/setup",
|
||||
json=payload,
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
)
|
||||
|
||||
assert response.status_code == 400, response.get_json()
|
||||
assert user_store.get_user(user["username"]) == before
|
||||
|
||||
|
||||
def test_create_user_requires_literal_boolean_must_setup(user_store):
|
||||
with pytest.raises(AuthError, match="must_setup must be boolean"):
|
||||
user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="malformed-must-setup-create",
|
||||
password="malformed-password",
|
||||
name="Malformed Must Setup Create",
|
||||
role="user",
|
||||
must_setup="false",
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("accepted_terms", [False, "true", 1])
|
||||
def test_complete_setup_requires_literal_true_terms(
|
||||
user_store, accepted_terms
|
||||
):
|
||||
"""The service boundary must enforce literal consent, not just the HTTP route."""
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username=f"service-terms-{str(accepted_terms).lower()}",
|
||||
password="terms-password",
|
||||
name="Service Terms User",
|
||||
role="user",
|
||||
must_setup=True,
|
||||
)
|
||||
before = user_store.get_user(user["username"])
|
||||
|
||||
with pytest.raises(AuthError, match="terms acceptance is required"):
|
||||
user_store.complete_setup(
|
||||
user["username"],
|
||||
"[email protected]",
|
||||
"terms-new-password",
|
||||
accepted_terms=accepted_terms,
|
||||
accepted_terms_at="2026-08-13T00:00:00Z",
|
||||
)
|
||||
|
||||
assert user_store.get_user(user["username"]) == before
|
||||
|
||||
|
||||
def test_setup_failure_paths_do_not_partially_update_account(
|
||||
user_store, monkeypatch
|
||||
):
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="setup-failure-paths",
|
||||
password="old-password",
|
||||
name="Setup Failure Paths",
|
||||
role="user",
|
||||
must_setup=True,
|
||||
)
|
||||
before = user_store.get_user(user["username"])
|
||||
shared_email = "[email protected]"
|
||||
user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="email-owner",
|
||||
password="owner-password",
|
||||
name="Email Owner",
|
||||
role="user",
|
||||
email=shared_email,
|
||||
)
|
||||
|
||||
with pytest.raises(AuthError, match="invalid email"):
|
||||
user_store.complete_setup(
|
||||
user["username"],
|
||||
"invalid-email",
|
||||
"new-password",
|
||||
accepted_terms=True,
|
||||
accepted_terms_at="2026-08-13T00:00:00Z",
|
||||
)
|
||||
assert user_store.get_user(user["username"]) == before
|
||||
|
||||
with pytest.raises(AuthError, match="email already exists"):
|
||||
user_store.complete_setup(
|
||||
user["username"],
|
||||
shared_email,
|
||||
"new-password",
|
||||
accepted_terms=True,
|
||||
accepted_terms_at="2026-08-13T00:00:00Z",
|
||||
)
|
||||
assert user_store.get_user(user["username"]) == before
|
||||
|
||||
def explode(_password: str) -> str:
|
||||
raise RuntimeError("hashing failed")
|
||||
|
||||
monkeypatch.setattr("app.auth.users.generate_password_hash", explode)
|
||||
with pytest.raises(RuntimeError, match="hashing failed"):
|
||||
user_store.complete_setup(
|
||||
user["username"],
|
||||
"[email protected]",
|
||||
"new-password",
|
||||
accepted_terms=True,
|
||||
accepted_terms_at="2026-08-13T00:00:00Z",
|
||||
)
|
||||
assert user_store.get_user(user["username"]) == before
|
||||
|
||||
|
||||
def test_password_policy_rejects_short_setup_and_password_change(
|
||||
user_store,
|
||||
):
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="password-policy-user",
|
||||
password="initial-password",
|
||||
name="Password Policy User",
|
||||
role="user",
|
||||
must_setup=True,
|
||||
)
|
||||
before = user_store.get_user(user["username"])
|
||||
|
||||
with pytest.raises(AuthError, match="password must be at least 12 characters"):
|
||||
user_store.complete_setup(
|
||||
user["username"],
|
||||
"[email protected]",
|
||||
"12345678901",
|
||||
accepted_terms=True,
|
||||
accepted_terms_at="2026-08-13T00:00:00Z",
|
||||
)
|
||||
assert user_store.get_user(user["username"]) == before
|
||||
|
||||
with pytest.raises(AuthError, match="password must be at least 12 characters"):
|
||||
user_store.set_password(user["username"], "12345678901")
|
||||
assert user_store.get_user(user["username"]) == before
|
||||
|
||||
with pytest.raises(AuthError, match="password must be at least 12 characters"):
|
||||
user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="weak-created-user",
|
||||
password="12345678901",
|
||||
name="Weak Created User",
|
||||
role="user",
|
||||
)
|
||||
assert user_store.get_user_or_none("weak-created-user") is None
|
||||
|
||||
|
||||
def test_http_setup_rejects_weak_password_without_mutation(client, user_store, login):
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="http-password-policy-user",
|
||||
password="initial-password",
|
||||
name="HTTP Password Policy User",
|
||||
role="user",
|
||||
must_setup=True,
|
||||
)
|
||||
before = user_store.get_user(user["username"])
|
||||
token = login(user["username"], "initial-password")["token"]
|
||||
|
||||
response = client.post(
|
||||
"/api/auth/setup",
|
||||
json={
|
||||
"email": "[email protected]",
|
||||
"password": "12345678901",
|
||||
"accepted_terms": True,
|
||||
},
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
)
|
||||
|
||||
assert response.status_code == 400, response.get_json()
|
||||
assert user_store.get_user(user["username"]) == before
|
||||
|
||||
|
||||
def test_http_setup_rejects_non_string_email_without_mutation(
|
||||
client, user_store, login
|
||||
):
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="http-email-type-user",
|
||||
password="initial-password",
|
||||
name="HTTP Email Type User",
|
||||
role="user",
|
||||
must_setup=True,
|
||||
)
|
||||
before = user_store.get_user(user["username"])
|
||||
token = login(user["username"], "initial-password")["token"]
|
||||
|
||||
response = client.post(
|
||||
"/api/auth/setup",
|
||||
json={
|
||||
"email": 123,
|
||||
"password": "valid-setup-password",
|
||||
"accepted_terms": True,
|
||||
},
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
)
|
||||
|
||||
assert response.status_code == 400, response.get_json()
|
||||
assert user_store.get_user(user["username"]) == before
|
||||
|
||||
|
||||
def test_profile_update_invalid_email_does_not_partially_change_name(
|
||||
client, user_store, login
|
||||
):
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="profile-partial-user",
|
||||
password="profile-original-password",
|
||||
name="Original Profile Name",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
before = user_store.get_user(user["username"])
|
||||
token = login(user["username"], "profile-original-password")["token"]
|
||||
|
||||
response = client.patch(
|
||||
"/api/auth/profile",
|
||||
json={"name": "Updated Profile Name", "email": "invalid-email"},
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
)
|
||||
|
||||
assert response.status_code == 400, response.get_json()
|
||||
assert user_store.get_user(user["username"]) == before
|
||||
|
||||
|
||||
def test_set_email_and_complete_setup_share_email_reservation_lock(
|
||||
user_store, monkeypatch
|
||||
):
|
||||
"""Profile email updates cannot race first-time setup for the same email."""
|
||||
pending = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="setup-profile-race",
|
||||
password="old-password",
|
||||
name="Setup Profile Race",
|
||||
role="user",
|
||||
must_setup=True,
|
||||
)
|
||||
profile = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="profile-email-race",
|
||||
password="profile-password",
|
||||
name="Profile Email Race",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"app.auth.users.generate_password_hash",
|
||||
lambda password: f"test-hash:{password}",
|
||||
)
|
||||
|
||||
original_by_email = user_store.by_email
|
||||
first_lookup_paused = threading.Event()
|
||||
release_first_lookup = threading.Event()
|
||||
pause_once = threading.Lock()
|
||||
already_paused = False
|
||||
|
||||
def paused_by_email(email: str):
|
||||
nonlocal already_paused
|
||||
found = original_by_email(email)
|
||||
with pause_once:
|
||||
should_pause = not already_paused
|
||||
already_paused = True
|
||||
if should_pause:
|
||||
first_lookup_paused.set()
|
||||
assert release_first_lookup.wait(timeout=5)
|
||||
return found
|
||||
|
||||
monkeypatch.setattr(user_store, "by_email", paused_by_email)
|
||||
shared_email = "[email protected]"
|
||||
|
||||
def profile_attempt() -> tuple[str, str]:
|
||||
try:
|
||||
updated = user_store.set_email(profile["username"], shared_email)
|
||||
except AuthError as exc:
|
||||
return ("error", str(exc))
|
||||
return ("ok", updated["username"])
|
||||
|
||||
def setup_attempt() -> tuple[str, str]:
|
||||
try:
|
||||
updated = user_store.complete_setup(
|
||||
pending["username"],
|
||||
shared_email,
|
||||
"new-password",
|
||||
accepted_terms=True,
|
||||
accepted_terms_at="2026-08-13T00:00:00Z",
|
||||
)
|
||||
except AuthError as exc:
|
||||
return ("error", str(exc))
|
||||
return ("ok", updated["username"])
|
||||
|
||||
with ThreadPoolExecutor(max_workers=2) as pool:
|
||||
profile_future = pool.submit(profile_attempt)
|
||||
assert first_lookup_paused.wait(timeout=5)
|
||||
setup_future = pool.submit(setup_attempt)
|
||||
release_first_lookup.set()
|
||||
results = [
|
||||
profile_future.result(timeout=5),
|
||||
setup_future.result(timeout=5),
|
||||
]
|
||||
|
||||
assert [status for status, _value in results].count("ok") == 1
|
||||
assert [status for status, _value in results].count("error") == 1
|
||||
assert {value for status, value in results if status == "error"} == {
|
||||
"a user with this email already exists"
|
||||
}
|
||||
matching = [
|
||||
user for user in user_store.users.all() if user.get("email") == shared_email
|
||||
]
|
||||
assert len(matching) == 1
|
||||
|
||||
|
||||
def test_setup_rejects_user_who_already_completed_setup(client, user_store, login):
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="already-ready",
|
||||
password="old-password",
|
||||
name="Already Ready",
|
||||
role="user",
|
||||
email="[email protected]",
|
||||
must_setup=False,
|
||||
)
|
||||
token = login(user["username"], "old-password")["token"]
|
||||
|
||||
response = client.post(
|
||||
"/api/auth/setup",
|
||||
json={
|
||||
"email": "[email protected]",
|
||||
"password": "new-password",
|
||||
"accepted_terms": True,
|
||||
},
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
)
|
||||
|
||||
assert response.status_code == 409, response.get_json()
|
||||
|
||||
|
||||
def test_complete_setup_is_atomic_under_concurrency(user_store, monkeypatch):
|
||||
"""Only one concurrent first-time setup may transition the account."""
|
||||
user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="setup-race",
|
||||
password="old-password",
|
||||
name="Setup Race",
|
||||
role="user",
|
||||
must_setup=True,
|
||||
)
|
||||
|
||||
import app.auth.users as users_module
|
||||
|
||||
real_hash = users_module.generate_password_hash
|
||||
entered_hash = threading.Barrier(2)
|
||||
|
||||
def synchronized_hash(password: str) -> str:
|
||||
entered_hash.wait(timeout=2)
|
||||
return real_hash(password)
|
||||
|
||||
monkeypatch.setattr(users_module, "generate_password_hash", synchronized_hash)
|
||||
|
||||
def attempt(index: int) -> tuple[str, str]:
|
||||
try:
|
||||
updated = user_store.complete_setup(
|
||||
"setup-race",
|
||||
f"race-{index}@example.com",
|
||||
f"new-password-{index}",
|
||||
accepted_terms=True,
|
||||
accepted_terms_at="2026-08-13T00:00:00Z",
|
||||
)
|
||||
except AuthError as exc:
|
||||
return ("error", str(exc))
|
||||
return ("ok", updated["email"])
|
||||
|
||||
with ThreadPoolExecutor(max_workers=2) as pool:
|
||||
results = list(pool.map(attempt, (1, 2)))
|
||||
|
||||
assert [status for status, _value in results].count("ok") == 1
|
||||
assert [status for status, _value in results].count("error") == 1
|
||||
assert {value for status, value in results if status == "error"} == {
|
||||
"setup already completed"
|
||||
}
|
||||
final = user_store.get_user("setup-race")
|
||||
assert final["must_setup"] is False
|
||||
assert final["email"] in {"[email protected]", "[email protected]"}
|
||||
|
||||
|
||||
def test_complete_setup_reserves_email_across_concurrent_users(user_store, monkeypatch):
|
||||
"""Concurrent setup cannot assign one email address to two accounts."""
|
||||
for username in ("setup-email-race-1", "setup-email-race-2"):
|
||||
user_store.create_user(
|
||||
org_id="org-default",
|
||||
username=username,
|
||||
password="old-password",
|
||||
name=username,
|
||||
role="user",
|
||||
must_setup=True,
|
||||
)
|
||||
|
||||
import app.auth.users as users_module
|
||||
|
||||
real_hash = users_module.generate_password_hash
|
||||
entered_hash = threading.Barrier(2)
|
||||
|
||||
def synchronized_hash(password: str) -> str:
|
||||
entered_hash.wait(timeout=2)
|
||||
return real_hash(password)
|
||||
|
||||
monkeypatch.setattr(users_module, "generate_password_hash", synchronized_hash)
|
||||
|
||||
def attempt(username: str) -> tuple[str, str]:
|
||||
try:
|
||||
updated = user_store.complete_setup(
|
||||
username,
|
||||
"[email protected]",
|
||||
"new-password",
|
||||
accepted_terms=True,
|
||||
accepted_terms_at="2026-08-13T00:00:00Z",
|
||||
)
|
||||
except AuthError as exc:
|
||||
return ("error", str(exc))
|
||||
return ("ok", updated["username"])
|
||||
|
||||
with ThreadPoolExecutor(max_workers=2) as pool:
|
||||
results = list(pool.map(attempt, ("setup-email-race-1", "setup-email-race-2")))
|
||||
|
||||
assert [status for status, _value in results].count("ok") == 1
|
||||
assert [status for status, _value in results].count("error") == 1
|
||||
assert {value for status, value in results if status == "error"} == {
|
||||
"a user with this email already exists"
|
||||
}
|
||||
|
||||
|
||||
def test_create_user_and_setup_share_email_reservation_lock(user_store, monkeypatch):
|
||||
"""Invite creation cannot race first-time setup for the same email."""
|
||||
user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="setup-create-race",
|
||||
password="old-password",
|
||||
name="Setup Create Race",
|
||||
role="user",
|
||||
must_setup=True,
|
||||
)
|
||||
|
||||
import app.auth.users as users_module
|
||||
|
||||
monkeypatch.setattr(
|
||||
users_module,
|
||||
"generate_password_hash",
|
||||
lambda password: f"test-hash:{password}",
|
||||
)
|
||||
original_email_exists = user_store.email_exists
|
||||
original_by_email = user_store.by_email
|
||||
original_complete_setup = user_store.complete_setup
|
||||
create_checked = threading.Event()
|
||||
release_create = threading.Event()
|
||||
setup_checked = threading.Event()
|
||||
setup_called = threading.Event()
|
||||
setup_context = threading.local()
|
||||
|
||||
def paused_email_exists(email: str) -> bool:
|
||||
found = original_email_exists(email)
|
||||
create_checked.set()
|
||||
assert release_create.wait(timeout=5)
|
||||
return found
|
||||
|
||||
def tracked_by_email(email: str):
|
||||
found = original_by_email(email)
|
||||
if getattr(setup_context, "active", False):
|
||||
setup_checked.set()
|
||||
return found
|
||||
|
||||
def tracked_complete_setup(*args, **kwargs):
|
||||
setup_called.set()
|
||||
setup_context.active = True
|
||||
try:
|
||||
return original_complete_setup(*args, **kwargs)
|
||||
finally:
|
||||
setup_context.active = False
|
||||
|
||||
monkeypatch.setattr(user_store, "email_exists", paused_email_exists)
|
||||
monkeypatch.setattr(user_store, "by_email", tracked_by_email)
|
||||
monkeypatch.setattr(user_store, "complete_setup", tracked_complete_setup)
|
||||
|
||||
shared_email = "[email protected]"
|
||||
|
||||
def create_attempt() -> tuple[str, str]:
|
||||
try:
|
||||
created = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="invited-during-setup",
|
||||
password="invite-password",
|
||||
name="Invited During Setup",
|
||||
role="user",
|
||||
email=shared_email,
|
||||
)
|
||||
except AuthError as exc:
|
||||
return ("error", str(exc))
|
||||
return ("ok", created["username"])
|
||||
|
||||
def setup_attempt() -> tuple[str, str]:
|
||||
try:
|
||||
updated = user_store.complete_setup(
|
||||
"setup-create-race",
|
||||
shared_email,
|
||||
"new-password",
|
||||
accepted_terms=True,
|
||||
accepted_terms_at="2026-08-13T00:00:00Z",
|
||||
)
|
||||
except AuthError as exc:
|
||||
return ("error", str(exc))
|
||||
return ("ok", updated["username"])
|
||||
|
||||
with ThreadPoolExecutor(max_workers=2) as pool:
|
||||
create_future = pool.submit(create_attempt)
|
||||
assert create_checked.wait(timeout=5)
|
||||
setup_future = pool.submit(setup_attempt)
|
||||
assert setup_called.wait(timeout=5)
|
||||
setup_reached_email_before_release = setup_checked.wait(timeout=1)
|
||||
release_create.set()
|
||||
results = [
|
||||
create_future.result(timeout=5),
|
||||
setup_future.result(timeout=5),
|
||||
]
|
||||
|
||||
assert setup_reached_email_before_release is False
|
||||
assert [status for status, _value in results].count("ok") == 1
|
||||
assert [status for status, _value in results].count("error") == 1
|
||||
assert {value for status, value in results if status == "error"} == {
|
||||
"a user with this email already exists"
|
||||
}
|
||||
matching = [user for user in user_store.users.all() if user.get("email") == shared_email]
|
||||
assert [user["username"] for user in matching] in [
|
||||
["invited-during-setup"],
|
||||
["setup-create-race"],
|
||||
]
|
||||
|
||||
|
||||
def test_create_user_and_setup_share_email_reservation_lock_across_processes(user_store):
|
||||
"""The email reservation must hold when invite and setup run in separate workers."""
|
||||
user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="setup-create-process-race",
|
||||
password="old-password",
|
||||
name="Setup Create Process Race",
|
||||
role="user",
|
||||
must_setup=True,
|
||||
)
|
||||
|
||||
context = mp.get_context("fork")
|
||||
email_checked = context.Event()
|
||||
release_email_check = context.Event()
|
||||
create_started = context.Event()
|
||||
result_queue = context.Queue()
|
||||
data_dir = str(user_store.users.root.parent)
|
||||
shared_email = "[email protected]"
|
||||
setup_process = context.Process(
|
||||
target=_complete_setup_holding_email_lock,
|
||||
args=(data_dir, shared_email, email_checked, release_email_check, result_queue),
|
||||
)
|
||||
setup_process.start()
|
||||
create_process = None
|
||||
try:
|
||||
assert email_checked.wait(timeout=10)
|
||||
create_process = context.Process(
|
||||
target=_create_user_in_process,
|
||||
args=(data_dir, shared_email, create_started, result_queue),
|
||||
)
|
||||
create_process.start()
|
||||
assert create_started.wait(timeout=5)
|
||||
create_process.join(timeout=1)
|
||||
assert create_process.is_alive()
|
||||
release_email_check.set()
|
||||
setup_process.join(timeout=10)
|
||||
create_process.join(timeout=10)
|
||||
assert setup_process.exitcode == 0
|
||||
assert create_process.exitcode == 0
|
||||
results = [result_queue.get(timeout=2) for _ in range(2)]
|
||||
assert [status for status, _value in results].count("ok") == 1
|
||||
assert [status for status, _value in results].count("error") == 1
|
||||
assert {value for status, value in results if status == "error"} == {
|
||||
"a user with this email already exists"
|
||||
}
|
||||
finally:
|
||||
release_email_check.set()
|
||||
for process in (setup_process, create_process):
|
||||
if process is not None and process.is_alive():
|
||||
process.terminate()
|
||||
if process is not None:
|
||||
process.join(timeout=2)
|
||||
|
||||
|
||||
def test_complete_setup_is_atomic_across_worker_processes(user_store):
|
||||
"""The one-time setup claim must hold when workers do not share Python locks."""
|
||||
user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="setup-process-race",
|
||||
password="old-password",
|
||||
name="Setup Process Race",
|
||||
role="user",
|
||||
must_setup=True,
|
||||
)
|
||||
|
||||
context = mp.get_context("fork")
|
||||
barrier = context.Barrier(2)
|
||||
result_queue = context.Queue()
|
||||
data_dir = str(user_store.users.root.parent)
|
||||
processes = [
|
||||
context.Process(
|
||||
target=_complete_setup_in_process,
|
||||
args=(data_dir, f"process-{index}@example.com", barrier, result_queue),
|
||||
)
|
||||
for index in (1, 2)
|
||||
]
|
||||
for process in processes:
|
||||
process.start()
|
||||
try:
|
||||
for process in processes:
|
||||
process.join(timeout=15)
|
||||
|
||||
assert all(not process.is_alive() for process in processes)
|
||||
assert all(process.exitcode == 0 for process in processes)
|
||||
results = [result_queue.get(timeout=2) for _ in processes]
|
||||
assert [status for status, _value in results].count("ok") == 1
|
||||
assert [status for status, _value in results].count("error") == 1
|
||||
assert {value for status, value in results if status == "error"} == {
|
||||
"setup already completed"
|
||||
}
|
||||
finally:
|
||||
for process in processes:
|
||||
if process.is_alive():
|
||||
process.terminate()
|
||||
process.join(timeout=2)
|
||||
@@ -0,0 +1,159 @@
|
||||
"""Regression tests for fail-closed bootstrap and production auth configuration."""
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
def _configure(
|
||||
monkeypatch,
|
||||
tmp_path,
|
||||
*,
|
||||
app_env: str,
|
||||
secret: str,
|
||||
bootstrap: str,
|
||||
debug: bool | None = None,
|
||||
):
|
||||
from app.config import Config
|
||||
|
||||
monkeypatch.setattr(Config, "APP_ENV", app_env)
|
||||
monkeypatch.setattr(Config, "DATA_DIR", tmp_path)
|
||||
monkeypatch.setattr(Config, "SECRET_KEY", secret)
|
||||
monkeypatch.setattr(Config, "BOOTSTRAP_ADMIN_PASSWORD", bootstrap)
|
||||
monkeypatch.setattr(
|
||||
Config, "FLASK_DEBUG", app_env != "production" if debug is None else debug
|
||||
)
|
||||
return Config
|
||||
|
||||
|
||||
def test_production_empty_store_requires_bootstrap_password(monkeypatch, tmp_path):
|
||||
config = _configure(
|
||||
monkeypatch,
|
||||
tmp_path,
|
||||
app_env="production",
|
||||
secret="production-secret-0123456789abcdef0123456789abcdef",
|
||||
bootstrap="",
|
||||
)
|
||||
from app.factory import create_app
|
||||
|
||||
with pytest.raises(RuntimeError, match="BOOTSTRAP_ADMIN_PASSWORD"):
|
||||
create_app()
|
||||
|
||||
|
||||
def test_production_rejects_default_jwt_secret(monkeypatch, tmp_path):
|
||||
_configure(
|
||||
monkeypatch,
|
||||
tmp_path,
|
||||
app_env="production",
|
||||
secret="short-invalid-secret",
|
||||
bootstrap="bootstrap-password-0123456789",
|
||||
)
|
||||
from app.factory import create_app
|
||||
|
||||
with pytest.raises(RuntimeError, match="JWT_SECRET"):
|
||||
create_app()
|
||||
|
||||
|
||||
def test_debug_off_cannot_downgrade_runtime_to_development(
|
||||
monkeypatch, tmp_path
|
||||
):
|
||||
_configure(
|
||||
monkeypatch,
|
||||
tmp_path,
|
||||
app_env="development",
|
||||
debug=False,
|
||||
secret="short-secret",
|
||||
bootstrap="bootstrap-password-0123456789",
|
||||
)
|
||||
from app.factory import create_app
|
||||
|
||||
with pytest.raises(RuntimeError, match="JWT_SECRET"):
|
||||
create_app()
|
||||
|
||||
|
||||
def test_debug_off_requires_strong_bootstrap_password(
|
||||
monkeypatch, tmp_path
|
||||
):
|
||||
_configure(
|
||||
monkeypatch,
|
||||
tmp_path,
|
||||
app_env="development",
|
||||
debug=False,
|
||||
secret="production-secret-0123456789abcdef0123456789abcdef",
|
||||
bootstrap="short",
|
||||
)
|
||||
from app.factory import create_app
|
||||
|
||||
with pytest.raises(RuntimeError, match="BOOTSTRAP_ADMIN_PASSWORD"):
|
||||
create_app()
|
||||
|
||||
|
||||
def test_existing_user_store_does_not_need_bootstrap_password(
|
||||
monkeypatch, tmp_path
|
||||
):
|
||||
from app.auth.users import UserStore
|
||||
|
||||
seed_store = UserStore(tmp_path)
|
||||
seed_store.create_org("Existing Organization", org_id="org-default")
|
||||
seed_store.create_user(
|
||||
org_id="org-default",
|
||||
username="existing-admin",
|
||||
password="existing-password",
|
||||
name="Existing Admin",
|
||||
role="super_admin",
|
||||
must_setup=False,
|
||||
)
|
||||
_configure(
|
||||
monkeypatch,
|
||||
tmp_path,
|
||||
app_env="production",
|
||||
secret="production-secret-0123456789abcdef0123456789abcdef",
|
||||
bootstrap="",
|
||||
)
|
||||
from app.factory import create_app
|
||||
|
||||
app = create_app()
|
||||
|
||||
assert app.extensions["user_store"].get_user("existing-admin")["role"] == "super_admin"
|
||||
|
||||
|
||||
def test_test_mode_uses_injected_bootstrap_password(monkeypatch, tmp_path):
|
||||
_configure(
|
||||
monkeypatch,
|
||||
tmp_path,
|
||||
app_env="test",
|
||||
secret="pytest-secret-0123456789abcdef0123456789abcdef",
|
||||
bootstrap="pytest-bootstrap-password",
|
||||
)
|
||||
from app.factory import create_app
|
||||
|
||||
app = create_app()
|
||||
user_store = app.extensions["user_store"]
|
||||
|
||||
assert user_store.get_user("admin")["must_setup"] is True
|
||||
|
||||
|
||||
def test_default_runtime_environment_is_fail_closed(monkeypatch):
|
||||
from app.config import Config
|
||||
|
||||
monkeypatch.delenv("APP_ENV", raising=False)
|
||||
monkeypatch.delenv("FLASK_DEBUG", raising=False)
|
||||
monkeypatch.setattr(Config, "APP_ENV", "production")
|
||||
monkeypatch.setattr(Config, "FLASK_DEBUG", False)
|
||||
monkeypatch.setattr(Config, "SECRET_KEY", "")
|
||||
|
||||
with pytest.raises(RuntimeError, match="JWT_SECRET"):
|
||||
Config.validate_runtime_security()
|
||||
|
||||
|
||||
def test_known_bootstrap_placeholder_is_rejected(monkeypatch, tmp_path):
|
||||
_configure(
|
||||
monkeypatch,
|
||||
tmp_path,
|
||||
app_env="production",
|
||||
secret="production-secret-0123456789abcdef0123456789abcdef",
|
||||
bootstrap="replace_with_a_strong_initial_password",
|
||||
)
|
||||
from app.factory import create_app
|
||||
|
||||
with pytest.raises(RuntimeError, match="BOOTSTRAP_ADMIN_PASSWORD"):
|
||||
create_app()
|
||||
@@ -0,0 +1,290 @@
|
||||
"""Contract tests for the S4.2 relational schema foundation."""
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from alembic import command
|
||||
from alembic.config import Config as AlembicConfig
|
||||
from sqlalchemy import inspect, text
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.db import Base, create_db_engine
|
||||
from app.models import AuditEvent, Group, Message, Organization, Persona, TrainingSession, User
|
||||
|
||||
|
||||
EXPECTED_TABLES = {
|
||||
"organizations",
|
||||
"users",
|
||||
"groups",
|
||||
"personas",
|
||||
"sessions",
|
||||
"messages",
|
||||
"audit_events",
|
||||
}
|
||||
|
||||
|
||||
def _sqlite_engine(tmp_path: Path):
|
||||
return create_db_engine(f"sqlite:///{tmp_path / 'schema.db'}")
|
||||
|
||||
|
||||
def test_engine_rejects_dialects_without_partial_index_support():
|
||||
with pytest.raises(ValueError, match="Unsupported database dialect"):
|
||||
create_db_engine("mysql://user@localhost/sales_trainer")
|
||||
|
||||
|
||||
def test_metadata_exposes_required_tables_and_tenant_keys(tmp_path: Path):
|
||||
engine = _sqlite_engine(tmp_path)
|
||||
Base.metadata.create_all(engine)
|
||||
|
||||
inspector = inspect(engine)
|
||||
assert EXPECTED_TABLES <= set(inspector.get_table_names())
|
||||
|
||||
assert {column["name"] for column in inspector.get_columns("organizations")} >= {
|
||||
"id",
|
||||
"name",
|
||||
"plan",
|
||||
"seats",
|
||||
"active",
|
||||
"created_at",
|
||||
}
|
||||
assert {column["name"] for column in inspector.get_columns("users")} >= {
|
||||
"id",
|
||||
"org_id",
|
||||
"username",
|
||||
"password_hash",
|
||||
"email",
|
||||
"name",
|
||||
"role",
|
||||
"active",
|
||||
"must_setup",
|
||||
"accepted_terms",
|
||||
"accepted_terms_at",
|
||||
"auth_version",
|
||||
"created_at",
|
||||
}
|
||||
assert {column["name"] for column in inspector.get_columns("groups")} >= {
|
||||
"id",
|
||||
"org_id",
|
||||
"owner_user_id",
|
||||
"creator_user_id",
|
||||
"name",
|
||||
}
|
||||
assert {column["name"] for column in inspector.get_columns("sessions")} >= {
|
||||
"id",
|
||||
"org_id",
|
||||
"user_id",
|
||||
"group_id",
|
||||
"persona_id",
|
||||
"mode",
|
||||
"status",
|
||||
"outcome",
|
||||
"scenario_json",
|
||||
"internal_json",
|
||||
"debrief_json",
|
||||
}
|
||||
|
||||
|
||||
def test_constraints_preserve_tenant_links_and_attempt_uniqueness(tmp_path: Path):
|
||||
engine = _sqlite_engine(tmp_path)
|
||||
Base.metadata.create_all(engine)
|
||||
|
||||
with Session(engine) as db:
|
||||
org = Organization(id="org-a", name="A", plan="trial", seats=5)
|
||||
other_org = Organization(id="org-b", name="B", plan="trial", seats=5)
|
||||
user = User(
|
||||
id="user-a",
|
||||
org_id="org-a",
|
||||
username="a",
|
||||
password_hash="hashed",
|
||||
email="[email protected]",
|
||||
name="A User",
|
||||
role="user",
|
||||
accepted_terms=True,
|
||||
auth_version=3,
|
||||
)
|
||||
group = Group(
|
||||
id="group-a",
|
||||
org_id="org-a",
|
||||
owner_user_id="user-a",
|
||||
creator_user_id="user-a",
|
||||
name="Group A",
|
||||
)
|
||||
persona = Persona(id="persona-a", group_id="group-a", tier="base", public_json={})
|
||||
attempt = TrainingSession(
|
||||
id="session-a",
|
||||
org_id="org-a",
|
||||
user_id="user-a",
|
||||
group_id="group-a",
|
||||
persona_id="persona-a",
|
||||
mode="practice",
|
||||
status="active",
|
||||
scenario_json={},
|
||||
)
|
||||
db.add_all([org, other_org, user])
|
||||
db.commit()
|
||||
|
||||
db.add(group)
|
||||
db.commit()
|
||||
|
||||
db.add(persona)
|
||||
db.commit()
|
||||
|
||||
db.add(attempt)
|
||||
db.commit()
|
||||
|
||||
valid_audit = AuditEvent(
|
||||
org_id="org-a",
|
||||
actor_user_id="user-a",
|
||||
action="group.created",
|
||||
subject="group-a",
|
||||
)
|
||||
db.add(valid_audit)
|
||||
db.commit()
|
||||
|
||||
cross_tenant_audit = AuditEvent(
|
||||
org_id="org-b",
|
||||
actor_user_id="user-a",
|
||||
action="group.created",
|
||||
subject="group-b",
|
||||
)
|
||||
db.add(cross_tenant_audit)
|
||||
with pytest.raises(IntegrityError):
|
||||
db.commit()
|
||||
db.rollback()
|
||||
|
||||
partial_null_audit = AuditEvent(
|
||||
org_id=None,
|
||||
actor_user_id="user-a",
|
||||
action="group.created",
|
||||
subject="group-a",
|
||||
)
|
||||
db.add(partial_null_audit)
|
||||
with pytest.raises(IntegrityError):
|
||||
db.commit()
|
||||
db.rollback()
|
||||
|
||||
duplicate_attempt = TrainingSession(
|
||||
id="session-b",
|
||||
org_id="org-a",
|
||||
user_id="user-a",
|
||||
group_id="group-a",
|
||||
persona_id="persona-a",
|
||||
mode="practice",
|
||||
status="active",
|
||||
)
|
||||
db.add(duplicate_attempt)
|
||||
with pytest.raises(IntegrityError):
|
||||
db.commit()
|
||||
db.rollback()
|
||||
|
||||
preview_duplicate = TrainingSession(
|
||||
id="preview-b",
|
||||
org_id="org-a",
|
||||
user_id="user-a",
|
||||
group_id="group-a",
|
||||
persona_id="persona-a",
|
||||
mode="preview",
|
||||
status="completed",
|
||||
)
|
||||
db.add(preview_duplicate)
|
||||
db.commit()
|
||||
|
||||
cross_tenant_group = Group(
|
||||
id="group-b",
|
||||
org_id="org-b",
|
||||
owner_user_id="user-a",
|
||||
name="Invalid cross-tenant group",
|
||||
)
|
||||
db.add(cross_tenant_group)
|
||||
with pytest.raises(IntegrityError):
|
||||
db.commit()
|
||||
db.rollback()
|
||||
|
||||
|
||||
def test_metadata_create_all_exposes_relational_defaults(tmp_path: Path):
|
||||
engine = _sqlite_engine(tmp_path)
|
||||
Base.metadata.create_all(engine)
|
||||
|
||||
with engine.begin() as connection:
|
||||
connection.execute(text("INSERT INTO organizations (id, name) VALUES ('org-a', 'A')"))
|
||||
connection.execute(
|
||||
text(
|
||||
"INSERT INTO users "
|
||||
"(id, org_id, username, password_hash, name, role) "
|
||||
"VALUES ('user-a', 'org-a', 'a', 'hashed', 'A User', 'user')"
|
||||
)
|
||||
)
|
||||
organization = connection.execute(
|
||||
text("SELECT plan, seats, active FROM organizations WHERE id = 'org-a'")
|
||||
).one()
|
||||
user = connection.execute(
|
||||
text(
|
||||
"SELECT active, must_setup, accepted_terms, auth_version "
|
||||
"FROM users WHERE id = 'user-a'"
|
||||
)
|
||||
).one()
|
||||
|
||||
assert tuple(organization) == ("trial", 5, True)
|
||||
assert tuple(user) == (True, False, False, 0)
|
||||
|
||||
|
||||
def test_alembic_online_path_rejects_unsupported_dialects(monkeypatch: pytest.MonkeyPatch):
|
||||
alembic_cfg = AlembicConfig(str(Path(__file__).parents[1] / "alembic.ini"))
|
||||
alembic_cfg.set_main_option("sqlalchemy.url", "mysql://user@localhost/sales_trainer")
|
||||
monkeypatch.chdir(Path(__file__).parents[2])
|
||||
|
||||
with pytest.raises(ValueError, match="Unsupported database dialect"):
|
||||
command.upgrade(alembic_cfg, "head")
|
||||
|
||||
|
||||
def test_alembic_offline_path_rejects_unsupported_dialects(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
capsys: pytest.CaptureFixture[str],
|
||||
):
|
||||
alembic_cfg = AlembicConfig(str(Path(__file__).parents[1] / "alembic.ini"))
|
||||
alembic_cfg.set_main_option("sqlalchemy.url", "mysql://user@localhost/sales_trainer")
|
||||
monkeypatch.chdir(Path(__file__).parents[2])
|
||||
|
||||
with pytest.raises(ValueError, match="Unsupported database dialect"):
|
||||
command.upgrade(alembic_cfg, "head", sql=True)
|
||||
|
||||
assert capsys.readouterr().out == ""
|
||||
|
||||
|
||||
def test_alembic_sqlite_offline_render_includes_audit_constraint(
|
||||
tmp_path: Path,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
capsys: pytest.CaptureFixture[str],
|
||||
):
|
||||
alembic_cfg = AlembicConfig(str(Path(__file__).parents[1] / "alembic.ini"))
|
||||
alembic_cfg.set_main_option("sqlalchemy.url", f"sqlite:///{tmp_path / 'offline.db'}")
|
||||
monkeypatch.chdir(Path(__file__).parents[2])
|
||||
|
||||
command.upgrade(alembic_cfg, "head", sql=True)
|
||||
rendered = capsys.readouterr().out
|
||||
|
||||
assert "ck_audit_actor_requires_org" in rendered
|
||||
assert "CREATE TABLE _alembic_tmp_audit_events" in rendered
|
||||
|
||||
command.downgrade(alembic_cfg, "head:base", sql=True)
|
||||
downgrade_rendered = capsys.readouterr().out
|
||||
assert "DROP TABLE _alembic_tmp_audit_events" not in downgrade_rendered
|
||||
assert "_alembic_tmp_audit_events" in downgrade_rendered
|
||||
|
||||
|
||||
def test_alembic_migration_up_and_down(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
|
||||
database_url = f"sqlite:///{tmp_path / 'migration.db'}"
|
||||
alembic_cfg = AlembicConfig(str(Path(__file__).parents[1] / "alembic.ini"))
|
||||
alembic_cfg.set_main_option("sqlalchemy.url", database_url)
|
||||
monkeypatch.chdir(Path(__file__).parents[2])
|
||||
|
||||
command.upgrade(alembic_cfg, "head")
|
||||
engine = create_db_engine(database_url)
|
||||
assert EXPECTED_TABLES <= set(inspect(engine).get_table_names())
|
||||
with engine.connect() as connection:
|
||||
assert connection.execute(text("PRAGMA foreign_keys")).scalar() == 1
|
||||
|
||||
command.downgrade(alembic_cfg, "base")
|
||||
assert not (EXPECTED_TABLES & set(inspect(engine).get_table_names()))
|
||||
@@ -0,0 +1,29 @@
|
||||
"""Export-link authorization regression tests."""
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
def _headers(token: str) -> dict[str, str]:
|
||||
return {"Authorization": f"Bearer {token}"}
|
||||
|
||||
|
||||
def test_export_link_is_invalid_after_current_role_demotion(client, user_store, login):
|
||||
user_store.complete_setup(
|
||||
"admin",
|
||||
"[email protected]",
|
||||
"admin-ready-password",
|
||||
accepted_terms=True,
|
||||
accepted_terms_at="2026-08-13T00:00:00Z",
|
||||
)
|
||||
token = login("admin", "admin-ready-password")["token"]
|
||||
link_response = client.get("/api/analytics/export/token", headers=_headers(token))
|
||||
assert link_response.status_code == 200, link_response.get_json()
|
||||
export_token = link_response.get_json()["token"]
|
||||
|
||||
user_store.set_role("admin", "admin")
|
||||
response = client.get(
|
||||
f"/api/analytics/export?token={export_token}",
|
||||
headers=_headers(token),
|
||||
)
|
||||
|
||||
assert response.status_code == 403
|
||||
assert response.get_json() == {"error": "export link authorization is stale"}
|
||||
@@ -0,0 +1,174 @@
|
||||
"""Final-judge invariants for automatic and manual finish paths."""
|
||||
from __future__ import annotations
|
||||
|
||||
from copy import deepcopy
|
||||
|
||||
import pytest
|
||||
|
||||
from app.api import chat_routes
|
||||
from app.llm import LLMError
|
||||
|
||||
|
||||
class FakeSessions:
|
||||
def __init__(self, session):
|
||||
self.row = deepcopy(session)
|
||||
self.updates = []
|
||||
|
||||
def update(self, sid, **fields):
|
||||
assert sid == self.row["id"]
|
||||
self.updates.append(fields)
|
||||
self.row.update(fields)
|
||||
return deepcopy(self.row)
|
||||
|
||||
|
||||
class StubSim:
|
||||
def __init__(self, verdict=None, error=False):
|
||||
self.verdict = verdict or {
|
||||
"outcome": "won",
|
||||
"score": 88,
|
||||
"pain": "qualified pain",
|
||||
"why": "clear fit and acceptance",
|
||||
"failurePoints": [],
|
||||
"coaching": ["Keep the close concise"],
|
||||
"painProgress": {"qualified pain": 100},
|
||||
}
|
||||
self.error = error
|
||||
self.judge_calls = 0
|
||||
|
||||
def judge(self, **kwargs):
|
||||
self.judge_calls += 1
|
||||
if self.error:
|
||||
raise LLMError("provider unavailable")
|
||||
return deepcopy(self.verdict)
|
||||
|
||||
|
||||
def _session():
|
||||
return {
|
||||
"id": "session-1",
|
||||
"org_id": "org-1",
|
||||
"user_id": "user-1",
|
||||
"group_id": "group-1",
|
||||
"persona_id": "persona-1",
|
||||
"persona_name": "Customer",
|
||||
"mode": "trainee",
|
||||
"status": "active",
|
||||
"outcome": None,
|
||||
"messages": [{"role": "seller", "text": "สวัสดี"}],
|
||||
"internal": {"turns": 2, "score": 61},
|
||||
"debrief": None,
|
||||
"locale": "th",
|
||||
}
|
||||
|
||||
|
||||
def _run(monkeypatch, *, verdict=None, error=False, session=None):
|
||||
sessions = FakeSessions(session or _session())
|
||||
stores = {"sessions": sessions}
|
||||
sim = StubSim(verdict=verdict, error=error)
|
||||
monkeypatch.setattr(chat_routes, "_sim", lambda group, persona: sim)
|
||||
updated, debrief = chat_routes._finalize_session(
|
||||
stores,
|
||||
sessions.row,
|
||||
{"sales_kit": {}},
|
||||
{"name": "Customer", "pains": [{"description": "pain"}]},
|
||||
)
|
||||
return sessions, sim, updated, debrief
|
||||
|
||||
|
||||
def test_final_judge_supplies_score_and_debrief_for_automatic_buy(monkeypatch):
|
||||
sessions, sim, updated, debrief = _run(monkeypatch)
|
||||
|
||||
assert sim.judge_calls == 1
|
||||
assert updated["status"] == "finished"
|
||||
assert updated["outcome"] == "won"
|
||||
assert debrief["score"] == 88
|
||||
assert debrief["pain"] == "qualified pain"
|
||||
assert debrief["coaching"] == ["Keep the close concise"]
|
||||
assert debrief["revealed_persona"]["pains"]
|
||||
assert sessions.updates[0]["messages"]
|
||||
|
||||
|
||||
def test_final_judge_supplies_lost_outcome_for_automatic_walk(monkeypatch):
|
||||
sessions, sim, updated, debrief = _run(
|
||||
monkeypatch,
|
||||
verdict={
|
||||
"outcome": "lost",
|
||||
"score": 17,
|
||||
"pain": "unresolved",
|
||||
"why": "seller missed the need",
|
||||
"failurePoints": ["no discovery"],
|
||||
"coaching": ["Ask a deeper question"],
|
||||
"painProgress": {"unresolved": 20},
|
||||
},
|
||||
)
|
||||
|
||||
assert sim.judge_calls == 1
|
||||
assert updated["outcome"] == "lost"
|
||||
assert debrief["score"] == 17
|
||||
assert debrief["failurePoints"] == ["no discovery"]
|
||||
|
||||
|
||||
def test_manual_finish_uses_the_same_helper_and_score(monkeypatch):
|
||||
sessions, sim, updated, debrief = _run(
|
||||
monkeypatch,
|
||||
verdict={
|
||||
"outcome": "won",
|
||||
"score": 73,
|
||||
"pain": "fit",
|
||||
"why": "accepted",
|
||||
"failurePoints": [],
|
||||
"coaching": [],
|
||||
"painProgress": {"fit": 100},
|
||||
},
|
||||
)
|
||||
|
||||
assert sim.judge_calls == 1
|
||||
assert updated["outcome"] == "won"
|
||||
assert debrief["score"] == 73
|
||||
|
||||
|
||||
def test_judge_failure_persists_transcript_but_keeps_session_retryable(monkeypatch):
|
||||
session = _session()
|
||||
sessions = FakeSessions(session)
|
||||
sim = StubSim(error=True)
|
||||
monkeypatch.setattr(chat_routes, "_sim", lambda group, persona: sim)
|
||||
|
||||
with pytest.raises(LLMError):
|
||||
chat_routes._finalize_session(
|
||||
{"sessions": sessions},
|
||||
sessions.row,
|
||||
{"sales_kit": {}},
|
||||
{"name": "Customer", "pains": []},
|
||||
)
|
||||
|
||||
assert sim.judge_calls == 1
|
||||
assert sessions.row["status"] == "active"
|
||||
assert sessions.row["outcome"] is None
|
||||
assert sessions.updates[0]["messages"] == session["messages"]
|
||||
|
||||
|
||||
def test_finalization_is_idempotent_without_a_second_judge(monkeypatch):
|
||||
session = _session()
|
||||
session.update(
|
||||
status="finished",
|
||||
outcome="won",
|
||||
debrief={
|
||||
"outcome": "won",
|
||||
"score": 91,
|
||||
"pain": "fit",
|
||||
"why": "closed",
|
||||
"failurePoints": [],
|
||||
"coaching": [],
|
||||
"painProgress": {},
|
||||
},
|
||||
)
|
||||
sessions = FakeSessions(session)
|
||||
sim = StubSim()
|
||||
monkeypatch.setattr(chat_routes, "_sim", lambda group, persona: sim)
|
||||
|
||||
updated, debrief = chat_routes._finalize_session(
|
||||
{"sessions": sessions}, sessions.row, {}, {"name": "Customer"}
|
||||
)
|
||||
|
||||
assert sim.judge_calls == 0
|
||||
assert updated["status"] == "finished"
|
||||
assert debrief["score"] == 91
|
||||
@@ -0,0 +1,188 @@
|
||||
"""Regression tests for findings from the final adversarial review."""
|
||||
from __future__ import annotations
|
||||
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
from scripts.mock_llm import MockLLM
|
||||
|
||||
|
||||
def _headers(token: str) -> dict[str, str]:
|
||||
return {"Authorization": f"Bearer {token}"}
|
||||
|
||||
|
||||
def _setup_admin(user_store, login) -> str:
|
||||
user_store.complete_setup(
|
||||
"admin",
|
||||
"[email protected]",
|
||||
"admin-ready-password",
|
||||
accepted_terms=True,
|
||||
accepted_terms_at="2026-08-13T00:00:00Z",
|
||||
)
|
||||
return login("admin", "admin-ready-password")["token"]
|
||||
|
||||
|
||||
def test_trainee_cannot_read_draft_group_by_id(client, user_store, login):
|
||||
_setup_admin(user_store, login)
|
||||
group_store = client.application.extensions["group_store"]
|
||||
group = group_store.create(
|
||||
org_id="org-default", creator_id="admin", title="Draft group"
|
||||
)
|
||||
trainee = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="draft-reader",
|
||||
password="draft-reader-password",
|
||||
name="Draft Reader",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
token = login(trainee["username"], "draft-reader-password")["token"]
|
||||
|
||||
response = client.get(f"/api/groups/{group['id']}", headers=_headers(token))
|
||||
|
||||
assert response.status_code == 403, response.get_json()
|
||||
|
||||
|
||||
def test_analysis_publishes_ready_group_as_one_complete_record(
|
||||
client, user_store, login, monkeypatch
|
||||
):
|
||||
token = _setup_admin(user_store, login)
|
||||
client.application.extensions["llm"] = MockLLM()
|
||||
group_store = client.application.extensions["group_store"]
|
||||
group = group_store.create(
|
||||
org_id="org-default", creator_id="admin", title="Atomic analysis"
|
||||
)
|
||||
group_store.update(
|
||||
group["id"],
|
||||
input={
|
||||
"product": "CRM",
|
||||
"segment": "SME",
|
||||
"description": "sales tool",
|
||||
"channel": "facebook",
|
||||
"language": "th",
|
||||
},
|
||||
)
|
||||
observed_ready = []
|
||||
original_replace = group_store.groups.replace
|
||||
|
||||
def checked_replace(key, value):
|
||||
if value.get("status") == "ready":
|
||||
observed_ready.append(value)
|
||||
assert value.get("personas")
|
||||
assert value.get("report")
|
||||
assert value.get("sales_kit")
|
||||
return original_replace(key, value)
|
||||
|
||||
monkeypatch.setattr(group_store.groups, "replace", checked_replace)
|
||||
response = client.post(
|
||||
f"/api/groups/{group['id']}/analyze", headers=_headers(token)
|
||||
)
|
||||
|
||||
assert response.status_code == 200, response.get_json()
|
||||
assert len(observed_ready) == 1
|
||||
|
||||
|
||||
def test_group_delete_holds_each_session_record_lock(
|
||||
client, user_store, login, monkeypatch
|
||||
):
|
||||
token = _setup_admin(user_store, login)
|
||||
group_store = client.application.extensions["group_store"]
|
||||
group = group_store.create(
|
||||
org_id="org-default", creator_id="admin", title="Delete group"
|
||||
)
|
||||
group_id = group["id"]
|
||||
session_store = client.application.extensions["session_store"]
|
||||
session_store.sessions.create(
|
||||
{
|
||||
"id": "session-to-delete",
|
||||
"org_id": "org-default",
|
||||
"user_id": "admin",
|
||||
"group_id": group_id,
|
||||
"persona_id": "persona-1",
|
||||
"mode": "preview",
|
||||
"status": "active",
|
||||
},
|
||||
key="session-to-delete",
|
||||
)
|
||||
|
||||
from app.storage import store as storage_module
|
||||
|
||||
original_delete = session_store.sessions.delete
|
||||
|
||||
def checked_delete(key):
|
||||
path = str(session_store.sessions._path(key))
|
||||
assert path in storage_module._held_records.get()
|
||||
return original_delete(key)
|
||||
|
||||
monkeypatch.setattr(session_store.sessions, "delete", checked_delete)
|
||||
response = client.delete(f"/api/groups/{group_id}", headers=_headers(token))
|
||||
|
||||
assert response.status_code == 200, response.get_json()
|
||||
assert session_store.sessions.get_or_none("session-to-delete") is None
|
||||
|
||||
|
||||
def test_bootstrap_admin_is_process_safe_for_concurrent_first_start(
|
||||
monkeypatch, tmp_path
|
||||
):
|
||||
from app.auth.users import UserStore
|
||||
from app.config import Config
|
||||
from app.factory import bootstrap_admin
|
||||
|
||||
monkeypatch.setattr(Config, "DATA_DIR", tmp_path)
|
||||
monkeypatch.setattr(Config, "APP_ENV", "test")
|
||||
monkeypatch.setattr(Config, "SECRET_KEY", "pytest-secret-0123456789abcdef0123456789")
|
||||
monkeypatch.setattr(Config, "BOOTSTRAP_ADMIN_PASSWORD", "bootstrap-password")
|
||||
stores = [UserStore(tmp_path), UserStore(tmp_path)]
|
||||
|
||||
with ThreadPoolExecutor(max_workers=2) as pool:
|
||||
results = list(pool.map(bootstrap_admin, stores))
|
||||
|
||||
assert results == [None, None]
|
||||
assert len(UserStore(tmp_path).users.all()) == 1
|
||||
assert UserStore(tmp_path).get_user("admin")["role"] == "super_admin"
|
||||
|
||||
|
||||
def test_analytics_hardest_personas_prioritize_losses_then_low_score(
|
||||
client, user_store, login
|
||||
):
|
||||
token = _setup_admin(user_store, login)
|
||||
trainee = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="hardest-personas-trainee",
|
||||
password="hardest-personas-trainee-password",
|
||||
name="Hardest Personas Trainee",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
session_store = client.application.extensions["session_store"]
|
||||
analytics_group = client.application.extensions["group_store"].create(
|
||||
org_id="org-default", creator_id="admin", title="Analytics group"
|
||||
)
|
||||
rows = [
|
||||
("hard-1", "Hard persona", "lost", 20),
|
||||
("hard-2", "Hard persona", "lost", 30),
|
||||
("easy-1", "Easy persona", "won", 95),
|
||||
]
|
||||
for sid, persona_name, outcome, score in rows:
|
||||
session_store.sessions.create(
|
||||
{
|
||||
"id": sid,
|
||||
"org_id": "org-default",
|
||||
"user_id": trainee["id"],
|
||||
"group_id": analytics_group["id"],
|
||||
"persona_id": persona_name.lower().replace(" ", "-"),
|
||||
"persona_name": persona_name,
|
||||
"mode": "trainee",
|
||||
"status": "finished",
|
||||
"outcome": outcome,
|
||||
"debrief": {"score": score},
|
||||
},
|
||||
key=sid,
|
||||
)
|
||||
|
||||
response = client.get("/api/analytics", headers=_headers(token))
|
||||
|
||||
assert response.status_code == 200, response.get_json()
|
||||
assert [item["persona_name"] for item in response.get_json()["hardest_personas"][:2]] == [
|
||||
"Hard persona",
|
||||
"Easy persona",
|
||||
]
|
||||
@@ -0,0 +1,383 @@
|
||||
"""Regression tests for group response redaction."""
|
||||
from __future__ import annotations
|
||||
|
||||
from app.services.groups import GroupStore
|
||||
from scripts.mock_llm import MockLLM
|
||||
|
||||
|
||||
def _headers(token: str) -> dict[str, str]:
|
||||
return {"Authorization": f"Bearer {token}"}
|
||||
|
||||
|
||||
def _setup_admin(user_store, login):
|
||||
user_store.complete_setup(
|
||||
"admin",
|
||||
"[email protected]",
|
||||
"admin-ready-password",
|
||||
accepted_terms=True,
|
||||
accepted_terms_at="2026-08-13T00:00:00Z",
|
||||
)
|
||||
return login("admin", "admin-ready-password")["token"]
|
||||
|
||||
|
||||
def _ready_group(client, token):
|
||||
client.application.extensions["llm"] = MockLLM()
|
||||
headers = _headers(token)
|
||||
created = client.post(
|
||||
"/api/groups", json={"product": "CRM", "segment": "SME"}, headers=headers
|
||||
)
|
||||
assert created.status_code == 201, created.get_json()
|
||||
gid = created.get_json()["group"]["id"]
|
||||
analyzed = client.post(f"/api/groups/{gid}/analyze", headers=headers)
|
||||
assert analyzed.status_code == 200, analyzed.get_json()
|
||||
return gid, analyzed.get_json()
|
||||
|
||||
|
||||
def test_admin_analyze_get_and_list_use_same_redaction(client, user_store, login):
|
||||
_setup_admin(user_store, login)
|
||||
|
||||
admin = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="review-admin",
|
||||
password="review-admin-password",
|
||||
name="Review Admin",
|
||||
role="admin",
|
||||
email="[email protected]",
|
||||
must_setup=False,
|
||||
)
|
||||
admin_token = login(admin["username"], "review-admin-password")["token"]
|
||||
gid, analyzed = _ready_group(client, admin_token)
|
||||
headers = _headers(admin_token)
|
||||
|
||||
analyze_group = analyzed["group"]
|
||||
get_group = client.get(f"/api/groups/{gid}", headers=headers).get_json()["group"]
|
||||
listed = client.get(f"/api/groups/{gid}/personas", headers=headers).get_json()["personas"]
|
||||
|
||||
assert analyze_group.get("sales_kit") is None
|
||||
assert analyze_group.get("report") is None
|
||||
assert get_group.get("sales_kit") is None
|
||||
assert get_group.get("report") is None
|
||||
for persona in (analyze_group["personas"][0], get_group["personas"][0], listed[0]):
|
||||
assert "pains" not in persona
|
||||
assert "tolerance" not in persona
|
||||
assert "negotiation_levers" not in persona
|
||||
|
||||
|
||||
def test_admin_response_does_not_expose_future_secret_fields(
|
||||
client, user_store, login
|
||||
):
|
||||
super_token = _setup_admin(user_store, login)
|
||||
gid, analyzed = _ready_group(client, super_token)
|
||||
source_id = analyzed["personas"][0]["id"]
|
||||
stored = user_store # keep fixture dependency explicit for isolated data
|
||||
group_store = client.application.extensions["group_store"]
|
||||
group = group_store.get(gid)
|
||||
group["personas"][0]["future_hidden_signal"] = {"secret": "must-not-leak"}
|
||||
group_store.groups.replace(gid, group)
|
||||
|
||||
admin = stored.create_user(
|
||||
org_id="org-default",
|
||||
username="future-field-admin",
|
||||
password="future-field-admin-password",
|
||||
name="Future Field Admin",
|
||||
role="admin",
|
||||
email="[email protected]",
|
||||
must_setup=False,
|
||||
)
|
||||
token = login(admin["username"], "future-field-admin-password")["token"]
|
||||
|
||||
responses = [
|
||||
client.get(f"/api/groups/{gid}", headers=_headers(token)).get_json()["group"]["personas"][0],
|
||||
client.get(
|
||||
f"/api/groups/{gid}/personas", headers=_headers(token)
|
||||
).get_json()["personas"][0],
|
||||
client.get(
|
||||
f"/api/groups/{gid}/personas/{source_id}", headers=_headers(token)
|
||||
).get_json()["persona"],
|
||||
]
|
||||
|
||||
assert all("future_hidden_signal" not in persona for persona in responses)
|
||||
|
||||
|
||||
def test_tenant_admin_group_view_hides_uploaded_source_payload(
|
||||
client, user_store, login
|
||||
):
|
||||
_setup_admin(user_store, login)
|
||||
group_store = client.application.extensions["group_store"]
|
||||
group = group_store.create(
|
||||
org_id="org-default", creator_id="admin", title="Source protected group"
|
||||
)
|
||||
group_store.update(
|
||||
group["id"],
|
||||
input={
|
||||
"product": "CRM",
|
||||
"segment": "SME",
|
||||
"description": "safe summary",
|
||||
"channel": "social",
|
||||
"language": "th",
|
||||
"files": ["internal.txt"],
|
||||
"file_text": "provider-key=/private/path/source-secret",
|
||||
},
|
||||
)
|
||||
admin = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="source-protected-admin",
|
||||
password="source-protected-password",
|
||||
name="Source Protected Admin",
|
||||
role="admin",
|
||||
must_setup=False,
|
||||
)
|
||||
token = login(admin["username"], "source-protected-password")["token"]
|
||||
|
||||
response = client.get(
|
||||
f"/api/groups/{group['id']}", headers=_headers(token)
|
||||
)
|
||||
|
||||
assert response.status_code == 200, response.get_json()
|
||||
source = response.get_json()["group"]["input"]
|
||||
assert source["product"] == "CRM"
|
||||
assert "files" not in source
|
||||
assert "file_text" not in source
|
||||
assert "source-secret" not in response.get_data(as_text=True)
|
||||
|
||||
|
||||
def test_super_admin_receives_full_group_view(client, user_store, login):
|
||||
super_token = _setup_admin(user_store, login)
|
||||
gid, analyzed = _ready_group(client, super_token)
|
||||
|
||||
assert analyzed["group"]["personas"][0]["pains"]
|
||||
assert client.get(
|
||||
f"/api/groups/{gid}/personas", headers=_headers(super_token)
|
||||
).get_json()["personas"][0]["tolerance"]
|
||||
|
||||
|
||||
def test_trainee_receives_revealable_persona_only(client, user_store, login):
|
||||
super_token = _setup_admin(user_store, login)
|
||||
gid, _ = _ready_group(client, super_token)
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="trainee-redaction",
|
||||
password="trainee-redaction-password",
|
||||
name="Trainee",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
trainee_token = login(user["username"], "trainee-redaction-password")["token"]
|
||||
|
||||
persona = client.get(
|
||||
f"/api/groups/{gid}/personas", headers=_headers(trainee_token)
|
||||
).get_json()["personas"][0]
|
||||
|
||||
assert "name" in persona
|
||||
assert "profession" in persona
|
||||
assert "pains" not in persona
|
||||
assert "income" not in persona
|
||||
assert "personality" not in persona
|
||||
assert "tolerance" not in persona
|
||||
|
||||
|
||||
def test_trainee_group_view_hides_uploaded_source_data(
|
||||
client, user_store, login
|
||||
):
|
||||
super_token = _setup_admin(user_store, login)
|
||||
gid, _ = _ready_group(client, super_token)
|
||||
group_store = client.application.extensions["group_store"]
|
||||
group = group_store.get(gid)
|
||||
group_store.update(
|
||||
gid,
|
||||
input={
|
||||
"product": "CRM",
|
||||
"segment": "SME",
|
||||
"description": "safe product summary",
|
||||
"files": ["internal-brief.txt"],
|
||||
"file_text": "CONFIDENT INTERNAL SOURCE CONTENT",
|
||||
},
|
||||
)
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="trainee-source-redaction",
|
||||
password="trainee-source-redaction-password",
|
||||
name="Trainee",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
trainee_token = login(user["username"], "trainee-source-redaction-password")["token"]
|
||||
|
||||
response = client.get(
|
||||
f"/api/groups/{gid}", headers=_headers(trainee_token)
|
||||
)
|
||||
|
||||
assert response.status_code == 200, response.get_json()
|
||||
group_view = response.get_json()["group"]
|
||||
assert group_view["input"]["product"] == "CRM"
|
||||
assert "files" not in group_view["input"]
|
||||
assert "file_text" not in group_view["input"]
|
||||
|
||||
|
||||
def test_user_group_index_is_owner_scoped_and_revealable_only(tmp_path):
|
||||
store = GroupStore(tmp_path)
|
||||
private = store.create(
|
||||
org_id="org-default", creator_id="owner-1", title="Private group"
|
||||
)
|
||||
store.update(
|
||||
private["id"],
|
||||
status="ready",
|
||||
owner_user_id="owner-1",
|
||||
input={"product": "CRM", "file_text": "private source"},
|
||||
sales_kit={"productName": "CRM", "secret": "must-not-leak"},
|
||||
report={"internal": "must-not-leak"},
|
||||
personas=[
|
||||
{
|
||||
"id": "private-persona",
|
||||
"name": "Private Persona",
|
||||
"tier": "B",
|
||||
"pains": [{"description": "hidden pain"}],
|
||||
"future_hidden_signal": "must-not-leak",
|
||||
}
|
||||
],
|
||||
)
|
||||
shared = store.create(
|
||||
org_id="org-default", creator_id="admin-1", title="Shared group"
|
||||
)
|
||||
store.update(
|
||||
shared["id"],
|
||||
status="ready",
|
||||
input={"product": "Shared CRM"},
|
||||
personas=[{"id": "shared-persona", "name": "Shared Persona", "tier": "A"}],
|
||||
)
|
||||
|
||||
other_view = store.list_visible_to(
|
||||
role="user", org_id="org-default", user_id="other-user"
|
||||
)
|
||||
assert {group["id"] for group in other_view} == {shared["id"]}
|
||||
|
||||
owner_view = store.list_visible_to(
|
||||
role="user", org_id="org-default", user_id="owner-1"
|
||||
)
|
||||
private_view = next(group for group in owner_view if group["id"] == private["id"])
|
||||
assert private_view["sales_kit"] is None
|
||||
assert private_view["report"] is None
|
||||
assert private_view["input"] == {"product": "CRM"}
|
||||
assert private_view["personas"] == [
|
||||
{
|
||||
"id": "private-persona",
|
||||
"name": "Private Persona",
|
||||
"tier": "B",
|
||||
"initiation_mode": "customer",
|
||||
"channel": "facebook",
|
||||
"profession": "",
|
||||
"age_group": "",
|
||||
"location": "",
|
||||
"product_context": "",
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
def test_trainee_cannot_analyze(client, user_store, login):
|
||||
super_token = _setup_admin(user_store, login)
|
||||
gid, _ = _ready_group(client, super_token)
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="trainee-no-analyze",
|
||||
password="trainee-no-analyze-password",
|
||||
name="Trainee",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
token = login(user["username"], "trainee-no-analyze-password")["token"]
|
||||
|
||||
response = client.post(f"/api/groups/{gid}/analyze", headers=_headers(token))
|
||||
|
||||
assert response.status_code == 403, response.get_json()
|
||||
|
||||
|
||||
def test_trainee_variant_response_is_revealable_only(client, user_store, login):
|
||||
super_token = _setup_admin(user_store, login)
|
||||
gid, analyzed = _ready_group(client, super_token)
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="trainee-variant",
|
||||
password="trainee-variant-password",
|
||||
name="Trainee",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
trainee_token = login(user["username"], "trainee-variant-password")["token"]
|
||||
source_id = analyzed["personas"][0]["id"]
|
||||
|
||||
response = client.post(
|
||||
f"/api/groups/{gid}/personas/{source_id}/variant",
|
||||
headers=_headers(trainee_token),
|
||||
)
|
||||
|
||||
assert response.status_code == 201, response.get_json()
|
||||
persona = response.get_json()["persona"]
|
||||
assert "name" in persona
|
||||
assert "pains" not in persona
|
||||
assert "income" not in persona
|
||||
assert "tolerance" not in persona
|
||||
|
||||
|
||||
def test_trainee_variant_is_stored_in_owner_private_group(
|
||||
client, user_store, login
|
||||
):
|
||||
super_token = _setup_admin(user_store, login)
|
||||
gid, analyzed = _ready_group(client, super_token)
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="private-variant-owner",
|
||||
password="private-variant-owner-password",
|
||||
name="Private Variant Owner",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
trainee_token = login(user["username"], "private-variant-owner-password")["token"]
|
||||
headers = _headers(trainee_token)
|
||||
source_id = analyzed["personas"][0]["id"]
|
||||
|
||||
response = client.post(
|
||||
f"/api/groups/{gid}/personas/{source_id}/variant",
|
||||
headers=headers,
|
||||
)
|
||||
|
||||
assert response.status_code == 201, response.get_json()
|
||||
payload = response.get_json()
|
||||
variant = payload["persona"]
|
||||
private_gid = payload["group_id"]
|
||||
assert private_gid != gid
|
||||
source_group = client.application.extensions["group_store"].get(gid)
|
||||
private_group = client.application.extensions["group_store"].get(private_gid)
|
||||
assert private_group["sales_kit"] == source_group["sales_kit"]
|
||||
|
||||
shared_personas = client.get(
|
||||
f"/api/groups/{gid}/personas", headers=headers
|
||||
).get_json()["personas"]
|
||||
assert variant["id"] not in {p["id"] for p in shared_personas}
|
||||
|
||||
private_response = client.get(
|
||||
f"/api/groups/{private_gid}/personas", headers=headers
|
||||
)
|
||||
assert private_response.status_code == 200, private_response.get_json()
|
||||
private_personas = private_response.get_json()["personas"]
|
||||
assert [p["id"] for p in private_personas] == [variant["id"]]
|
||||
|
||||
other_user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="private-variant-other-user",
|
||||
password="private-variant-other-password",
|
||||
name="Other User",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
other_token = login(
|
||||
other_user["username"], "private-variant-other-password"
|
||||
)["token"]
|
||||
other_headers = _headers(other_token)
|
||||
assert client.get(
|
||||
f"/api/groups/{private_gid}/personas", headers=other_headers
|
||||
).status_code == 403
|
||||
listed_ids = {
|
||||
group["id"]
|
||||
for group in client.get("/api/groups", headers=other_headers).get_json()["groups"]
|
||||
}
|
||||
assert private_gid not in listed_ids
|
||||
@@ -0,0 +1,83 @@
|
||||
"""Human-readable redacted report contract tests."""
|
||||
from __future__ import annotations
|
||||
|
||||
from scripts.mock_llm import MockLLM
|
||||
|
||||
|
||||
def _headers(token: str) -> dict[str, str]:
|
||||
return {"Authorization": f"Bearer {token}"}
|
||||
|
||||
|
||||
def _setup(user_store, login):
|
||||
user_store.complete_setup(
|
||||
"admin",
|
||||
"[email protected]",
|
||||
"admin-ready-password",
|
||||
accepted_terms=True,
|
||||
accepted_terms_at="2026-08-13T00:00:00Z",
|
||||
)
|
||||
return login("admin", "admin-ready-password")["token"]
|
||||
|
||||
|
||||
def _ready(client, token):
|
||||
client.application.extensions["llm"] = MockLLM()
|
||||
created = client.post("/api/groups", json={"product": "CRM"}, headers=_headers(token))
|
||||
gid = created.get_json()["group"]["id"]
|
||||
analyzed = client.post(f"/api/groups/{gid}/analyze", headers=_headers(token))
|
||||
assert analyzed.status_code == 200
|
||||
return gid
|
||||
|
||||
|
||||
def test_admin_report_is_redacted_and_downloadable(client, user_store, login):
|
||||
super_token = _setup(user_store, login)
|
||||
gid = _ready(client, super_token)
|
||||
admin = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="report-admin",
|
||||
password="report-admin-password",
|
||||
name="Report Admin",
|
||||
role="admin",
|
||||
must_setup=False,
|
||||
)
|
||||
token = login(admin["username"], "report-admin-password")["token"]
|
||||
headers = _headers(token)
|
||||
|
||||
json_response = client.get(f"/api/groups/{gid}/report?format=json", headers=headers)
|
||||
assert json_response.status_code == 200, json_response.get_json()
|
||||
report = json_response.get_json()
|
||||
assert report["title"]
|
||||
assert "CRM" in report["markdown"]
|
||||
for forbidden in ("raw_personas", "negotiation_levers", "failurePoints", "private", "rootCause"):
|
||||
assert forbidden not in report["markdown"]
|
||||
|
||||
download = client.get(f"/api/groups/{gid}/report", headers=headers)
|
||||
assert download.status_code == 200
|
||||
assert download.mimetype == "text/markdown"
|
||||
assert "attachment" in download.headers["Content-Disposition"]
|
||||
|
||||
|
||||
def test_report_is_tenant_and_role_guarded(client, user_store, login):
|
||||
super_token = _setup(user_store, login)
|
||||
gid = _ready(client, super_token)
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="report-user",
|
||||
password="report-user-password",
|
||||
name="Report User",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
user_token = login(user["username"], "report-user-password")["token"]
|
||||
assert client.get(f"/api/groups/{gid}/report", headers=_headers(user_token)).status_code == 403
|
||||
|
||||
other_org = user_store.create_org("Other")
|
||||
other_admin = user_store.create_user(
|
||||
org_id=other_org["id"],
|
||||
username="other-report-admin",
|
||||
password="other-report-password",
|
||||
name="Other Report Admin",
|
||||
role="admin",
|
||||
must_setup=False,
|
||||
)
|
||||
other_token = login(other_admin["username"], "other-report-password")["token"]
|
||||
assert client.get(f"/api/groups/{gid}/report", headers=_headers(other_token)).status_code == 403
|
||||
@@ -0,0 +1,114 @@
|
||||
"""Contract tests for the S4.3 group/persona repository packet."""
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.db import Base, create_db_engine
|
||||
from app.repositories import (
|
||||
EntityNotFoundError,
|
||||
InvalidRepositoryField,
|
||||
SqlAlchemyGroupRepository,
|
||||
SqlAlchemyOrganizationRepository,
|
||||
SqlAlchemyUserRepository,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def repository_session(tmp_path: Path):
|
||||
engine = create_db_engine(f"sqlite:///{tmp_path / 'groups.db'}")
|
||||
Base.metadata.create_all(engine)
|
||||
with Session(engine) as session:
|
||||
yield session
|
||||
|
||||
|
||||
def test_group_repository_scopes_groups_and_personas_to_org(repository_session: Session):
|
||||
organizations = SqlAlchemyOrganizationRepository(repository_session)
|
||||
users = SqlAlchemyUserRepository(repository_session)
|
||||
groups = SqlAlchemyGroupRepository(repository_session)
|
||||
organizations.create(organization_id="org-a", name="Alpha")
|
||||
organizations.create(organization_id="org-b", name="Beta")
|
||||
users.create(
|
||||
user_id="user-a",
|
||||
org_id="org-a",
|
||||
username="alice",
|
||||
password_hash="hash-a",
|
||||
name="Alice",
|
||||
role="user",
|
||||
)
|
||||
repository_session.flush()
|
||||
|
||||
group_a = groups.create(
|
||||
group_id="group-a",
|
||||
org_id="org-a",
|
||||
name="Alpha Group",
|
||||
creator_user_id="user-a",
|
||||
input_json={"product": "A"},
|
||||
)
|
||||
groups.create(group_id="group-b", org_id="org-b", name="Beta Group")
|
||||
repository_session.flush()
|
||||
|
||||
persona_a = groups.create_persona(
|
||||
persona_id="persona-a",
|
||||
group_id="group-a",
|
||||
tier="base",
|
||||
public_json={"name": "Customer A"},
|
||||
latent_json={"objection": "price"},
|
||||
org_id="org-a",
|
||||
)
|
||||
repository_session.commit()
|
||||
|
||||
assert groups.get("group-a", org_id="org-a") is group_a
|
||||
assert groups.get("group-b", org_id="org-a") is None
|
||||
assert [item.id for item in groups.list_for_org("org-a")] == ["group-a"]
|
||||
assert groups.get_persona("group-a", "persona-a", org_id="org-a") is persona_a
|
||||
assert [item.id for item in groups.list_personas("group-a", org_id="org-a")] == ["persona-a"]
|
||||
assert groups.get_persona("group-a", "persona-a", org_id="org-b") is None
|
||||
|
||||
|
||||
def test_group_repository_updates_only_allowed_fields(repository_session: Session):
|
||||
organizations = SqlAlchemyOrganizationRepository(repository_session)
|
||||
groups = SqlAlchemyGroupRepository(repository_session)
|
||||
organizations.create(organization_id="org-a", name="Alpha")
|
||||
groups.create(group_id="group-a", org_id="org-a", name="Before")
|
||||
repository_session.commit()
|
||||
|
||||
updated = groups.update(
|
||||
"group-a",
|
||||
org_id="org-a",
|
||||
name="After",
|
||||
status="ready",
|
||||
report_text="Report",
|
||||
)
|
||||
repository_session.commit()
|
||||
assert updated.name == "After"
|
||||
assert updated.status == "ready"
|
||||
assert updated.report_text == "Report"
|
||||
|
||||
with pytest.raises(EntityNotFoundError):
|
||||
groups.update("group-a", org_id="org-b", name="cross-tenant mutation")
|
||||
|
||||
with pytest.raises(EntityNotFoundError):
|
||||
groups.update("missing", org_id="org-a", name="nope")
|
||||
with pytest.raises(InvalidRepositoryField):
|
||||
groups.update("group-a", org_id="org-a", creator_user_id="cannot-change")
|
||||
|
||||
|
||||
def test_group_repository_rejects_cross_org_persona_creation(repository_session: Session):
|
||||
organizations = SqlAlchemyOrganizationRepository(repository_session)
|
||||
groups = SqlAlchemyGroupRepository(repository_session)
|
||||
organizations.create(organization_id="org-a", name="Alpha")
|
||||
organizations.create(organization_id="org-b", name="Beta")
|
||||
groups.create(group_id="group-a", org_id="org-a", name="Alpha Group")
|
||||
repository_session.commit()
|
||||
|
||||
with pytest.raises(EntityNotFoundError):
|
||||
groups.create_persona(
|
||||
persona_id="persona-invalid",
|
||||
group_id="group-a",
|
||||
tier="base",
|
||||
public_json={},
|
||||
org_id="org-b",
|
||||
)
|
||||
@@ -0,0 +1,250 @@
|
||||
"""Tests for the fail-closed JSON-to-relational importer."""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.db import Base, create_db_engine
|
||||
from app.models import Group, Message, Organization, Persona, TrainingSession, User
|
||||
from scripts.migrate_json_to_postgres import ImportValidationError, run_import
|
||||
|
||||
|
||||
def _write_json(root: Path, collection: str, key: str, value: dict) -> None:
|
||||
path = root / collection / f"{key}.json"
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_text(json.dumps(value, ensure_ascii=False), encoding="utf-8")
|
||||
|
||||
|
||||
def _seed_source(root: Path) -> None:
|
||||
_write_json(
|
||||
root,
|
||||
"orgs",
|
||||
"org-a",
|
||||
{
|
||||
"id": "org-a",
|
||||
"name": "Alpha",
|
||||
"plan": "trial",
|
||||
"seats": 5,
|
||||
"active": True,
|
||||
"created_at": "2026-08-15T00:00:00Z",
|
||||
},
|
||||
)
|
||||
_write_json(
|
||||
root,
|
||||
"users",
|
||||
"user-a",
|
||||
{
|
||||
"id": "user-a",
|
||||
"org_id": "org-a",
|
||||
"username": "alice",
|
||||
"email": "[email protected]",
|
||||
"name": "Alice",
|
||||
"password_hash": "pbkdf2:sha256:source-hash",
|
||||
"role": "user",
|
||||
"active": True,
|
||||
"must_setup": False,
|
||||
"accepted_terms": True,
|
||||
"accepted_terms_at": "2026-08-15T00:00:00Z",
|
||||
"auth_version": 2,
|
||||
"created_at": "2026-08-15T00:00:00Z",
|
||||
},
|
||||
)
|
||||
_write_json(
|
||||
root,
|
||||
"groups",
|
||||
"group-a",
|
||||
{
|
||||
"id": "group-a",
|
||||
"org_id": "org-a",
|
||||
"creator_id": "user-a",
|
||||
"title": "Alpha Group",
|
||||
"status": "ready",
|
||||
"input": {"product": "A"},
|
||||
"sales_kit": {"productName": "Product A"},
|
||||
"report": {"summary": "Report A"},
|
||||
"personas": [
|
||||
{
|
||||
"id": "persona-a",
|
||||
"name": "Customer A",
|
||||
"tier": "A",
|
||||
"channel": "facebook",
|
||||
"initiation_mode": "customer",
|
||||
"profession": "Owner",
|
||||
"pains": [{"name": "time"}],
|
||||
}
|
||||
],
|
||||
"created_at": "2026-08-15T00:00:00Z",
|
||||
"updated_at": "2026-08-15T00:00:01Z",
|
||||
},
|
||||
)
|
||||
_write_json(
|
||||
root,
|
||||
"sessions",
|
||||
"session-a",
|
||||
{
|
||||
"id": "session-a",
|
||||
"org_id": "org-a",
|
||||
"user_id": "user-a",
|
||||
"group_id": "group-a",
|
||||
"persona_id": "persona-a",
|
||||
"persona_name": "Customer A",
|
||||
"persona_meta": {"tier": "A"},
|
||||
"scenario": "social",
|
||||
"locale": "th",
|
||||
"mode": "trainee",
|
||||
"status": "finished",
|
||||
"outcome": "won",
|
||||
"messages": [
|
||||
{"role": "customer", "text": "Hello", "ts": "2026-08-15T00:00:02Z"},
|
||||
{"role": "trainee", "text": "Hi", "ts": "2026-08-15T00:00:03Z"},
|
||||
],
|
||||
"internal": {"trust": 80},
|
||||
"debrief": {"score": 90},
|
||||
"created_at": "2026-08-15T00:00:00Z",
|
||||
"updated_at": "2026-08-15T00:00:04Z",
|
||||
},
|
||||
)
|
||||
_write_json(
|
||||
root,
|
||||
"my_personas",
|
||||
"user-a__persona-private",
|
||||
{
|
||||
"key": "user-a__persona-private",
|
||||
"user_id": "user-a",
|
||||
"persona": {
|
||||
"id": "persona-private",
|
||||
"name": "Private Customer",
|
||||
"tier": "B",
|
||||
"channel": "line",
|
||||
"initiation_mode": "customer",
|
||||
},
|
||||
"created_at": "2026-08-15T00:00:05Z",
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def _prepare_target(path: Path):
|
||||
engine = create_db_engine(f"sqlite:///{path}")
|
||||
Base.metadata.create_all(engine)
|
||||
return engine
|
||||
|
||||
|
||||
def test_dry_run_validates_without_writing_source_or_target(tmp_path: Path):
|
||||
source = tmp_path / "source"
|
||||
_seed_source(source)
|
||||
target = tmp_path / "target.db"
|
||||
_prepare_target(target)
|
||||
before = sorted(path.relative_to(source).as_posix() for path in source.rglob("*.json"))
|
||||
|
||||
report = run_import(source, f"sqlite:///{target}")
|
||||
|
||||
assert report["mode"] == "dry_run"
|
||||
assert report["source_checksum"]
|
||||
assert report["counts"]["organizations"]["would_create"] == 1
|
||||
assert report["counts"]["users"]["would_create"] == 1
|
||||
assert report["counts"]["groups"]["would_create"] == 2
|
||||
assert report["counts"]["personas"]["would_create"] == 2
|
||||
assert report["counts"]["sessions"]["would_create"] == 1
|
||||
assert report["counts"]["messages"]["would_create"] == 2
|
||||
assert sorted(path.relative_to(source).as_posix() for path in source.rglob("*.json")) == before
|
||||
|
||||
with Session(create_db_engine(f"sqlite:///{target}")) as db:
|
||||
assert db.query(Organization).count() == 0
|
||||
|
||||
|
||||
def test_apply_is_duplicate_safe_and_redacts_payloads(tmp_path: Path):
|
||||
source = tmp_path / "source"
|
||||
_seed_source(source)
|
||||
target = tmp_path / "target.db"
|
||||
_prepare_target(target)
|
||||
|
||||
first = run_import(
|
||||
source,
|
||||
f"sqlite:///{target}",
|
||||
apply=True,
|
||||
backup_dir=tmp_path / "backup-1",
|
||||
)
|
||||
second = run_import(
|
||||
source,
|
||||
f"sqlite:///{target}",
|
||||
apply=True,
|
||||
backup_dir=tmp_path / "backup-2",
|
||||
)
|
||||
|
||||
assert first["counts"]["users"]["created"] == 1
|
||||
assert second["counts"]["users"]["created"] == 0
|
||||
assert second["counts"]["users"]["unchanged"] == 1
|
||||
assert "source-hash" not in json.dumps(first)
|
||||
assert (tmp_path / "backup-1" / "users" / "user-a.json").exists()
|
||||
|
||||
with Session(create_db_engine(f"sqlite:///{target}")) as db:
|
||||
assert db.query(Organization).count() == 1
|
||||
assert db.query(User).count() == 1
|
||||
assert db.query(Group).count() == 2
|
||||
assert db.query(Persona).count() == 2
|
||||
assert db.query(TrainingSession).count() == 1
|
||||
assert db.query(Message).count() == 2
|
||||
|
||||
|
||||
def test_cross_tenant_reference_fails_closed_before_apply(tmp_path: Path):
|
||||
source = tmp_path / "source"
|
||||
_seed_source(source)
|
||||
group_path = source / "groups" / "group-a.json"
|
||||
group = json.loads(group_path.read_text(encoding="utf-8"))
|
||||
group["org_id"] = "org-missing"
|
||||
group_path.write_text(json.dumps(group), encoding="utf-8")
|
||||
target = tmp_path / "target.db"
|
||||
_prepare_target(target)
|
||||
|
||||
with pytest.raises(ImportValidationError, match="cross-tenant"):
|
||||
run_import(source, f"sqlite:///{target}")
|
||||
|
||||
with Session(create_db_engine(f"sqlite:///{target}")) as db:
|
||||
assert db.query(Organization).count() == 0
|
||||
|
||||
|
||||
def test_target_conflict_aborts_without_overwrite(tmp_path: Path):
|
||||
source = tmp_path / "source"
|
||||
_seed_source(source)
|
||||
target = tmp_path / "target.db"
|
||||
_prepare_target(target)
|
||||
run_import(source, f"sqlite:///{target}", apply=True, backup_dir=tmp_path / "backup-1")
|
||||
|
||||
group_path = source / "groups" / "group-a.json"
|
||||
group = json.loads(group_path.read_text(encoding="utf-8"))
|
||||
group["title"] = "Changed after import"
|
||||
group_path.write_text(json.dumps(group), encoding="utf-8")
|
||||
|
||||
with pytest.raises(ImportValidationError, match="target conflict"):
|
||||
run_import(source, f"sqlite:///{target}", apply=True, backup_dir=tmp_path / "backup-2")
|
||||
|
||||
with Session(create_db_engine(f"sqlite:///{target}")) as db:
|
||||
imported = db.get(Group, "group-a")
|
||||
assert imported is not None
|
||||
assert imported.name == "Alpha Group"
|
||||
|
||||
|
||||
def test_nonempty_audit_jsonl_is_not_silently_dropped(tmp_path: Path):
|
||||
source = tmp_path / "source"
|
||||
_seed_source(source)
|
||||
audit = source / "audit" / "audit.jsonl"
|
||||
audit.parent.mkdir(parents=True, exist_ok=True)
|
||||
audit.write_text('{"action":"login"}\n', encoding="utf-8")
|
||||
|
||||
with pytest.raises(ImportValidationError, match="audit"):
|
||||
run_import(source, None)
|
||||
|
||||
|
||||
def test_malformed_scalar_fields_fail_closed(tmp_path: Path):
|
||||
source = tmp_path / "source"
|
||||
_seed_source(source)
|
||||
user_path = source / "users" / "user-a.json"
|
||||
user = json.loads(user_path.read_text(encoding="utf-8"))
|
||||
user["active"] = "yes"
|
||||
user_path.write_text(json.dumps(user), encoding="utf-8")
|
||||
|
||||
with pytest.raises(ImportValidationError, match="active"):
|
||||
run_import(source, None)
|
||||
@@ -0,0 +1,991 @@
|
||||
"""Regression tests for the exact-current legacy security review blockers."""
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import io
|
||||
import json
|
||||
import math
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
import jwt
|
||||
import pytest
|
||||
from flask import g
|
||||
|
||||
from app.api.analytics_routes import _csv_cell
|
||||
from app.api.helpers import ApiError, current_org_id, require_roles
|
||||
from app.auth.users import AuthError, is_valid_tenant_id, normalize_identifier
|
||||
from app.config import Config
|
||||
from app.services.groups import GroupStore
|
||||
from app.services.file_parser import ParseError, parse_pdf
|
||||
from app.services import rate_limit
|
||||
from app.services.trainee import analyze_weak_areas
|
||||
from app.storage.store import JsonStore, StoreError
|
||||
|
||||
|
||||
def _headers(token: str) -> dict[str, str]:
|
||||
return {"Authorization": f"Bearer {token}"}
|
||||
|
||||
|
||||
def _ready_persona() -> dict:
|
||||
return {
|
||||
"id": "p-1",
|
||||
"name": "Persona One",
|
||||
"tier": "B",
|
||||
"profession": "Engineer",
|
||||
"pains": [],
|
||||
"objections": [],
|
||||
"negotiation_levers": [],
|
||||
"opener": "hello",
|
||||
"rootCause": "",
|
||||
"resolutionConditions": [],
|
||||
"tolerance": 1,
|
||||
}
|
||||
|
||||
|
||||
def test_group_indexes_hide_private_groups_from_tenant_admins_and_fail_closed(tmp_path: Path):
|
||||
store = GroupStore(tmp_path)
|
||||
shared = store.create(org_id="org-1", creator_id="admin-1", title="Shared")
|
||||
private = store.create(org_id="org-1", creator_id="user-1", title="Private")
|
||||
store.update(shared["id"], status="ready")
|
||||
store.update(private["id"], status="ready", owner_user_id="user-1")
|
||||
|
||||
visible = store.list_visible_to(role="admin", org_id="org-1", user_id="admin-1")
|
||||
assert [group["id"] for group in visible] == [shared["id"]]
|
||||
assert store.list_visible_to(role="admin", org_id="") == []
|
||||
assert store.list_for_org("") == []
|
||||
|
||||
|
||||
def test_group_owner_marker_is_fail_closed_for_falsey_malformed_values(tmp_path: Path):
|
||||
store = GroupStore(tmp_path)
|
||||
group = store.create(org_id="org-1", creator_id="user-1", title="Malformed")
|
||||
store.update(group["id"], status="ready", owner_user_id=0)
|
||||
|
||||
assert store.list_visible_to(role="user", org_id="org-1", user_id="user-2") == []
|
||||
|
||||
|
||||
def test_id_addressed_private_group_with_null_owner_is_not_public(client, user_store, login):
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="null-owner-reader",
|
||||
password="null-owner-reader-password",
|
||||
name="Null Owner Reader",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
token = login(user["username"], "null-owner-reader-password")["token"]
|
||||
groups = client.application.extensions["group_store"]
|
||||
group = groups.create(org_id="org-default", creator_id="admin", title="Legacy private")
|
||||
groups.update(group["id"], status="ready", owner_user_id=None)
|
||||
|
||||
response = client.get(f"/api/groups/{group['id']}", headers=_headers(token))
|
||||
|
||||
assert response.status_code == 403
|
||||
|
||||
|
||||
def test_private_group_reuse_rejects_stale_owner_record(tmp_path: Path):
|
||||
store = GroupStore(tmp_path)
|
||||
group = store.create(org_id="org-1", creator_id="user-1", title="Stale")
|
||||
store.update(group["id"], owner_user_id="user-1", status="failed")
|
||||
|
||||
with pytest.raises(ValueError, match="private group is not ready"):
|
||||
store.get_or_create_private_group(org_id="org-1", owner_user_id="user-1")
|
||||
|
||||
|
||||
def test_tenant_admin_persona_update_rejects_unknown_fields(client, user_store, login):
|
||||
admin = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="legacy-tenant-admin",
|
||||
password="legacy-tenant-admin-password",
|
||||
name="Tenant Admin",
|
||||
role="admin",
|
||||
email="[email protected]",
|
||||
must_setup=False,
|
||||
)
|
||||
token = login(admin["username"], "legacy-tenant-admin-password")["token"]
|
||||
groups = client.application.extensions["group_store"]
|
||||
group = groups.create(org_id="org-default", creator_id=admin["id"], title="Editable")
|
||||
groups.update(group["id"], status="ready")
|
||||
groups.set_personas(group["id"], [_ready_persona()])
|
||||
|
||||
response = client.put(
|
||||
f"/api/groups/{group['id']}/personas/p-1",
|
||||
json={"future_internal_field": "must not persist"},
|
||||
headers=_headers(token),
|
||||
)
|
||||
|
||||
assert response.status_code == 403
|
||||
assert "future_internal_field" not in groups.get_persona(group["id"], "p-1")
|
||||
|
||||
|
||||
def test_group_index_handles_malformed_summary_fields(client, user_store, login):
|
||||
user_store.complete_setup(
|
||||
"admin",
|
||||
"[email protected]",
|
||||
"legacy-summary-admin-password",
|
||||
accepted_terms=True,
|
||||
accepted_terms_at="2026-08-15T00:00:00Z",
|
||||
)
|
||||
token = login("admin", "legacy-summary-admin-password")["token"]
|
||||
groups = client.application.extensions["group_store"]
|
||||
group = groups.create(org_id="org-default", creator_id="admin", title="Malformed summary")
|
||||
groups.update(group["id"], input="not-a-mapping", personas="not-a-list")
|
||||
|
||||
response = client.get("/api/groups", headers=_headers(token))
|
||||
|
||||
assert response.status_code == 200
|
||||
summary = next(row for row in response.get_json()["groups"] if row["id"] == group["id"])
|
||||
assert summary["persona_count"] == 0
|
||||
assert summary["input"]["product"] == ""
|
||||
|
||||
|
||||
def test_persona_index_handles_malformed_collection(client, user_store, login):
|
||||
user_store.complete_setup(
|
||||
"admin",
|
||||
"[email protected]",
|
||||
"legacy-persona-list-admin-password",
|
||||
accepted_terms=True,
|
||||
accepted_terms_at="2026-08-15T00:00:00Z",
|
||||
)
|
||||
token = login("admin", "legacy-persona-list-admin-password")["token"]
|
||||
groups = client.application.extensions["group_store"]
|
||||
group = groups.create(org_id="org-default", creator_id="admin", title="Malformed personas")
|
||||
groups.update(group["id"], status="ready", personas="not-a-list")
|
||||
|
||||
response = client.get(f"/api/groups/{group['id']}/personas", headers=_headers(token))
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.get_json()["personas"] == []
|
||||
|
||||
|
||||
def test_board_skips_personas_without_ids(client, user_store, login):
|
||||
user_store.complete_setup(
|
||||
"admin",
|
||||
"[email protected]",
|
||||
"legacy-board-persona-admin-password",
|
||||
accepted_terms=True,
|
||||
accepted_terms_at="2026-08-15T00:00:00Z",
|
||||
)
|
||||
token = login("admin", "legacy-board-persona-admin-password")["token"]
|
||||
groups = client.application.extensions["group_store"]
|
||||
group = groups.create(org_id="org-default", creator_id="admin", title="Malformed board personas")
|
||||
groups.update(group["id"], status="ready", personas=["not-a-dict", _ready_persona()])
|
||||
|
||||
response = client.get("/api/me/board", headers=_headers(token))
|
||||
|
||||
assert response.status_code == 200
|
||||
assert [item["persona_id"] for item in response.get_json()["board"]] == ["p-1"]
|
||||
|
||||
|
||||
def test_analysis_report_failure_marks_group_failed(client, user_store, login, monkeypatch):
|
||||
user_store.complete_setup(
|
||||
"admin",
|
||||
"[email protected]",
|
||||
"legacy-report-admin-password",
|
||||
accepted_terms=True,
|
||||
accepted_terms_at="2026-08-15T00:00:00Z",
|
||||
)
|
||||
token = login("admin", "legacy-report-admin-password")["token"]
|
||||
client.application.extensions["llm"] = __import__("scripts.mock_llm", fromlist=["MockLLM"]).MockLLM()
|
||||
groups = client.application.extensions["group_store"]
|
||||
group = groups.create(org_id="org-default", creator_id="admin", title="Report failure")
|
||||
groups.update(group["id"], input={"product": "CRM", "language": "th", "channel": "facebook"})
|
||||
|
||||
def fail_report(**_kwargs):
|
||||
raise RuntimeError("report builder failed")
|
||||
|
||||
monkeypatch.setattr("app.services.report.build_report", fail_report)
|
||||
response = client.post(f"/api/groups/{group['id']}/analyze", headers=_headers(token))
|
||||
|
||||
assert response.status_code == 500
|
||||
stored = groups.get(group["id"])
|
||||
assert stored["status"] == "failed"
|
||||
assert stored["error"] == "analysis_failed"
|
||||
|
||||
|
||||
def test_jwt_without_exp_is_rejected(client, user_store, login):
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="missing-exp-user",
|
||||
password="missing-exp-password",
|
||||
name="Missing Exp",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
token = login(user["username"], "missing-exp-password")["token"]
|
||||
payload = user_store.decode_token(token)
|
||||
payload.pop("exp")
|
||||
legacy_token = jwt.encode(payload, Config.SECRET_KEY, algorithm=Config.JWT_ALGO)
|
||||
|
||||
response = client.get("/api/auth/me", headers=_headers(legacy_token))
|
||||
|
||||
assert response.status_code == 401
|
||||
|
||||
|
||||
def test_jwt_nonfinite_temporal_claims_are_rejected(client, user_store):
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="nonfinite-claims-user",
|
||||
password="nonfinite-claims-password",
|
||||
name="Nonfinite Claims",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
token = jwt.encode(
|
||||
{
|
||||
"sub": user["id"],
|
||||
"org_id": user["org_id"],
|
||||
"role": user["role"],
|
||||
"auth_version": user["auth_version"],
|
||||
"iat": math.inf,
|
||||
"exp": math.inf,
|
||||
},
|
||||
Config.SECRET_KEY,
|
||||
algorithm=Config.JWT_ALGO,
|
||||
)
|
||||
|
||||
response = client.get("/api/auth/me", headers=_headers(token))
|
||||
|
||||
assert response.status_code == 401
|
||||
|
||||
|
||||
def test_jwt_rejects_non_increasing_temporal_claims(user_store, monkeypatch):
|
||||
now = int(time.time())
|
||||
payload = {
|
||||
"sub": "temporal-order-user",
|
||||
"org_id": "org-default",
|
||||
"role": "user",
|
||||
"auth_version": 0,
|
||||
"iat": now,
|
||||
"exp": now,
|
||||
}
|
||||
monkeypatch.setattr(jwt, "decode", lambda *_args, **_kwargs: payload)
|
||||
|
||||
with pytest.raises(AuthError, match="invalid or expired token"):
|
||||
user_store.decode_token("opaque-token")
|
||||
|
||||
|
||||
def test_noncanonical_legacy_username_cannot_issue_token(user_store):
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="legacy-case-user",
|
||||
password="legacy-case-password",
|
||||
name="Legacy Case",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
malformed = {**user, "id": "Legacy-Case-User", "username": "Legacy-Case-User"}
|
||||
|
||||
with pytest.raises(AuthError, match="invalid account state"):
|
||||
user_store.issue_token(malformed)
|
||||
|
||||
|
||||
def test_malformed_login_identifier_and_corrupt_record_are_controlled(user_store, tmp_path: Path):
|
||||
with pytest.raises(AuthError, match="invalid credentials"):
|
||||
user_store.verify(" ", "some-password")
|
||||
|
||||
user_path = tmp_path / "users" / "corrupt-login.json"
|
||||
user_path.write_text("[]", encoding="utf-8")
|
||||
with pytest.raises(AuthError, match="invalid credentials"):
|
||||
user_store.verify("corrupt-login", "some-password")
|
||||
|
||||
|
||||
def test_auth_rejects_malformed_present_persisted_email(user_store):
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="malformed-persisted-email",
|
||||
password="malformed-persisted-email-password",
|
||||
name="Malformed Email",
|
||||
role="user",
|
||||
email="[email protected]",
|
||||
must_setup=False,
|
||||
)
|
||||
user_store.users.update(user["id"], email="Not An Email")
|
||||
|
||||
with pytest.raises(AuthError, match="invalid credentials"):
|
||||
user_store.verify(user["username"], "malformed-persisted-email-password")
|
||||
|
||||
with pytest.raises(AuthError, match="invalid account state"):
|
||||
user_store.issue_token(user_store.users.get(user["id"]))
|
||||
|
||||
|
||||
def test_existing_token_is_rejected_after_persisted_email_corruption(
|
||||
client, user_store, login
|
||||
):
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="corrupted-email-token",
|
||||
password="corrupted-email-token-password",
|
||||
name="Corrupted Email Token",
|
||||
email="[email protected]",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
token = login(user["username"], "corrupted-email-token-password")["token"]
|
||||
user_store.users.update(user["id"], email="Not An Email")
|
||||
|
||||
response = client.get("/api/auth/me", headers=_headers(token))
|
||||
|
||||
assert response.status_code == 401, response.get_json()
|
||||
|
||||
|
||||
def test_rate_limit_store_initialization_failure_fails_closed(monkeypatch):
|
||||
monkeypatch.setattr(
|
||||
rate_limit,
|
||||
"_record_store",
|
||||
lambda: (_ for _ in ()).throw(OSError("read-only")),
|
||||
)
|
||||
|
||||
assert rate_limit.check("login:user", "someone", limit=1, window=60) is False
|
||||
|
||||
|
||||
def test_rate_limit_migration_rejects_ambiguous_legacy_key(monkeypatch, tmp_path: Path):
|
||||
monkeypatch.setattr(Config, "DATA_DIR", tmp_path)
|
||||
(tmp_path / "ratelimit.json").write_text(
|
||||
json.dumps({"login:foo :bar": [999.0]}), encoding="utf-8"
|
||||
)
|
||||
|
||||
with pytest.raises(StoreError, match="legacy rate-limit key is invalid"):
|
||||
rate_limit._record_store()
|
||||
|
||||
|
||||
def test_rate_limit_migration_retry_does_not_duplicate_after_marker_failure(
|
||||
monkeypatch, tmp_path: Path
|
||||
):
|
||||
monkeypatch.setattr(Config, "DATA_DIR", tmp_path)
|
||||
legacy_key = rate_limit._key("login:user", "retry-user")
|
||||
(tmp_path / "ratelimit.json").write_text(
|
||||
json.dumps({legacy_key: [999.0, 999.0]}), encoding="utf-8"
|
||||
)
|
||||
original_marker = rate_limit._write_migration_marker
|
||||
attempts = {"count": 0}
|
||||
|
||||
def fail_once(path, _legacy_digest):
|
||||
if attempts["count"] == 0:
|
||||
attempts["count"] += 1
|
||||
raise StoreError("marker write failed")
|
||||
return original_marker(path, _legacy_digest)
|
||||
|
||||
monkeypatch.setattr(rate_limit, "_write_migration_marker", fail_once)
|
||||
with pytest.raises(StoreError, match="marker write failed"):
|
||||
rate_limit._record_store()
|
||||
|
||||
rate_limit._record_store()
|
||||
store = rate_limit._record_store()
|
||||
record_id = rate_limit._record_id(legacy_key)
|
||||
assert store.get(record_id)["timestamps"] == [999.0, 999.0]
|
||||
|
||||
|
||||
def test_rate_limit_migration_rejects_stale_marker(monkeypatch, tmp_path: Path):
|
||||
monkeypatch.setattr(Config, "DATA_DIR", tmp_path)
|
||||
legacy_path = tmp_path / "ratelimit.json"
|
||||
legacy_path.write_text(
|
||||
json.dumps({rate_limit._key("login:user", "stale-marker-user"): [999.0]}),
|
||||
encoding="utf-8",
|
||||
)
|
||||
(tmp_path / "ratelimit.json.migrated").write_text(
|
||||
"version=2\nsha256=" + hashlib.sha256(b"different").hexdigest() + "\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
with pytest.raises(StoreError, match="migration marker"):
|
||||
rate_limit._record_store()
|
||||
|
||||
|
||||
def test_rate_limit_structured_keys_do_not_collide(monkeypatch, tmp_path: Path):
|
||||
monkeypatch.setattr(Config, "DATA_DIR", tmp_path)
|
||||
assert rate_limit._key("login", "user:foo") != rate_limit._key("login:user", "foo")
|
||||
|
||||
|
||||
def test_scoped_user_listing_with_empty_org_is_empty(user_store):
|
||||
user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="scoped-list-user",
|
||||
password="scoped-list-password",
|
||||
name="Scoped List",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
|
||||
assert user_store.list_users(org_id="") == []
|
||||
|
||||
|
||||
def test_whitespace_tenant_token_is_rejected_before_route_scope(client, user_store):
|
||||
user = user_store.get_user("admin")
|
||||
now = int(time.time())
|
||||
token = jwt.encode(
|
||||
{
|
||||
"sub": user["id"],
|
||||
"org_id": " ",
|
||||
"role": user["role"],
|
||||
"auth_version": user["auth_version"],
|
||||
"iat": now,
|
||||
"exp": now + 3600,
|
||||
},
|
||||
Config.SECRET_KEY,
|
||||
algorithm=Config.JWT_ALGO,
|
||||
)
|
||||
|
||||
response = client.get("/api/me/weak-areas", headers=_headers(token))
|
||||
|
||||
assert response.status_code == 401
|
||||
|
||||
|
||||
def test_json_store_rejects_non_object_records_and_does_not_skip_corruption(tmp_path: Path):
|
||||
store = JsonStore(tmp_path / "records")
|
||||
store.root.mkdir(parents=True, exist_ok=True)
|
||||
(store.root / "bad.json").write_text("[]", encoding="utf-8")
|
||||
|
||||
with pytest.raises(StoreError):
|
||||
store.get("bad")
|
||||
with pytest.raises(StoreError):
|
||||
store.all()
|
||||
|
||||
|
||||
def test_token_identity_mismatch_is_rejected(user_store):
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="identity-consistency",
|
||||
password="identity-password",
|
||||
name="Identity",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
|
||||
with pytest.raises(AuthError):
|
||||
user_store.issue_token({**user, "id": "different-id"})
|
||||
|
||||
|
||||
def test_csv_formula_guard_handles_leading_whitespace_and_controls():
|
||||
for prefix in (" ", "\t", "\n", "\r", "\x00", "\ufeff"):
|
||||
assert _csv_cell(f"{prefix}=SUM(A1:A2)").startswith("'")
|
||||
|
||||
|
||||
def test_export_handles_malformed_debrief_without_500(client, user_store, login):
|
||||
user_store.complete_setup(
|
||||
"admin",
|
||||
"[email protected]",
|
||||
"legacy-export-admin-password",
|
||||
accepted_terms=True,
|
||||
accepted_terms_at="2026-08-15T00:00:00Z",
|
||||
)
|
||||
token = login("admin", "legacy-export-admin-password")["token"]
|
||||
trainee = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="legacy-export-trainee",
|
||||
password="legacy-export-trainee-password",
|
||||
name="Legacy Export Trainee",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
groups = client.application.extensions["group_store"]
|
||||
sessions = client.application.extensions["session_store"]
|
||||
group = groups.create(org_id="org-default", creator_id="admin", title="Export")
|
||||
record = sessions.create(
|
||||
org_id="org-default",
|
||||
user_id=trainee["id"],
|
||||
group_id=group["id"],
|
||||
persona_id="p-1",
|
||||
persona_name="Export Persona",
|
||||
)
|
||||
sessions.sessions.update(record["id"], status="finished", outcome="lost", debrief="not-a-mapping")
|
||||
|
||||
response = client.get("/api/analytics/export", headers=_headers(token))
|
||||
|
||||
assert response.status_code == 200
|
||||
assert "Export Persona" in response.get_data(as_text=True)
|
||||
|
||||
|
||||
def test_weak_area_aggregation_skips_unhashable_metadata_and_nonfinite_scores():
|
||||
session = {
|
||||
"id": "malformed-weak-area",
|
||||
"user_id": "u1",
|
||||
"org_id": "org-1",
|
||||
"mode": "trainee",
|
||||
"status": "finished",
|
||||
"outcome": "lost",
|
||||
"persona_id": "p-1",
|
||||
"persona_name": "Malformed",
|
||||
"persona_meta": {"tier": [], "initiation_mode": {"x": 1}, "channel": "line"},
|
||||
"debrief": {"score": math.nan, "failurePoints": [], "coaching": []},
|
||||
}
|
||||
|
||||
result = analyze_weak_areas([session], user_id="u1", org_id="org-1")
|
||||
|
||||
assert result["losses"] == 1
|
||||
assert result["by_channel"] == [{"value": "line", "losses": 1}]
|
||||
assert result["by_tier"] == []
|
||||
assert result["top_loss_personas"][0]["score"] == 0
|
||||
|
||||
|
||||
def test_non_super_admin_analytics_requires_session_and_group_tenant_scope(
|
||||
client, user_store, login
|
||||
):
|
||||
admin = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="legacy-analytics-scope-admin",
|
||||
password="legacy-analytics-scope-admin-password",
|
||||
name="Analytics Scope Admin",
|
||||
role="admin",
|
||||
email="[email protected]",
|
||||
must_setup=False,
|
||||
)
|
||||
trainee = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="legacy-analytics-scope-trainee",
|
||||
password="legacy-analytics-scope-trainee-password",
|
||||
name="Analytics Scope Trainee",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
token = login(admin["username"], "legacy-analytics-scope-admin-password")["token"]
|
||||
user_store.create_org("Other tenant", org_id="org-other")
|
||||
groups = client.application.extensions["group_store"]
|
||||
sessions = client.application.extensions["session_store"]
|
||||
own_group = groups.create(org_id="org-default", creator_id="admin", title="Own")
|
||||
other_group = groups.create(org_id="org-other", creator_id="admin", title="Other")
|
||||
rows = [
|
||||
("scope-own", "org-default", own_group["id"]),
|
||||
("scope-stale-session", "org-other", own_group["id"]),
|
||||
("scope-cross-group", "org-default", other_group["id"]),
|
||||
]
|
||||
for sid, org_id, group_id in rows:
|
||||
sessions.sessions.create(
|
||||
{
|
||||
"id": sid,
|
||||
"org_id": org_id,
|
||||
"user_id": trainee["id"],
|
||||
"group_id": group_id,
|
||||
"persona_id": sid,
|
||||
"persona_name": sid,
|
||||
"mode": "trainee",
|
||||
"status": "finished",
|
||||
"outcome": "won",
|
||||
"debrief": {"score": 90},
|
||||
},
|
||||
key=sid,
|
||||
)
|
||||
|
||||
response = client.get("/api/analytics", headers=_headers(token))
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.get_json()["overall"]["total_sessions"] == 1
|
||||
|
||||
|
||||
def test_export_rejects_non_super_admin_without_tenant_scope(client):
|
||||
from app.api import analytics_routes
|
||||
|
||||
with client.application.app_context(), pytest.raises(ApiError, match="permission denied"):
|
||||
analytics_routes._export_csv_for_actor({"id": "admin", "role": "admin"}, audit=False)
|
||||
|
||||
|
||||
def test_unknown_upload_size_is_rejected_before_save():
|
||||
from app.api import group_routes
|
||||
|
||||
class UnknownSizeStream(io.BytesIO):
|
||||
def tell(self):
|
||||
return None
|
||||
|
||||
with pytest.raises(ApiError, match="determine uploaded file size"):
|
||||
group_routes._inspect_upload_size(UnknownSizeStream(b"payload"))
|
||||
|
||||
|
||||
def test_malformed_signed_export_record_returns_403(client, user_store, login):
|
||||
user_store.complete_setup(
|
||||
"admin",
|
||||
"[email protected]",
|
||||
"legacy-token-admin-password",
|
||||
accepted_terms=True,
|
||||
accepted_terms_at="2026-08-15T00:00:00Z",
|
||||
)
|
||||
token = login("admin", "legacy-token-admin-password")["token"]
|
||||
link = client.get("/api/analytics/export/token", headers=_headers(token)).get_json()
|
||||
jti = link["token"].split(".", 1)[1].rsplit(":", 1)[-1]
|
||||
token_path = Config.DATA_DIR / "export_tokens" / f"{jti}.json"
|
||||
token_path.write_text("[]", encoding="utf-8")
|
||||
|
||||
response = client.get(link["url"])
|
||||
|
||||
assert response.status_code == 403
|
||||
|
||||
|
||||
def test_pdf_extraction_is_chunked_before_total_cap(monkeypatch, tmp_path: Path):
|
||||
calls = []
|
||||
|
||||
class FakePage:
|
||||
rect = type("Rect", (), {"x0": 0, "y0": 0, "x1": 10, "y1": 100, "height": 100})()
|
||||
|
||||
def get_text(self, *args, **kwargs):
|
||||
calls.append((args, kwargs))
|
||||
return "0123"
|
||||
|
||||
class FakeDocument:
|
||||
page_count = 1
|
||||
|
||||
def __iter__(self):
|
||||
return iter((FakePage(),))
|
||||
|
||||
def close(self):
|
||||
pass
|
||||
|
||||
monkeypatch.setitem(__import__("sys").modules, "fitz", type("Fitz", (), {"open": staticmethod(lambda _path: FakeDocument()), "Rect": lambda *args: args})())
|
||||
monkeypatch.setattr(Config, "UPLOAD_MAX_EXTRACTED_CHARS", 5)
|
||||
|
||||
with pytest.raises(ParseError, match="extracted text too large"):
|
||||
parse_pdf(tmp_path / "large.pdf")
|
||||
assert len(calls) > 1
|
||||
assert all("clip" in kwargs for _args, kwargs in calls)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("identifier", ["bad\x00id", "a" * 255, "bad\ud800"])
|
||||
def test_auth_rejects_malformed_identifiers_without_storage_exceptions(user_store, identifier):
|
||||
with pytest.raises(AuthError):
|
||||
user_store.verify(identifier, "irrelevant-password")
|
||||
|
||||
|
||||
def test_unicode_control_identifiers_are_rejected_across_boundaries(tmp_path: Path):
|
||||
with pytest.raises(AuthError):
|
||||
normalize_identifier("valid\u0080id")
|
||||
assert is_valid_tenant_id("org-\u0080") is False
|
||||
assert is_valid_tenant_id(".") is False
|
||||
|
||||
store = JsonStore(tmp_path / "records")
|
||||
with pytest.raises(StoreError):
|
||||
store._path("record-\u0080")
|
||||
|
||||
|
||||
def test_current_org_id_rejects_present_falsey_context_without_fallback(app):
|
||||
with app.app_context():
|
||||
g.user = {"org_id": "org-default"}
|
||||
for malformed in ("", None, [], 0):
|
||||
g.org_id = malformed
|
||||
assert current_org_id() == ""
|
||||
|
||||
|
||||
def test_require_roles_rejects_unhashable_corrupt_role(app):
|
||||
@require_roles("admin")
|
||||
def protected():
|
||||
return "ok"
|
||||
|
||||
with app.test_request_context():
|
||||
g.user = {"role": []}
|
||||
with pytest.raises(ApiError, match="permission denied"):
|
||||
protected()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"bad_email",
|
||||
[
|
||||
"bad\x00@example.com",
|
||||
"bad\u0080@example.com",
|
||||
"bad\ud800@example.com",
|
||||
"bad/[email protected]",
|
||||
"[email protected]",
|
||||
("a" * 190) + "@example.com",
|
||||
],
|
||||
)
|
||||
def test_validate_email_rejects_values_login_cannot_normalize(user_store, bad_email):
|
||||
with pytest.raises(AuthError):
|
||||
user_store.validate_email(bad_email, allow_empty=False)
|
||||
|
||||
|
||||
def test_auth_rejects_role_claim_mismatch_and_role_mutations_revoke_tokens(
|
||||
client, user_store
|
||||
):
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="role-claim-user",
|
||||
password="role-claim-password",
|
||||
name="Role Claim User",
|
||||
role="user",
|
||||
)
|
||||
token = user_store.issue_token(user)
|
||||
claims = jwt.decode(token, options={"verify_signature": False})
|
||||
claims["role"] = "admin"
|
||||
mismatched = jwt.encode(claims, Config.SECRET_KEY, algorithm=Config.JWT_ALGO)
|
||||
|
||||
response = client.get("/api/auth/me", headers=_headers(mismatched))
|
||||
assert response.status_code == 401
|
||||
|
||||
updated = user_store.update_user_fields(user["username"], role="admin")
|
||||
assert updated["auth_version"] == user["auth_version"] + 1
|
||||
assert client.get("/api/auth/me", headers=_headers(token)).status_code == 401
|
||||
|
||||
|
||||
def test_active_mutations_revoke_tokens_and_increment_auth_version(client, user_store):
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="active-mutation-user",
|
||||
password="active-mutation-password",
|
||||
name="Active Mutation User",
|
||||
)
|
||||
token = user_store.issue_token(user)
|
||||
|
||||
deactivated = user_store.update_user_fields(user["username"], active=False)
|
||||
assert deactivated["auth_version"] == user["auth_version"] + 1
|
||||
assert client.get("/api/auth/me", headers=_headers(token)).status_code == 401
|
||||
|
||||
reactivated = user_store.update_user_fields(user["username"], active=True)
|
||||
assert reactivated["auth_version"] == deactivated["auth_version"] + 1
|
||||
fresh_token = user_store.issue_token(reactivated)
|
||||
assert client.get("/api/auth/me", headers=_headers(fresh_token)).status_code == 200
|
||||
|
||||
|
||||
def test_by_email_rejects_noncanonical_lookup_values(user_store):
|
||||
with pytest.raises(AuthError):
|
||||
user_store.by_email("bad\x00@example.com")
|
||||
|
||||
|
||||
def test_rate_limit_rejects_malformed_identifier_without_unicode_error(monkeypatch, tmp_path: Path):
|
||||
monkeypatch.setattr(Config, "DATA_DIR", tmp_path)
|
||||
|
||||
assert rate_limit.check("login:user", "bad\ud800", limit=1, window=60) is False
|
||||
|
||||
|
||||
def test_rate_limit_migrates_legacy_json_state_without_resetting_counters(
|
||||
monkeypatch, tmp_path: Path
|
||||
):
|
||||
monkeypatch.setattr(Config, "DATA_DIR", tmp_path)
|
||||
monkeypatch.setattr(rate_limit.time, "time", lambda: 1_000.0)
|
||||
legacy_key = rate_limit._key("login:user", "legacy-user")
|
||||
(tmp_path / "ratelimit.json").write_text(
|
||||
json.dumps({legacy_key: [999.0]}), encoding="utf-8"
|
||||
)
|
||||
|
||||
assert rate_limit.check("login:user", "legacy-user", limit=1, window=60) is False
|
||||
|
||||
store = rate_limit._record_store()
|
||||
record_id = rate_limit._record_id(legacy_key)
|
||||
assert store.get(record_id)["timestamps"] == [999.0]
|
||||
assert (tmp_path / "ratelimit.json.migrated").is_file()
|
||||
|
||||
|
||||
def test_rate_limit_migration_preserves_duplicate_attempt_timestamps(
|
||||
monkeypatch, tmp_path: Path
|
||||
):
|
||||
monkeypatch.setattr(Config, "DATA_DIR", tmp_path)
|
||||
legacy_key = rate_limit._key("login:user", "duplicate-attempt-user")
|
||||
(tmp_path / "ratelimit.json").write_text(
|
||||
json.dumps({legacy_key: [999.0, 999.0]}), encoding="utf-8"
|
||||
)
|
||||
|
||||
store = rate_limit._record_store()
|
||||
record_id = rate_limit._record_id(legacy_key)
|
||||
|
||||
assert store.get(record_id)["timestamps"] == [999.0, 999.0]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("limit", "window"),
|
||||
[(0, 60), (-1, 60), (1, 0), (1, -1), (math.inf, 60), (1, math.nan), (True, 60)],
|
||||
)
|
||||
def test_rate_limit_rejects_invalid_parameters_before_storage(
|
||||
monkeypatch, limit, window
|
||||
):
|
||||
def unexpected_store_access():
|
||||
raise AssertionError("invalid configuration must fail before storage access")
|
||||
|
||||
monkeypatch.setattr(rate_limit, "_record_store", unexpected_store_access)
|
||||
assert rate_limit.check("login:user", "someone", limit=limit, window=window) is False
|
||||
|
||||
|
||||
def test_json_store_rejects_non_mapping_and_key_identity_mismatch(tmp_path: Path):
|
||||
store = JsonStore(tmp_path / "records")
|
||||
|
||||
with pytest.raises(StoreError):
|
||||
store.create([], key="record-1") # type: ignore[arg-type]
|
||||
with pytest.raises(StoreError):
|
||||
store.create({"id": "record-2"}, key="record-1")
|
||||
|
||||
(store.root / "record-3.json").write_text('{"id": "different"}', encoding="utf-8")
|
||||
with pytest.raises(StoreError):
|
||||
store.get("record-3")
|
||||
|
||||
|
||||
def test_json_store_revalidates_identity_after_update_and_update_if(tmp_path: Path):
|
||||
store = JsonStore(tmp_path / "records")
|
||||
store.create({"id": "record-1", "value": "initial"}, key="record-1")
|
||||
|
||||
with pytest.raises(StoreError):
|
||||
store.update("record-1", id="record-2")
|
||||
assert store.get("record-1")["value"] == "initial"
|
||||
|
||||
with pytest.raises(StoreError):
|
||||
store.update_if("record-1", lambda current: True, id="record-2")
|
||||
assert store.get("record-1")["value"] == "initial"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("bad_key", ["", ".", False])
|
||||
def test_json_store_rejects_explicit_falsey_or_special_keys(tmp_path: Path, bad_key):
|
||||
store = JsonStore(tmp_path / "records")
|
||||
|
||||
with pytest.raises(StoreError):
|
||||
store.create({"id": "fallback"}, key=bad_key) # type: ignore[arg-type]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("bad_id", ["", ".", False, None])
|
||||
def test_json_store_rejects_explicit_falsey_or_special_record_ids(tmp_path: Path, bad_id):
|
||||
store = JsonStore(tmp_path / "records")
|
||||
|
||||
with pytest.raises(StoreError):
|
||||
store.create({"id": bad_id})
|
||||
|
||||
|
||||
def test_json_store_all_rejects_invalid_filename_key(tmp_path: Path):
|
||||
store = JsonStore(tmp_path / "records")
|
||||
store.root.mkdir(parents=True, exist_ok=True)
|
||||
(store.root / "..json").write_text('{"id": "."}', encoding="utf-8")
|
||||
|
||||
with pytest.raises(StoreError):
|
||||
store.all()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("seats", ["bad", float("nan"), float("inf")])
|
||||
def test_create_user_rejects_malformed_org_seats(user_store, seats):
|
||||
user_store.orgs.update("org-default", seats=seats)
|
||||
|
||||
with pytest.raises(AuthError):
|
||||
user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="malformed-seat-user",
|
||||
password="malformed-seat-password",
|
||||
name="Malformed Seat User",
|
||||
)
|
||||
|
||||
|
||||
def test_create_user_translates_corrupt_org_storage_to_auth_error(user_store):
|
||||
(user_store.orgs.root / "org-default.json").write_text("[]", encoding="utf-8")
|
||||
|
||||
with pytest.raises(AuthError):
|
||||
user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="corrupt-org-user",
|
||||
password="corrupt-org-password",
|
||||
name="Corrupt Org User",
|
||||
)
|
||||
|
||||
|
||||
def test_analytics_and_export_exclude_private_groups_from_tenant_admin(
|
||||
client, user_store, login
|
||||
):
|
||||
admin = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="private-analytics-admin",
|
||||
password="private-analytics-admin-password",
|
||||
name="Private Analytics Admin",
|
||||
role="admin",
|
||||
email="[email protected]",
|
||||
must_setup=False,
|
||||
)
|
||||
trainee = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="private-analytics-trainee",
|
||||
password="private-analytics-trainee-password",
|
||||
name="Private Analytics Trainee",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
token = login(admin["username"], "private-analytics-admin-password")["token"]
|
||||
groups = client.application.extensions["group_store"]
|
||||
sessions = client.application.extensions["session_store"]
|
||||
private = groups.create(org_id="org-default", creator_id=trainee["id"], title="Private")
|
||||
groups.update(private["id"], status="ready", owner_user_id=trainee["id"])
|
||||
sessions.sessions.create(
|
||||
{
|
||||
"id": "private-analytics-session",
|
||||
"org_id": "org-default",
|
||||
"user_id": trainee["id"],
|
||||
"group_id": private["id"],
|
||||
"persona_id": "private-persona",
|
||||
"persona_name": "Private Persona",
|
||||
"mode": "trainee",
|
||||
"status": "finished",
|
||||
"outcome": "lost",
|
||||
"debrief": {"score": 50},
|
||||
},
|
||||
key="private-analytics-session",
|
||||
)
|
||||
|
||||
analytics_response = client.get("/api/analytics", headers=_headers(token))
|
||||
export_response = client.get("/api/analytics/export", headers=_headers(token))
|
||||
|
||||
assert analytics_response.status_code == 200
|
||||
assert analytics_response.get_json()["overall"]["total_sessions"] == 0
|
||||
assert "Private Persona" not in export_response.get_data(as_text=True)
|
||||
|
||||
|
||||
def test_analytics_uses_only_finished_won_or_lost_sessions_and_clamps_scores(
|
||||
client, user_store, login
|
||||
):
|
||||
admin = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="bounded-analytics-admin",
|
||||
password="bounded-analytics-admin-password",
|
||||
name="Bounded Analytics Admin",
|
||||
role="admin",
|
||||
email="[email protected]",
|
||||
must_setup=False,
|
||||
)
|
||||
trainee = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="bounded-analytics-trainee",
|
||||
password="bounded-analytics-trainee-password",
|
||||
name="Bounded Analytics Trainee",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
token = login(admin["username"], "bounded-analytics-admin-password")["token"]
|
||||
groups = client.application.extensions["group_store"]
|
||||
sessions = client.application.extensions["session_store"]
|
||||
group = groups.create(org_id="org-default", creator_id=admin["id"], title="Bounded")
|
||||
for sid, status, outcome, score in (
|
||||
("bounded-active", "active", "won", 99),
|
||||
("bounded-abandoned", "finished", "abandoned", 99),
|
||||
("bounded-won", "finished", "won", 10**308),
|
||||
):
|
||||
sessions.sessions.create(
|
||||
{
|
||||
"id": sid,
|
||||
"org_id": "org-default",
|
||||
"user_id": trainee["id"],
|
||||
"group_id": group["id"],
|
||||
"persona_id": sid,
|
||||
"persona_name": sid,
|
||||
"mode": "trainee",
|
||||
"status": status,
|
||||
"outcome": outcome,
|
||||
"debrief": {"score": score},
|
||||
},
|
||||
key=sid,
|
||||
)
|
||||
|
||||
response = client.get("/api/analytics", headers=_headers(token))
|
||||
|
||||
assert response.status_code == 200
|
||||
body = response.get_json()["overall"]
|
||||
assert body == {"total_sessions": 1, "wins": 1, "losses": 0, "close_rate": 100.0, "avg_score": 100.0}
|
||||
|
||||
|
||||
def test_pdf_rejects_one_oversized_extraction_chunk_before_yield(monkeypatch, tmp_path: Path):
|
||||
from app.services.file_parser import _iter_bounded_page_text
|
||||
|
||||
class FakePage:
|
||||
rect = type("Rect", (), {"x0": 0, "y0": 0, "x1": 10, "y1": 10})()
|
||||
|
||||
def get_text(self, *_args, **_kwargs):
|
||||
return "x" * 101
|
||||
|
||||
monkeypatch.setattr(Config, "UPLOAD_MAX_EXTRACTED_CHARS", 100)
|
||||
iterator = _iter_bounded_page_text(FakePage(), type("Fitz", (), {"Rect": lambda *args: args})())
|
||||
|
||||
with pytest.raises(ParseError, match="extracted text too large"):
|
||||
next(iterator)
|
||||
|
||||
|
||||
def test_upload_budget_rejects_unknown_total_and_aggregate_limits():
|
||||
from app.api.group_routes import _validate_upload_budget
|
||||
|
||||
with pytest.raises(ApiError, match="upload request size is unknown"):
|
||||
_validate_upload_budget(content_length=None, file_count=1, total_bytes=1)
|
||||
with pytest.raises(ApiError, match="too many uploaded files"):
|
||||
_validate_upload_budget(content_length=1, file_count=11, total_bytes=1)
|
||||
@@ -0,0 +1,61 @@
|
||||
"""The group generator has one deterministic auto-15 path."""
|
||||
from __future__ import annotations
|
||||
|
||||
from scripts.mock_llm import MockLLM
|
||||
|
||||
|
||||
def _headers(token: str) -> dict[str, str]:
|
||||
return {"Authorization": f"Bearer {token}"}
|
||||
|
||||
|
||||
def _setup(user_store, login):
|
||||
user_store.complete_setup(
|
||||
"admin",
|
||||
"[email protected]",
|
||||
"admin-ready-password",
|
||||
accepted_terms=True,
|
||||
accepted_terms_at="2026-08-13T00:00:00Z",
|
||||
)
|
||||
return login("admin", "admin-ready-password")["token"]
|
||||
|
||||
|
||||
def test_append_generation_is_rejected_and_does_not_mutate_group(client, user_store, login):
|
||||
token = _setup(user_store, login)
|
||||
client.application.extensions["llm"] = MockLLM()
|
||||
created = client.post(
|
||||
"/api/groups", json={"product": "CRM"}, headers=_headers(token)
|
||||
)
|
||||
gid = created.get_json()["group"]["id"]
|
||||
first = client.post(f"/api/groups/{gid}/analyze", headers=_headers(token))
|
||||
assert first.status_code == 200, first.get_json()
|
||||
before = client.application.extensions["group_store"].get(gid)
|
||||
|
||||
response = client.post(
|
||||
f"/api/groups/{gid}/analyze?append=true", headers=_headers(token)
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
assert response.get_json() == {"error": "append mode is no longer supported"}
|
||||
after = client.application.extensions["group_store"].get(gid)
|
||||
assert after["personas"] == before["personas"]
|
||||
assert len(after["personas"]) == 15
|
||||
|
||||
|
||||
def test_reanalyze_replaces_with_exactly_fifteen_personas(client, user_store, login):
|
||||
token = _setup(user_store, login)
|
||||
client.application.extensions["llm"] = MockLLM()
|
||||
created = client.post(
|
||||
"/api/groups", json={"product": "CRM"}, headers=_headers(token)
|
||||
)
|
||||
gid = created.get_json()["group"]["id"]
|
||||
assert client.post(f"/api/groups/{gid}/analyze", headers=_headers(token)).status_code == 200
|
||||
first_ids = {p["id"] for p in client.application.extensions["group_store"].get(gid)["personas"]}
|
||||
|
||||
response = client.post(f"/api/groups/{gid}/reanalyze", headers=_headers(token))
|
||||
|
||||
assert response.status_code == 200, response.get_json()
|
||||
personas = response.get_json()["personas"]
|
||||
assert len(personas) == 15
|
||||
assert len({p["id"] for p in personas}) == 15
|
||||
assert {p["tier"] for p in personas} == {"A", "B", "C"}
|
||||
assert {p["id"] for p in personas} == first_ids
|
||||
@@ -0,0 +1,308 @@
|
||||
"""Sprint 3.7 separate password-change contract tests."""
|
||||
from __future__ import annotations
|
||||
|
||||
import threading
|
||||
from contextlib import contextmanager
|
||||
|
||||
|
||||
def _headers(token: str) -> dict[str, str]:
|
||||
return {"Authorization": f"Bearer {token}"}
|
||||
|
||||
|
||||
def _setup_admin(user_store) -> None:
|
||||
user_store.complete_setup(
|
||||
"admin",
|
||||
"[email protected]",
|
||||
"admin-ready-password",
|
||||
accepted_terms=True,
|
||||
accepted_terms_at="2026-08-15T00:00:00Z",
|
||||
)
|
||||
|
||||
|
||||
def test_password_change_verifies_current_password_and_replaces_old(
|
||||
client, user_store, login
|
||||
):
|
||||
_setup_admin(user_store)
|
||||
old_password = "admin-ready-password"
|
||||
new_password = "admin-new-password"
|
||||
bearer = login("admin", old_password)["token"]
|
||||
|
||||
response = client.post(
|
||||
"/api/auth/password",
|
||||
headers=_headers(bearer),
|
||||
json={"current_password": old_password, "new_password": new_password},
|
||||
)
|
||||
|
||||
assert response.status_code == 200, response.get_json()
|
||||
assert login("admin", new_password)["user"]["username"] == "admin"
|
||||
old_login = client.post(
|
||||
"/api/auth/login",
|
||||
json={"username": "admin", "password": old_password},
|
||||
)
|
||||
assert old_login.status_code == 401
|
||||
|
||||
|
||||
def test_password_change_invalidates_existing_bearer_token(client, user_store, login):
|
||||
_setup_admin(user_store)
|
||||
old_bearer = login("admin", "admin-ready-password")["token"]
|
||||
|
||||
response = client.post(
|
||||
"/api/auth/password",
|
||||
headers=_headers(old_bearer),
|
||||
json={
|
||||
"current_password": "admin-ready-password",
|
||||
"new_password": "admin-new-password",
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == 200, response.get_json()
|
||||
stale = client.get("/api/auth/me", headers=_headers(old_bearer))
|
||||
assert stale.status_code == 401
|
||||
|
||||
|
||||
def test_concurrent_admin_reset_cannot_reuse_stale_auth_version(user_store, monkeypatch):
|
||||
user_store.complete_setup(
|
||||
"admin",
|
||||
"[email protected]",
|
||||
"admin-ready-password",
|
||||
accepted_terms=True,
|
||||
accepted_terms_at="2026-08-15T00:00:00Z",
|
||||
)
|
||||
|
||||
from app.auth import users as users_module
|
||||
|
||||
real_generate = users_module.generate_password_hash
|
||||
self_hash_started = threading.Event()
|
||||
release_self_hash = threading.Event()
|
||||
admin_collection_entered = threading.Event()
|
||||
admin_read = threading.Event()
|
||||
release_admin_read = threading.Event()
|
||||
original_get = user_store.users.get_or_none
|
||||
original_collection_lock = user_store.users.collection_lock
|
||||
|
||||
def blocking_hash(password):
|
||||
if password == "admin-self-password":
|
||||
self_hash_started.set()
|
||||
if not release_self_hash.wait(timeout=5):
|
||||
raise RuntimeError("self-service hash test gate timed out")
|
||||
return real_generate(password)
|
||||
|
||||
def tracked_get(key):
|
||||
result = original_get(key)
|
||||
if threading.current_thread().name == "admin-reset" and key == "admin":
|
||||
admin_read.set()
|
||||
if not release_admin_read.wait(timeout=5):
|
||||
raise RuntimeError("admin read test gate timed out")
|
||||
return result
|
||||
|
||||
@contextmanager
|
||||
def tracked_collection_lock():
|
||||
if threading.current_thread().name == "admin-reset":
|
||||
admin_collection_entered.set()
|
||||
with original_collection_lock():
|
||||
yield
|
||||
|
||||
monkeypatch.setattr(users_module, "generate_password_hash", blocking_hash)
|
||||
monkeypatch.setattr(user_store.users, "get_or_none", tracked_get)
|
||||
monkeypatch.setattr(user_store.users, "collection_lock", tracked_collection_lock)
|
||||
errors = []
|
||||
|
||||
def self_change():
|
||||
try:
|
||||
user_store.change_password(
|
||||
"admin", "admin-ready-password", "admin-self-password"
|
||||
)
|
||||
except Exception as exc: # pragma: no cover - assertion below reports it
|
||||
errors.append(exc)
|
||||
|
||||
def admin_reset():
|
||||
try:
|
||||
user_store.set_password("admin", "admin-reset-password")
|
||||
except Exception as exc: # pragma: no cover - assertion below reports it
|
||||
errors.append(exc)
|
||||
|
||||
self_thread = threading.Thread(name="self-change", target=self_change)
|
||||
admin_thread = threading.Thread(name="admin-reset", target=admin_reset)
|
||||
self_thread.start()
|
||||
assert self_hash_started.wait(timeout=5)
|
||||
admin_thread.start()
|
||||
assert admin_collection_entered.wait(timeout=5)
|
||||
|
||||
# Old code reads the user before waiting on record_lock; new code waits first.
|
||||
admin_read.wait(timeout=1)
|
||||
release_self_hash.set()
|
||||
assert admin_read.wait(timeout=5)
|
||||
release_admin_read.set()
|
||||
self_thread.join(timeout=5)
|
||||
admin_thread.join(timeout=5)
|
||||
|
||||
assert not self_thread.is_alive()
|
||||
assert not admin_thread.is_alive()
|
||||
assert errors == []
|
||||
assert user_store.users.get("admin")["auth_version"] == 3
|
||||
|
||||
|
||||
def test_concurrent_setup_cannot_reuse_stale_auth_version(user_store, monkeypatch):
|
||||
from app.auth import users as users_module
|
||||
|
||||
real_generate = users_module.generate_password_hash
|
||||
change_hash_started = threading.Event()
|
||||
release_change_hash = threading.Event()
|
||||
setup_collection_entered = threading.Event()
|
||||
setup_read = threading.Event()
|
||||
release_setup_read = threading.Event()
|
||||
original_get = user_store.users.get_or_none
|
||||
original_collection_lock = user_store.users.collection_lock
|
||||
|
||||
def blocking_hash(password):
|
||||
if password == "admin-change-password":
|
||||
change_hash_started.set()
|
||||
if not release_change_hash.wait(timeout=5):
|
||||
raise RuntimeError("change hash test gate timed out")
|
||||
return real_generate(password)
|
||||
|
||||
def tracked_get(key):
|
||||
result = original_get(key)
|
||||
if threading.current_thread().name == "setup-complete" and key == "admin":
|
||||
setup_read.set()
|
||||
if not release_setup_read.wait(timeout=5):
|
||||
raise RuntimeError("setup read test gate timed out")
|
||||
return result
|
||||
|
||||
@contextmanager
|
||||
def tracked_collection_lock():
|
||||
if threading.current_thread().name == "setup-complete":
|
||||
setup_collection_entered.set()
|
||||
with original_collection_lock():
|
||||
yield
|
||||
|
||||
monkeypatch.setattr(users_module, "generate_password_hash", blocking_hash)
|
||||
monkeypatch.setattr(user_store.users, "get_or_none", tracked_get)
|
||||
monkeypatch.setattr(user_store.users, "collection_lock", tracked_collection_lock)
|
||||
errors = []
|
||||
|
||||
def change_password():
|
||||
try:
|
||||
user_store.change_password(
|
||||
"admin", "pytest-bootstrap-password", "admin-change-password"
|
||||
)
|
||||
except Exception as exc: # pragma: no cover - assertion below reports it
|
||||
errors.append(exc)
|
||||
|
||||
def complete_setup():
|
||||
try:
|
||||
user_store.complete_setup(
|
||||
"admin",
|
||||
"[email protected]",
|
||||
"admin-setup-password",
|
||||
accepted_terms=True,
|
||||
accepted_terms_at="2026-08-15T00:00:00Z",
|
||||
)
|
||||
except Exception as exc: # pragma: no cover - assertion below reports it
|
||||
errors.append(exc)
|
||||
|
||||
change_thread = threading.Thread(name="password-change", target=change_password)
|
||||
setup_thread = threading.Thread(name="setup-complete", target=complete_setup)
|
||||
change_thread.start()
|
||||
assert change_hash_started.wait(timeout=5)
|
||||
setup_thread.start()
|
||||
assert setup_collection_entered.wait(timeout=5)
|
||||
|
||||
setup_read.wait(timeout=1)
|
||||
release_change_hash.set()
|
||||
assert setup_read.wait(timeout=5)
|
||||
release_setup_read.set()
|
||||
change_thread.join(timeout=5)
|
||||
setup_thread.join(timeout=5)
|
||||
|
||||
assert not change_thread.is_alive()
|
||||
assert not setup_thread.is_alive()
|
||||
assert errors == []
|
||||
assert user_store.users.get("admin")["auth_version"] == 2
|
||||
|
||||
|
||||
def test_password_change_rejects_wrong_current_password(client, user_store, login):
|
||||
_setup_admin(user_store)
|
||||
bearer = login("admin", "admin-ready-password")["token"]
|
||||
|
||||
response = client.post(
|
||||
"/api/auth/password",
|
||||
headers=_headers(bearer),
|
||||
json={
|
||||
"current_password": "wrong-current-password",
|
||||
"new_password": "admin-new-password",
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == 401
|
||||
|
||||
|
||||
def test_password_change_cannot_target_another_username(client, user_store, login):
|
||||
_setup_admin(user_store)
|
||||
bearer = login("admin", "admin-ready-password")["token"]
|
||||
|
||||
response = client.post(
|
||||
"/api/auth/password",
|
||||
headers=_headers(bearer),
|
||||
json={
|
||||
"username": "someone-else",
|
||||
"current_password": "admin-ready-password",
|
||||
"new_password": "admin-new-password",
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == 403
|
||||
|
||||
|
||||
def test_password_change_does_not_require_setup_terms(client, user_store, login):
|
||||
_setup_admin(user_store)
|
||||
bearer = login("admin", "admin-ready-password")["token"]
|
||||
|
||||
response = client.post(
|
||||
"/api/auth/password",
|
||||
headers=_headers(bearer),
|
||||
json={
|
||||
"current_password": "admin-ready-password",
|
||||
"new_password": "admin-new-password",
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == 200, response.get_json()
|
||||
|
||||
|
||||
def test_password_change_is_rate_limited(client, user_store, login):
|
||||
_setup_admin(user_store)
|
||||
bearer = login("admin", "admin-ready-password")["token"]
|
||||
body = {
|
||||
"current_password": "wrong-current-password",
|
||||
"new_password": "admin-new-password",
|
||||
}
|
||||
|
||||
responses = [
|
||||
client.post("/api/auth/password", headers=_headers(bearer), json=body)
|
||||
for _ in range(6)
|
||||
]
|
||||
|
||||
assert [response.status_code for response in responses[:5]] == [401] * 5
|
||||
assert responses[5].status_code == 429
|
||||
|
||||
|
||||
def test_password_change_rate_limit_survives_worker_memory_reset(
|
||||
client, user_store, login
|
||||
):
|
||||
_setup_admin(user_store)
|
||||
bearer = login("admin", "admin-ready-password")["token"]
|
||||
body = {
|
||||
"current_password": "wrong-current-password",
|
||||
"new_password": "admin-new-password",
|
||||
}
|
||||
|
||||
for _ in range(5):
|
||||
response = client.post("/api/auth/password", headers=_headers(bearer), json=body)
|
||||
assert response.status_code == 401
|
||||
|
||||
from app.services import rate_limit
|
||||
|
||||
rate_limit._mem.clear()
|
||||
blocked = client.post("/api/auth/password", headers=_headers(bearer), json=body)
|
||||
assert blocked.status_code == 429
|
||||
@@ -0,0 +1,107 @@
|
||||
"""Exact persona cardinality and schema contract tests."""
|
||||
from __future__ import annotations
|
||||
|
||||
import copy
|
||||
|
||||
import pytest
|
||||
|
||||
from app.services.persona_generator import PersonaGenerator
|
||||
|
||||
|
||||
class QueueLLM:
|
||||
def __init__(self, results):
|
||||
self.results = [copy.deepcopy(result) for result in results]
|
||||
self.calls = 0
|
||||
|
||||
def complete_json(self, system_prompt, user_prompt, **kwargs):
|
||||
self.calls += 1
|
||||
return self.results[min(self.calls - 1, len(self.results) - 1)]
|
||||
|
||||
|
||||
def _persona(index: int, tier: str, *, special: str = ""):
|
||||
return {
|
||||
"id": f"source-{index}",
|
||||
"name": f"Persona {index}",
|
||||
"tier": tier,
|
||||
"channel": "line",
|
||||
"initiation_mode": "customer" if index % 2 else "seller",
|
||||
"profession": "owner",
|
||||
"age_group": "adult",
|
||||
"location": "Bangkok",
|
||||
"product_context": "uses the product category",
|
||||
"background": "background",
|
||||
"income": "middle",
|
||||
"lifestyle": "busy",
|
||||
"personality": "careful",
|
||||
"communication_style": "direct",
|
||||
"budget": "1000",
|
||||
"decision_timeline": "this month",
|
||||
"goal": "solve pain",
|
||||
"objections": ["price"],
|
||||
"pains": [{"name": "pain", "fit": "strong", "description": "pain", "resolutionConditions": ["proof"]}],
|
||||
"negotiation_levers": ["price"],
|
||||
"opener": "Hello",
|
||||
"special": special,
|
||||
"difficulty": 3,
|
||||
"notes": "notes",
|
||||
"tolerance": 3,
|
||||
"recontact": False,
|
||||
}
|
||||
|
||||
|
||||
def _valid_personas():
|
||||
people = []
|
||||
index = 1
|
||||
for tier in ("A", "B", "C"):
|
||||
for slot in range(5):
|
||||
people.append(_persona(index, tier, special="wrong_text" if tier == "C" and slot == 0 else ""))
|
||||
index += 1
|
||||
return people
|
||||
|
||||
|
||||
def _generate(results):
|
||||
return PersonaGenerator(QueueLLM(results)).generate(
|
||||
sales_kit={}, language="th", channel="line"
|
||||
)
|
||||
|
||||
|
||||
def test_generator_returns_exact_15_with_five_per_tier_and_special_c():
|
||||
personas = _generate([{"personas": _valid_personas()}])
|
||||
|
||||
assert len(personas) == 15
|
||||
assert {tier: sum(p["tier"] == tier for p in personas) for tier in "ABC"} == {
|
||||
"A": 5, "B": 5, "C": 5
|
||||
}
|
||||
assert sum(p.get("special") == "wrong_text" for p in personas if p["tier"] == "C") >= 1
|
||||
assert len({p["id"] for p in personas}) == 15
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"mutate, message",
|
||||
[
|
||||
(lambda people: people.pop(), "exactly 15"),
|
||||
(lambda people: people.__setitem__(0, {**people[0], "tier": "D"}), "tier"),
|
||||
(lambda people: people.__setitem__(1, {**people[1], "id": people[0]["id"]}), "duplicate"),
|
||||
(lambda people: people.__setitem__(0, {**people[0], "channel": "social"}), "channel"),
|
||||
(lambda people: people.__setitem__(0, {**people[0], "initiation_mode": "f2f_call"}), "initiation"),
|
||||
(lambda people: [p.update(special="") for p in people if p["tier"] == "C"], "wrong_text"),
|
||||
],
|
||||
)
|
||||
def test_invalid_generation_retries_then_fails_closed(mutate, message):
|
||||
people = _valid_personas()
|
||||
mutate(people)
|
||||
|
||||
llm = QueueLLM([{"personas": people}])
|
||||
generator = PersonaGenerator(llm)
|
||||
with pytest.raises(ValueError, match=message):
|
||||
generator.generate(sales_kit={}, language="th", channel="line")
|
||||
assert llm.calls == 3
|
||||
|
||||
|
||||
def test_malformed_provider_container_retries_then_fails():
|
||||
llm = QueueLLM([{"personas": "not-a-list"}])
|
||||
generator = PersonaGenerator(llm)
|
||||
|
||||
with pytest.raises(ValueError, match="persona"):
|
||||
generator.generate(sales_kit={}, language="th", channel="line")
|
||||
assert llm.calls == 3
|
||||
@@ -0,0 +1,73 @@
|
||||
"""Persona-specific channel and initiation contracts."""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
|
||||
import pytest
|
||||
|
||||
from app.services.persona_generator import PersonaGenerator
|
||||
from app.services.store import ensure_persona_shape, validate_persona_traits
|
||||
from app.api.chat_routes import _scenario_config
|
||||
|
||||
|
||||
class StubPersonaLLM:
|
||||
def __init__(self, personas):
|
||||
self.personas = personas
|
||||
|
||||
def complete_json(self, system_prompt, user_prompt, **kwargs):
|
||||
return {"personas": self.personas}
|
||||
|
||||
|
||||
def _persona(channel="line", initiation_mode="customer", **extra):
|
||||
value = {
|
||||
"name": "Customer",
|
||||
"tier": "B",
|
||||
"channel": channel,
|
||||
"initiation_mode": initiation_mode,
|
||||
}
|
||||
value.update(extra)
|
||||
return value
|
||||
|
||||
|
||||
def _fifteen(**kwargs):
|
||||
return [_persona(**kwargs) for _ in range(15)]
|
||||
|
||||
|
||||
def test_persona_owns_initiation_even_when_scenario_changes():
|
||||
customer = ensure_persona_shape(_persona(initiation_mode="customer"))
|
||||
seller = ensure_persona_shape(_persona(initiation_mode="seller"))
|
||||
|
||||
_, customer_mode = _scenario_config("f2f_call", customer, "th")
|
||||
_, seller_mode = _scenario_config("social", seller, "th")
|
||||
|
||||
assert customer_mode == "customer"
|
||||
assert seller_mode == "seller"
|
||||
|
||||
|
||||
def test_channel_and_initiation_are_retained_in_normalized_persona():
|
||||
persona = ensure_persona_shape(_persona(channel="facebook", initiation_mode="seller"))
|
||||
|
||||
assert persona["channel"] == "facebook"
|
||||
assert persona["initiation_mode"] == "seller"
|
||||
validate_persona_traits(persona)
|
||||
|
||||
|
||||
def test_invalid_channel_is_rejected_not_mapped_to_social():
|
||||
generator = PersonaGenerator(StubPersonaLLM(_fifteen(channel="social")))
|
||||
|
||||
with pytest.raises(ValueError, match="channel"):
|
||||
generator.generate(sales_kit={}, language="th", channel="facebook")
|
||||
|
||||
|
||||
def test_invalid_initiation_is_rejected_not_mapped_to_customer():
|
||||
generator = PersonaGenerator(StubPersonaLLM(_fifteen(initiation_mode="f2f_call")))
|
||||
|
||||
with pytest.raises(ValueError, match="initiation_mode"):
|
||||
generator.generate(sales_kit={}, language="th", channel="facebook")
|
||||
|
||||
|
||||
def test_generator_rejects_invalid_requested_channel():
|
||||
generator = PersonaGenerator(StubPersonaLLM(_fifteen()))
|
||||
|
||||
with pytest.raises(ValueError, match="channel"):
|
||||
generator.generate(sales_kit={}, language="th", channel="social")
|
||||
@@ -0,0 +1,126 @@
|
||||
"""Persona reply contract tests: visible text must stay natural and bounded."""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
|
||||
import pytest
|
||||
|
||||
from app.llm import LLMError
|
||||
from app.services.simulator import MAX_PERSONA_REPLY_CHARS, Simulator
|
||||
|
||||
|
||||
class StubLLM:
|
||||
def __init__(self, responses: list[str]):
|
||||
self.responses = list(responses)
|
||||
self.calls: list[list[dict[str, str]]] = []
|
||||
|
||||
def complete_conversation(self, messages, **kwargs):
|
||||
self.calls.append(messages)
|
||||
if not self.responses:
|
||||
raise AssertionError("stub response exhausted")
|
||||
return self.responses.pop(0)
|
||||
|
||||
|
||||
def _persona(**overrides):
|
||||
persona = {
|
||||
"name": "คุณนิด",
|
||||
"tier": "B",
|
||||
"initiation_mode": "customer",
|
||||
"channel": "line",
|
||||
"profession": "เจ้าของร้าน",
|
||||
"age_group": "ผู้ใหญ่",
|
||||
"background": "ทำร้านเล็ก",
|
||||
"personality": "ระวังตัว",
|
||||
"lifestyle": "ยุ่ง",
|
||||
"income": "กลาง",
|
||||
"budget": "จำกัด",
|
||||
"decision_timeline": "เดือนนี้",
|
||||
"pains": [{"name": "ยอดขาย", "description": "ยอดขายผันผวน"}],
|
||||
"negotiation_levers": ["ราคา"],
|
||||
"goal": "อยากลดความเสี่ยง",
|
||||
"tolerance": 2,
|
||||
"recontact": True,
|
||||
"special": "wrong_text",
|
||||
}
|
||||
persona.update(overrides)
|
||||
return persona
|
||||
|
||||
|
||||
def _reply(stub: StubLLM):
|
||||
return Simulator(stub).persona_reply(
|
||||
persona=_persona(),
|
||||
sales_kit={"productName": "สินค้า"},
|
||||
messages=[{"role": "seller", "text": "ขอถามปัญหาหลักหน่อยครับ"}],
|
||||
internal={"turns": 1, "misses": 0, "score": 50},
|
||||
scenario="social",
|
||||
)
|
||||
|
||||
|
||||
def test_fenced_json_exposes_only_reply_and_keeps_metadata_internal():
|
||||
stub = StubLLM([
|
||||
"```json\n" + json.dumps(
|
||||
{"reply": "ขอคิดดูก่อนนะคะ", "decision": "buy", "mood": 2, "secret": "hidden"},
|
||||
ensure_ascii=False,
|
||||
) + "\n```"
|
||||
])
|
||||
|
||||
reply, meta = _reply(stub)
|
||||
|
||||
assert reply == "ขอคิดดูก่อนนะคะ"
|
||||
assert "decision" not in reply
|
||||
assert "mood" not in reply
|
||||
assert meta == {"decision": "buy", "mood": 2}
|
||||
|
||||
|
||||
def test_plain_natural_text_is_a_safe_fallback():
|
||||
stub = StubLLM(["ลูกค้ายังไม่แน่ใจ ขอถามเรื่องราคาเพิ่มได้ไหมคะ"])
|
||||
|
||||
reply, meta = _reply(stub)
|
||||
|
||||
assert reply == "ลูกค้ายังไม่แน่ใจ ขอถามเรื่องราคาเพิ่มได้ไหมคะ"
|
||||
assert meta == {"decision": "none", "mood": 0}
|
||||
|
||||
|
||||
def test_missing_reply_gets_one_bounded_retry():
|
||||
stub = StubLLM([
|
||||
json.dumps({"decision": "none", "mood": 0}),
|
||||
json.dumps({"reply": "ได้ค่ะ เล่ารายละเอียดเพิ่มได้เลย", "decision": "none", "mood": 1}),
|
||||
])
|
||||
|
||||
reply, meta = _reply(stub)
|
||||
|
||||
assert reply == "ได้ค่ะ เล่ารายละเอียดเพิ่มได้เลย"
|
||||
assert meta == {"decision": "none", "mood": 1}
|
||||
assert len(stub.calls) == 2
|
||||
assert "reply" in stub.calls[1][-1]["content"].lower()
|
||||
|
||||
|
||||
def test_blank_reply_after_retry_fails_closed():
|
||||
stub = StubLLM([json.dumps({"reply": ""}), json.dumps({"reply": " "})])
|
||||
|
||||
with pytest.raises(LLMError, match="persona reply"):
|
||||
_reply(stub)
|
||||
|
||||
assert len(stub.calls) == 2
|
||||
|
||||
|
||||
def test_reply_is_bounded_without_losing_thai_unicode():
|
||||
text = "ก" * (MAX_PERSONA_REPLY_CHARS + 100)
|
||||
stub = StubLLM([json.dumps({"reply": text}, ensure_ascii=False)])
|
||||
|
||||
reply, _ = _reply(stub)
|
||||
|
||||
assert len(reply) == MAX_PERSONA_REPLY_CHARS
|
||||
assert reply == "ก" * MAX_PERSONA_REPLY_CHARS
|
||||
|
||||
|
||||
def test_prompt_contains_persona_behavior_contract():
|
||||
stub = StubLLM([json.dumps({"reply": "ค่ะ"}, ensure_ascii=False)])
|
||||
|
||||
_reply(stub)
|
||||
|
||||
system = stub.calls[0][0]["content"]
|
||||
assert "tier" in system.lower()
|
||||
assert "recontact" in system.lower()
|
||||
assert "wrong_text" in system
|
||||
assert "tolerance" in system.lower()
|
||||
@@ -0,0 +1,959 @@
|
||||
"""Regression coverage for findings from the 2026-08-15 independent review."""
|
||||
from __future__ import annotations
|
||||
|
||||
import io
|
||||
import math
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
from app.api import analytics_routes, group_routes
|
||||
from app.api.helpers import ApiError
|
||||
from app.config import Config
|
||||
from app.services.file_parser import ParseError, parse_pdf
|
||||
from app.services.groups import GroupStore
|
||||
from app.services.trainee import _session_evidence
|
||||
from scripts.mock_llm import MockLLM
|
||||
|
||||
|
||||
def _headers(token: str) -> dict[str, str]:
|
||||
return {"Authorization": f"Bearer {token}"}
|
||||
|
||||
|
||||
def _setup_default_admin(user_store, login) -> str:
|
||||
user_store.complete_setup(
|
||||
"admin",
|
||||
"[email protected]",
|
||||
"post-review-admin-password",
|
||||
accepted_terms=True,
|
||||
accepted_terms_at="2026-08-15T00:00:00Z",
|
||||
)
|
||||
return login("admin", "post-review-admin-password")["token"]
|
||||
|
||||
|
||||
def _raw_group(group_store, *, personas=None, sales_kit=None):
|
||||
group = group_store.create(
|
||||
org_id="org-default",
|
||||
creator_id="admin",
|
||||
title="Post-review group",
|
||||
)
|
||||
return group_store.update(
|
||||
group["id"],
|
||||
status="ready",
|
||||
input={"product": "CRM", "channel": "social", "language": "th"},
|
||||
sales_kit=sales_kit,
|
||||
personas=[] if personas is None else personas,
|
||||
)
|
||||
|
||||
|
||||
def _cross_tenant_chat_session(client, user_store, login, *, status="active"):
|
||||
_setup_default_admin(user_store, login)
|
||||
other_org = user_store.create_org("Other chat tenant")
|
||||
trainee = user_store.create_user(
|
||||
org_id=other_org["id"],
|
||||
username=f"stale-chat-{status}",
|
||||
password="stale-chat-password",
|
||||
name="Stale Chat User",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
token = login(trainee["username"], "stale-chat-password")["token"]
|
||||
group_store = client.application.extensions["group_store"]
|
||||
group = group_store.create(
|
||||
org_id="org-default",
|
||||
creator_id="admin",
|
||||
title="Other tenant chat group",
|
||||
)
|
||||
group = group_store.update(
|
||||
group["id"],
|
||||
status="ready",
|
||||
personas=[
|
||||
{
|
||||
"id": "hidden-persona",
|
||||
"name": "Hidden Persona",
|
||||
"tier": "A",
|
||||
"pains": ["must not be revealed"],
|
||||
}
|
||||
],
|
||||
)
|
||||
session_id = f"stale-chat-{status}-session"
|
||||
session_store = client.application.extensions["session_store"]
|
||||
session_store.sessions.create(
|
||||
{
|
||||
"id": session_id,
|
||||
"org_id": other_org["id"],
|
||||
"user_id": trainee["id"],
|
||||
"group_id": group["id"],
|
||||
"persona_id": "hidden-persona",
|
||||
"persona_name": "Hidden Persona",
|
||||
"mode": "trainee",
|
||||
"status": status,
|
||||
"outcome": "won" if status == "finished" else None,
|
||||
"messages": [],
|
||||
"internal": {},
|
||||
"debrief": {"score": 99} if status == "finished" else None,
|
||||
},
|
||||
key=session_id,
|
||||
)
|
||||
return token, group, session_id
|
||||
|
||||
|
||||
def test_chat_session_reads_recheck_group_tenant_context(client, user_store, login):
|
||||
token, group, session_id = _cross_tenant_chat_session(
|
||||
client, user_store, login, status="active"
|
||||
)
|
||||
|
||||
direct = client.get(f"/api/chat/sessions/{session_id}", headers=_headers(token))
|
||||
resumed = client.get(
|
||||
f"/api/chat/{group['id']}/personas/hidden-persona/chat/resume",
|
||||
headers=_headers(token),
|
||||
)
|
||||
listed = client.get("/api/chat/sessions", headers=_headers(token))
|
||||
|
||||
assert direct.status_code == 404, direct.get_json()
|
||||
assert resumed.status_code == 404, resumed.get_json()
|
||||
assert listed.status_code == 200, listed.get_json()
|
||||
assert listed.get_json()["sessions"] == []
|
||||
|
||||
|
||||
def test_chat_send_rechecks_group_tenant_context_before_persona_use(
|
||||
client, user_store, login
|
||||
):
|
||||
token, group, _session_id = _cross_tenant_chat_session(
|
||||
client, user_store, login, status="active"
|
||||
)
|
||||
client.application.extensions["llm"] = MockLLM()
|
||||
|
||||
response = client.post(
|
||||
f"/api/chat/{group['id']}/personas/hidden-persona/chat/send",
|
||||
json={"text": "try to use stale context"},
|
||||
headers=_headers(token),
|
||||
)
|
||||
|
||||
assert response.status_code == 404, response.get_json()
|
||||
|
||||
|
||||
def test_chat_finish_rechecks_group_tenant_context_before_debrief(
|
||||
client, user_store, login
|
||||
):
|
||||
token, group, _session_id = _cross_tenant_chat_session(
|
||||
client, user_store, login, status="finished"
|
||||
)
|
||||
|
||||
response = client.post(
|
||||
f"/api/chat/{group['id']}/personas/hidden-persona/chat/finish",
|
||||
headers=_headers(token),
|
||||
)
|
||||
|
||||
assert response.status_code == 404, response.get_json()
|
||||
|
||||
|
||||
def test_chat_start_rechecks_resumed_session_context(client, user_store, login):
|
||||
_setup_default_admin(user_store, login)
|
||||
trainee = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="malformed-resume-mode",
|
||||
password="malformed-resume-password",
|
||||
name="Malformed Resume Mode",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
token = login(trainee["username"], "malformed-resume-password")["token"]
|
||||
group_store = client.application.extensions["group_store"]
|
||||
group = group_store.create(org_id="org-default", creator_id="admin", title="Resume")
|
||||
group_store.update(
|
||||
group["id"],
|
||||
status="ready",
|
||||
personas=[{"id": "resume-persona", "name": "Resume Persona", "pains": []}],
|
||||
)
|
||||
session_store = client.application.extensions["session_store"]
|
||||
session_store.sessions.create(
|
||||
{
|
||||
"id": "malformed-resume-session",
|
||||
"org_id": "org-default",
|
||||
"user_id": trainee["id"],
|
||||
"group_id": group["id"],
|
||||
"persona_id": "resume-persona",
|
||||
"persona_name": "Resume Persona",
|
||||
"mode": "",
|
||||
"status": "active",
|
||||
"outcome": None,
|
||||
},
|
||||
key="malformed-resume-session",
|
||||
)
|
||||
|
||||
response = client.post(
|
||||
f"/api/chat/{group['id']}/personas/resume-persona/chat/start",
|
||||
json={},
|
||||
headers=_headers(token),
|
||||
)
|
||||
|
||||
assert response.status_code == 404, response.get_json()
|
||||
|
||||
|
||||
def test_weak_area_routes_ignore_sessions_with_unauthorized_group(
|
||||
client, user_store, login
|
||||
):
|
||||
_setup_default_admin(user_store, login)
|
||||
trainee = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="stale-weak-area-user",
|
||||
password="stale-weak-area-password",
|
||||
name="Stale Weak Area User",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
token = login(trainee["username"], "stale-weak-area-password")["token"]
|
||||
group_store = client.application.extensions["group_store"]
|
||||
group = group_store.create(org_id="org-default", creator_id="admin", title="Private stale")
|
||||
group_store.update(
|
||||
group["id"],
|
||||
status="ready",
|
||||
owner_user_id="another-owner",
|
||||
personas=[{"id": "stale-persona", "name": "Stale Persona", "pains": []}],
|
||||
)
|
||||
session_store = client.application.extensions["session_store"]
|
||||
session_store.sessions.create(
|
||||
{
|
||||
"id": "stale-weak-area-session",
|
||||
"org_id": "org-default",
|
||||
"user_id": trainee["id"],
|
||||
"group_id": group["id"],
|
||||
"persona_id": "stale-persona",
|
||||
"persona_name": "Stale Persona",
|
||||
"mode": "trainee",
|
||||
"status": "finished",
|
||||
"outcome": "lost",
|
||||
"debrief": {"score": 1, "why": "price objection"},
|
||||
},
|
||||
key="stale-weak-area-session",
|
||||
)
|
||||
|
||||
response = client.get("/api/me/weak-areas", headers=_headers(token))
|
||||
|
||||
assert response.status_code == 200, response.get_json()
|
||||
assert response.get_json()["insight"]["total_sessions"] == 0
|
||||
|
||||
|
||||
def test_chat_direct_read_fails_closed_on_malformed_session_row(
|
||||
client, user_store, login, monkeypatch
|
||||
):
|
||||
token = _setup_default_admin(user_store, login)
|
||||
session_store = client.application.extensions["session_store"]
|
||||
monkeypatch.setattr(session_store.sessions, "get_or_none", lambda _sid: object())
|
||||
|
||||
response = client.get("/api/chat/sessions/malformed-row", headers=_headers(token))
|
||||
|
||||
assert response.status_code == 404, response.get_json()
|
||||
|
||||
|
||||
def test_private_group_is_published_atomically(tmp_path: Path, monkeypatch):
|
||||
store = GroupStore(tmp_path)
|
||||
|
||||
def fail_if_called(*args, **kwargs):
|
||||
raise AssertionError("private publication must not require a second update")
|
||||
|
||||
monkeypatch.setattr(store, "update", fail_if_called)
|
||||
|
||||
group = store.get_or_create_private_group(
|
||||
org_id="org-default",
|
||||
owner_user_id="trainee-1",
|
||||
owner_name="Trainee",
|
||||
)
|
||||
|
||||
assert group["owner_user_id"] == "trainee-1"
|
||||
assert group["status"] == "ready"
|
||||
assert group["input"]["language"] == "th"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"owner_marker, suffix",
|
||||
[(None, "none"), (0, "zero"), ("", "empty")],
|
||||
)
|
||||
def test_chat_rejects_falsey_private_owner_markers(
|
||||
client, user_store, login, owner_marker, suffix
|
||||
):
|
||||
_setup_default_admin(user_store, login)
|
||||
trainee = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username=f"falsey-owner-reader-{suffix}",
|
||||
password="falsey-owner-reader-password",
|
||||
name="Falsey Owner Reader",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
token = login(trainee["username"], "falsey-owner-reader-password")["token"]
|
||||
group_store = client.application.extensions["group_store"]
|
||||
group = group_store.create(
|
||||
org_id="org-default",
|
||||
creator_id="private-owner",
|
||||
title="Malformed private group",
|
||||
)
|
||||
group_store.update(
|
||||
group["id"],
|
||||
status="ready",
|
||||
owner_user_id=owner_marker,
|
||||
personas=[{"id": "persona-1", "name": "Persona", "tier": "A"}],
|
||||
)
|
||||
|
||||
response = client.post(
|
||||
f"/api/chat/{group['id']}/personas/persona-1/chat/start",
|
||||
json={},
|
||||
headers=_headers(token),
|
||||
)
|
||||
|
||||
assert response.status_code == 403, response.get_json()
|
||||
|
||||
|
||||
def test_group_store_ignores_non_mapping_records_in_visibility_indexes(tmp_path: Path, monkeypatch):
|
||||
store = GroupStore(tmp_path)
|
||||
monkeypatch.setattr(store.groups, "all", lambda: [[], "malformed"])
|
||||
|
||||
assert store.list_for_org("org-default") == []
|
||||
assert store.list_visible_to(role="admin", org_id="org-default") == []
|
||||
|
||||
|
||||
def test_malformed_stored_auth_records_fail_closed(client, user_store, login, monkeypatch):
|
||||
token = _setup_default_admin(user_store, login)
|
||||
monkeypatch.setattr(user_store, "get_user_or_none", lambda _subject: ["malformed"])
|
||||
|
||||
response = client.get("/api/auth/me", headers=_headers(token))
|
||||
|
||||
assert response.status_code == 503
|
||||
assert response.get_json()["error"] == "account state unavailable"
|
||||
|
||||
|
||||
def test_malformed_stored_org_record_fails_closed(client, user_store, login, monkeypatch):
|
||||
token = _setup_default_admin(user_store, login)
|
||||
monkeypatch.setattr(user_store, "get_org_or_none", lambda _org_id: ["malformed"])
|
||||
|
||||
response = client.get("/api/auth/me", headers=_headers(token))
|
||||
|
||||
assert response.status_code == 503
|
||||
assert response.get_json()["error"] == "account state unavailable"
|
||||
|
||||
|
||||
def test_group_delete_keeps_cross_tenant_stale_session(client, user_store, login):
|
||||
_setup_default_admin(user_store, login)
|
||||
admin = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="post-review-delete-admin",
|
||||
password="post-review-delete-admin-password",
|
||||
name="Delete Admin",
|
||||
role="admin",
|
||||
must_setup=False,
|
||||
)
|
||||
token = login(admin["username"], "post-review-delete-admin-password")["token"]
|
||||
user_store.create_org("Other tenant", org_id="org-other")
|
||||
group_store = client.application.extensions["group_store"]
|
||||
session_store = client.application.extensions["session_store"]
|
||||
group = group_store.create(
|
||||
org_id="org-default", creator_id=admin["id"], title="Delete me"
|
||||
)
|
||||
session_store.sessions.create(
|
||||
{
|
||||
"id": "stale-cross-tenant-session",
|
||||
"org_id": "org-other",
|
||||
"user_id": "someone",
|
||||
"group_id": group["id"],
|
||||
"persona_id": "persona-1",
|
||||
"persona_name": "Persona",
|
||||
"mode": "trainee",
|
||||
"status": "finished",
|
||||
"outcome": "won",
|
||||
},
|
||||
key="stale-cross-tenant-session",
|
||||
)
|
||||
|
||||
response = client.delete(
|
||||
f"/api/groups/{group['id']}", headers=_headers(token)
|
||||
)
|
||||
|
||||
assert response.status_code == 200, response.get_json()
|
||||
assert session_store.sessions.get_or_none("stale-cross-tenant-session") is not None
|
||||
|
||||
|
||||
def test_persona_update_skips_malformed_records_before_target(
|
||||
client, user_store, login
|
||||
):
|
||||
token = _setup_default_admin(user_store, login)
|
||||
group_store = client.application.extensions["group_store"]
|
||||
group = _raw_group(
|
||||
group_store,
|
||||
personas=[
|
||||
{"name": "missing id"},
|
||||
{"id": "persona-1", "name": "Before", "tier": "A"},
|
||||
],
|
||||
)
|
||||
|
||||
response = client.put(
|
||||
f"/api/groups/{group['id']}/personas/persona-1",
|
||||
json={"name": "After"},
|
||||
headers=_headers(token),
|
||||
)
|
||||
|
||||
assert response.status_code == 200, response.get_json()
|
||||
assert response.get_json()["persona"]["name"] == "After"
|
||||
|
||||
|
||||
def test_report_and_persona_routes_fail_closed_on_malformed_collections(
|
||||
client, user_store, login, monkeypatch
|
||||
):
|
||||
token = _setup_default_admin(user_store, login)
|
||||
group_store = client.application.extensions["group_store"]
|
||||
group = _raw_group(group_store, personas=[])
|
||||
group_store.update(group["id"], personas=None)
|
||||
|
||||
report = client.get(
|
||||
f"/api/groups/{group['id']}/report?format=json",
|
||||
headers=_headers(token),
|
||||
)
|
||||
|
||||
assert report.status_code == 200, report.get_json()
|
||||
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="malformed-session-user",
|
||||
password="malformed-session-password",
|
||||
name="Malformed Session User",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
user_token = login(user["username"], "malformed-session-password")["token"]
|
||||
group_store.update(
|
||||
group["id"],
|
||||
status="ready",
|
||||
personas=[{"id": "persona-1", "name": "Persona", "tier": "A"}],
|
||||
)
|
||||
sessions = client.application.extensions["session_store"].sessions
|
||||
monkeypatch.setattr(sessions, "all", lambda: [[], "malformed"])
|
||||
|
||||
personas = client.get(
|
||||
f"/api/groups/{group['id']}/personas",
|
||||
headers=_headers(user_token),
|
||||
)
|
||||
|
||||
assert personas.status_code == 200, personas.get_json()
|
||||
assert personas.get_json()["personas"][0]["my_outcome"] == "not_tried"
|
||||
|
||||
|
||||
def test_trainee_outcome_requires_finished_valid_result(client, user_store, login):
|
||||
_setup_default_admin(user_store, login)
|
||||
group_store = client.application.extensions["group_store"]
|
||||
group = _raw_group(
|
||||
group_store,
|
||||
personas=[{"id": "persona-1", "name": "Persona", "tier": "A"}],
|
||||
)
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="outcome-gating-user",
|
||||
password="outcome-gating-password",
|
||||
name="Outcome User",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
token = login(user["username"], "outcome-gating-password")["token"]
|
||||
client.application.extensions["session_store"].sessions.create(
|
||||
{
|
||||
"id": "active-but-won",
|
||||
"org_id": "org-default",
|
||||
"user_id": user["id"],
|
||||
"group_id": group["id"],
|
||||
"persona_id": "persona-1",
|
||||
"mode": "trainee",
|
||||
"status": "active",
|
||||
"outcome": "won",
|
||||
},
|
||||
key="active-but-won",
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
f"/api/groups/{group['id']}/personas",
|
||||
headers=_headers(token),
|
||||
)
|
||||
|
||||
assert response.status_code == 200, response.get_json()
|
||||
assert response.get_json()["personas"][0]["my_outcome"] == "not_tried"
|
||||
|
||||
|
||||
def test_report_skips_nested_secret_values_and_nonfinite_numbers(
|
||||
client, user_store, login
|
||||
):
|
||||
token = _setup_default_admin(user_store, login)
|
||||
group_store = client.application.extensions["group_store"]
|
||||
group = _raw_group(
|
||||
group_store,
|
||||
personas=[
|
||||
{
|
||||
"id": "persona-1",
|
||||
"name": "Safe persona",
|
||||
"tier": "A",
|
||||
"difficulty": math.nan,
|
||||
}
|
||||
],
|
||||
sales_kit={
|
||||
"productName": "CRM",
|
||||
"valueProps": [{"rootCause": "must-not-leak"}, "safe value"],
|
||||
"features": [math.inf, "safe feature"],
|
||||
},
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
f"/api/groups/{group['id']}/report?format=json",
|
||||
headers=_headers(token),
|
||||
)
|
||||
|
||||
assert response.status_code == 200, response.get_json()
|
||||
markdown = response.get_json()["markdown"]
|
||||
assert "must-not-leak" not in markdown
|
||||
assert "rootCause" not in markdown
|
||||
assert "safe value" in markdown
|
||||
assert "nan" not in markdown.lower()
|
||||
assert "inf" not in markdown.lower()
|
||||
|
||||
|
||||
def test_report_json_title_rejects_nested_title_values(client, user_store, login):
|
||||
token = _setup_default_admin(user_store, login)
|
||||
group_store = client.application.extensions["group_store"]
|
||||
group = _raw_group(group_store, personas=[])
|
||||
group_store.update(group["id"], title={"secret": "must-not-leak"})
|
||||
|
||||
response = client.get(
|
||||
f"/api/groups/{group['id']}/report?format=json",
|
||||
headers=_headers(token),
|
||||
)
|
||||
|
||||
assert response.status_code == 200, response.get_json()
|
||||
assert response.get_json()["title"] == "Sales training report"
|
||||
assert "must-not-leak" not in response.get_data(as_text=True)
|
||||
|
||||
|
||||
def test_weak_area_evidence_ignores_nonfinite_progress():
|
||||
evidence = _session_evidence(
|
||||
{
|
||||
"debrief": {
|
||||
"painProgress": {"pain_discovery": float("-inf")},
|
||||
"why": "ordinary explanation",
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
assert "pain progress" not in evidence
|
||||
assert "ordinary explanation" in evidence
|
||||
|
||||
|
||||
def test_pdf_budget_counts_join_separators(monkeypatch, tmp_path: Path):
|
||||
class FakePage:
|
||||
rect = SimpleNamespace(x0=0, y0=0, x1=10, y1=1)
|
||||
|
||||
def get_text(self, *args, **kwargs):
|
||||
return "aa"
|
||||
|
||||
class FakeDocument:
|
||||
page_count = 2
|
||||
|
||||
def __iter__(self):
|
||||
return iter((FakePage(), FakePage()))
|
||||
|
||||
def close(self):
|
||||
pass
|
||||
|
||||
monkeypatch.setitem(
|
||||
__import__("sys").modules,
|
||||
"fitz",
|
||||
SimpleNamespace(open=lambda _path: FakeDocument(), Rect=lambda *args: args),
|
||||
)
|
||||
monkeypatch.setattr(Config, "UPLOAD_MAX_EXTRACTED_CHARS", 4)
|
||||
|
||||
with pytest.raises(ParseError, match="extracted text too large"):
|
||||
parse_pdf(tmp_path / "separator-boundary.pdf")
|
||||
|
||||
|
||||
def test_analytics_counts_only_trainees(client, user_store, login):
|
||||
_setup_default_admin(user_store, login)
|
||||
admin = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="analytics-count-admin",
|
||||
password="analytics-count-admin-password",
|
||||
name="Analytics Admin",
|
||||
role="admin",
|
||||
must_setup=False,
|
||||
)
|
||||
user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="analytics-count-user",
|
||||
password="analytics-count-user-password",
|
||||
name="Analytics User",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
token = login(admin["username"], "analytics-count-admin-password")["token"]
|
||||
|
||||
response = client.get("/api/analytics", headers=_headers(token))
|
||||
|
||||
assert response.status_code == 200, response.get_json()
|
||||
assert response.get_json()["trainee_count"] == 1
|
||||
|
||||
|
||||
def test_analytics_excludes_finished_sessions_owned_by_tenant_admin(
|
||||
client, user_store, login
|
||||
):
|
||||
_setup_default_admin(user_store, login)
|
||||
admin = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="analytics-admin-session-owner",
|
||||
password="analytics-admin-session-owner-password",
|
||||
name="Analytics Admin Session Owner",
|
||||
role="admin",
|
||||
must_setup=False,
|
||||
)
|
||||
token = login(admin["username"], "analytics-admin-session-owner-password")["token"]
|
||||
group_store = client.application.extensions["group_store"]
|
||||
session_store = client.application.extensions["session_store"]
|
||||
group = group_store.create(
|
||||
org_id="org-default", creator_id=admin["id"], title="Admin-owned session"
|
||||
)
|
||||
session_store.sessions.create(
|
||||
{
|
||||
"id": "analytics-admin-owned-session",
|
||||
"org_id": "org-default",
|
||||
"user_id": admin["id"],
|
||||
"group_id": group["id"],
|
||||
"mode": "trainee",
|
||||
"status": "finished",
|
||||
"outcome": "won",
|
||||
},
|
||||
key="analytics-admin-owned-session",
|
||||
)
|
||||
|
||||
response = client.get("/api/analytics", headers=_headers(token))
|
||||
|
||||
assert response.status_code == 200, response.get_json()
|
||||
assert response.get_json()["overall"]["total_sessions"] == 0
|
||||
|
||||
|
||||
def test_super_admin_analytics_excludes_finished_sessions_owned_by_super_admin(
|
||||
client, user_store, login
|
||||
):
|
||||
token = _setup_default_admin(user_store, login)
|
||||
super_admin = user_store.get_user("admin")
|
||||
group_store = client.application.extensions["group_store"]
|
||||
session_store = client.application.extensions["session_store"]
|
||||
group = group_store.create(
|
||||
org_id="org-default", creator_id=super_admin["id"], title="Super-admin session"
|
||||
)
|
||||
session_store.sessions.create(
|
||||
{
|
||||
"id": "analytics-super-admin-owned-session",
|
||||
"org_id": "org-default",
|
||||
"user_id": super_admin["id"],
|
||||
"group_id": group["id"],
|
||||
"mode": "trainee",
|
||||
"status": "finished",
|
||||
"outcome": "lost",
|
||||
},
|
||||
key="analytics-super-admin-owned-session",
|
||||
)
|
||||
|
||||
response = client.get("/api/analytics", headers=_headers(token))
|
||||
|
||||
assert response.status_code == 200, response.get_json()
|
||||
assert response.get_json()["overall"]["total_sessions"] == 0
|
||||
|
||||
|
||||
def test_super_admin_csv_export_excludes_non_trainee_owned_sessions(
|
||||
client, user_store, login
|
||||
):
|
||||
token = _setup_default_admin(user_store, login)
|
||||
tenant_admin = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="export-tenant-admin",
|
||||
password="export-tenant-admin-password",
|
||||
name="Export Tenant Admin",
|
||||
role="admin",
|
||||
must_setup=False,
|
||||
)
|
||||
trainee = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="export-trainee",
|
||||
password="export-trainee-password",
|
||||
name="Export Trainee",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
group_store = client.application.extensions["group_store"]
|
||||
session_store = client.application.extensions["session_store"]
|
||||
group = group_store.create(
|
||||
org_id="org-default", creator_id="admin", title="Global export group"
|
||||
)
|
||||
for key, user_id in (
|
||||
("export-super-admin-session", "admin"),
|
||||
("export-tenant-admin-session", tenant_admin["id"]),
|
||||
("export-trainee-session", trainee["id"]),
|
||||
):
|
||||
session_store.sessions.create(
|
||||
{
|
||||
"id": key,
|
||||
"org_id": "org-default",
|
||||
"user_id": user_id,
|
||||
"group_id": group["id"],
|
||||
"persona_id": key,
|
||||
"persona_name": "Export Persona",
|
||||
"mode": "trainee",
|
||||
"status": "finished",
|
||||
"outcome": "won",
|
||||
"debrief": {"score": 80},
|
||||
},
|
||||
key=key,
|
||||
)
|
||||
|
||||
response = client.get("/api/analytics/export", headers=_headers(token))
|
||||
|
||||
assert response.status_code == 200, response.get_json()
|
||||
csv_text = response.get_data(as_text=True)
|
||||
assert "export-trainee" in csv_text
|
||||
assert "admin" not in csv_text
|
||||
assert "export-tenant-admin" not in csv_text
|
||||
|
||||
|
||||
def test_export_token_store_failures_are_controlled(app, monkeypatch):
|
||||
class BrokenStore:
|
||||
def update_if(self, *args, **kwargs):
|
||||
raise OSError("storage unavailable")
|
||||
|
||||
monkeypatch.setattr(analytics_routes, "_export_token_store", lambda: BrokenStore())
|
||||
with app.app_context():
|
||||
assert analytics_routes._consume_export_token(
|
||||
{"jti": "export-jti", "expires_at": 2_000_000_000, "user_id": "admin", "org_id": "org-default", "role": "admin"}
|
||||
) is False
|
||||
|
||||
|
||||
def test_csv_export_has_a_row_cap(client, user_store, login, monkeypatch):
|
||||
token = _setup_default_admin(user_store, login)
|
||||
group_store = client.application.extensions["group_store"]
|
||||
session_store = client.application.extensions["session_store"]
|
||||
group = group_store.create(
|
||||
org_id="org-default", creator_id="admin", title="Export group"
|
||||
)
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="export-row-user",
|
||||
password="export-row-password",
|
||||
name="Export User",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
for sid in ("export-row-1", "export-row-2"):
|
||||
session_store.sessions.create(
|
||||
{
|
||||
"id": sid,
|
||||
"org_id": "org-default",
|
||||
"user_id": user["id"],
|
||||
"group_id": group["id"],
|
||||
"persona_id": sid,
|
||||
"persona_name": "Persona",
|
||||
"mode": "trainee",
|
||||
"status": "finished",
|
||||
"outcome": "won",
|
||||
"debrief": {"score": 80},
|
||||
},
|
||||
key=sid,
|
||||
)
|
||||
monkeypatch.setattr(Config, "ANALYTICS_EXPORT_MAX_ROWS", 1)
|
||||
|
||||
with client.application.app_context(), pytest.raises(ApiError, match="too many"):
|
||||
analytics_routes._export_csv_for_actor(
|
||||
{"id": "admin", "org_id": "org-default", "role": "admin"},
|
||||
audit=False,
|
||||
)
|
||||
|
||||
|
||||
def test_csv_export_legacy_collection_does_not_materialize_where(
|
||||
client, user_store, login, monkeypatch
|
||||
):
|
||||
_setup_default_admin(user_store, login)
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="legacy-export-user",
|
||||
password="legacy-export-password",
|
||||
name="Legacy Export User",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
|
||||
rows = [
|
||||
{
|
||||
"id": "legacy-export-1",
|
||||
"org_id": "org-default",
|
||||
"user_id": user["id"],
|
||||
"group_id": "shared-group",
|
||||
"mode": "trainee",
|
||||
"status": "finished",
|
||||
"outcome": "won",
|
||||
},
|
||||
{
|
||||
"id": "legacy-export-2",
|
||||
"org_id": "org-default",
|
||||
"user_id": user["id"],
|
||||
"group_id": "shared-group",
|
||||
"mode": "trainee",
|
||||
"status": "finished",
|
||||
"outcome": "lost",
|
||||
},
|
||||
]
|
||||
|
||||
class LegacyCollection:
|
||||
def iter_all(self):
|
||||
return iter(rows)
|
||||
|
||||
def where(self, _predicate):
|
||||
raise AssertionError("legacy export must not materialize where()")
|
||||
|
||||
fake_stores = {
|
||||
"groups": SimpleNamespace(
|
||||
groups=SimpleNamespace(
|
||||
iter_all=lambda: iter([{"id": "shared-group", "org_id": "org-default"}])
|
||||
)
|
||||
),
|
||||
"sessions": SimpleNamespace(sessions=LegacyCollection()),
|
||||
}
|
||||
monkeypatch.setattr(analytics_routes, "_stores", lambda: fake_stores)
|
||||
monkeypatch.setattr(Config, "ANALYTICS_EXPORT_MAX_ROWS", 1)
|
||||
|
||||
with client.application.app_context(), pytest.raises(ApiError, match="too many"):
|
||||
analytics_routes._export_csv_for_actor(
|
||||
{"id": "admin", "org_id": "org-default", "role": "admin"},
|
||||
audit=False,
|
||||
)
|
||||
|
||||
|
||||
def test_csv_export_scans_session_collection_once(
|
||||
client, user_store, login, monkeypatch
|
||||
):
|
||||
_setup_default_admin(user_store, login)
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="single-export-scan-user",
|
||||
password="single-export-scan-password",
|
||||
name="Single Export Scan User",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
|
||||
class CountingCollection:
|
||||
def __init__(self, rows):
|
||||
self.rows = rows
|
||||
self.calls = 0
|
||||
|
||||
def iter_all(self):
|
||||
self.calls += 1
|
||||
return iter(self.rows)
|
||||
|
||||
groups = CountingCollection(
|
||||
[
|
||||
{"id": "shared-group-1", "org_id": "org-default"},
|
||||
{"id": "shared-group-2", "org_id": "org-default"},
|
||||
]
|
||||
)
|
||||
sessions = CountingCollection(
|
||||
[
|
||||
{
|
||||
"id": "single-export-scan-1",
|
||||
"org_id": "org-default",
|
||||
"user_id": user["id"],
|
||||
"group_id": "shared-group-1",
|
||||
"mode": "trainee",
|
||||
"status": "finished",
|
||||
"outcome": "won",
|
||||
},
|
||||
{
|
||||
"id": "single-export-scan-2",
|
||||
"org_id": "org-default",
|
||||
"user_id": user["id"],
|
||||
"group_id": "shared-group-2",
|
||||
"mode": "trainee",
|
||||
"status": "finished",
|
||||
"outcome": "lost",
|
||||
},
|
||||
]
|
||||
)
|
||||
fake_stores = {
|
||||
"groups": SimpleNamespace(groups=groups),
|
||||
"sessions": SimpleNamespace(sessions=sessions),
|
||||
}
|
||||
monkeypatch.setattr(analytics_routes, "_stores", lambda: fake_stores)
|
||||
|
||||
with client.application.app_context():
|
||||
response = analytics_routes._export_csv_for_actor(
|
||||
{"id": "admin", "org_id": "org-default", "role": "admin"},
|
||||
audit=False,
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert sessions.calls == 1
|
||||
|
||||
|
||||
def test_csv_export_rejects_total_scan_limit(
|
||||
client, user_store, login, monkeypatch
|
||||
):
|
||||
_setup_default_admin(user_store, login)
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="bounded-export-scan-user",
|
||||
password="bounded-export-scan-password",
|
||||
name="Bounded Export Scan User",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
|
||||
class BoundedCollection:
|
||||
def __init__(self, rows):
|
||||
self.rows = rows
|
||||
|
||||
def iter_all(self):
|
||||
return iter(self.rows)
|
||||
|
||||
fake_stores = {
|
||||
"groups": SimpleNamespace(
|
||||
groups=BoundedCollection(
|
||||
[{"id": "bounded-scan-group", "org_id": "org-default"}]
|
||||
)
|
||||
),
|
||||
"sessions": SimpleNamespace(
|
||||
sessions=BoundedCollection(
|
||||
[
|
||||
{
|
||||
"id": "bounded-scan-session",
|
||||
"org_id": "org-default",
|
||||
"user_id": user["id"],
|
||||
"group_id": "bounded-scan-group",
|
||||
"mode": "trainee",
|
||||
"status": "finished",
|
||||
"outcome": "won",
|
||||
}
|
||||
]
|
||||
)
|
||||
),
|
||||
}
|
||||
monkeypatch.setattr(analytics_routes, "_stores", lambda: fake_stores)
|
||||
monkeypatch.setattr(Config, "ANALYTICS_EXPORT_MAX_SCAN_RECORDS", 1)
|
||||
|
||||
with client.application.app_context(), pytest.raises(ApiError, match="scan limit"):
|
||||
analytics_routes._export_csv_for_actor(
|
||||
{"id": "admin", "org_id": "org-default", "role": "admin"},
|
||||
audit=False,
|
||||
)
|
||||
|
||||
|
||||
def test_upload_rejects_control_character_filename(client, user_store, login):
|
||||
token = _setup_default_admin(user_store, login)
|
||||
|
||||
response = client.post(
|
||||
"/api/groups",
|
||||
data={"files": (io.BytesIO(b"product brief"), "bad\x01.txt")},
|
||||
content_type="multipart/form-data",
|
||||
headers=_headers(token),
|
||||
)
|
||||
|
||||
assert response.status_code == 400, response.get_json()
|
||||
@@ -0,0 +1,73 @@
|
||||
"""Private group lookup/create race regression tests."""
|
||||
from __future__ import annotations
|
||||
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from pathlib import Path
|
||||
from threading import Barrier
|
||||
|
||||
from app.services.groups import GroupStore
|
||||
|
||||
|
||||
def test_private_group_lookup_and_create_is_atomic(tmp_path: Path):
|
||||
store = GroupStore(tmp_path)
|
||||
barrier = Barrier(8)
|
||||
|
||||
def get_group():
|
||||
barrier.wait(timeout=5)
|
||||
return store.get_or_create_private_group(
|
||||
org_id="org-1",
|
||||
owner_user_id="user-1",
|
||||
owner_name="Trainee",
|
||||
)
|
||||
|
||||
with ThreadPoolExecutor(max_workers=8) as pool:
|
||||
groups = list(pool.map(lambda _: get_group(), range(8)))
|
||||
|
||||
assert len({group["id"] for group in groups}) == 1
|
||||
assert len(store.list_for_org("org-1")) == 1
|
||||
assert groups[0]["owner_user_id"] == "user-1"
|
||||
|
||||
|
||||
def test_private_groups_are_scoped_by_org_and_owner(tmp_path: Path):
|
||||
store = GroupStore(tmp_path)
|
||||
|
||||
first = store.get_or_create_private_group(org_id="org-1", owner_user_id="user-1")
|
||||
same_owner = store.get_or_create_private_group(org_id="org-1", owner_user_id="user-1")
|
||||
other_org = store.get_or_create_private_group(org_id="org-2", owner_user_id="user-1")
|
||||
other_owner = store.get_or_create_private_group(org_id="org-1", owner_user_id="user-2")
|
||||
|
||||
assert same_owner["id"] == first["id"]
|
||||
assert other_org["id"] != first["id"]
|
||||
assert other_owner["id"] != first["id"]
|
||||
assert len(store.list_for_org("org-1")) == 2
|
||||
assert len(store.list_for_org("org-2")) == 1
|
||||
|
||||
|
||||
def test_locked_persona_appends_preserve_all_concurrent_variants(tmp_path: Path):
|
||||
store = GroupStore(tmp_path)
|
||||
group = store.get_or_create_private_group(org_id="org-1", owner_user_id="user-1")
|
||||
barrier = Barrier(8)
|
||||
|
||||
def append_one(index: int):
|
||||
barrier.wait(timeout=5)
|
||||
with store.record_lock(group["id"]):
|
||||
current = store.get(group["id"])
|
||||
store.set_personas(
|
||||
group["id"],
|
||||
current.get("personas", []) + [{"id": f"variant-{index}", "name": str(index)}],
|
||||
)
|
||||
|
||||
with ThreadPoolExecutor(max_workers=8) as pool:
|
||||
list(pool.map(append_one, range(8)))
|
||||
|
||||
personas = store.get(group["id"])["personas"]
|
||||
assert {persona["id"] for persona in personas} == {f"variant-{i}" for i in range(8)}
|
||||
|
||||
|
||||
def test_record_lock_is_reentrant_for_nested_service_writes(tmp_path: Path):
|
||||
store = GroupStore(tmp_path)
|
||||
group = store.create(org_id="org-1", creator_id="user-1", title="Nested")
|
||||
with store.record_lock(group["id"]):
|
||||
with store.record_lock(group["id"]):
|
||||
store.set_personas(group["id"], [{"id": "persona-1", "name": "Nested"}])
|
||||
assert store.get(group["id"])["personas"][0]["id"] == "persona-1"
|
||||
@@ -0,0 +1,135 @@
|
||||
"""Private variant isolation and locked-core regression tests."""
|
||||
from __future__ import annotations
|
||||
|
||||
from scripts.mock_llm import MockLLM
|
||||
|
||||
|
||||
class VariantLLM:
|
||||
def complete_json(self, system_prompt, user_prompt, **kwargs):
|
||||
return {
|
||||
"name": "Fresh Identity",
|
||||
"channel": "line",
|
||||
"initiation_mode": "customer",
|
||||
"profession": "Different profession",
|
||||
"age_group": "40s",
|
||||
"location": "Chiang Mai",
|
||||
"background": "Different background",
|
||||
"pains": [{"name": "wrong pain"}],
|
||||
"objections": ["wrong objection"],
|
||||
"negotiation_levers": ["wrong lever"],
|
||||
"tolerance": 5,
|
||||
"special": "",
|
||||
"recontact": False,
|
||||
"goal": "different goal",
|
||||
"decision_timeline": "later",
|
||||
"budget": "different budget",
|
||||
"difficulty": 1,
|
||||
"tier": "A",
|
||||
"product_context": "different context",
|
||||
}
|
||||
|
||||
|
||||
def _headers(token: str) -> dict[str, str]:
|
||||
return {"Authorization": f"Bearer {token}"}
|
||||
|
||||
|
||||
def _setup(user_store, login):
|
||||
user_store.complete_setup(
|
||||
"admin",
|
||||
"[email protected]",
|
||||
"admin-ready-password",
|
||||
accepted_terms=True,
|
||||
accepted_terms_at="2026-08-13T00:00:00Z",
|
||||
)
|
||||
return login("admin", "admin-ready-password")["token"]
|
||||
|
||||
|
||||
def test_private_variant_locks_core_and_does_not_mutate_shared_group(client, user_store, login):
|
||||
admin_token = _setup(user_store, login)
|
||||
client.application.extensions["llm"] = MockLLM()
|
||||
created = client.post(
|
||||
"/api/groups",
|
||||
json={"product": "CRM", "segment": "SME"},
|
||||
headers=_headers(admin_token),
|
||||
)
|
||||
gid = created.get_json()["group"]["id"]
|
||||
analyzed = client.post(f"/api/groups/{gid}/analyze", headers=_headers(admin_token))
|
||||
assert analyzed.status_code == 200
|
||||
group_store = client.application.extensions["group_store"]
|
||||
shared_before = group_store.get(gid)
|
||||
source = shared_before["personas"][0]
|
||||
|
||||
trainee = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="variant-trainee",
|
||||
password="variant-trainee-password",
|
||||
name="Variant Trainee",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
trainee_token = login(trainee["username"], "variant-trainee-password")["token"]
|
||||
client.application.extensions["llm"] = VariantLLM()
|
||||
|
||||
response = client.post(
|
||||
f"/api/groups/{gid}/personas/{source['id']}/variant",
|
||||
headers=_headers(trainee_token),
|
||||
)
|
||||
|
||||
assert response.status_code == 201, response.get_json()
|
||||
payload = response.get_json()
|
||||
variant = payload["persona"]
|
||||
assert payload["group_id"] != gid
|
||||
assert variant["id"] != source["id"]
|
||||
|
||||
shared_after = group_store.get(gid)
|
||||
assert len(shared_after["personas"]) == len(shared_before["personas"])
|
||||
assert shared_after["personas"] == shared_before["personas"]
|
||||
|
||||
private = group_store.get(payload["group_id"])
|
||||
stored_variant = private["personas"][0]
|
||||
for key in ("pains", "objections", "negotiation_levers", "tolerance", "special", "recontact", "goal", "decision_timeline", "budget", "difficulty", "tier", "product_context"):
|
||||
assert stored_variant[key] == source[key]
|
||||
assert stored_variant["source_persona_id"] == source["id"]
|
||||
|
||||
|
||||
def test_other_trainee_cannot_access_private_variant(client, user_store, login):
|
||||
admin_token = _setup(user_store, login)
|
||||
client.application.extensions["llm"] = MockLLM()
|
||||
created = client.post(
|
||||
"/api/groups",
|
||||
json={"product": "CRM"},
|
||||
headers=_headers(admin_token),
|
||||
)
|
||||
gid = created.get_json()["group"]["id"]
|
||||
analyzed = client.post(f"/api/groups/{gid}/analyze", headers=_headers(admin_token))
|
||||
source_id = analyzed.get_json()["personas"][0]["id"]
|
||||
owner = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="variant-owner",
|
||||
password="variant-owner-password",
|
||||
name="Owner",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
other = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="variant-other",
|
||||
password="variant-other-password",
|
||||
name="Other",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
owner_token = login(owner["username"], "variant-owner-password")["token"]
|
||||
other_token = login(other["username"], "variant-other-password")["token"]
|
||||
client.application.extensions["llm"] = VariantLLM()
|
||||
created_variant = client.post(
|
||||
f"/api/groups/{gid}/personas/{source_id}/variant",
|
||||
headers=_headers(owner_token),
|
||||
)
|
||||
private_gid = created_variant.get_json()["group_id"]
|
||||
|
||||
response = client.get(
|
||||
f"/api/groups/{private_gid}/personas", headers=_headers(other_token)
|
||||
)
|
||||
|
||||
assert response.status_code == 403
|
||||
@@ -0,0 +1,180 @@
|
||||
"""Cross-process rate-limit regression tests."""
|
||||
from __future__ import annotations
|
||||
|
||||
import multiprocessing
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
from app.storage.store import JsonStore
|
||||
|
||||
|
||||
def _hold_record_lock(data_dir: str, key: str, ready, release) -> None:
|
||||
from app.storage.store import JsonStore
|
||||
|
||||
store = JsonStore(Path(data_dir))
|
||||
with store.record_lock(key):
|
||||
ready.set()
|
||||
release.wait(timeout=10)
|
||||
|
||||
|
||||
def _mutate_record(data_dir: str, key: str, operation: str, done, results) -> None:
|
||||
from app.storage.store import JsonStore
|
||||
|
||||
store = JsonStore(Path(data_dir))
|
||||
try:
|
||||
if operation == "create":
|
||||
store.create({"id": key, "value": "created"}, key=key)
|
||||
elif operation == "replace":
|
||||
store.replace(key, {"id": key, "value": "replaced"})
|
||||
else:
|
||||
store.delete(key)
|
||||
except BaseException as exc: # pragma: no cover - child diagnostic
|
||||
results.put((operation, "error", type(exc).__name__))
|
||||
else:
|
||||
results.put((operation, "ok"))
|
||||
finally:
|
||||
done.set()
|
||||
|
||||
|
||||
def _read_record_while_paused(data_dir: str, key: str, ready, release, results) -> None:
|
||||
import app.storage.store as store_module
|
||||
|
||||
original_read_json = store_module._read_json
|
||||
|
||||
def paused_read(path):
|
||||
ready.set()
|
||||
release.wait(timeout=10)
|
||||
return original_read_json(path)
|
||||
|
||||
store_module._read_json = paused_read
|
||||
try:
|
||||
value = JsonStore(Path(data_dir)).get(key)
|
||||
except BaseException as exc: # pragma: no cover - child diagnostic
|
||||
results.put(("read", "error", type(exc).__name__))
|
||||
else:
|
||||
results.put(("read", "ok", value["value"]))
|
||||
|
||||
|
||||
def _check_worker(data_dir: str, barrier, results) -> None:
|
||||
os.environ["DATA_DIR"] = data_dir
|
||||
from app.config import Config
|
||||
|
||||
Config.DATA_DIR = Path(data_dir)
|
||||
from app.services.rate_limit import check
|
||||
|
||||
barrier.wait()
|
||||
results.put(check("login:user", "same-user", limit=5, window=300))
|
||||
|
||||
|
||||
def test_rate_limit_is_atomic_across_worker_processes(tmp_path, monkeypatch):
|
||||
monkeypatch.setenv("DATA_DIR", str(tmp_path))
|
||||
context = multiprocessing.get_context("spawn")
|
||||
worker_count = 6
|
||||
barrier = context.Barrier(worker_count)
|
||||
results = context.Queue()
|
||||
workers = [
|
||||
context.Process(
|
||||
target=_check_worker,
|
||||
args=(str(tmp_path), barrier, results),
|
||||
)
|
||||
for _ in range(worker_count)
|
||||
]
|
||||
|
||||
for worker in workers:
|
||||
worker.start()
|
||||
for worker in workers:
|
||||
worker.join(timeout=30)
|
||||
assert not worker.is_alive()
|
||||
assert worker.exitcode == 0
|
||||
|
||||
observed = sorted(results.get(timeout=5) for _ in workers)
|
||||
assert observed == [False, True, True, True, True, True]
|
||||
|
||||
|
||||
def test_rate_limit_fails_closed_on_malformed_persisted_state(tmp_path, monkeypatch):
|
||||
monkeypatch.setattr("app.config.Config.DATA_DIR", tmp_path)
|
||||
|
||||
from app.services import rate_limit
|
||||
|
||||
store = rate_limit._record_store()
|
||||
record_id = rate_limit._record_id(rate_limit._key("login:user", "corrupt-user"))
|
||||
store.create({"id": record_id, "timestamps": "not-a-list"}, key=record_id)
|
||||
|
||||
assert rate_limit.check("login:user", "corrupt-user", limit=5, window=300) is False
|
||||
|
||||
store.update(record_id, timestamps=["not-a-number"])
|
||||
assert rate_limit.check("login:user", "corrupt-user", limit=5, window=300) is False
|
||||
|
||||
store._path(record_id).write_text("{", encoding="utf-8")
|
||||
assert rate_limit.check("login:user", "corrupt-user", limit=5, window=300) is False
|
||||
|
||||
store._path(record_id).write_text("[]", encoding="utf-8")
|
||||
assert rate_limit.check("login:user", "corrupt-user", limit=5, window=300) is False
|
||||
|
||||
|
||||
def test_jsonstore_mutations_honor_cross_process_record_lock(tmp_path):
|
||||
context = multiprocessing.get_context("spawn")
|
||||
|
||||
for operation in ("create", "replace", "delete"):
|
||||
root = tmp_path / operation
|
||||
store = JsonStore(root)
|
||||
key = f"item-{operation}"
|
||||
if operation != "create":
|
||||
store.create({"id": key, "value": "initial"}, key=key)
|
||||
|
||||
ready = context.Event()
|
||||
release = context.Event()
|
||||
done = context.Event()
|
||||
results = context.Queue()
|
||||
holder = context.Process(
|
||||
target=_hold_record_lock,
|
||||
args=(str(root), key, ready, release),
|
||||
)
|
||||
mutator = context.Process(
|
||||
target=_mutate_record,
|
||||
args=(str(root), key, operation, done, results),
|
||||
)
|
||||
holder.start()
|
||||
assert ready.wait(timeout=5)
|
||||
mutator.start()
|
||||
assert not done.wait(timeout=0.5)
|
||||
release.set()
|
||||
holder.join(timeout=5)
|
||||
mutator.join(timeout=5)
|
||||
assert holder.exitcode == 0
|
||||
assert mutator.exitcode == 0
|
||||
assert results.get(timeout=5) == (operation, "ok")
|
||||
|
||||
|
||||
def test_jsonstore_reader_holds_cross_process_lock_against_delete(tmp_path):
|
||||
context = multiprocessing.get_context("spawn")
|
||||
root = tmp_path / "reader-delete"
|
||||
store = JsonStore(root)
|
||||
key = "item-reader-delete"
|
||||
store.create({"id": key, "value": "initial"}, key=key)
|
||||
ready = context.Event()
|
||||
release = context.Event()
|
||||
done = context.Event()
|
||||
results = context.Queue()
|
||||
reader = context.Process(
|
||||
target=_read_record_while_paused,
|
||||
args=(str(root), key, ready, release, results),
|
||||
)
|
||||
deleter = context.Process(
|
||||
target=_mutate_record,
|
||||
args=(str(root), key, "delete", done, results),
|
||||
)
|
||||
reader.start()
|
||||
assert ready.wait(timeout=5)
|
||||
deleter.start()
|
||||
assert not done.wait(timeout=0.5)
|
||||
release.set()
|
||||
reader.join(timeout=5)
|
||||
deleter.join(timeout=5)
|
||||
|
||||
assert reader.exitcode == 0
|
||||
assert deleter.exitcode == 0
|
||||
assert sorted(results.get(timeout=5) for _ in range(2)) == [
|
||||
("delete", "ok"),
|
||||
("read", "ok", "initial"),
|
||||
]
|
||||
@@ -0,0 +1,20 @@
|
||||
"""Production readiness endpoint contract."""
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
def test_health_and_ready_are_distinct(client):
|
||||
health = client.get("/health")
|
||||
ready = client.get("/ready")
|
||||
|
||||
assert health.status_code == 200
|
||||
assert health.get_json() == {"status": "ok", "service": "Sales Trainer"}
|
||||
assert ready.status_code == 200
|
||||
assert ready.get_json() == {"status": "ready", "checks": {"stores": "ok", "config": "ok"}}
|
||||
|
||||
|
||||
def test_ready_never_exposes_configuration_values(app):
|
||||
app.extensions["user_store"] = None
|
||||
response = app.test_client().get("/ready")
|
||||
|
||||
assert response.status_code == 503
|
||||
assert response.get_json() == {"status": "not_ready", "checks": {"stores": "failed", "config": "ok"}}
|
||||
@@ -0,0 +1,141 @@
|
||||
"""Contract tests for the S4.3 organization/user repository packet."""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.db import Base, create_db_engine
|
||||
from app.repositories import (
|
||||
EntityNotFoundError,
|
||||
InvalidRepositoryField,
|
||||
SqlAlchemyOrganizationRepository,
|
||||
SqlAlchemyUserRepository,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def repository_session(tmp_path: Path):
|
||||
engine = create_db_engine(f"sqlite:///{tmp_path / 'repositories.db'}")
|
||||
Base.metadata.create_all(engine)
|
||||
with Session(engine) as session:
|
||||
yield session
|
||||
|
||||
|
||||
def test_organization_repository_scopes_reads_and_updates(repository_session: Session):
|
||||
repository = SqlAlchemyOrganizationRepository(repository_session)
|
||||
|
||||
created = repository.create(organization_id="org-a", name="Alpha")
|
||||
repository.create(organization_id="org-b", name="Beta", active=False)
|
||||
repository_session.commit()
|
||||
|
||||
assert repository.get("org-a") is created
|
||||
assert [org.id for org in repository.list()] == ["org-a", "org-b"]
|
||||
assert [org.id for org in repository.list(active_only=True)] == ["org-a"]
|
||||
|
||||
updated = repository.update("org-a", name="Alpha Updated", seats=12)
|
||||
repository_session.commit()
|
||||
assert updated.name == "Alpha Updated"
|
||||
assert updated.seats == 12
|
||||
|
||||
with pytest.raises(EntityNotFoundError):
|
||||
repository.update("missing", name="nope")
|
||||
with pytest.raises(InvalidRepositoryField):
|
||||
repository.update("org-a", id="cannot-change")
|
||||
|
||||
|
||||
def test_user_repository_preserves_auth_fields_and_tenant_scope(repository_session: Session):
|
||||
organizations = SqlAlchemyOrganizationRepository(repository_session)
|
||||
users = SqlAlchemyUserRepository(repository_session)
|
||||
organizations.create(organization_id="org-a", name="Alpha")
|
||||
organizations.create(organization_id="org-b", name="Beta")
|
||||
repository_session.flush()
|
||||
|
||||
accepted_at = datetime(2026, 8, 15, tzinfo=timezone.utc)
|
||||
user = users.create(
|
||||
user_id="user-a",
|
||||
org_id="org-a",
|
||||
username="alice",
|
||||
password_hash="hash-a",
|
||||
name="Alice",
|
||||
role="user",
|
||||
email="[email protected]",
|
||||
accepted_terms=True,
|
||||
accepted_terms_at=accepted_at,
|
||||
auth_version=4,
|
||||
)
|
||||
users.create(
|
||||
user_id="user-b",
|
||||
org_id="org-b",
|
||||
username="bob",
|
||||
password_hash="hash-b",
|
||||
name="Bob",
|
||||
role="user",
|
||||
)
|
||||
repository_session.commit()
|
||||
|
||||
assert users.get("user-a", org_id="org-a") is user
|
||||
assert users.get("user-a", org_id="org-b") is None
|
||||
assert users.get_by_username("alice", org_id="org-a") is user
|
||||
assert users.get_by_username("alice", org_id="org-b") is None
|
||||
assert users.get_by_email("[email protected]", org_id="org-a") is user
|
||||
assert users.get_by_email("[email protected]", org_id="org-b") is None
|
||||
assert [item.id for item in users.list_for_org("org-a")] == ["user-a"]
|
||||
assert user.email == "[email protected]"
|
||||
assert user.name == "Alice"
|
||||
assert user.accepted_terms is True
|
||||
assert user.accepted_terms_at is not None
|
||||
assert user.accepted_terms_at.replace(tzinfo=timezone.utc) == accepted_at
|
||||
assert user.auth_version == 4
|
||||
|
||||
users.update("user-a", org_id="org-a", auth_version=5, email=None, name="Alice Updated")
|
||||
repository_session.commit()
|
||||
refreshed = users.get("user-a", org_id="org-a")
|
||||
assert refreshed is not None
|
||||
assert refreshed.auth_version == 5
|
||||
assert refreshed.email is None
|
||||
assert refreshed.name == "Alice Updated"
|
||||
|
||||
with pytest.raises(EntityNotFoundError):
|
||||
users.update("user-a", org_id="org-b", name="cross-tenant mutation")
|
||||
|
||||
with pytest.raises(EntityNotFoundError):
|
||||
users.update("missing", org_id="org-a", name="nope")
|
||||
|
||||
|
||||
def test_user_repository_rejects_invalid_updates_and_duplicate_email(repository_session: Session):
|
||||
organizations = SqlAlchemyOrganizationRepository(repository_session)
|
||||
users = SqlAlchemyUserRepository(repository_session)
|
||||
organizations.create(organization_id="org-a", name="Alpha")
|
||||
repository_session.flush()
|
||||
users.create(
|
||||
user_id="user-a",
|
||||
org_id="org-a",
|
||||
username="alice",
|
||||
password_hash="hash-a",
|
||||
name="Alice",
|
||||
role="user",
|
||||
email="[email protected]",
|
||||
)
|
||||
repository_session.commit()
|
||||
|
||||
with pytest.raises(EntityNotFoundError):
|
||||
users.update("user-a", org_id="org-b")
|
||||
with pytest.raises(InvalidRepositoryField):
|
||||
users.update("user-a", org_id="org-a", username="cannot-change")
|
||||
|
||||
with pytest.raises(IntegrityError):
|
||||
users.create(
|
||||
user_id="user-b",
|
||||
org_id="org-a",
|
||||
username="bob",
|
||||
password_hash="hash-b",
|
||||
name="Bob",
|
||||
role="user",
|
||||
email="[email protected]",
|
||||
)
|
||||
repository_session.flush()
|
||||
repository_session.rollback()
|
||||
@@ -0,0 +1,377 @@
|
||||
"""Regression tests for request-level authentication and tenant activity guards."""
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from app.api.helpers import ApiError, assert_tenant
|
||||
from app.auth.users import AuthError, UserStore
|
||||
from app.config import Config
|
||||
|
||||
|
||||
def auth_headers(token: str) -> dict[str, str]:
|
||||
return {"Authorization": f"Bearer {token}"}
|
||||
|
||||
|
||||
def test_must_setup_user_can_only_use_setup_safe_endpoints(client, user_store, login):
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="pending-user",
|
||||
password="pending-password",
|
||||
name="Pending User",
|
||||
role="user",
|
||||
must_setup=True,
|
||||
)
|
||||
token = login(user["username"], "pending-password")["token"]
|
||||
headers = auth_headers(token)
|
||||
|
||||
assert client.get("/api/auth/me", headers=headers).status_code == 200
|
||||
assert client.post(
|
||||
"/api/auth/setup",
|
||||
headers=headers,
|
||||
json={
|
||||
"email": "[email protected]",
|
||||
"password": "pending-new-password",
|
||||
"accepted_terms": True,
|
||||
},
|
||||
).status_code == 200
|
||||
|
||||
|
||||
def test_must_setup_user_cannot_use_business_routes_before_setup(
|
||||
client, user_store, login
|
||||
):
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="pending-business",
|
||||
password="pending-password",
|
||||
name="Pending Business",
|
||||
role="user",
|
||||
must_setup=True,
|
||||
)
|
||||
token = login(user["username"], "pending-password")["token"]
|
||||
headers = auth_headers(token)
|
||||
|
||||
response = client.get("/api/groups", headers=headers)
|
||||
|
||||
assert response.status_code == 403, response.get_json()
|
||||
|
||||
|
||||
def test_must_setup_user_cannot_use_admin_chat_or_analytics(
|
||||
client, user_store, login
|
||||
):
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="pending-all-routes",
|
||||
password="pending-password",
|
||||
name="Pending User",
|
||||
role="user",
|
||||
must_setup=True,
|
||||
)
|
||||
token = login(user["username"], "pending-password")["token"]
|
||||
headers = auth_headers(token)
|
||||
|
||||
responses = [
|
||||
client.post(
|
||||
"/api/admin/users",
|
||||
json={"username": "should-not-create", "password": "password"},
|
||||
headers=headers,
|
||||
),
|
||||
client.get("/api/analytics", headers=headers),
|
||||
client.post(
|
||||
"/api/chat/missing-group/personas/missing-persona/chat/start",
|
||||
json={"scenario": "social"},
|
||||
headers=headers,
|
||||
),
|
||||
]
|
||||
|
||||
assert [response.status_code for response in responses] == [403, 403, 403]
|
||||
|
||||
|
||||
def test_inactive_user_token_is_rejected(client, user_store, login):
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="inactive-user",
|
||||
password="inactive-password",
|
||||
name="Inactive User",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
token = login(user["username"], "inactive-password")["token"]
|
||||
user_store.set_active(user["username"], False)
|
||||
|
||||
response = client.get("/api/auth/me", headers=auth_headers(token))
|
||||
|
||||
assert response.status_code == 401, response.get_json()
|
||||
|
||||
|
||||
def test_inactive_org_rejects_existing_token(client, user_store, login):
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="inactive-org-user",
|
||||
password="inactive-org-password",
|
||||
name="Inactive Org User",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
token = login(user["username"], "inactive-org-password")["token"]
|
||||
user_store.orgs.update("org-default", active=False)
|
||||
|
||||
response = client.get("/api/auth/me", headers=auth_headers(token))
|
||||
|
||||
assert response.status_code == 401, response.get_json()
|
||||
|
||||
|
||||
def test_missing_org_rejects_token(client, user_store, login):
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="missing-org-user",
|
||||
password="missing-org-password",
|
||||
name="Missing Org User",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
token = login(user["username"], "missing-org-password")["token"]
|
||||
user_store.orgs.delete("org-default")
|
||||
|
||||
response = client.get("/api/auth/me", headers=auth_headers(token))
|
||||
|
||||
assert response.status_code == 401, response.get_json()
|
||||
|
||||
|
||||
def test_token_org_claim_mismatch_is_rejected(client, user_store, login):
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="mismatched-org-user",
|
||||
password="mismatched-org-password",
|
||||
name="Mismatched Org User",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
token = login(user["username"], "mismatched-org-password")["token"]
|
||||
payload = user_store.decode_token(token)
|
||||
payload["org_id"] = "org-other"
|
||||
forged_token = __import__("jwt").encode(
|
||||
payload, Config.SECRET_KEY, algorithm=Config.JWT_ALGO
|
||||
)
|
||||
|
||||
response = client.get("/api/auth/me", headers=auth_headers(forged_token))
|
||||
|
||||
assert response.status_code == 401, response.get_json()
|
||||
|
||||
|
||||
def test_malformed_jwt_claim_types_are_rejected(client, user_store, login):
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="malformed-claim-user",
|
||||
password="malformed-claim-password",
|
||||
name="Malformed Claim User",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
token = login(user["username"], "malformed-claim-password")["token"]
|
||||
payload = user_store.decode_token(token)
|
||||
jwt = __import__("jwt")
|
||||
|
||||
for claim, value in (("sub", 123), ("org_id", 123)):
|
||||
malformed = dict(payload)
|
||||
malformed[claim] = value
|
||||
forged_token = jwt.encode(
|
||||
malformed, Config.SECRET_KEY, algorithm=Config.JWT_ALGO
|
||||
)
|
||||
response = client.get(
|
||||
"/api/auth/me", headers=auth_headers(forged_token)
|
||||
)
|
||||
assert response.status_code == 401, response.get_json()
|
||||
|
||||
|
||||
def test_malformed_jwt_subject_and_org_claims_do_not_500(client, user_store, login):
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="malformed-claim-input-user",
|
||||
password="malformed-claim-input-password",
|
||||
name="Malformed Claim Input User",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
token = login(user["username"], "malformed-claim-input-password")["token"]
|
||||
payload = user_store.decode_token(token)
|
||||
jwt = __import__("jwt")
|
||||
|
||||
for claim, value in (("sub", {"nested": True}), ("org_id", ["org-default"])):
|
||||
malformed = dict(payload)
|
||||
malformed[claim] = value
|
||||
forged_token = jwt.encode(
|
||||
malformed, Config.SECRET_KEY, algorithm=Config.JWT_ALGO
|
||||
)
|
||||
response = client.get(
|
||||
"/api/auth/me", headers=auth_headers(forged_token)
|
||||
)
|
||||
assert response.status_code == 401, response.get_json()
|
||||
|
||||
|
||||
def test_malformed_stored_org_id_is_rejected_without_500(client, user_store, login):
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="malformed-stored-org-user",
|
||||
password="malformed-stored-org-password",
|
||||
name="Malformed Stored Org User",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
token = login(user["username"], "malformed-stored-org-password")["token"]
|
||||
user_store.users.update(user["username"], org_id=123)
|
||||
|
||||
response = client.get(
|
||||
"/api/auth/me", headers=auth_headers(token)
|
||||
)
|
||||
|
||||
assert response.status_code == 401, response.get_json()
|
||||
|
||||
|
||||
def test_login_rejects_user_when_organization_becomes_inactive(
|
||||
client, user_store
|
||||
):
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="inactive-org-login",
|
||||
password="inactive-org-login-password",
|
||||
name="Inactive Org Login",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
user_store.orgs.update("org-default", active=False)
|
||||
|
||||
response = client.post(
|
||||
"/api/auth/login",
|
||||
json={
|
||||
"username": user["username"],
|
||||
"password": "inactive-org-login-password",
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == 401, response.get_json()
|
||||
|
||||
|
||||
def test_non_boolean_active_state_is_fail_closed_for_user_and_org(
|
||||
client, user_store, login
|
||||
):
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="legacy-active-shape",
|
||||
password="legacy-active-password",
|
||||
name="Legacy Active Shape",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
token = login(user["username"], "legacy-active-password")["token"]
|
||||
|
||||
user_store.users.update(user["username"], active="false")
|
||||
user_response = client.get("/api/auth/me", headers=auth_headers(token))
|
||||
assert user_response.status_code == 401, user_response.get_json()
|
||||
|
||||
user_store.users.update(user["username"], active=True)
|
||||
user_store.orgs.update("org-default", active="false")
|
||||
org_response = client.get("/api/auth/me", headers=auth_headers(token))
|
||||
assert org_response.status_code == 401, org_response.get_json()
|
||||
|
||||
|
||||
def test_malformed_must_setup_state_cannot_enter_setup_flow(
|
||||
client, user_store, login
|
||||
):
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="malformed-must-setup",
|
||||
password="malformed-must-setup-password",
|
||||
name="Malformed Must Setup",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
token = login(user["username"], "malformed-must-setup-password")["token"]
|
||||
user_store.users.update(user["username"], must_setup="false")
|
||||
|
||||
response = client.get(
|
||||
"/api/auth/me",
|
||||
headers=auth_headers(token),
|
||||
)
|
||||
|
||||
assert response.status_code == 401, response.get_json()
|
||||
assert user_store.get_user(user["username"])["must_setup"] == "false"
|
||||
|
||||
|
||||
def test_malformed_must_setup_state_cannot_login(client, user_store):
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="malformed-must-setup-login",
|
||||
password="malformed-must-setup-login-password",
|
||||
name="Malformed Must Setup Login",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
user_store.users.update(user["username"], must_setup="true")
|
||||
|
||||
response = client.post(
|
||||
"/api/auth/login",
|
||||
json={
|
||||
"username": user["username"],
|
||||
"password": "malformed-must-setup-login-password",
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == 401, response.get_json()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("bad_version", ["corrupt", True, -1, 1.5])
|
||||
def test_malformed_auth_version_cannot_be_repaired_into_token_reuse(
|
||||
client, user_store, login, bad_version
|
||||
):
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username=f"bad-auth-version-{str(bad_version).replace('.', '-')}",
|
||||
password="bad-auth-version-password",
|
||||
name="Bad Auth Version",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
token = login(user["username"], "bad-auth-version-password")["token"]
|
||||
user_store.users.update(user["username"], auth_version=bad_version)
|
||||
|
||||
response = client.get("/api/auth/me", headers=auth_headers(token))
|
||||
assert response.status_code == 401, response.get_json()
|
||||
with pytest.raises(AuthError):
|
||||
user_store.set_password(user["username"], "replacement-password")
|
||||
assert user_store.users.get(user["username"])["auth_version"] == bad_version
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("field", "value"),
|
||||
[("password_hash", "not-a-password-hash"), ("role", "unknown-role"), ("org_id", 123)],
|
||||
)
|
||||
def test_malformed_login_record_fails_as_invalid_credentials(
|
||||
client, user_store, field, value
|
||||
):
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username=f"malformed-login-{field}",
|
||||
password="malformed-login-password",
|
||||
name="Malformed Login",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
user_store.users.update(user["username"], **{field: value})
|
||||
|
||||
response = client.post(
|
||||
"/api/auth/login",
|
||||
json={"username": user["username"], "password": "malformed-login-password"},
|
||||
)
|
||||
|
||||
assert response.status_code == 401, response.get_json()
|
||||
|
||||
|
||||
def test_tenant_guard_rejects_missing_object_tenant(client):
|
||||
from flask import g
|
||||
|
||||
with client.application.test_request_context():
|
||||
g.user = {"role": "user", "org_id": "org-default"}
|
||||
g.org_id = "org-default"
|
||||
|
||||
with pytest.raises(ApiError, match="permission denied"):
|
||||
assert_tenant(None)
|
||||
@@ -0,0 +1,64 @@
|
||||
"""Concurrent one-shot session start tests."""
|
||||
from __future__ import annotations
|
||||
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from pathlib import Path
|
||||
from threading import Barrier
|
||||
|
||||
from app.services.sessions import SessionStore
|
||||
|
||||
|
||||
def test_synchronized_starts_share_one_active_session(tmp_path: Path):
|
||||
store = SessionStore(tmp_path)
|
||||
barrier = Barrier(8)
|
||||
|
||||
def start_one():
|
||||
barrier.wait(timeout=5)
|
||||
return store.start(
|
||||
org_id="org-1",
|
||||
user_id="user-1",
|
||||
group_id="group-a",
|
||||
persona_id="persona-01",
|
||||
persona_name="Customer",
|
||||
)
|
||||
|
||||
with ThreadPoolExecutor(max_workers=8) as pool:
|
||||
results = list(pool.map(lambda _: start_one(), range(8)))
|
||||
|
||||
sessions = [session for session, _ in results]
|
||||
assert len({session["id"] for session in sessions}) == 1
|
||||
assert sum(1 for _, resumed in results if resumed is False) == 1
|
||||
assert sum(1 for _, resumed in results if resumed is True) == 7
|
||||
|
||||
|
||||
def test_finished_scope_remains_blocked_after_concurrent_start(tmp_path: Path):
|
||||
store = SessionStore(tmp_path)
|
||||
session, _ = store.start(
|
||||
org_id="org-1",
|
||||
user_id="user-1",
|
||||
group_id="group-a",
|
||||
persona_id="persona-01",
|
||||
persona_name="Customer",
|
||||
)
|
||||
store.update(session["id"], status="finished", outcome="won")
|
||||
|
||||
barrier = Barrier(8)
|
||||
|
||||
def try_start():
|
||||
barrier.wait(timeout=5)
|
||||
try:
|
||||
store.start(
|
||||
org_id="org-1",
|
||||
user_id="user-1",
|
||||
group_id="group-a",
|
||||
persona_id="persona-01",
|
||||
persona_name="Customer",
|
||||
)
|
||||
except ValueError as exc:
|
||||
return str(exc)
|
||||
return "unexpected-success"
|
||||
|
||||
with ThreadPoolExecutor(max_workers=8) as pool:
|
||||
results = list(pool.map(lambda _: try_start(), range(8)))
|
||||
|
||||
assert results == ["you have already trained on this persona (one-shot)"] * 8
|
||||
@@ -0,0 +1,139 @@
|
||||
"""Canonical session identity and one-shot scope regression tests."""
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from app.services.sessions import SessionStore
|
||||
|
||||
|
||||
def _start(store: SessionStore, *, org: str, user: str, group: str, persona: str):
|
||||
return store.start(
|
||||
org_id=org,
|
||||
user_id=user,
|
||||
group_id=group,
|
||||
persona_id=persona,
|
||||
persona_name=f"Persona {persona}",
|
||||
)
|
||||
|
||||
|
||||
def test_same_user_and_persona_in_different_groups_are_independent(tmp_path: Path):
|
||||
store = SessionStore(tmp_path)
|
||||
|
||||
first, resumed_first = _start(
|
||||
store, org="org-1", user="user-1", group="group-a", persona="persona-01"
|
||||
)
|
||||
store.update(first["id"], status="finished", outcome="won")
|
||||
|
||||
second, resumed_second = _start(
|
||||
store, org="org-1", user="user-1", group="group-b", persona="persona-01"
|
||||
)
|
||||
|
||||
assert resumed_first is False
|
||||
assert resumed_second is False
|
||||
assert second["id"] != first["id"]
|
||||
assert second["group_id"] == "group-b"
|
||||
|
||||
|
||||
def test_finished_session_blocks_only_the_same_scope(tmp_path: Path):
|
||||
store = SessionStore(tmp_path)
|
||||
session, _ = _start(
|
||||
store, org="org-1", user="user-1", group="group-a", persona="persona-01"
|
||||
)
|
||||
store.update(session["id"], status="finished", outcome="lost")
|
||||
|
||||
with pytest.raises(ValueError, match="already trained"):
|
||||
_start(store, org="org-1", user="user-1", group="group-a", persona="persona-01")
|
||||
|
||||
|
||||
def test_active_session_resumes_only_for_same_org_user_group_and_persona(tmp_path: Path):
|
||||
store = SessionStore(tmp_path)
|
||||
original, resumed = _start(
|
||||
store, org="org-1", user="user-1", group="group-a", persona="persona-01"
|
||||
)
|
||||
assert resumed is False
|
||||
|
||||
same, same_resumed = _start(
|
||||
store, org="org-1", user="user-1", group="group-a", persona="persona-01"
|
||||
)
|
||||
different_group, group_resumed = _start(
|
||||
store, org="org-1", user="user-1", group="group-b", persona="persona-01"
|
||||
)
|
||||
different_org, org_resumed = _start(
|
||||
store, org="org-2", user="user-1", group="group-a", persona="persona-01"
|
||||
)
|
||||
different_user, user_resumed = _start(
|
||||
store, org="org-1", user="user-2", group="group-a", persona="persona-01"
|
||||
)
|
||||
|
||||
assert same_resumed is True
|
||||
assert same["id"] == original["id"]
|
||||
assert group_resumed is False
|
||||
assert org_resumed is False
|
||||
assert user_resumed is False
|
||||
assert len({different_group["id"], different_org["id"], different_user["id"]}) == 3
|
||||
|
||||
|
||||
def test_list_for_user_is_org_scoped_and_preview_is_opt_in(tmp_path: Path):
|
||||
store = SessionStore(tmp_path)
|
||||
_start(store, org="org-1", user="user-1", group="group-a", persona="persona-01")
|
||||
preview, _ = store.start(
|
||||
org_id="org-1",
|
||||
user_id="user-1",
|
||||
group_id="group-a",
|
||||
persona_id="persona-02",
|
||||
persona_name="Preview",
|
||||
mode="preview",
|
||||
)
|
||||
other_org, _ = _start(
|
||||
store, org="org-2", user="user-1", group="group-b", persona="persona-01"
|
||||
)
|
||||
|
||||
visible = store.list_for_user("user-1", org_id="org-1")
|
||||
assert {row["id"] for row in visible} != {other_org["id"]}
|
||||
assert preview["id"] not in {row["id"] for row in visible}
|
||||
assert preview["mode"] == "preview"
|
||||
assert preview["org_id"] == "org-1"
|
||||
assert len(store.list_for_user("user-1", org_id="org-1", include_preview=True)) == 2
|
||||
|
||||
|
||||
def test_start_publishes_seeded_session_state_atomically(tmp_path: Path):
|
||||
store = SessionStore(tmp_path)
|
||||
session, resumed = store.start(
|
||||
org_id="org-1",
|
||||
user_id="user-1",
|
||||
group_id="group-a",
|
||||
persona_id="persona-01",
|
||||
persona_name="Customer",
|
||||
persona_meta={"scenario": "f2f_call", "locale": "th"},
|
||||
scenario="f2f_call",
|
||||
locale="th",
|
||||
messages=[{"role": "customer", "text": "สวัสดีค่ะ"}],
|
||||
internal={"turns": 0, "score": 50, "signals": []},
|
||||
)
|
||||
|
||||
assert resumed is False
|
||||
assert session["status"] == "active"
|
||||
assert session["scenario"] == "f2f_call"
|
||||
assert session["messages"] == [{"role": "customer", "text": "สวัสดีค่ะ"}]
|
||||
assert session["internal"]["turns"] == 0
|
||||
|
||||
|
||||
def test_scope_scans_ignore_non_mapping_rows(tmp_path: Path, monkeypatch):
|
||||
store = SessionStore(tmp_path)
|
||||
monkeypatch.setattr(store.sessions, "where", lambda _predicate: [object()])
|
||||
|
||||
assert store.active_for_scope(
|
||||
org_id="org-1",
|
||||
user_id="user-1",
|
||||
group_id="group-a",
|
||||
persona_id="persona-01",
|
||||
) is None
|
||||
assert store.latest_for_scope(
|
||||
org_id="org-1",
|
||||
user_id="user-1",
|
||||
group_id="group-a",
|
||||
persona_id="persona-01",
|
||||
) is None
|
||||
assert store.list_for_user("user-1", org_id="org-1") == []
|
||||
@@ -0,0 +1,165 @@
|
||||
"""Contract tests for the S4.3 session/message repository packet."""
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.db import Base, create_db_engine
|
||||
from app.repositories import (
|
||||
EntityNotFoundError,
|
||||
SqlAlchemyGroupRepository,
|
||||
SqlAlchemyOrganizationRepository,
|
||||
SqlAlchemySessionRepository,
|
||||
SqlAlchemyUserRepository,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def repository_session(tmp_path: Path):
|
||||
engine = create_db_engine(f"sqlite:///{tmp_path / 'sessions.db'}")
|
||||
Base.metadata.create_all(engine)
|
||||
with Session(engine) as session:
|
||||
yield session
|
||||
|
||||
|
||||
def _seed_training_context(repository_session: Session):
|
||||
organizations = SqlAlchemyOrganizationRepository(repository_session)
|
||||
users = SqlAlchemyUserRepository(repository_session)
|
||||
groups = SqlAlchemyGroupRepository(repository_session)
|
||||
organizations.create(organization_id="org-a", name="Alpha")
|
||||
organizations.create(organization_id="org-b", name="Beta")
|
||||
users.create(
|
||||
user_id="user-a",
|
||||
org_id="org-a",
|
||||
username="alice",
|
||||
password_hash="hash-a",
|
||||
name="Alice",
|
||||
role="user",
|
||||
)
|
||||
groups.create(group_id="group-a", org_id="org-a", name="Alpha Group")
|
||||
groups.create(group_id="group-b", org_id="org-b", name="Beta Group")
|
||||
groups.create_persona(
|
||||
persona_id="persona-a",
|
||||
group_id="group-a",
|
||||
tier="base",
|
||||
public_json={"name": "Customer A"},
|
||||
)
|
||||
repository_session.flush()
|
||||
|
||||
|
||||
def test_session_repository_scopes_session_and_messages(repository_session: Session):
|
||||
_seed_training_context(repository_session)
|
||||
sessions = SqlAlchemySessionRepository(repository_session)
|
||||
|
||||
training = sessions.create(
|
||||
session_id="session-a",
|
||||
org_id="org-a",
|
||||
user_id="user-a",
|
||||
group_id="group-a",
|
||||
persona_id="persona-a",
|
||||
scenario_json={"channel": "social"},
|
||||
)
|
||||
sessions.append_message(
|
||||
session_id="session-a",
|
||||
org_id="org-a",
|
||||
message_id="message-a",
|
||||
sequence=1,
|
||||
role="customer",
|
||||
text="Hello",
|
||||
)
|
||||
sessions.append_message(
|
||||
session_id="session-a",
|
||||
org_id="org-a",
|
||||
message_id="message-b",
|
||||
sequence=2,
|
||||
role="trainee",
|
||||
text="Hi",
|
||||
)
|
||||
repository_session.commit()
|
||||
|
||||
assert sessions.get("session-a", org_id="org-a") is training
|
||||
assert sessions.get("session-a", org_id="org-b") is None
|
||||
assert [item.id for item in sessions.list_for_user("org-a", "user-a")] == ["session-a"]
|
||||
assert [item.sequence for item in sessions.list_messages("session-a", org_id="org-a")] == [1, 2]
|
||||
|
||||
updated = sessions.update(
|
||||
"session-a",
|
||||
org_id="org-a",
|
||||
status="completed",
|
||||
outcome="won",
|
||||
debrief_json={"score": 90},
|
||||
)
|
||||
repository_session.commit()
|
||||
assert updated.status == "completed"
|
||||
assert updated.outcome == "won"
|
||||
|
||||
with pytest.raises(EntityNotFoundError):
|
||||
sessions.update("session-a", org_id="org-b", status="completed")
|
||||
with pytest.raises(EntityNotFoundError):
|
||||
sessions.append_message(
|
||||
session_id="session-a",
|
||||
org_id="org-b",
|
||||
message_id="message-cross",
|
||||
sequence=3,
|
||||
role="trainee",
|
||||
text="blocked",
|
||||
)
|
||||
|
||||
|
||||
def test_session_repository_preserves_one_shot_and_message_constraints(repository_session: Session):
|
||||
_seed_training_context(repository_session)
|
||||
sessions = SqlAlchemySessionRepository(repository_session)
|
||||
sessions.create(
|
||||
session_id="session-a",
|
||||
org_id="org-a",
|
||||
user_id="user-a",
|
||||
group_id="group-a",
|
||||
persona_id="persona-a",
|
||||
mode="trainee",
|
||||
)
|
||||
repository_session.commit()
|
||||
|
||||
with pytest.raises(IntegrityError):
|
||||
sessions.create(
|
||||
session_id="session-b",
|
||||
org_id="org-a",
|
||||
user_id="user-a",
|
||||
group_id="group-a",
|
||||
persona_id="persona-a",
|
||||
mode="trainee",
|
||||
)
|
||||
repository_session.flush()
|
||||
repository_session.rollback()
|
||||
|
||||
sessions.create(
|
||||
session_id="preview-b",
|
||||
org_id="org-a",
|
||||
user_id="user-a",
|
||||
group_id="group-a",
|
||||
persona_id="persona-a",
|
||||
mode="preview",
|
||||
)
|
||||
repository_session.commit()
|
||||
|
||||
sessions.append_message(
|
||||
session_id="preview-b",
|
||||
org_id="org-a",
|
||||
message_id="message-a",
|
||||
sequence=1,
|
||||
role="customer",
|
||||
text="Hello",
|
||||
)
|
||||
with pytest.raises(IntegrityError):
|
||||
sessions.append_message(
|
||||
session_id="preview-b",
|
||||
org_id="org-a",
|
||||
message_id="message-b",
|
||||
sequence=1,
|
||||
role="trainee",
|
||||
text="duplicate sequence",
|
||||
)
|
||||
repository_session.flush()
|
||||
repository_session.rollback()
|
||||
@@ -0,0 +1,128 @@
|
||||
"""Sprint 3.6 signed export contract tests."""
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
def _headers(token: str) -> dict[str, str]:
|
||||
return {"Authorization": f"Bearer {token}"}
|
||||
|
||||
|
||||
def _setup_admin(user_store) -> None:
|
||||
user_store.complete_setup(
|
||||
"admin",
|
||||
"[email protected]",
|
||||
"admin-ready-password",
|
||||
accepted_terms=True,
|
||||
accepted_terms_at="2026-08-15T00:00:00Z",
|
||||
)
|
||||
|
||||
|
||||
def _issue_export_link(client, token: str) -> dict:
|
||||
response = client.get("/api/analytics/export/token", headers=_headers(token))
|
||||
assert response.status_code == 200, response.get_json()
|
||||
return response.get_json()
|
||||
|
||||
|
||||
def test_signed_export_works_without_bearer_and_replay_is_denied(
|
||||
client, user_store, login
|
||||
):
|
||||
_setup_admin(user_store)
|
||||
bearer = login("admin", "admin-ready-password")["token"]
|
||||
link = _issue_export_link(client, bearer)
|
||||
|
||||
first = client.get(link["url"])
|
||||
replay = client.get(link["url"])
|
||||
|
||||
assert first.status_code == 200, first.get_json()
|
||||
assert first.headers["Content-Type"].startswith("text/csv")
|
||||
assert replay.status_code == 403
|
||||
|
||||
from app.api import analytics_routes
|
||||
|
||||
jti = link["token"].split(".", 1)[1].rsplit(":", 1)[-1]
|
||||
record = analytics_routes._export_token_store().get(jti)
|
||||
assert record["used"] is True
|
||||
|
||||
|
||||
def test_signed_export_rejects_expired_and_tampered_links(
|
||||
client, user_store, login, monkeypatch
|
||||
):
|
||||
_setup_admin(user_store)
|
||||
bearer = login("admin", "admin-ready-password")["token"]
|
||||
|
||||
from app.api import analytics_routes
|
||||
|
||||
monkeypatch.setattr(analytics_routes, "_EXPORT_TTL", -1)
|
||||
expired = _issue_export_link(client, bearer)
|
||||
assert client.get(expired["url"]).status_code == 403
|
||||
|
||||
monkeypatch.setattr(analytics_routes, "_EXPORT_TTL", 300)
|
||||
fresh = _issue_export_link(client, bearer)
|
||||
tampered_token = fresh["token"][:-1] + ("a" if fresh["token"][-1] != "a" else "b")
|
||||
tampered = client.get(f"/api/analytics/export?token={tampered_token}")
|
||||
|
||||
assert tampered.status_code == 403
|
||||
|
||||
|
||||
def test_signed_export_rejects_wrong_current_bearer_tenant(
|
||||
client, user_store, login
|
||||
):
|
||||
_setup_admin(user_store)
|
||||
admin_bearer = login("admin", "admin-ready-password")["token"]
|
||||
link = _issue_export_link(client, admin_bearer)
|
||||
|
||||
other_org = user_store.create_org("Other tenant", org_id="org-other")
|
||||
other = user_store.create_user(
|
||||
org_id=other_org["id"],
|
||||
username="other-admin",
|
||||
password="other-admin-password",
|
||||
name="Other Admin",
|
||||
role="admin",
|
||||
email="[email protected]",
|
||||
must_setup=False,
|
||||
)
|
||||
other_bearer = login(other["username"], "other-admin-password")["token"]
|
||||
|
||||
response = client.get(link["url"], headers=_headers(other_bearer))
|
||||
|
||||
assert response.status_code == 403
|
||||
assert client.get(link["url"]).status_code == 200
|
||||
|
||||
|
||||
def test_signed_export_failure_does_not_burn_link(client, user_store, login, monkeypatch):
|
||||
_setup_admin(user_store)
|
||||
bearer = login("admin", "admin-ready-password")["token"]
|
||||
link = _issue_export_link(client, bearer)
|
||||
|
||||
from app.api import analytics_routes
|
||||
original_export = analytics_routes._export_csv_for_actor
|
||||
|
||||
def fail_export(*_args, **_kwargs):
|
||||
raise RuntimeError("simulated export failure")
|
||||
|
||||
monkeypatch.setattr(analytics_routes, "_export_csv_for_actor", fail_export)
|
||||
failed = client.get(link["url"])
|
||||
assert failed.status_code == 500
|
||||
|
||||
monkeypatch.setattr(analytics_routes, "_export_csv_for_actor", original_export)
|
||||
assert client.get(link["url"]).status_code == 200
|
||||
|
||||
|
||||
def test_normal_bearer_export_still_works(client, user_store, login):
|
||||
_setup_admin(user_store)
|
||||
bearer = login("admin", "admin-ready-password")["token"]
|
||||
|
||||
response = client.get("/api/analytics/export", headers=_headers(bearer))
|
||||
|
||||
assert response.status_code == 200, response.get_json()
|
||||
assert response.headers["Content-Type"].startswith("text/csv")
|
||||
|
||||
|
||||
def test_csv_cells_escape_spreadsheet_formula_prefixes():
|
||||
from app.api.analytics_routes import _csv_cell
|
||||
|
||||
assert _csv_cell("=SUM(A1:A2)") == "'=SUM(A1:A2)"
|
||||
assert _csv_cell("+1") == "'+1"
|
||||
assert _csv_cell("-1") == "'-1"
|
||||
assert _csv_cell("@user") == "'@user"
|
||||
assert _csv_cell("safe") == "safe"
|
||||
assert _csv_cell(None) == ""
|
||||
@@ -0,0 +1,893 @@
|
||||
"""Regression tests for the final Sprint 1 reviewer findings."""
|
||||
from __future__ import annotations
|
||||
|
||||
import io
|
||||
import multiprocessing as mp
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
import jwt
|
||||
|
||||
from app.api.chat_routes import _safe_judge_debrief, serialize_session
|
||||
from app.api.group_routes import serialize_group, strip_secret_fields
|
||||
from app.auth.users import AuthError, UserStore
|
||||
from app.config import Config
|
||||
from app.services.analyzer import Analyzer
|
||||
from app.services.file_parser import ParseError
|
||||
from app.services.groups import GroupStore
|
||||
from app.services.sessions import SessionStore
|
||||
from app.storage.store import JsonStore
|
||||
from scripts.mock_llm import MockLLM
|
||||
|
||||
|
||||
def _headers(token: str) -> dict[str, str]:
|
||||
return {"Authorization": f"Bearer {token}"}
|
||||
|
||||
|
||||
def _setup_super_admin(user_store, login) -> str:
|
||||
user_store.complete_setup(
|
||||
"admin",
|
||||
"[email protected]",
|
||||
"admin-ready-password",
|
||||
accepted_terms=True,
|
||||
accepted_terms_at="2026-08-13T00:00:00Z",
|
||||
)
|
||||
return login("admin", "admin-ready-password")["token"]
|
||||
|
||||
|
||||
def _update_org_in_process(
|
||||
data_dir: str,
|
||||
fields: dict[str, object],
|
||||
read_event,
|
||||
release_read,
|
||||
result_queue,
|
||||
) -> None:
|
||||
"""Pause after reading one org so cross-process read-modify-write races are deterministic."""
|
||||
import app.storage.store as store_module
|
||||
|
||||
original_read_json = store_module._read_json
|
||||
|
||||
def synchronized_read(path):
|
||||
value = original_read_json(path)
|
||||
if path.name == "org-1.json":
|
||||
read_event.set()
|
||||
release_read.wait(timeout=10)
|
||||
return value
|
||||
|
||||
store_module._read_json = synchronized_read
|
||||
try:
|
||||
JsonStore(Path(data_dir)).update("org-1", **fields)
|
||||
except BaseException as exc: # pragma: no cover - child diagnostic
|
||||
result_queue.put(("error", type(exc).__name__))
|
||||
else:
|
||||
result_queue.put(("ok", fields))
|
||||
|
||||
|
||||
def test_org_updates_preserve_concurrent_fields_across_processes(tmp_path):
|
||||
"""Organization PATCH read-modify-write updates must not lose another worker's field."""
|
||||
store = JsonStore(tmp_path / "orgs")
|
||||
store.create(
|
||||
{
|
||||
"id": "org-1",
|
||||
"name": "Organization One",
|
||||
"plan": "trial",
|
||||
"seats": 5,
|
||||
"active": True,
|
||||
},
|
||||
key="org-1",
|
||||
)
|
||||
|
||||
context = mp.get_context("fork")
|
||||
first_read = context.Event()
|
||||
release_first = context.Event()
|
||||
second_read = context.Event()
|
||||
release_second = context.Event()
|
||||
result_queue = context.Queue()
|
||||
first = context.Process(
|
||||
target=_update_org_in_process,
|
||||
args=(str(store.root), {"active": False}, first_read, release_first, result_queue),
|
||||
)
|
||||
second = context.Process(
|
||||
target=_update_org_in_process,
|
||||
args=(str(store.root), {"seats": 10}, second_read, release_second, result_queue),
|
||||
)
|
||||
|
||||
first.start()
|
||||
second.start()
|
||||
try:
|
||||
# Forked workers can take longer to import the Flask app under a full
|
||||
# suite on a loaded CI runner; keep the synchronization deterministic
|
||||
# without changing the race it is testing.
|
||||
assert first_read.wait(timeout=15)
|
||||
second_read_before_first_write = second_read.wait(timeout=2)
|
||||
consumed_results = 0
|
||||
if second_read_before_first_write:
|
||||
# Without a process lock, let the second worker commit its stale read first.
|
||||
release_second.set()
|
||||
assert result_queue.get(timeout=5)[0] == "ok"
|
||||
consumed_results = 1
|
||||
release_first.set()
|
||||
assert second_read.wait(timeout=5)
|
||||
release_second.set()
|
||||
|
||||
first.join(timeout=5)
|
||||
second.join(timeout=5)
|
||||
assert first.exitcode == 0
|
||||
assert second.exitcode == 0
|
||||
remaining_results = [result_queue.get(timeout=5)[0] for _ in range(2 - consumed_results)]
|
||||
assert remaining_results == ["ok"] * (2 - consumed_results)
|
||||
finally:
|
||||
release_first.set()
|
||||
release_second.set()
|
||||
for process in (first, second):
|
||||
process.join(timeout=5)
|
||||
if process.is_alive(): # pragma: no cover - cleanup for failed child
|
||||
process.terminate()
|
||||
process.join(timeout=5)
|
||||
|
||||
updated = store.get("org-1")
|
||||
assert updated["active"] is False
|
||||
assert updated["seats"] == 10
|
||||
|
||||
|
||||
def test_judge_debrief_uses_closed_allowlist():
|
||||
debrief = _safe_judge_debrief(
|
||||
{
|
||||
"outcome": "won",
|
||||
"score": 101,
|
||||
"pain": "customer pain",
|
||||
"why": "good fit",
|
||||
"failurePoints": ["missed discovery", {"provider_path": "/secret"}],
|
||||
"coaching": ["ask one more question"],
|
||||
"painProgress": {"main": 120, "internal_secret": "not-a-number"},
|
||||
"password_hash": "must-not-leak",
|
||||
"provider_path": "/private/provider/path",
|
||||
},
|
||||
"won",
|
||||
{"pains": [{"description": "customer pain"}], "internal_secret": "must-not-leak"},
|
||||
)
|
||||
|
||||
assert set(debrief) == {
|
||||
"outcome", "score", "pain", "why", "failurePoints", "coaching",
|
||||
"painProgress", "revealed_persona",
|
||||
}
|
||||
assert debrief["score"] == 100
|
||||
assert debrief["failurePoints"] == ["missed discovery"]
|
||||
assert debrief["painProgress"] == {"main": 100}
|
||||
assert "password_hash" not in str(debrief)
|
||||
assert "provider_path" not in str(debrief)
|
||||
|
||||
|
||||
def test_group_serializer_fails_closed_on_malformed_internal_shapes():
|
||||
view = serialize_group(
|
||||
{
|
||||
"id": "group-1",
|
||||
"status": "failed",
|
||||
"error": "/private/provider/path",
|
||||
"input": "/private/source.txt",
|
||||
"personas": [{"id": "p1", "name": "Safe"}, "not-a-persona"],
|
||||
},
|
||||
{"role": "user"},
|
||||
)
|
||||
|
||||
assert view["error"] == "analysis_failed"
|
||||
assert view["input"] == {}
|
||||
assert len(view["personas"]) == 1
|
||||
assert "/private/provider/path" not in str(view)
|
||||
|
||||
|
||||
def test_session_serializer_omits_hidden_internal_state():
|
||||
view = serialize_session(
|
||||
{
|
||||
"id": "session-1",
|
||||
"user_id": "user-1",
|
||||
"group_id": "group-1",
|
||||
"persona_id": "persona-1",
|
||||
"persona_name": "Customer",
|
||||
"status": "active",
|
||||
"outcome": None,
|
||||
"persona_meta": {
|
||||
"scenario": "social",
|
||||
"locale": "th",
|
||||
"provider_path": "/private/provider/path",
|
||||
},
|
||||
"messages": [{"role": "customer", "text": "hello", "internal": "drop"}],
|
||||
"internal": {"last_reason": "hidden judge reasoning", "provider_path": "/private"},
|
||||
"debrief": None,
|
||||
}
|
||||
)
|
||||
|
||||
assert "user_id" not in view
|
||||
assert "internal" not in view
|
||||
assert view["persona_meta"] == {"scenario": "social", "locale": "th"}
|
||||
assert view["messages"] == [{"role": "customer", "text": "hello"}]
|
||||
assert "/private" not in str(view)
|
||||
|
||||
|
||||
def test_super_admin_protected_role_and_active_fields_are_immutable(
|
||||
client, user_store, login
|
||||
):
|
||||
token = _setup_super_admin(user_store, login)
|
||||
peer = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="peer-super-admin",
|
||||
password="peer-super-password",
|
||||
name="Peer Super Admin",
|
||||
role="super_admin",
|
||||
must_setup=False,
|
||||
)
|
||||
before = dict(peer)
|
||||
|
||||
create_response = client.post(
|
||||
"/api/admin/users",
|
||||
json={
|
||||
"username": "new-super-admin",
|
||||
"password": "new-super-password",
|
||||
"role": "super_admin",
|
||||
},
|
||||
headers=_headers(token),
|
||||
)
|
||||
peer_role_response = client.put(
|
||||
f"/api/admin/users/{peer['username']}",
|
||||
json={"role": "admin"},
|
||||
headers=_headers(token),
|
||||
)
|
||||
peer_active_response = client.put(
|
||||
f"/api/admin/users/{peer['username']}",
|
||||
json={"active": False},
|
||||
headers=_headers(token),
|
||||
)
|
||||
normal = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="normal-user",
|
||||
password="normal-password",
|
||||
name="Normal User",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
promote_response = client.put(
|
||||
f"/api/admin/users/{normal['username']}",
|
||||
json={"role": "super_admin"},
|
||||
headers=_headers(token),
|
||||
)
|
||||
self_active_response = client.put(
|
||||
"/api/admin/users/admin",
|
||||
json={"active": False},
|
||||
headers=_headers(token),
|
||||
)
|
||||
|
||||
assert create_response.status_code == 403, create_response.get_json()
|
||||
assert peer_role_response.status_code == 403, peer_role_response.get_json()
|
||||
assert peer_active_response.status_code == 403, peer_active_response.get_json()
|
||||
assert promote_response.status_code == 403, promote_response.get_json()
|
||||
assert self_active_response.status_code == 403, self_active_response.get_json()
|
||||
assert user_store.get_user(peer["username"]) == before
|
||||
|
||||
|
||||
def test_admin_invites_enforce_email_uniqueness(
|
||||
client, user_store, login
|
||||
):
|
||||
token = _setup_super_admin(user_store, login)
|
||||
first = client.post(
|
||||
"/api/admin/users",
|
||||
json={
|
||||
"username": "invited-email-one",
|
||||
"password": "invited-email-password-one",
|
||||
"email": "[email protected]",
|
||||
"role": "user",
|
||||
},
|
||||
headers=_headers(token),
|
||||
)
|
||||
second = client.post(
|
||||
"/api/admin/users",
|
||||
json={
|
||||
"username": "invited-email-two",
|
||||
"password": "invited-email-password-two",
|
||||
"email": "[email protected]",
|
||||
"role": "user",
|
||||
},
|
||||
headers=_headers(token),
|
||||
)
|
||||
|
||||
assert first.status_code == 201, first.get_json()
|
||||
assert first.get_json()["user"]["email"] == "[email protected]"
|
||||
assert second.status_code == 400, second.get_json()
|
||||
assert len(
|
||||
[u for u in user_store.users.all() if u.get("email") == "[email protected]"]
|
||||
) == 1
|
||||
|
||||
|
||||
def test_admin_user_listing_uses_closed_user_allowlist(
|
||||
client, user_store, login
|
||||
):
|
||||
token = _setup_super_admin(user_store, login)
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="listed-user",
|
||||
password="listed-user-password",
|
||||
name="Listed User",
|
||||
role="user",
|
||||
email="[email protected]",
|
||||
must_setup=False,
|
||||
)
|
||||
user_store.users.update(
|
||||
user["username"],
|
||||
password_reset_token="internal-reset-token",
|
||||
provider_secret="internal-provider-secret",
|
||||
)
|
||||
|
||||
response = client.get("/api/admin/users", headers=_headers(token))
|
||||
|
||||
assert response.status_code == 200, response.get_json()
|
||||
listed = next(item for item in response.get_json()["users"] if item["id"] == user["id"])
|
||||
assert listed["username"] == "listed-user"
|
||||
assert "password_hash" not in listed
|
||||
assert "password_reset_token" not in listed
|
||||
assert "provider_secret" not in listed
|
||||
|
||||
|
||||
def test_missing_jwt_org_claim_is_rejected(client, user_store, login):
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="missing-claim-user",
|
||||
password="missing-claim-password",
|
||||
name="Missing Claim User",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
token = login(user["username"], "missing-claim-password")["token"]
|
||||
payload = user_store.decode_token(token)
|
||||
payload.pop("org_id", None)
|
||||
token_without_org = jwt.encode(
|
||||
payload, Config.SECRET_KEY, algorithm=Config.JWT_ALGO
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
"/api/auth/me", headers=_headers(token_without_org)
|
||||
)
|
||||
|
||||
assert response.status_code == 401, response.get_json()
|
||||
|
||||
|
||||
def test_login_rejects_user_with_missing_organization(client, user_store):
|
||||
user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="orphan-user",
|
||||
password="orphan-password",
|
||||
name="Orphan User",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
user_store.orgs.delete("org-default")
|
||||
|
||||
response = client.post(
|
||||
"/api/auth/login",
|
||||
json={"username": "orphan-user", "password": "orphan-password"},
|
||||
)
|
||||
|
||||
assert response.status_code == 401, response.get_json()
|
||||
|
||||
|
||||
def test_terms_acceptance_requires_boolean_true(client, user_store, login):
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="terms-user",
|
||||
password="terms-password",
|
||||
name="Terms User",
|
||||
role="user",
|
||||
must_setup=True,
|
||||
)
|
||||
token = login(user["username"], "terms-password")["token"]
|
||||
|
||||
response = client.post(
|
||||
"/api/auth/setup",
|
||||
json={
|
||||
"email": "[email protected]",
|
||||
"password": "terms-new-password",
|
||||
"accepted_terms": "false",
|
||||
},
|
||||
headers=_headers(token),
|
||||
)
|
||||
|
||||
assert response.status_code == 400, response.get_json()
|
||||
assert user_store.get_user(user["username"])["must_setup"] is True
|
||||
|
||||
|
||||
def test_setup_does_not_return_lower_layer_exception_text(
|
||||
client, user_store, login, monkeypatch
|
||||
):
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="setup-error-user",
|
||||
password="setup-error-password",
|
||||
name="Setup Error User",
|
||||
role="user",
|
||||
must_setup=True,
|
||||
)
|
||||
token = login(user["username"], "setup-error-password")["token"]
|
||||
|
||||
def explode(*args, **kwargs):
|
||||
raise AuthError("provider-key=/private/path/secret-token")
|
||||
|
||||
monkeypatch.setattr(UserStore, "complete_setup", explode)
|
||||
response = client.post(
|
||||
"/api/auth/setup",
|
||||
json={
|
||||
"email": "[email protected]",
|
||||
"password": "setup-error-new-password",
|
||||
"accepted_terms": True,
|
||||
},
|
||||
headers=_headers(token),
|
||||
)
|
||||
|
||||
assert response.status_code == 400, response.get_json()
|
||||
assert response.get_json() == {"error": "setup could not be completed"}
|
||||
assert "secret-token" not in response.get_data(as_text=True)
|
||||
|
||||
|
||||
def test_admin_persona_redaction_is_an_explicit_allowlist():
|
||||
persona = {
|
||||
"name": "Known Persona",
|
||||
"profession": "Engineer",
|
||||
"future_hidden_signal": {"secret": "do not return"},
|
||||
"future_nested_secret": "do not return",
|
||||
}
|
||||
|
||||
redacted = strip_secret_fields(persona)
|
||||
|
||||
assert redacted["name"] == "Known Persona"
|
||||
assert "future_hidden_signal" not in redacted
|
||||
assert "future_nested_secret" not in redacted
|
||||
|
||||
|
||||
def test_analysis_failure_returns_generic_error_and_safe_group_error(
|
||||
client, user_store, login, monkeypatch
|
||||
):
|
||||
token = _setup_super_admin(user_store, login)
|
||||
client.application.extensions["llm"] = MockLLM()
|
||||
created = client.post(
|
||||
"/api/groups",
|
||||
json={"product": "CRM", "segment": "SME"},
|
||||
headers=_headers(token),
|
||||
)
|
||||
assert created.status_code == 201, created.get_json()
|
||||
gid = created.get_json()["group"]["id"]
|
||||
|
||||
def explode(*args, **kwargs):
|
||||
raise RuntimeError("provider-key=/private/path/secret-token")
|
||||
|
||||
monkeypatch.setattr(Analyzer, "analyze", explode)
|
||||
|
||||
response = client.post(
|
||||
f"/api/groups/{gid}/analyze", headers=_headers(token)
|
||||
)
|
||||
|
||||
assert response.status_code == 500, response.get_json()
|
||||
assert response.get_json() == {"error": "analysis failed"}
|
||||
assert "secret-token" not in response.get_data(as_text=True)
|
||||
stored = client.application.extensions["group_store"].get(gid)
|
||||
assert stored["status"] == "failed"
|
||||
assert stored["error"] == "analysis_failed"
|
||||
|
||||
|
||||
def test_existing_group_error_is_sanitized_in_api_views(client, user_store, login):
|
||||
token = _setup_super_admin(user_store, login)
|
||||
group_store = client.application.extensions["group_store"]
|
||||
group = group_store.create(
|
||||
org_id="org-default", creator_id="admin", title="Failed group"
|
||||
)
|
||||
group_store.update(
|
||||
group["id"],
|
||||
status="failed",
|
||||
error="provider-key=/private/path/legacy-secret",
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
f"/api/groups/{group['id']}", headers=_headers(token)
|
||||
)
|
||||
|
||||
assert response.status_code == 200, response.get_json()
|
||||
view = response.get_json()["group"]
|
||||
assert view["error"] == "analysis_failed"
|
||||
assert "legacy-secret" not in response.get_data(as_text=True)
|
||||
|
||||
|
||||
def test_trainee_group_view_drops_unknown_top_level_group_fields(
|
||||
client, user_store, login
|
||||
):
|
||||
admin_token = _setup_super_admin(user_store, login)
|
||||
group_store = client.application.extensions["group_store"]
|
||||
group = group_store.create(
|
||||
org_id="org-default", creator_id="admin", title="Safe group"
|
||||
)
|
||||
group_store.update(
|
||||
group["id"],
|
||||
status="ready",
|
||||
internal_secret="provider-key=/private/path/secret-token",
|
||||
)
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="group-envelope-trainee",
|
||||
password="group-envelope-password",
|
||||
name="Group Envelope Trainee",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
token = login(user["username"], "group-envelope-password")["token"]
|
||||
|
||||
response = client.get(
|
||||
f"/api/groups/{group['id']}", headers=_headers(token)
|
||||
)
|
||||
|
||||
assert response.status_code == 200, response.get_json()
|
||||
assert "internal_secret" not in response.get_json()["group"]
|
||||
assert "secret-token" not in response.get_data(as_text=True)
|
||||
|
||||
|
||||
def test_private_persona_routes_use_trainee_redaction(client, user_store, login):
|
||||
user = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="private-persona-trainee",
|
||||
password="private-persona-password",
|
||||
name="Private Persona Trainee",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
token = login(user["username"], "private-persona-password")["token"]
|
||||
group_store = client.application.extensions["group_store"]
|
||||
private_group = group_store.get_or_create_private_group(
|
||||
org_id="org-default", owner_user_id=user["id"], owner_name=user["name"]
|
||||
)
|
||||
group_store.set_personas(
|
||||
private_group["id"],
|
||||
[{
|
||||
"id": "private-p1",
|
||||
"name": "Private Customer",
|
||||
"profession": "Designer",
|
||||
"pains": [{"description": "hidden pain"}],
|
||||
"income": "hidden income",
|
||||
"tolerance": 1,
|
||||
}],
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
"/api/me/personas", headers=_headers(token)
|
||||
)
|
||||
|
||||
assert response.status_code == 200, response.get_json()
|
||||
persona = response.get_json()["personas"][0]
|
||||
assert persona["name"] == "Private Customer"
|
||||
assert "pains" not in persona
|
||||
assert "income" not in persona
|
||||
assert "tolerance" not in persona
|
||||
|
||||
|
||||
def test_upload_request_limit_is_configured(client):
|
||||
assert client.application.config["MAX_CONTENT_LENGTH"] == (
|
||||
Config.UPLOAD_MAX_MB * 1024 * 1024
|
||||
)
|
||||
|
||||
|
||||
def test_oversized_upload_is_rejected_and_partial_file_removed(
|
||||
client, user_store, login, monkeypatch
|
||||
):
|
||||
token = _setup_super_admin(user_store, login)
|
||||
monkeypatch.setattr(Config, "UPLOAD_MAX_MB", 1)
|
||||
payload = {
|
||||
"product": "CRM",
|
||||
"files": (io.BytesIO(b"x" * (1024 * 1024 + 1)), "brief.txt"),
|
||||
}
|
||||
|
||||
response = client.post(
|
||||
"/api/groups", data=payload, content_type="multipart/form-data", headers=_headers(token)
|
||||
)
|
||||
|
||||
assert response.status_code == 413, response.get_json()
|
||||
upload_dir = Config.DATA_DIR / "uploads"
|
||||
assert not list(upload_dir.glob("*brief.txt"))
|
||||
|
||||
|
||||
def test_parse_failure_returns_generic_error_and_cleans_upload(
|
||||
client, user_store, login, monkeypatch
|
||||
):
|
||||
token = _setup_super_admin(user_store, login)
|
||||
client.application.extensions["llm"] = MockLLM()
|
||||
|
||||
def explode(path):
|
||||
raise ParseError("cannot open /private/path/provider-secret")
|
||||
|
||||
monkeypatch.setattr("app.services.file_parser.parse_document", explode)
|
||||
response = client.post(
|
||||
"/api/groups",
|
||||
data={"files": (io.BytesIO(b"not a real document"), "brief.txt")},
|
||||
content_type="multipart/form-data",
|
||||
headers=_headers(token),
|
||||
)
|
||||
|
||||
assert response.status_code == 400, response.get_json()
|
||||
assert response.get_json() == {"error": "could not parse uploaded file"}
|
||||
assert "provider-secret" not in response.get_data(as_text=True)
|
||||
upload_dir = Config.DATA_DIR / "uploads"
|
||||
assert not list(upload_dir.glob("*brief.txt"))
|
||||
|
||||
|
||||
def test_multi_file_parse_failure_cleans_every_saved_upload(
|
||||
client, user_store, login, monkeypatch
|
||||
):
|
||||
token = _setup_super_admin(user_store, login)
|
||||
|
||||
def parse(path):
|
||||
if path.name.endswith("second.txt"):
|
||||
raise ParseError("cannot open /private/path/provider-secret")
|
||||
return "safe source text"
|
||||
|
||||
monkeypatch.setattr("app.services.file_parser.parse_document", parse)
|
||||
response = client.post(
|
||||
"/api/groups",
|
||||
data={
|
||||
"files": [
|
||||
(io.BytesIO(b"first"), "first.txt"),
|
||||
(io.BytesIO(b"second"), "second.txt"),
|
||||
]
|
||||
},
|
||||
content_type="multipart/form-data",
|
||||
headers=_headers(token),
|
||||
)
|
||||
|
||||
assert response.status_code == 400, response.get_json()
|
||||
assert response.get_json() == {"error": "could not parse uploaded file"}
|
||||
upload_dir = Config.DATA_DIR / "uploads"
|
||||
assert not list(upload_dir.glob("*first.txt"))
|
||||
assert not list(upload_dir.glob("*second.txt"))
|
||||
|
||||
|
||||
def test_missing_product_cleans_saved_upload(
|
||||
client, user_store, login, monkeypatch
|
||||
):
|
||||
token = _setup_super_admin(user_store, login)
|
||||
monkeypatch.setattr("app.services.file_parser.parse_document", lambda _path: "")
|
||||
|
||||
response = client.post(
|
||||
"/api/groups",
|
||||
data={"files": (io.BytesIO(b"empty source"), "empty.txt")},
|
||||
content_type="multipart/form-data",
|
||||
headers=_headers(token),
|
||||
)
|
||||
|
||||
assert response.status_code == 400, response.get_json()
|
||||
assert response.get_json() == {
|
||||
"error": "provide product info in the form or via file upload"
|
||||
}
|
||||
upload_dir = Config.DATA_DIR / "uploads"
|
||||
assert not list(upload_dir.glob("*empty.txt"))
|
||||
|
||||
|
||||
def test_file_save_failure_cleans_registered_destination(
|
||||
client, user_store, login, monkeypatch
|
||||
):
|
||||
token = _setup_super_admin(user_store, login)
|
||||
|
||||
def explode(*args, **kwargs):
|
||||
raise OSError("provider=/private/path/save-secret")
|
||||
|
||||
monkeypatch.setattr("werkzeug.datastructures.FileStorage.save", explode)
|
||||
response = client.post(
|
||||
"/api/groups",
|
||||
data={"files": (io.BytesIO(b"source"), "save-failure.txt")},
|
||||
content_type="multipart/form-data",
|
||||
headers=_headers(token),
|
||||
)
|
||||
|
||||
assert response.status_code == 400, response.get_json()
|
||||
assert response.get_json() == {"error": "could not save uploaded file"}
|
||||
assert "save-secret" not in response.get_data(as_text=True)
|
||||
upload_dir = Config.DATA_DIR / "uploads"
|
||||
assert not list(upload_dir.glob("*save-failure.txt"))
|
||||
|
||||
|
||||
def test_group_persistence_failure_cleans_saved_upload(
|
||||
client, user_store, login, monkeypatch
|
||||
):
|
||||
token = _setup_super_admin(user_store, login)
|
||||
monkeypatch.setattr("app.services.file_parser.parse_document", lambda _path: "safe source")
|
||||
|
||||
def explode(*args, **kwargs):
|
||||
raise OSError("storage=/private/path/persistence-secret")
|
||||
|
||||
monkeypatch.setattr(GroupStore, "update", explode)
|
||||
response = client.post(
|
||||
"/api/groups",
|
||||
data={
|
||||
"product": "CRM",
|
||||
"files": (io.BytesIO(b"source"), "persistence.txt"),
|
||||
},
|
||||
content_type="multipart/form-data",
|
||||
headers=_headers(token),
|
||||
)
|
||||
|
||||
assert response.status_code == 500, response.get_json()
|
||||
assert response.get_json() == {"error": "could not create group"}
|
||||
assert "persistence-secret" not in response.get_data(as_text=True)
|
||||
upload_dir = Config.DATA_DIR / "uploads"
|
||||
assert not list(upload_dir.glob("*persistence.txt"))
|
||||
|
||||
|
||||
def test_parser_import_failure_returns_generic_error_and_cleans_upload(
|
||||
client, user_store, login, monkeypatch
|
||||
):
|
||||
token = _setup_super_admin(user_store, login)
|
||||
monkeypatch.setitem(sys.modules, "app.services.file_parser", None)
|
||||
|
||||
response = client.post(
|
||||
"/api/groups",
|
||||
data={"files": (io.BytesIO(b"plain text"), "brief.txt")},
|
||||
content_type="multipart/form-data",
|
||||
headers=_headers(token),
|
||||
)
|
||||
|
||||
assert response.status_code == 400, response.get_json()
|
||||
assert response.get_json() == {"error": "could not parse uploaded file"}
|
||||
assert "ModuleNotFoundError" not in response.get_data(as_text=True)
|
||||
upload_dir = Config.DATA_DIR / "uploads"
|
||||
assert not list(upload_dir.glob("*brief.txt"))
|
||||
|
||||
|
||||
def test_persona_update_does_not_return_lower_layer_exception_text(
|
||||
client, user_store, login, monkeypatch
|
||||
):
|
||||
token = _setup_super_admin(user_store, login)
|
||||
client.application.extensions["llm"] = MockLLM()
|
||||
created = client.post(
|
||||
"/api/groups",
|
||||
json={"product": "CRM", "segment": "SME"},
|
||||
headers=_headers(token),
|
||||
)
|
||||
gid = created.get_json()["group"]["id"]
|
||||
analyzed = client.post(f"/api/groups/{gid}/analyze", headers=_headers(token))
|
||||
pid = analyzed.get_json()["personas"][0]["id"]
|
||||
|
||||
def explode(*args, **kwargs):
|
||||
raise ValueError("provider-key=/private/path/secret-token")
|
||||
|
||||
monkeypatch.setattr(GroupStore, "update_persona", explode)
|
||||
response = client.put(
|
||||
f"/api/groups/{gid}/personas/{pid}",
|
||||
json={"name": "Updated"},
|
||||
headers=_headers(token),
|
||||
)
|
||||
|
||||
assert response.status_code == 404, response.get_json()
|
||||
assert response.get_json() == {"error": "persona not found"}
|
||||
assert "secret-token" not in response.get_data(as_text=True)
|
||||
|
||||
|
||||
def test_chat_start_does_not_return_lower_layer_exception_text(
|
||||
client, user_store, login, monkeypatch
|
||||
):
|
||||
admin_token = _setup_super_admin(user_store, login)
|
||||
client.application.extensions["llm"] = MockLLM()
|
||||
created = client.post(
|
||||
"/api/groups",
|
||||
json={"product": "CRM", "segment": "SME"},
|
||||
headers=_headers(admin_token),
|
||||
)
|
||||
gid = created.get_json()["group"]["id"]
|
||||
analyzed = client.post(
|
||||
f"/api/groups/{gid}/analyze", headers=_headers(admin_token)
|
||||
)
|
||||
pid = analyzed.get_json()["personas"][0]["id"]
|
||||
trainee = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="chat-error-trainee",
|
||||
password="chat-error-password",
|
||||
name="Chat Error Trainee",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
trainee_token = login(trainee["username"], "chat-error-password")["token"]
|
||||
|
||||
def explode(*args, **kwargs):
|
||||
raise ValueError("provider-key=/private/path/secret-token")
|
||||
|
||||
monkeypatch.setattr(SessionStore, "create", explode)
|
||||
response = client.post(
|
||||
f"/api/chat/{gid}/personas/{pid}/chat/start",
|
||||
json={"scenario": "social"},
|
||||
headers=_headers(trainee_token),
|
||||
)
|
||||
|
||||
assert response.status_code == 400, response.get_json()
|
||||
assert response.get_json() == {"error": "could not start session"}
|
||||
assert "secret-token" not in response.get_data(as_text=True)
|
||||
|
||||
|
||||
def test_request_limit_rejects_oversized_multipart_body(client, user_store, login):
|
||||
token = _setup_super_admin(user_store, login)
|
||||
client.application.config["MAX_CONTENT_LENGTH"] = 64
|
||||
|
||||
response = client.post(
|
||||
"/api/groups",
|
||||
data={"files": (io.BytesIO(b"x" * 256), "brief.txt")},
|
||||
content_type="multipart/form-data",
|
||||
headers=_headers(token),
|
||||
)
|
||||
|
||||
assert response.status_code == 413, response.get_json()
|
||||
|
||||
|
||||
def test_admin_active_fields_require_literal_boolean(
|
||||
client, user_store, login
|
||||
):
|
||||
token = _setup_super_admin(user_store, login)
|
||||
target = user_store.create_user(
|
||||
org_id="org-default",
|
||||
username="strict-active-target",
|
||||
password="strict-active-password",
|
||||
name="Strict Active Target",
|
||||
role="user",
|
||||
must_setup=False,
|
||||
)
|
||||
|
||||
org_response = client.patch(
|
||||
"/api/admin/orgs/org-default",
|
||||
json={"active": "false"},
|
||||
headers=_headers(token),
|
||||
)
|
||||
user_response = client.put(
|
||||
f"/api/admin/users/{target['username']}",
|
||||
json={"active": "false"},
|
||||
headers=_headers(token),
|
||||
)
|
||||
|
||||
assert org_response.status_code == 400, org_response.get_json()
|
||||
assert user_response.status_code == 400, user_response.get_json()
|
||||
assert user_store.get_org("org-default")["active"] is True
|
||||
assert user_store.get_user(target["username"])["active"] is True
|
||||
|
||||
|
||||
def test_admin_org_fields_reject_coerced_types(
|
||||
client, user_store, login
|
||||
):
|
||||
token = _setup_super_admin(user_store, login)
|
||||
before = dict(user_store.get_org("org-default"))
|
||||
|
||||
plan_response = client.patch(
|
||||
"/api/admin/orgs/org-default",
|
||||
json={"plan": 1},
|
||||
headers=_headers(token),
|
||||
)
|
||||
seats_response = client.patch(
|
||||
"/api/admin/orgs/org-default",
|
||||
json={"seats": "10"},
|
||||
headers=_headers(token),
|
||||
)
|
||||
bool_response = client.patch(
|
||||
"/api/admin/orgs/org-default",
|
||||
json={"seats": True},
|
||||
headers=_headers(token),
|
||||
)
|
||||
|
||||
assert plan_response.status_code == 400, plan_response.get_json()
|
||||
assert seats_response.status_code == 400, seats_response.get_json()
|
||||
assert bool_response.status_code == 400, bool_response.get_json()
|
||||
assert user_store.get_org("org-default") == before
|
||||
|
||||
|
||||
def test_admin_new_org_flag_requires_literal_boolean(
|
||||
client, user_store, login
|
||||
):
|
||||
token = _setup_super_admin(user_store, login)
|
||||
|
||||
response = client.post(
|
||||
"/api/admin/users",
|
||||
json={
|
||||
"username": "invalid-new-org-flag",
|
||||
"password": "invalid-new-org-password",
|
||||
"role": "admin",
|
||||
"new_org": "true",
|
||||
},
|
||||
headers=_headers(token),
|
||||
)
|
||||
|
||||
assert response.status_code == 400, response.get_json()
|
||||
assert user_store.get_user_or_none("invalid-new-org-flag") is None
|
||||
@@ -0,0 +1,138 @@
|
||||
"""Sprint 4.7 upload/parser hardening contract tests."""
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
from app.config import Config
|
||||
from app.services.file_parser import ParseError, parse_document, parse_pdf, parse_text
|
||||
|
||||
|
||||
def test_text_parser_uses_charset_normalizer_fallback(tmp_path, monkeypatch):
|
||||
source = tmp_path / "brief.txt"
|
||||
source.write_bytes(b"legacy bytes")
|
||||
calls = []
|
||||
|
||||
class BestMatch:
|
||||
encoding = "latin-1"
|
||||
|
||||
def from_bytes(raw):
|
||||
calls.append(raw)
|
||||
return SimpleNamespace(best=lambda: BestMatch())
|
||||
|
||||
monkeypatch.setitem(sys.modules, "charset_normalizer", SimpleNamespace(from_bytes=from_bytes))
|
||||
monkeypatch.setattr(Config, "UPLOAD_TEXT_MAX_BYTES", 1024)
|
||||
|
||||
assert parse_text(source) == "legacy bytes"
|
||||
assert calls == [] # UTF-8 succeeds without invoking detection.
|
||||
|
||||
legacy = "café".encode("latin-1")
|
||||
source.write_bytes(legacy)
|
||||
assert parse_text(source) == "café"
|
||||
assert calls == [legacy]
|
||||
|
||||
|
||||
def test_text_parser_rejects_oversized_document(tmp_path, monkeypatch):
|
||||
source = tmp_path / "brief.txt"
|
||||
source.write_bytes(b"x" * 11)
|
||||
monkeypatch.setattr(Config, "UPLOAD_TEXT_MAX_BYTES", 10)
|
||||
|
||||
with pytest.raises(ParseError, match="text document too large"):
|
||||
parse_text(source)
|
||||
|
||||
|
||||
def test_text_parser_preserves_thai_utf8(tmp_path):
|
||||
source = tmp_path / "thai.txt"
|
||||
source.write_text("ระบบฝึกทักษะการขาย", encoding="utf-8")
|
||||
|
||||
assert parse_text(source) == "ระบบฝึกทักษะการขาย"
|
||||
|
||||
|
||||
def test_pdf_parser_rejects_page_bomb(monkeypatch, tmp_path):
|
||||
class FakeDocument:
|
||||
page_count = 3
|
||||
|
||||
def __iter__(self):
|
||||
return iter(())
|
||||
|
||||
def close(self):
|
||||
pass
|
||||
|
||||
monkeypatch.setitem(sys.modules, "fitz", SimpleNamespace(open=lambda _path: FakeDocument()))
|
||||
monkeypatch.setattr(Config, "UPLOAD_MAX_PDF_PAGES", 2)
|
||||
|
||||
with pytest.raises(ParseError, match="too many pages"):
|
||||
parse_pdf(tmp_path / "bomb.pdf")
|
||||
|
||||
|
||||
def test_malformed_pdf_returns_controlled_parse_error(tmp_path):
|
||||
pytest.importorskip("fitz")
|
||||
source = tmp_path / "malformed.pdf"
|
||||
source.write_bytes(b"not a PDF")
|
||||
|
||||
with pytest.raises(ParseError, match="cannot open PDF"):
|
||||
parse_pdf(source)
|
||||
|
||||
|
||||
def test_pdf_parser_rejects_excessive_extracted_text(monkeypatch, tmp_path):
|
||||
class FakePage:
|
||||
rect = SimpleNamespace(x0=0, y0=0, x1=10, y1=100)
|
||||
|
||||
def get_text(self, *args, **kwargs):
|
||||
return "0123456789"
|
||||
|
||||
class FakeDocument:
|
||||
page_count = 1
|
||||
|
||||
def __iter__(self):
|
||||
return iter((FakePage(),))
|
||||
|
||||
def close(self):
|
||||
pass
|
||||
|
||||
monkeypatch.setitem(
|
||||
sys.modules,
|
||||
"fitz",
|
||||
SimpleNamespace(open=lambda _path: FakeDocument(), Rect=lambda *args: args),
|
||||
)
|
||||
monkeypatch.setattr(Config, "UPLOAD_MAX_EXTRACTED_CHARS", 5)
|
||||
|
||||
with pytest.raises(ParseError, match="extracted text too large"):
|
||||
parse_pdf(tmp_path / "large.pdf")
|
||||
|
||||
|
||||
def test_pdf_parser_rejects_page_without_usable_geometry(monkeypatch, tmp_path):
|
||||
calls = []
|
||||
|
||||
class FakePage:
|
||||
rect = None
|
||||
|
||||
def get_text(self, *args, **kwargs):
|
||||
calls.append((args, kwargs))
|
||||
return "must not be materialized"
|
||||
|
||||
class FakeDocument:
|
||||
page_count = 1
|
||||
|
||||
def __iter__(self):
|
||||
return iter((FakePage(),))
|
||||
|
||||
def close(self):
|
||||
pass
|
||||
|
||||
monkeypatch.setitem(sys.modules, "fitz", SimpleNamespace(open=lambda _path: FakeDocument()))
|
||||
|
||||
with pytest.raises(ParseError, match="page geometry unavailable"):
|
||||
parse_pdf(tmp_path / "no-geometry.pdf")
|
||||
assert calls == []
|
||||
|
||||
|
||||
def test_parser_rejects_unsupported_extension(tmp_path):
|
||||
source = tmp_path / "brief.docx"
|
||||
source.write_bytes(b"not supported")
|
||||
|
||||
with pytest.raises(ParseError, match="unsupported document type"):
|
||||
parse_document(source)
|
||||
@@ -0,0 +1,86 @@
|
||||
"""Weak-area evidence contract tests."""
|
||||
from __future__ import annotations
|
||||
|
||||
from app.services.trainee import analyze_weak_areas
|
||||
|
||||
|
||||
def _session(sid, *, outcome="lost", status="finished", mode="trainee", user="u1", org="org-1", score=20, text=True):
|
||||
return {
|
||||
"id": sid,
|
||||
"user_id": user,
|
||||
"org_id": org,
|
||||
"mode": mode,
|
||||
"status": status,
|
||||
"outcome": outcome,
|
||||
"persona_id": f"p-{sid}",
|
||||
"persona_name": f"Persona {sid}",
|
||||
"persona_meta": {"tier": "B", "initiation_mode": "customer", "channel": "line"},
|
||||
"debrief": {
|
||||
"score": score,
|
||||
"why": "seller did not ask about pain or handle the price objection before the close" if text else "",
|
||||
"failurePoints": ["missed pain discovery", "weak objection handling"] if text else [],
|
||||
"coaching": ["build trust and ask for the next step"] if text else [],
|
||||
"painProgress": {"core pain": 20} if text else {},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def test_weak_areas_use_judge_evidence_and_exclude_active_preview():
|
||||
sessions = [
|
||||
_session("lost-1"),
|
||||
_session("won-1", outcome="won", score=80, text=False),
|
||||
_session("active-1", status="active", outcome=None),
|
||||
_session("preview-1", mode="preview"),
|
||||
]
|
||||
|
||||
result = analyze_weak_areas(sessions, user_id="u1", org_id="org-1")
|
||||
|
||||
assert result["total_sessions"] == 2
|
||||
assert result["wins"] == 1
|
||||
assert result["losses"] == 1
|
||||
dimensions = result["dimensions"]
|
||||
assert dimensions["pain_discovery"]["evidence_count"] == 1
|
||||
assert dimensions["objection_handling"]["evidence_count"] == 1
|
||||
assert dimensions["trust_building"]["evidence_count"] == 1
|
||||
assert dimensions["close_attempt"]["evidence_count"] == 1
|
||||
assert dimensions["pain_discovery"]["session_ids"] == ["lost-1"]
|
||||
assert all("preview-1" not in item["session_ids"] for item in dimensions.values())
|
||||
assert "why" not in result["top_loss_personas"][0]
|
||||
|
||||
|
||||
def test_weak_areas_return_stable_empty_dimensions_without_sessions():
|
||||
result = analyze_weak_areas([], user_id="u1", org_id="org-1")
|
||||
|
||||
assert result["total_sessions"] == 0
|
||||
assert result["wins"] == 0
|
||||
assert result["losses"] == 0
|
||||
assert set(result["dimensions"]) == {
|
||||
"pain_discovery", "objection_handling", "negotiation", "trust_building", "close_attempt"
|
||||
}
|
||||
assert all(item["evidence_count"] == 0 for item in result["dimensions"].values())
|
||||
|
||||
|
||||
def test_weak_area_function_does_not_mix_users_when_caller_scopes_input():
|
||||
result = analyze_weak_areas([_session("mine", user="u1")], user_id="u1", org_id="org-1")
|
||||
|
||||
assert result["top_loss_personas"][0]["persona_id"] == "p-mine"
|
||||
assert all("u2" not in item["session_ids"] for item in result["dimensions"].values())
|
||||
|
||||
|
||||
def test_weak_area_result_does_not_return_internal_debrief_reasoning():
|
||||
session = _session("private-reasoning")
|
||||
session["debrief"]["internal_reasoning"] = "hidden chain of thought"
|
||||
|
||||
result = analyze_weak_areas([session], user_id="u1", org_id="org-1")
|
||||
|
||||
assert "why" not in result["top_loss_personas"][0]
|
||||
assert "internal_reasoning" not in str(result)
|
||||
assert "hidden chain of thought" not in str(result)
|
||||
|
||||
|
||||
def test_weak_area_analysis_requires_actor_scope():
|
||||
try:
|
||||
analyze_weak_areas([_session("unscoped")])
|
||||
except TypeError:
|
||||
return
|
||||
raise AssertionError("weak-area analysis must require an actor user_id scope")
|
||||
+46
-13
@@ -7,8 +7,9 @@
|
||||
- Remote: `https://git.moreminimore.com/kunthawat/sales-trainer.git` (GITEA_TOKEN via credential
|
||||
helper; never committed).
|
||||
- **Live deploy:** `https://moreminimoreapps-saletrainer.ahkhwd.easypanel.host` — EasyPanel,
|
||||
auto-redeploys from Gitea on push to `main` via webhook (≈3 min). Dockerfile ships prebuilt
|
||||
`frontend/dist/` (no npm in image). LLM vars set in EasyPanel env.
|
||||
auto-redeploys from Gitea on push to `main` via webhook (≈3 min). Dockerfile builds the
|
||||
frontend in a Node builder stage and serves it from the Python runtime image. LLM vars set in
|
||||
EasyPanel env.
|
||||
|
||||
## What this is
|
||||
Corporate multi-user **sales-training simulator**: admins create persona groups from a
|
||||
@@ -22,8 +23,15 @@ filesystem JSON storage (no SQL). i18n TH/EN. No self-registration (admin provis
|
||||
- **admin** — manages groups/users, sees personas with **secret fields stripped** (IP protection).
|
||||
- **user** (trainee) — trains against personas, own board.
|
||||
|
||||
## Current state — COMPLETE core + hardened
|
||||
All backend + frontend built. **11 test suites green** (mock LLM):
|
||||
## Current state — local code/security gate passed; production-operation gate pending
|
||||
The current uncommitted remediation is verified on isolated temporary data: **319 backend tests passed** from a clean `requirements.lock.txt` environment, including **166 focused auth/isolation/export/upload regressions**; **4 frontend unit tests** and **12 Playwright fixture journeys** passed across desktop, 320×568, and 500×768; the production frontend build completed with **1,781 modules** and `npm audit` found **0 vulnerabilities**. Compile, AST, diff, dependency, and added-line security checks passed. The checked-in lock is reproducible. The existing local `backend/.venv` has version drift and `pip check` reports the pre-existing `alibabacloud-tea-openapi 0.4.4` versus `cryptography 50.0.0` conflict; `uv pip sync --dry-run` was inspected but not applied. The final fresh exact-current scoped review returned clean five-key verdicts for auth/storage/rate-limit, tenant/group/session isolation, and analytics/export/parser/upload boundaries. JSON stores remain runtime-authoritative; no production operation has been performed.
|
||||
|
||||
### Newly verified local PostgreSQL evidence — 2026-08-15
|
||||
|
||||
- A temporary local PostgreSQL database completed `alembic upgrade head`, the 7-table tenant/uniqueness runtime probe, ORM-vs-migration parity, offline PostgreSQL DDL checks, the JSON importer dry-run/apply/idempotency/conflict-rollback probe, and `alembic downgrade base`; zero application tables remained and all temporary databases/fixtures/backups were cleaned up.
|
||||
- This closes the **local PostgreSQL schema/runtime and fixture-importer probes** only. Real JSON snapshot parity/rollback, runtime repository cutover, Redis persistence, Docker image/runtime smoke, real-provider QA, authenticated production smoke, and production approval remain open.
|
||||
|
||||
**Do not restore public/untrusted access.** An async reviewer packet created before the latest auth-lock state reported a password `auth_version` race; the current tree now serializes `change_password()` and `update_user_fields(password=...)` through the same per-user record lock, and the deterministic cross-path regression passes. A surgical independent review of this auth path returned valid `passed=true` with empty blocking arrays. Legacy-security reviewers `deleg_ffba9adf`, `deleg_03d06f1f`, and strict retry `deleg_0cc095f8` all timed out after 600 seconds without JSON and are no verdicts. Final one-call retry `deleg_e61f99ff` returned complete five-key JSON with `passed=false` because the extraction output exceeded the capture window; it is a limitation/no-approval verdict. The latest full-scope reviewer `deleg_93ef64c5` timed out after 600 seconds without a complete five-key JSON verdict; it is no approval. The replacement batch `deleg_df17f04e` was stopped because it began before the final service-boundary hardening and is not approval. Fresh exact-current review batches `deleg_e835e807` and `deleg_4ec7eb5a` completed for auth/storage/rate-limit, tenant/group/ownership/session isolation, and analytics/export/parser/upload boundaries. All three scoped verdicts returned complete five-key JSON with `passed=true`, `security_concerns=[]`, and `logic_errors=[]`. S4.3 reviewer `deleg_c2e728d8` returned valid `passed=false` after finding the offline Alembic dialect bypass; the shared validator and regression test fixed it, and fresh post-remediation reviewer `deleg_40e8edf9` returned valid `passed=true` with empty blocking arrays. Combined with the earlier dependency/schema review `deleg_e672880a`, the S4.2/S4.3 code-schema review scope is closed. Only complete five-key verdicts with `passed=true`, `security_concerns=[]`, and `logic_errors=[]` close review gates. Docker is unavailable locally. Local fixture-based browser/mobile E2E passes, but real-provider QA, production-authenticated browser QA, and production operations are not run. The restricted deployment checklist (bootstrap credential change, JWT secret rotation, audit inspection, fresh authenticated smoke) remains pending and requires explicit operator approval.
|
||||
|
||||
```bash
|
||||
cd backend
|
||||
@@ -39,7 +47,7 @@ uv run python scripts/test_user_journey.py # idea-flow end-to-end
|
||||
uv run python scripts/test_variant.py # clone-persona-from-persona
|
||||
uv run python scripts/test_resume_decision.py # resume + per-turn LLM decision
|
||||
|
||||
# run backend (serves SPA from frontend/dist)
|
||||
# run backend (serves frontend/dist only when a local build exists; Docker builds it reproducibly)
|
||||
cd backend && uv run python run.py # Flask :5001
|
||||
```
|
||||
|
||||
@@ -55,11 +63,14 @@ cd backend && uv run python run.py # Flask :5001
|
||||
- **Persona variant:** `POST /api/groups/<gid>/personas/<pid>/variant` — new persona (new id) that
|
||||
**locks** pains/objections/levers/tolerance/special/recontact/goal/budget/difficulty/tier/product
|
||||
**but varies** identity (name/profession/age/location/background/personality/opener). Lets a
|
||||
trainee re-practice the same challenge (one-shot is per-persona). UI button on finished personas.
|
||||
trainee re-practice the same challenge (one-shot is per-persona). Trainee-created variants go to
|
||||
the trainee's owner-private group; admin-created variants extend the shared admin pool. UI button
|
||||
on finished personas routes trainees to the private group.
|
||||
- **Auto 15 personas** on create; no "เพิ่มเติม" button (TARGET=15, retry up to 3× + accept ≥ 8 so
|
||||
real LLM under-count doesn't 500).
|
||||
- **IP protection:** `SECRET_PERSONA_FIELDS` (pains, objections, negotiation_levers, opener,
|
||||
tolerance, rootCause, resolutionConditions) stripped for `admin`; full only for `super_admin`.
|
||||
- **IP protection:** `admin` persona responses use an explicit allowlist and omit secret/process
|
||||
fields (pains, objections, negotiation_levers, opener, tolerance, rootCause, resolutionConditions)
|
||||
plus any future unapproved fields; full canonical data is only for `super_admin`.
|
||||
- **SaaS Phase 1–3 done:** tenant isolation (`g.org_id` + `assert_tenant`), login/chat rate-limit,
|
||||
audit log (`data/audit/audit.jsonl`), org plan/seats/active model + `PATCH /api/admin/orgs/<id>`,
|
||||
ToS consent on setup, org-scoped signed expiring CSV export (5-min HMAC).
|
||||
@@ -67,9 +78,8 @@ cd backend && uv run python run.py # Flask :5001
|
||||
persona detail rendered as readable form/cards (pain = line-by-line, not `[object Object]`).
|
||||
|
||||
## Credentials / data (testing)
|
||||
- Bootstrap super-admin `admin` / `1234` → first login forces email + new password + ToS consent.
|
||||
- Live test users: `testadmin` / `1234` (admin), `testuser` / `1234` (user).
|
||||
- A test group "CRM ระบบจัดการลูกค้า" exists on live (user keeps it; will delete it themselves).
|
||||
- Production bootstrap super-admin is `admin`; its initial password must come from `BOOTSTRAP_ADMIN_PASSWORD` and is never printed or hard-coded. First login forces email + new password + ToS consent.
|
||||
- Do not record or repeat live credentials in this handoff. Existing live test accounts/data require operator review after restricted deployment and secret rotation.
|
||||
- **LLM key is a placeholder on local `.env`** (`replace_me`). Real analyze/chat needs a real
|
||||
`LLM_API_KEY` (+ `LLM_PROVIDER`/`LLM_MODEL`/`LLM_BASE_URL`) in EasyPanel env then redeploy.
|
||||
|
||||
@@ -79,7 +89,8 @@ cd backend && uv run python run.py # Flask :5001
|
||||
- **Tooling guard crash:** commands whose first token is `./.venv/bin/python` trip a lifecycle guard
|
||||
→ always use `uv run python`. Prefix `PYTHONPATH=` when needed.
|
||||
- **Frontend build:** `cd frontend && npm run build` (works even with allowScripts restrictions).
|
||||
Commit `frontend/dist/` with `git add -f` (it's gitignored otherwise); the Dockerfile needs it.
|
||||
`frontend/dist/` is generated and ignored; Docker builds it in the image, and CI builds to a
|
||||
temporary output directory so clean clones never depend on stale hashed bundles.
|
||||
- **Deploy pattern:** commit + push → webhook auto-deploys in ~3 min. Cannot run Docker locally
|
||||
(no Docker on this Mac) → test with nginx/`python http.server` or `uv run python run.py`.
|
||||
- **No remote push without asking** unless it's the established auto-deploy cadence.
|
||||
@@ -93,6 +104,16 @@ cd backend && uv run python run.py # Flask :5001
|
||||
Responsive CSS (640px, single-column, `flex-wrap`, `.btn-back`) is present + deployed.
|
||||
|
||||
## Next actions / backlog (also docs/FUTURE_WORK.md)
|
||||
- **Sprint 1 live-operation gate:** deploy behind restricted access, set/verify `BOOTSTRAP_ADMIN_PASSWORD`, rotate `JWT_SECRET` after the patch, inspect audit data, and run a fresh authenticated smoke. Do not open public access before this checklist.
|
||||
- **Sprint 2:** implementation and local regression gate complete; retain current tests as the contract.
|
||||
- **P1:** run real-provider QA and browser/mobile E2E; local contract tests use deterministic fakes.
|
||||
- **P1:** verify final-judge coaching quality with representative provider outputs; correctness and idempotency are locally covered.
|
||||
- **P1:** remove any stale admin chat entry points in live UI after restricted authenticated smoke.
|
||||
- **P1/P2 ops:** add PostgreSQL repository adapters, browser E2E, and real-provider QA before production rollout. Upload limits, cleanup, fail-closed JWT/bootstrap configuration, Gunicorn, Docker healthcheck, and `.dockerignore` are implemented and tested locally.
|
||||
- **S4.2:** schema reviewer findings (nullable audit actor link, CWD-relative Alembic paths, ORM/migration defaults drift, unsupported partial-index dialect, and offline batch rendering) plus dependency reproducibility were remediated locally; combined exact-current evidence from `deleg_e672880a` and post-remediation `deleg_40e8edf9` closes the code/schema/dependency review scope. Temporary-local PostgreSQL execution, ORM/migration parity, offline DDL, and schema rollback now pass; Docker build, importer data parity/rollback, repository cutover, and production-safe rate-limit/audit storage remain blocked.
|
||||
- **S4.3:** org/users, groups/personas, and sessions/messages repository contracts and SQLAlchemy adapters are local-only; cross-tenant user lookup and offline-dialect remediations are locally verified and independently approved by `deleg_40e8edf9`. PostgreSQL schema parity passes on temporary local databases, while runtime repository cutover remains blocked.
|
||||
- **S4.4:** JSON importer dry-run, backup, idempotency, conflict rejection, cross-tenant validation, temporary-local PostgreSQL apply, and transaction rollback all pass; real target snapshot parity, retained-backup rollback rehearsal, audit migration, and apply approval remain blocked.
|
||||
- **2026-08-15 legacy-security remediation:** the latest valid reviewer finding about legacy `ratelimit.json` migration was remediated with fail-closed marker/digest validation, structured keys, duplicate-preserving import, and idempotent import metadata. Current local evidence is 319 backend tests, 166 focused regressions, and 4 frontend unit tests. Three fresh exact-current scoped reviewers returned clean five-key verdicts; see `docs/engineering-log/2026-08-15-final-security-gate.md`.
|
||||
- Real `/legal` page (setup links to it), billing/payments, per-tenant storage volume, compressed
|
||||
persona recipe, export-token polish.
|
||||
- Mobile visual polish per user feedback on real device.
|
||||
@@ -100,6 +121,18 @@ cd backend && uv run python run.py # Flask :5001
|
||||
|
||||
## Related docs
|
||||
- `docs/PLAN.md`, `docs/SAAS_PLAN.md`, `docs/FUTURE_WORK.md`.
|
||||
- `docs/engineering-log.md` + `docs/engineering-log/2026-08-09-idea-flow-qa-deploy.md` (this session:
|
||||
- `docs/engineering-log/2026-08-13-idea-implementation-audit.md` — latest evidence-based audit, runtime probes, and prioritized fixes.
|
||||
- `docs/engineering-log.md` — status index.
|
||||
- `docs/engineering-log/2026-08-15-final-security-gate.md` — final local verification and clean exact-current scoped review gate; production-operation limits remain.
|
||||
- `docs/engineering-log/2026-08-15-postgresql-runtime-gate.md` — temporary-local PostgreSQL schema/runtime, parity, offline DDL, and migration rollback evidence.
|
||||
- `docs/engineering-log/2026-08-15-postgresql-import-gate.md` — temporary-local PostgreSQL importer dry-run/apply/idempotency/conflict-rollback evidence.
|
||||
- `docs/test-evidence/2026-08-15-postgresql-runtime.md` — PostgreSQL automated gate evidence and remaining operational boundaries.
|
||||
- `docs/test-evidence/2026-08-15-postgresql-import.md` — importer evidence separated from real target apply and audit migration.
|
||||
- `docs/engineering-log/2026-08-09-idea-flow-qa-deploy.md` (this session:
|
||||
idea-flow UX, 2-scenario + recontact trait, live QA + auto-deploy, per-turn LLM judge, persona
|
||||
variant, 15-persona auto-gen).
|
||||
- `docs/engineering-log/2026-08-14-final-review.md` — prior five-finding remediation and review history; current exact-tree approval remains pending.
|
||||
- `docs/engineering-log/2026-08-15-auth-version-review-reconciliation.md` — current auth-version lock-path verification and pending fresh reviewer gate.
|
||||
- `docs/engineering-log/2026-08-15-s4-2-schema-foundation.md` — test-first SQLAlchemy/Alembic schema foundation, tenant constraints, and generated-dist cleanup.
|
||||
- `docs/engineering-log/2026-08-15-s4-3-org-users-repositories.md` — tenant-scoped repository contracts and adapters, with no runtime cutover.
|
||||
- `docs/engineering-log/2026-08-15-s4-3-offline-dialect-remediation.md` — offline Alembic dialect finding, test-first fix, verification, and pending review.
|
||||
+3
-2
@@ -379,8 +379,9 @@ i18n: en + th (mirrors MiroFish pattern). Role-based navigation (admin vs traine
|
||||
- **File safety**: upload allowed types + size caps; parse text server-side; strip anything
|
||||
executable; keep raw uploads out of any served path.
|
||||
- **LLM route discipline**: the chat + persona gen + judge are the only LLM-touching callers.
|
||||
- **Deploy**: single `Dockerfile` (python:3.11 + Node 18, build Vue → serve static via
|
||||
Flask or nginx) + `docker-compose.yml` with `.env`, per the user's EasyPanel pattern.
|
||||
- **Deploy**: single source-built `Dockerfile` (Node 22 frontend builder + Python 3.11 runtime,
|
||||
serving the generated SPA through Flask) + `docker-compose.yml` with `.env`, per the user's
|
||||
EasyPanel pattern. Generated `frontend/dist/` remains ignored; clean clones build it in Docker/CI.
|
||||
Local tests via `http.server` / Flask dev (no Docker on local Mac).
|
||||
|
||||
---
|
||||
|
||||
+26
-1
@@ -24,6 +24,12 @@ Informed by MiroFish (CrowdSight engine) + the hermes-brain-and-tools CrowdSight
|
||||
| M5 Trainee loop (board, weak-areas, gen-persona) | complete | 2026-08-07 | `test_e2e.py` | — |
|
||||
| M6 Frontend (Vue SPA) + static serving fix | complete | 2026-08-07 | build + live HTTP 200 | — |
|
||||
| M7 Docker/deploy/docs | complete | 2026-08-07 | Dockerfile/compose/README | live-key E2E |
|
||||
| Idea-to-implementation audit | deployment blocked | 2026-08-13 | `docs/engineering-log/2026-08-13-idea-implementation-audit.md` | complete Sprint 1 live-operation checklist |
|
||||
| Sprint 1 security hotfix | local verification + exact-tree reviewer passed; live-operation gate pending | 2026-08-14 | `docs/test-evidence/2026-08-13-sprint-1-security.md`, `docs/engineering-log/2026-08-14-sprint-1-current-tree.md` | operator-approved restricted deploy/rotation/smoke; then reassess Sprint 2 start |
|
||||
| Sprint 2–4 final code gate | S4.2/S4.3 code-schema review passed; legacy-security remediation and exact-current scoped review passed; live-operation gate pending | 2026-08-15 | `docs/engineering-log/2026-08-15-final-security-gate.md` | operator-approved restricted deploy + authenticated smoke only; production readiness remains pending |
|
||||
| S4.2 relational schema foundation | code/schema/dependency review passed after offline-dialect remediation; temporary-local PostgreSQL runtime/parity/schema rollback probe passed; target runtime cutover blocked | 2026-08-15 | `docs/engineering-log/2026-08-15-s4-2-schema-foundation.md`, `docs/engineering-log/2026-08-15-postgresql-runtime-gate.md`, `docs/test-evidence/2026-08-15-postgresql-runtime.md`, `backend/requirements.lock.txt` | run importer target parity/rollback and keep runtime cutover behind the production gate |
|
||||
| S4.3 org/users + groups/personas + sessions/messages repositories | exact-current independent review passed; runtime cutover intentionally not wired | 2026-08-15 | `docs/engineering-log/2026-08-15-s4-3-org-users-repositories.md`, `docs/engineering-log/2026-08-15-s4-3-offline-dialect-remediation.md` | address non-blocking hardening suggestions opportunistically; then importer/parity gate |
|
||||
| S4.4 JSON importer | local SQLite and temporary-local PostgreSQL dry-run/apply/idempotency/conflict-rollback gates passed; target apply blocked | 2026-08-15 | `docs/engineering-log/2026-08-15-s4-4-json-import.md`, `docs/engineering-log/2026-08-15-postgresql-import-gate.md`, `docs/test-evidence/2026-08-15-postgresql-import.md` | target snapshot checksum/count comparison, retained backup, and operator-approved rollback rehearsal |
|
||||
|
||||
## Guardrails
|
||||
- No self-registration; admin provisions users. (Verified: register => 404.)
|
||||
@@ -35,8 +41,27 @@ Informed by MiroFish (CrowdSight engine) + the hermes-brain-and-tools CrowdSight
|
||||
- `2026-08-07-build-out.md` — M0–M7 build-out, decisions, verification, current state.
|
||||
- `2026-08-07-security-ux.md` — security hardening (path traversal, IDOR, XSS) + UX/UI polish.
|
||||
- `2026-08-07-auth-gitea.md` — username login + first-time admin setup + Gitea push.
|
||||
- `2026-08-07-docker-final.md` — Docker build fix: ship prebuilt frontend/dist, no npm in image (resolves repeated `vite: not found`).
|
||||
- `2026-08-07-docker-final.md` — historical prebuilt-`frontend/dist` fix; superseded on 2026-08-15 by the source-built Docker/CI contract in the current Dockerfile.
|
||||
- `2026-08-07-login-after-setup.md` — can't-login-after-setup = deployment data non-persistence, not login logic (verified).
|
||||
- `2026-08-07-login-email-fix.md` — REAL fix: verify() resolves username OR email; "wrong password" after logout→login was email-login not resolving a user.
|
||||
- `2026-08-07-ui-tablayout.md` — 3-tab UI per spec: admin dashboard + date filter, personal dashboard, training w/ difficulty, settings profile.
|
||||
- `2026-08-09-idea-flow-qa-deploy.md` — idea-flow UX cleanups, 2-scenario model + recontact-as-trait, live QA on EasyPanel + auto-deploy, per-turn LLM judge for win/loss, persona variant, 15-persona auto-gen (this session).
|
||||
- `2026-08-13-idea-implementation-audit.md` — source-to-runtime audit; core flow verified, but independent probes reproduced arbitrary account takeover, two tenant bypasses, and additional product/runtime gaps.
|
||||
- `2026-08-13-sprint-1-security-hotfix.md` — S1.1–S1.6 implementation, verification, and deployment warning.
|
||||
- `2026-08-14-sprint-1-current-tree.md` — latest response-boundary and organization-update race fixes, exact verification evidence, and pending reviewer/live gates.
|
||||
- `2026-08-14-final-review.md` — final exact-tree reviewer approval, regression evidence, and explicit operational limits.
|
||||
- `2026-08-15-auth-version-review-reconciliation.md` — stale async auth-version finding, current lock-path verification, and pending fresh exact-tree review.
|
||||
- `2026-08-15-s4-2-schema-foundation.md` — test-first SQLAlchemy/Alembic schema foundation, tenant constraints, dependency lock, and generated-dist cleanup.
|
||||
- `2026-08-15-s4-3-org-users-repositories.md` — org/user repository contracts and SQLAlchemy adapters, with no runtime cutover.
|
||||
- `2026-08-15-s4-3-offline-dialect-remediation.md` — exact-current reviewer finding, test-first offline Alembic guard remediation, and fresh-review status.
|
||||
- `2026-08-15-legacy-security-review.md` — timed-out independent reviews and fail-closed gate handling.
|
||||
- `2026-08-15-legacy-security-review-retries.md` — repeated bounded retry timeouts and final pending reviewer.
|
||||
- `2026-08-15-legacy-security-remediation-verification.md` — historical 247-test verification, timeout handling, and prior scoped-review gate.
|
||||
- `2026-08-15-legacy-rate-limit-migration.md` — current legacy rate-limit migration remediation, 280/4 local evidence, and fresh pending review gates.
|
||||
- `2026-08-15-final-security-gate.md` — 319/166/4 local verification, dependency/tooling blocker, and three clean exact-current scoped reviewer verdicts; production-operation gate remains pending.
|
||||
- `2026-08-15-postgresql-runtime-gate.md` — temporary-local PostgreSQL upgrade, tenant/uniqueness probe, ORM/migration parity, offline DDL, and schema downgrade evidence; target cutover remains pending.
|
||||
- `2026-08-15-postgresql-import-gate.md` — temporary-local PostgreSQL importer dry-run, apply, idempotency, conflict rejection, transaction rollback, and cleanup evidence; target apply remains pending.
|
||||
- `docs/test-evidence/2026-08-15-legacy-security-remediation.md` — test-first remediation, local verification, and independent-review boundary.
|
||||
- `docs/test-evidence/2026-08-15-postgresql-runtime.md` — automated PostgreSQL gate evidence separated from Docker, Redis, importer, and production blockers.
|
||||
- `docs/test-evidence/2026-08-15-postgresql-import.md` — temporary-local PostgreSQL importer evidence and target-operation boundary.
|
||||
- `2026-08-15-s4-4-json-import.md` — fail-closed dry-run/apply importer, idempotency, backup, and parity blockers.
|
||||
@@ -8,19 +8,19 @@ first-time admin setup (set email + change password), pushed the repo to Gitea.
|
||||
1. **User id = username** (was email). Email is now a separate optional field with uniqueness.
|
||||
- `UserStore.create_user(username, email=None, ...)`; login via username; JWT `sub` = username.
|
||||
- Admin user-creation uses `username` (email fallback kept for compatibility).
|
||||
2. **Default admin**: `admin` / `1234`, with `must_setup=True`.
|
||||
2. **Default admin**: `admin` / `[REDACTED]`, with `must_setup=True`.
|
||||
3. **Forced first-time setup**: after login with the default creds, the frontend router guards
|
||||
and sends the user to `/setup` — set email + new password (+ confirm), then `must_setup` clears.
|
||||
- Backend: new `POST /api/auth/setup` (`complete_setup` sets email + password, clears flag).
|
||||
- Login response now includes `must_setup`; `me` includes it too.
|
||||
- Frontend: `Login.vue` uses username, `Setup.vue` (new), router guard, `auth.finishSetup`.
|
||||
4. **Docs**: README + HANDOFF + build-out log updated to `admin`/`1234` + setup flow.
|
||||
4. **Docs**: README + HANDOFF + build-out log updated to the historical bootstrap + setup flow.
|
||||
|
||||
## Verification
|
||||
- Added `test_setup.py` (admin/1234 → must_setup → set email+password → old pw invalid, new pw
|
||||
- Added `test_setup.py` (bootstrap → must_setup → set email+password → old pw invalid, new pw
|
||||
works, admin can use app). ALL PASS.
|
||||
- Adapted m0/m1/routes/security/e2e to username creds. **All 6 suites PASS.**
|
||||
- Frontend `npm run build` ok. Live server: `admin`/`1234` login returns `must_setup=true`,
|
||||
- Frontend `npm run build` ok. Live server: bootstrap login returns `must_setup=true`,
|
||||
SPA served at `http://localhost:5001`.
|
||||
|
||||
## Push
|
||||
|
||||
@@ -54,7 +54,7 @@ sessions→debrief(latent reveal+coaching)→one-shot→board→weak-areas→gen
|
||||
|
||||
## Current state / runtime
|
||||
- Backend runs via `cd backend && uv run python run.py`; frontend dev via `cd frontend && npm run dev` (proxies /api -> :5001).
|
||||
- Default super-admin: `admin` / `1234` (bootstrap; forces email + password change on first login).
|
||||
- Default super-admin: `admin` / `[REDACTED]` (historical bootstrap; forces email + password change on first login).
|
||||
- Deploy files: root `Dockerfile`, `docker-compose.yml`, `.env.example`; repo-root `frontend/dist` build.
|
||||
|
||||
## Risks / remaining
|
||||
|
||||
@@ -28,7 +28,7 @@ Single-stage, no node/npm/vite anywhere in commands:
|
||||
- `frontend/dist/` (28 files) tracked and pushed on `origin/main`.
|
||||
- Backend `_register_frontend` path (`Path(__file__).parent.parent.parent/frontend/dist`)
|
||||
matches the Docker `COPY` target `/app/frontend/dist`.
|
||||
- Local server: `GET /` 200 (serves the new tabbed SPA), health ok, `admin`/`1234` login 200.
|
||||
- Local server: `GET /` 200 (serves the new tabbed SPA), health ok, bootstrap login 200.
|
||||
|
||||
## Convoying workflow for UI changes
|
||||
1. `cd frontend && npm run build`
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
# 2026-08-07 — Bug: can't log in after first-run setup (password wrong) — root cause + fix
|
||||
|
||||
## Symptom
|
||||
After first login (admin/1234) → change password in the forced setup screen → logout → logging
|
||||
After first login (admin/[REDACTED]) → change password in the forced setup screen → logout → logging
|
||||
back in with the new password reports "wrong password".
|
||||
|
||||
## Investigation (verified via reproduction)
|
||||
@@ -16,7 +16,7 @@ back in with the new password reports "wrong password".
|
||||
On EasyPanel / any deploy that doesn't mount a volume, the container's `DATA_DIR`
|
||||
(`/app/backend/data`) is **ephemeral — wiped on every container recreate/restart**. So after
|
||||
setup, when the container is recreated:
|
||||
- the changed admin password is lost (record resets to `1234` / `must_setup=true`),
|
||||
- the changed admin password is lost (record resets to `[REDACTED]` / `must_setup=true`),
|
||||
- logging back in with the new password fails ("รหัสผ่านผิด").
|
||||
|
||||
## Fix
|
||||
@@ -29,7 +29,7 @@ setup, when the container is recreated:
|
||||
- **EasyPanel**: add a persistent volume mount → `/app/backend/data` (or set `DATA_DIR` env to a
|
||||
mounted path). After that, data survives redeploys.
|
||||
- If they already lost data (old password gone): delete/reset the container data so a fresh
|
||||
`admin`/`1234` is bootstrapped, then redo setup and NEITHER redeploy without a volume.
|
||||
`admin`/`[REDACTED]` is bootstrapped, then redo setup and NEITHER redeploy without a volume.
|
||||
|
||||
## Note
|
||||
Local dev: `backend/data/` is gitignored and lives on disk, so this only bites container deploys.
|
||||
@@ -31,7 +31,7 @@ delivered it to the live deployment.
|
||||
|
||||
## 3. Live QA + the bugs it surfaced
|
||||
Deployed to `moreminimoreapps-saletrainer.ahkhwd.easypanel.host` (auto-deploy via Gitea webhook on
|
||||
push to main). Verified via live API (deterministic) + Chrome via computer-use (login testadmin/1234,
|
||||
push to main). Verified via live API (deterministic) + Chrome via computer-use (login test accounts,
|
||||
testuser/1234).
|
||||
- **Bug (blocker): analyze 500 when LLM returns < 15 personas.** Real deepseek occasionally returns
|
||||
14. Fixed `persona_generator.generate`: **retry up to 3×**, then **accept short (≥ 8)** instead of
|
||||
@@ -72,7 +72,7 @@ test_saas_tenant, test_user_journey, test_variant, test_resume_decision` — **1
|
||||
## Live state
|
||||
- Live serves `index-rfeHzF-F.js` = latest build (verified). Auto-deploy on push (Gitea webhook).
|
||||
- Real LLM (deepseek) requires `LLM_API_KEY` set in EasyPanel env — local `.env` has placeholder.
|
||||
- testadmin=admin, testuser=user (both pass `1234`); bootstrap super-admin also exists.
|
||||
- testadmin=admin, testuser=user (historical test credentials redacted); bootstrap super-admin also exists.
|
||||
- A test group "CRM ระบบจัดการลูกค้า" exists on live (user keeps it; will delete themselves).
|
||||
|
||||
## Open / future (see docs/FUTURE_WORK.md)
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
# 2026-08-13 — Idea-to-implementation audit
|
||||
|
||||
## Scope
|
||||
|
||||
Compared `IDEA.md`, `README.md`, `docs/PLAN.md`, and `docs/FUTURE_WORK.md` against the Vue frontend, Flask routes/services, JSON storage, deployment files, and all executable test scripts. No production data or credentials were inspected.
|
||||
|
||||
## Architecture traced
|
||||
|
||||
- Admin setup: `GroupBuilder.vue` → `POST /api/groups` → document parser/`GroupStore` → `POST /api/groups/<gid>/analyze` → `Analyzer` → `PersonaGenerator` → report → JSON group storage.
|
||||
- Training: `Training.vue` → `Personas.vue` → `Chat.vue` → chat start/send routes → `Simulator.persona_reply` + `Simulator.evaluate_turn` → `SessionStore` → debrief/board/analytics.
|
||||
- Trainee loop backend: `/api/me/weak-areas` + `/api/me/personas/generate` → weak-area analyzer/own-persona generator → private group. API clients exist, but no Vue view invokes them.
|
||||
- Persistence: one JSON file per entity, atomic file replacement and in-process per-path locks.
|
||||
|
||||
## Verification performed
|
||||
|
||||
- All 12 executable backend scripts passed: M0, M1, setup, security, scenario, E2E, IP protection, SaaS tenant, user journey, routes, resume/decision, variant.
|
||||
- `python -m compileall app scripts` passed.
|
||||
- `npm run build` passed (1,772 modules; production bundle generated).
|
||||
- Flask smoke test passed: `/health` 200, SPA `/` 200, unauthenticated `/api/groups` 401.
|
||||
- Docker image was not built because Docker is not installed on this Mac.
|
||||
- Real-provider LLM behavior was not exercised because no live key was used.
|
||||
|
||||
## Findings
|
||||
|
||||
### P0 — Any authenticated user can take over an arbitrary account
|
||||
|
||||
`auth_routes.py:56-72` accepts a client-supplied `username` in `/api/auth/setup` and passes it to `complete_setup()` instead of deriving the target exclusively from `current_user()`. It also does not require the caller's `must_setup` flag. A fresh isolated-data runtime probe confirmed that an ordinary user can target `admin`, replace its email/password, and then log in as that super-admin (setup 200; takeover login 200). This is an immediate deployment blocker.
|
||||
|
||||
### P0 — Tenant admins can reset credentials across tenants
|
||||
|
||||
`admin_routes.py:141-177` loads the requested user but never verifies that a non-super-admin actor and target share the same `org_id`. Password and email mutation therefore cross tenant boundaries. A runtime probe confirmed an admin in `org-two` could reset a user in `org-default` and then log in as that victim (update 200; hijacked login 200).
|
||||
|
||||
### P0 — Tenant admins can provision users into another tenant
|
||||
|
||||
`admin_routes.py:87-126` trusts the request's `org_id` for existing organizations. The super-admin guard applies only when `new_org=true`. A runtime probe confirmed an `org-two` admin could create a user directly inside `org-default` (create 201; new-user login 200), bypassing tenant membership and seat ownership boundaries.
|
||||
|
||||
### P0 — Admin secret fields leak from analyze response
|
||||
|
||||
`group_routes.py:233-243` returns the full `group`, `sales_kit`, and full personas after analysis, without applying the admin redaction used by the GET routes at `group_routes.py:253-265`. A runtime probe as a non-super admin confirmed `pains` and `tolerance` are present in the analyze response, while the subsequent list endpoint strips them. This defeats the documented IP boundary during the normal group-creation flow.
|
||||
|
||||
### P1 — Persona replies render as raw JSON with compliant real LLMs
|
||||
|
||||
`simulator.py:43-48` instructs the role-play model to return a JSON object, but `simulator.py:142-149` now treats the entire response as plain text instead of parsing `reply`. A deterministic probe returned the full `{"reply":...,"decision":...,"mood":...}` blob as the customer message. The mock also returns JSON (`scripts/mock_llm.py:113-120`), but tests only assert that `reply` is truthy, so this escaped detection.
|
||||
|
||||
### P1 — Normal auto-finish bypasses the promised final judge debrief
|
||||
|
||||
Every turn is judged by `evaluate_turn`; when it says buy/walk, `chat_routes.py:301-318` builds `_build_abbrev_debrief` instead of calling `Simulator.judge`. That abbreviated report uses fixed scores (60/25) and generic coaching (`chat_routes.py:62-118`). The detailed final judge exists only in the separate finish endpoint (`chat_routes.py:325-367`), but the current `Chat.vue` has no finish action. Therefore the normal flow does not deliver the README promise of a separately judged, message-specific scored/coached debrief.
|
||||
|
||||
### P1 — Admin UI offers chat actions that backend rejects
|
||||
|
||||
Admin-facing persona screens link to the trainee chat (`GroupEdit.vue:40-44`, `Personas.vue:42-45`), while chat start/send/finish routes require role `user` (`chat_routes.py:137-140`, `208-211`, `325-328`). Admins following the visible action receive 403.
|
||||
|
||||
### P1 — Claimed training loop is backend-only
|
||||
|
||||
README lines 32-33 promise weak-area analysis and user-generated personas. Backend routes are implemented (`me_routes.py:51-59`, `83-122`) and API client methods exist (`frontend/src/api/index.js:58-60`), but no Vue view calls them. `MyBoard.vue:1-55` only shows counts and sessions. Trainees cannot use the promised weak-area lock/manual persona generation through the product UI.
|
||||
|
||||
### P1 — Users cannot manually finish a stalled conversation or revisit a completed debrief
|
||||
|
||||
The API client and backend expose manual finish (`frontend/src/api/index.js:55`; `chat_routes.py:325-367`), but `Chat.vue` never calls it and only transitions to done when a send response reports `finished` (`Chat.vue:155-165`). If the per-turn judge never returns buy/walk, the user has no end-session control. After leaving a completed chat, `MyBoard.vue:22-28` provides no session-detail link and the API client has no finished-session detail method, so transcripts and coaching cannot be revisited from the UI.
|
||||
|
||||
### P1 — “15 personas” is not an enforced invariant
|
||||
|
||||
`persona_generator.py:35-53` retries, then accepts any non-empty list; a runtime probe with an LLM returning one persona produced a ready result containing one persona. The normalization can also underfill tiers (`persona_generator.py:55-90`). This conflicts with the central 15-persona/5-per-tier promise. Current E2E passes only because the mock always supplies 15.
|
||||
|
||||
### P0 — One-shot identity collides across groups and is raceable
|
||||
|
||||
`SessionStore.create` keys the business rule by `(user_id, persona_id)` but omits `group_id` (`sessions.py:33-40`), while independently generated groups reuse IDs such as `persona-01` (`persona_generator.py:67`). A runtime probe confirmed finishing `persona-01` in one group blocks `persona-01` in another unrelated group. Active-session discovery has the same missing scope (`sessions.py:69-75`). The check and random-file creation are also separate operations: a synchronized 8-thread probe created eight sessions for one user/persona. The implementation therefore both over-blocks legitimate training and under-blocks concurrent duplicates.
|
||||
|
||||
### P1 — Bootstrap credentials are privileged before mandatory setup
|
||||
|
||||
`factory.py:13-31` creates a known bootstrap super-admin and marks it `must_setup`, but `require_auth` never enforces that flag (`helpers.py:44-63`). The independent runtime probe confirmed the pre-setup bootstrap account could perform a privileged group creation. A fresh Internet-facing deployment can therefore be claimed before the operator changes credentials.
|
||||
|
||||
### P1 — Tenant deactivation does not revoke existing tokens
|
||||
|
||||
Organization activity is checked at login (`auth/users.py:172-186`) but not in `require_auth` (`helpers.py:44-63`). An isolated runtime probe confirmed a token issued before tenant deactivation continued to access `/api/auth/me` with 200 afterward. With the default 24-hour JWT lifetime, disabling an organization does not promptly disable its sessions.
|
||||
|
||||
### P1 — Production container runs Flask development server
|
||||
|
||||
Docker executes `python run.py` (`Dockerfile:29-39`), and `run.py:13-20` uses `app.run`. Flask itself warns this is not a production server. It may work for a small internal pilot but is not production-grade serving, graceful lifecycle, or concurrency management.
|
||||
|
||||
### P1 — Trainee-created variants mutated the shared corporate group (resolved in Sprint 1 follow-up)
|
||||
|
||||
The original audit found that the variant route appended directly to the shared group's personas. The follow-up now calls `get_or_create_private_group()` for trainee actors, stores the variant there, returns the target group ID, and routes the UI to that group (`backend/app/api/group_routes.py:373-402`, `backend/app/services/groups.py:74-102`, `backend/tests/test_group_redaction.py:180-242`). Admin-created variants still extend the shared admin pool by design. Collection-level scan/create atomicity remains deferred to Sprint 2.
|
||||
|
||||
### P2 — Upload size configuration is unused
|
||||
|
||||
`Config.UPLOAD_MAX_MB` is defined (`config.py:62`) but never applied to Flask `MAX_CONTENT_LENGTH` or route-level size validation. File type and path checks exist, but upload memory/disk consumption is effectively unbounded at the application layer.
|
||||
|
||||
### P2 — Product copy and dead append flow contradict the decided UX
|
||||
|
||||
The intended design says auto-create 15 with no add-more flow, but `GroupBuilder.vue:12` and `GroupEdit.vue:21-23` still tell admins to click an absent “create more” button. `GroupEdit.vue:83-92` retains an unreachable append function, and the backend still supports `?append=true` (`group_routes.py:183-190`).
|
||||
|
||||
If append mode is later re-exposed, it also concatenates newly generated personas without replacing IDs (`group_routes.py:219-228`) while generation restarts at `persona-01` (`persona_generator.py:55-68`). This would create duplicate Vue keys, ambiguous persona lookup, and shared one-shot history.
|
||||
|
||||
### P2 — Deployment defaults are unsafe if operators omit configuration
|
||||
|
||||
`config.py:50` falls back to a short public development JWT secret; runtime emitted an insecure HMAC key warning. This should fail closed outside development rather than silently issue production tokens with the fallback.
|
||||
|
||||
### P2 — Non-UTF-8 file parsing imports an undeclared dependency
|
||||
|
||||
`file_parser.py:26` imports `chardet`, but `requirements.txt:1-9` installs `charset-normalizer` instead. The reviewer probe confirmed a non-UTF-8 input reaches `ModuleNotFoundError: chardet`. UTF-8 inputs pass, which is why the current scripts do not expose the deployment defect.
|
||||
|
||||
### P2 — Settings password change is wired to first-time setup and fails
|
||||
|
||||
`Settings.vue:88-102` calls `/auth/setup` without `accepted_terms`, while that route requires the field at `auth_routes.py:65-69`. The same route is also semantically unsafe for repeat password changes and caused the P0 arbitrary-target takeover above. Password change needs a separate current-user endpoint with appropriate current-password/session safeguards.
|
||||
|
||||
### P2 — Docker build context can include local secrets
|
||||
|
||||
The Dockerfile copies the complete backend directory (`Dockerfile:26-27`), and the repository has no `.dockerignore`. Git ignore rules do not constrain Docker build context, so a local `backend/.env` or other untracked development artifact can be copied into an image layer. Add a restrictive `.dockerignore` and verify the built image contains no local configuration or credentials.
|
||||
|
||||
### P2 — Active sessions are displayed as losses
|
||||
|
||||
`/api/chat/sessions` returns active and finished sessions, but `MyBoard.vue:27` renders every outcome other than `won` as `lost`. Since active sessions have `outcome: null` (`sessions.py:49-53`), unfinished training is falsely reported as a loss instead of an active/resume state.
|
||||
|
||||
### Reviewer claim rejected — analytics CSV Bearer header is correct
|
||||
|
||||
One reviewer reported a malformed authorization header because its tool output had redacted the token scheme. Direct byte/codepoint inspection of `Analytics.vue:92` confirmed the source contains the correct `Bearer ${auth.token}` template string, and the frontend production build succeeds. This is not a defect. The separate signed-link route remains unreachable without Bearer auth because route decorators run before query-token verification, but the currently shipped frontend uses Bearer auth directly.
|
||||
|
||||
## Capability verdict
|
||||
|
||||
Core concept is substantially implemented: roles, group creation, file parsing, LLM sales-kit/persona generation, tiers, scenarios, chat/resume, per-turn decision, variants, session results, analytics, and static deployment all exist and pass mock-based tests.
|
||||
|
||||
However, the application must not be deployed to untrusted users in its current state. Independent review plus isolated-data reproduction confirmed arbitrary super-admin takeover and two cross-tenant administration bypasses. Beyond those security blockers, the product path has a secret-data leak, likely raw-JSON customer messages with a compliant real model, generic rather than final-judge coaching, inaccessible weak-area training UI, a broken one-shot identity invariant, and a development server in production packaging. Passing mock scripts materially overstate readiness because none cover these attack paths.
|
||||
|
||||
## Recommended order
|
||||
|
||||
1. Freeze deployment and fix `/auth/setup` arbitrary-target takeover; add an ordinary-user-to-super-admin regression test.
|
||||
2. Enforce tenant scope on admin user creation/update and add cross-org denial tests.
|
||||
3. Enforce `must_setup` server-side, remove known bootstrap privilege exposure, and re-check organization activity on authenticated requests.
|
||||
4. Fix analyze-response redaction and add a regression test.
|
||||
5. Redesign session identity as `(org_id, user_id, group_id, persona_id)` and enforce creation atomically.
|
||||
6. Align persona prompt/response parsing and make auto-finish invoke the final judge; test exact visible messages and non-generic coaching.
|
||||
7. Remove admin chat affordances or deliberately support admin preview mode; wire weak-area/manual persona generation into the trainee UI.
|
||||
8. Enforce exactly 15/5-per-tier or make partial generation explicit and recoverable.
|
||||
9. Add a transactional database, production WSGI server, upload limit, fail-closed JWT configuration, dependency lock, and CI security gates.
|
||||
10. Run real-provider, hostile-input, concurrency, and mobile browser QA after the above.
|
||||
@@ -0,0 +1,136 @@
|
||||
# 2026-08-13 — Sprint 1 security hotfix
|
||||
|
||||
## Scope
|
||||
|
||||
Executed the first gated remediation sprint from `.hermes/plans/2026-08-13_094159-sales-trainer-remediation-all-sprints.md`. Work stayed on isolated temporary test data; no production `.env`, credentials, commit, push, or deployment action was performed.
|
||||
|
||||
## Changes
|
||||
|
||||
- Added standard pytest discovery and isolated Flask fixtures under `backend/tests/`.
|
||||
- Bound `/api/auth/setup` to the authenticated user and required `must_setup` plus terms consent.
|
||||
- Enforced tenant scope on admin user provisioning and updates.
|
||||
- Added request-level checks for active user, active/existing organization, JWT org claim consistency, and setup-safe endpoint allowlist.
|
||||
- Removed known bootstrap password and implicit production JWT fallback; production now requires explicit `JWT_SECRET` and first-boot `BOOTSTRAP_ADMIN_PASSWORD`.
|
||||
- Unified role-aware group/persona serializers so analyze, GET, list, update, and variant responses follow the same redaction policy.
|
||||
- Kept trainee-created persona variants out of the shared corporate group by storing them in an owner-private group, returning the target `group_id`, and routing the trainee UI to that private group.
|
||||
- Redacted trainee group input down to high-level context only; uploaded filenames and parsed source text are not returned by the group view.
|
||||
- Preserved the platform-admin contract on group listing: super-admins can index groups across tenants, while tenant admins remain organization-scoped.
|
||||
- Updated deterministic legacy scripts to inject synthetic test bootstrap configuration rather than relying on a known default credential.
|
||||
- Follow-up remediation replaced admin persona blacklist stripping with an explicit allowlist,
|
||||
sanitized client/persisted exception messages, added request/per-file upload limits and cleanup,
|
||||
blocked super-admin creation/promotion and protected-field mutation through admin APIs, required
|
||||
strict JWT tenant claims and boolean terms consent, denied orphan-tenant login, and routed private
|
||||
trainee-persona responses through the revealable serializer.
|
||||
|
||||
## Verification
|
||||
|
||||
- `backend/.venv/bin/python -m pytest backend/tests -q` → **51 passed**, including threaded and forked-worker atomic setup regressions plus concurrent email-reservation coverage across setup, profile, and invited-user creation.
|
||||
- `backend/.venv/bin/python -m pytest backend/tests/test_auth_security.py -q` → **12 passed**, including literal terms-consent boundary and no-partial-update failure-path checks.
|
||||
- `backend/.venv/bin/python -m pytest backend/tests/test_group_redaction.py -q` → **8 passed**, including source-data redaction and cross-user private-variant isolation.
|
||||
- `backend/.venv/bin/python -m pytest backend/tests/test_sprint1_review_findings.py -q` → **11 passed**.
|
||||
- `backend/.venv/bin/python -m compileall -q backend/app backend/tests backend/scripts` → **passed**.
|
||||
- All 12 `backend/scripts/test_*.py` suites → **12/12 passed**.
|
||||
- `cd frontend && npm run build` → **passed; 1,772 modules transformed**.
|
||||
- `git diff --check` → **passed**.
|
||||
- Added the reviewer-requested race regressions for first-time setup, protected cross-user email
|
||||
uniqueness with a collection-level lock covering setup, profile, and invited-user creation, and
|
||||
changed the transition to a conditional per-record locked update with an OS file lock for forked
|
||||
workers; all focused concurrency tests and the full suite now pass.
|
||||
|
||||
Full evidence: `docs/test-evidence/2026-08-13-sprint-1-security.md`.
|
||||
|
||||
## Remaining gate / deployment warning
|
||||
|
||||
Sprint 1 code-side tests pass, including the private-variant, source-data-redaction, upload-boundary,
|
||||
generic-error, strict-tenant-claim, protected-super-admin, and create-user/setup email-race regressions.
|
||||
The earlier focused reviewer verdict found the create-user email race and predates this follow-up
|
||||
remediation; a fresh independent reviewer rerun is pending. Docker is
|
||||
unavailable locally, real-provider QA is not run, and live credential/JWT rotation has not been
|
||||
performed. Do not restore public/untrusted access until both the fresh reviewer acceptance and the
|
||||
operator restricted-deployment checklist are complete: set a strong bootstrap password, rotate
|
||||
`JWT_SECRET` after patch deployment, inspect audit data, and run a fresh authenticated smoke test.
|
||||
|
||||
Sprint 2 is not started. Known Sprint 2 targets remain one-shot identity/concurrency, persona reply parsing, final-judge debrief, and admin Preview Mode.
|
||||
|
||||
## Follow-up reviewer remediation — 2026-08-13
|
||||
|
||||
The fresh reviewer identified blocking classes: raw exception disclosure in setup/session/persona
|
||||
routes, a parser-import upload-cleanup hole, open-ended group envelopes, unsafe implicit runtime
|
||||
defaults, and `bool("false")` active-state coercion. A RED/GREEN cycle added regression tests before
|
||||
each fix.
|
||||
|
||||
Implemented fixes:
|
||||
|
||||
- Parser import and all post-save parsing/persistence now share a `finally` cleanup boundary; multi-file,
|
||||
import-failure, missing-input, request-limit, save-failure, and persistence-failure paths remove every
|
||||
saved file.
|
||||
- Auth/setup/profile/session/persona public errors use fixed messages; logs retain only exception types.
|
||||
- Group envelope and tenant-admin input serialization are explicit allowlists; legacy error text remains
|
||||
normalized and trainee/admin source payloads are omitted.
|
||||
- Runtime defaults are production/secure (`APP_ENV=production`, `FLASK_DEBUG=false`); `.env` no longer
|
||||
overrides explicitly supplied process environment; placeholder JWT and bootstrap values are rejected;
|
||||
malformed stored active state fails closed.
|
||||
- Admin invites now pass the requested email into `create_user()`, preserving the global email invariant;
|
||||
admin `active` and `new_org` fields require literal JSON booleans. Setup uses a dedicated typed error for
|
||||
its 409 response rather than inspecting exception text.
|
||||
|
||||
Verification after the follow-up:
|
||||
|
||||
- Full backend pytest before the final reviewer follow-up: **65 passed**.
|
||||
- Focused security suites before the final reviewer follow-up: auth **12**, group redaction **9**,
|
||||
Sprint 1 findings **23**, request guards **9**, bootstrap config **8**.
|
||||
- Backend scripts: **12/12 passed**; compileall, AST parse, diff check, and production added-line security
|
||||
scan passed with **0 blocking matches**.
|
||||
- Frontend build: **1,772 modules transformed; passed**.
|
||||
|
||||
The final fresh independent reviewer gate is still pending. No commit, push, deploy, credential rotation,
|
||||
JWT rotation, or public-access change occurred.
|
||||
|
||||
## Final reviewer follow-up — 2026-08-13
|
||||
|
||||
The first fresh reviewer rerun identified two blocking service-boundary gaps: setup eligibility was
|
||||
not proven strict at every layer, and `complete_setup()` did not receive/validate terms consent itself.
|
||||
The follow-up used a RED/GREEN cycle:
|
||||
|
||||
- Added `accepted_terms` and `accepted_terms_at` as required keyword arguments to
|
||||
`UserStore.complete_setup()` and enforced `accepted_terms is True` before any mutation.
|
||||
- Added a `create_user()` boundary check rejecting non-boolean `must_setup` values.
|
||||
- Added a dedicated `set_email()` versus `complete_setup()` concurrency regression test.
|
||||
- Updated all direct setup callers to pass explicit literal consent and synchronized the test evidence.
|
||||
|
||||
Latest verification:
|
||||
|
||||
- Full backend pytest: **77 passed**.
|
||||
- Focused suites: auth **17**, group redaction **9**, Sprint 1 findings **24**, request guards **12**,
|
||||
bootstrap config **8**.
|
||||
- Auth security suite repeated **5/5** times for concurrency stability.
|
||||
- Backend scripts **12/12 passed**; frontend production build passed with **1,772 modules transformed**.
|
||||
- `compileall`, `git diff --check`, and direct `complete_setup()` call-contract scan passed.
|
||||
- Static added-line scan passed with zero matches for hardcoded secrets, shell injection, eval/exec,
|
||||
unsafe pickle deserialization, or SQL string formatting.
|
||||
- Login response serialization now uses `user.get("must_setup") is True`, removing the last reviewed
|
||||
truthiness coercion from the setup flag.
|
||||
- `assert_tenant(None)` now fails closed instead of treating a missing object tenant as `org-default`;
|
||||
a dedicated request-guard regression covers this boundary.
|
||||
|
||||
The independent reviewer verdict for this latest state remains pending. No commit, push, deploy,
|
||||
credential rotation, JWT rotation, or public-access change occurred.
|
||||
|
||||
## Final independent reviewer gate — 2026-08-13
|
||||
|
||||
The fresh independent read-only reviewer completed a schema-valid final review of the current
|
||||
uncommitted Sprint 1 packet and returned `passed=true` with empty `security_concerns` and
|
||||
`logic_errors`. The reviewer found no blocking security or logic issue in the reviewed packet.
|
||||
|
||||
The reviewer recorded only non-blocking follow-up suggestions: move private-group scan/create to a
|
||||
collection-level atomic primitive in Sprint 2, and keep Docker, real-provider, browser/mobile,
|
||||
deployment, and bootstrap/JWT rotation checks as separate operational gates.
|
||||
|
||||
Therefore the Sprint 1 **code/reviewer gate is closed**. The **live-operation gate remains pending**:
|
||||
no commit, push, deploy, credential rotation, JWT rotation, or public-access change occurred. Before
|
||||
restoring untrusted access, an operator must perform restricted deployment, set/rotate the bootstrap
|
||||
credential and `JWT_SECRET`, inspect audit data, and run a fresh authenticated smoke test.
|
||||
|
||||
## Superseded current-state note — 2026-08-14
|
||||
|
||||
The reviewer result described in the preceding historical section belonged to an earlier packet/state. It does not approve the current uncommitted tree after the subsequent password-policy, atomic-mutation, malformed-input, user/session-serialization, debrief-allowlist, and tenant-fallback fixes. The current exact-tree reviewer gate is pending; see `docs/engineering-log/2026-08-14-sprint-1-current-tree.md` and the current evidence section.
|
||||
@@ -0,0 +1,46 @@
|
||||
# 2026-08-14 — Final exact-tree review gate
|
||||
|
||||
## Scope
|
||||
Closed the post-remediation code-review loop for Sprint 2–4 implementation. No commit, push, deploy, credential rotation, JWT rotation, or public-access change was performed.
|
||||
|
||||
## Remediation verified
|
||||
The final independent review specifically checked and found no blocker in:
|
||||
|
||||
- trainee access to draft/failed/analyzing groups by guessed ID;
|
||||
- hardest-persona ranking (loss count descending, then average score ascending);
|
||||
- atomic group-analysis publication of `sales_kit`, `personas`, `report`, and `status=ready` under the group record lock;
|
||||
- group deletion versus session mutation/start, including per-session record/process locks;
|
||||
- organization active/seat invariants shared by user provisioning and org updates;
|
||||
- first-run bootstrap serialization across workers;
|
||||
- current-user/org/role-bound export authorization plus session `org_id` and user-membership filtering;
|
||||
- nested JSON record-lock reentrancy, tenant-bound session scope, and mutation paths.
|
||||
|
||||
Five regression tests were added in `backend/tests/test_final_review_regressions.py` for the concrete review findings.
|
||||
|
||||
## Verification evidence
|
||||
- Canonical backend suite: **152 passed** (`backend/.venv/bin/python -m pytest -q backend/tests`).
|
||||
- Final-review regression subset: **5 passed** (`backend/.venv/bin/python -m pytest -q backend/tests/test_final_review_regressions.py`).
|
||||
- Executable backend scripts: **12/12 passed**.
|
||||
- Frontend production build: **1,778 modules transformed**; temporary artifact check: **2 asset references / 0 missing**.
|
||||
- `compileall`, `git diff --check`, and CI YAML parsing: passed.
|
||||
- Added-line static security scan: no hardcoded-secret assignment, shell injection, dangerous `eval/exec`, pickle deserialization, or formatted SQL execution patterns detected.
|
||||
|
||||
## Independent reviewer verdict
|
||||
A fresh bounded reviewer inspected the exact current tree and the nine relevant implementation/test files without modifying them:
|
||||
|
||||
```json
|
||||
{
|
||||
"passed": true,
|
||||
"security_concerns": [],
|
||||
"logic_errors": [],
|
||||
"suggestions": [
|
||||
"The direct pytest invocation for tests/test_final_review_regressions.py reported no tests collected under the current configuration; rerun through the repository's intended test command.",
|
||||
"Consider adding an explicit interleaving test for group deletion versus concurrent session mutation/start to complement the lock-presence regression."
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
The first suggestion was verified with the repository-root command above and passed 5 tests. The second is non-blocking: lock-presence and existing concurrency tests pass, while a true multi-process interleaving test remains useful future hardening.
|
||||
|
||||
## Remaining gates
|
||||
Docker is unavailable on this Mac, so image build/runtime was not exercised locally; CI contains the clean multi-stage build and `/ready` smoke path. Real-provider QA, browser/mobile E2E, PostgreSQL/Redis validation, production authenticated smoke, restricted deploy, bootstrap-password rotation, and JWT rotation remain operational gates requiring explicit operator approval where applicable.
|
||||
@@ -0,0 +1,68 @@
|
||||
# 2026-08-14 — Sprint 1 current-tree security verification
|
||||
|
||||
## Status
|
||||
|
||||
Sprint 1 code changes remain uncommitted. Local verification is complete and passing, and the exact-current-tree independent reviewer gate is closed. The separate live-operation gate is pending. No commit, push, deploy, credential/JWT rotation, or public-access change was performed.
|
||||
|
||||
## Latest remediation
|
||||
|
||||
After the earlier password-policy finding, the current tree also closes these response and tenant-boundary classes:
|
||||
|
||||
- `UserStore` and HTTP/admin password paths share the 12-character minimum; weak/non-string credentials fail before mutation.
|
||||
- User/admin serialization uses a closed allowlist; `password_hash` and future internal user fields are omitted from listing and mutation responses.
|
||||
- Authenticated group/private-persona mutations reject missing tenant identity instead of falling back to `org-default`.
|
||||
- Admin organization `plan`, `seats`, `active`, and `new_org` inputs use strict types.
|
||||
- Provider-controlled final judge output is constrained to a closed debrief envelope.
|
||||
- Chat session start/resume/send/finish/list/get responses use a closed session serializer and omit `user_id`, hidden `internal` judge state, provider metadata, and unknown message fields.
|
||||
- Group serializers fail closed for malformed stored input/persona shapes; legacy group errors remain fixed public codes.
|
||||
- A bounded exact-tree review (`deleg_fccee30d`) identified that organization admin PATCH updates used a read-modify-write path without cross-process locking; `JsonStore.update()` now uses the same per-record process lock as conditional updates.
|
||||
- `test_org_updates_preserve_concurrent_fields_across_processes` reproduces the stale-read race and verifies that concurrent `active` and `seats` changes both persist.
|
||||
|
||||
The deferred private-group filesystem scan/create collection race remains a Sprint 2/S2.1 task and is not represented as fixed.
|
||||
|
||||
## Verification evidence
|
||||
|
||||
Executed from the repository root:
|
||||
|
||||
```text
|
||||
backend/.venv/bin/python -m pytest backend/tests -q
|
||||
97 passed in 23.50s
|
||||
|
||||
This is the post-remediation exact current-tree run after the organization
|
||||
admin update race fix. The earlier completed background run, `proc_ba80f06ca949`,
|
||||
returned `91 passed in 928.85s (0:15:28)` and is retained as historical
|
||||
supporting evidence.
|
||||
|
||||
Another earlier background run, `proc_efcee6c93382`, returned `93 passed in
|
||||
23.11s` before the current collection state and is also historical supporting
|
||||
evidence.
|
||||
|
||||
backend/scripts/test_*.py
|
||||
12/12 passed
|
||||
|
||||
cd frontend && npm run build
|
||||
passed; 1,772 modules transformed
|
||||
|
||||
backend/.venv/bin/python -m compileall -q backend/app backend/tests backend/scripts
|
||||
AST parse + git diff --check
|
||||
passed
|
||||
|
||||
Added-line security scan:
|
||||
hardcoded_secret=0
|
||||
shell_injection=0
|
||||
eval_exec=0
|
||||
pickle=0
|
||||
sql_format=0
|
||||
```
|
||||
|
||||
Additional focused checks for the latest response-boundary changes passed, including user-list redaction, provider-debrief allowlisting, session-internal redaction, malformed group serialization, strict organization field types, and tenant fallback removal.
|
||||
|
||||
## Reviewer and operational gates
|
||||
|
||||
The exact-tree read-only reviewer `deleg_6e386e71` timed out after 600.19 seconds and returned no schema-valid JSON. The earlier two-workstream batch, `deleg_da73aca4`, also timed out in both workstreams. The bounded exact-tree reviewer `deleg_fccee30d` returned schema-valid `passed=false` and found one concrete cross-process organization-update race: `JsonStore.update()` lacked a process lock. That finding was remediated in `backend/app/storage/store.py` and covered by the new regression test above. The fresh post-remediation exact-tree reviewer `deleg_7bcf0dfd` returned the required schema-valid verdict `passed=true`, with `security_concerns=[]` and `logic_errors=[]`. This closes the Sprint 1 code/reviewer gate. Prior reviewer timeouts, packet/scoped verdicts, and delegation completion status are not approval.
|
||||
|
||||
Docker is unavailable locally. Real-provider LLM QA, browser/mobile QA, restricted deployment, bootstrap credential change, `JWT_SECRET` rotation, audit inspection, and authenticated smoke have not been run. Public/untrusted access must remain disabled.
|
||||
|
||||
## Next action
|
||||
|
||||
The next gate is the separate operator-approved restricted deployment and credential/JWT rotation checklist. Do not restore public/untrusted access or start Sprint 2 until that live-operation gate is explicitly completed.
|
||||
Loaded 100 of 179 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user