- analyze returns 202 immediately; frontend polls group status (3s interval, 30min cap) - quota system: atomic check-and-consume, trial/team/enterprise plans, 402 on exceed - persona generation split into tier batches (Cloudflare 120s limit safe) - LLM client: 105s timeout, thinking param for reasoning models - tests: quota suite (4), persona cardinality/initiation updates
163 lines
6.0 KiB
Python
163 lines
6.0 KiB
Python
"""Quota enforcement tests: trial cap, paid per-seat, enterprise unlimited, resume no-consume."""
|
||
from __future__ import annotations
|
||
|
||
import os
|
||
from pathlib import Path
|
||
|
||
import pytest
|
||
|
||
|
||
@pytest.fixture()
|
||
def quota_app(monkeypatch: pytest.MonkeyPatch, tmp_path: Path):
|
||
"""App with a small trial quota (3) so tests don't need 50 sessions."""
|
||
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")
|
||
monkeypatch.setenv("QUOTA_TRIAL_LIMIT", "3")
|
||
monkeypatch.setenv("QUOTA_PAID_PER_SEAT", "2")
|
||
|
||
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)
|
||
|
||
# Inject mock LLM so analyze works without external API.
|
||
import sys
|
||
scripts = str(Path(__file__).resolve().parents[1] / "scripts")
|
||
if scripts not in sys.path:
|
||
sys.path.insert(0, scripts)
|
||
from mock_llm import MockLLM
|
||
|
||
application.extensions["llm"] = MockLLM()
|
||
return application
|
||
|
||
|
||
def _bootstrap(client):
|
||
"""Create super admin + return auth header."""
|
||
r = client.post("/api/auth/login", json={"username": "admin", "password": "pytest-bootstrap-password"})
|
||
assert r.status_code == 200, r.get_json()
|
||
token = r.get_json()["token"]
|
||
h = {"Authorization": f"Bearer {token}"}
|
||
client.post("/api/auth/setup", headers=h, json={
|
||
"username": "admin", "email": "[email protected]",
|
||
"password": "admin-ready-password", "accepted_terms": True,
|
||
})
|
||
r = client.post("/api/auth/login", json={"username": "admin", "password": "admin-ready-password"})
|
||
token = r.get_json()["token"]
|
||
return {"Authorization": f"Bearer {token}"}
|
||
|
||
|
||
def _make_group_with_personas(client, admin_h):
|
||
"""Create a group and analyze it → 15 personas."""
|
||
r = client.post("/api/groups", headers=admin_h, json={
|
||
"product": "CloudPOS", "segment": "SME", "channel": "line", "language": "th",
|
||
})
|
||
assert r.status_code == 201, r.get_json()
|
||
gid = r.get_json()["group"]["id"]
|
||
r = client.post(f"/api/groups/{gid}/analyze", headers=admin_h)
|
||
assert r.status_code == 200, r.get_json()
|
||
return gid
|
||
|
||
|
||
def _make_trainee(app, client, admin_h, username):
|
||
r = client.post("/api/admin/users", headers=admin_h, json={
|
||
"username": username, "password": "trainee-pw-123", "role": "user",
|
||
})
|
||
assert r.status_code == 201, r.get_json()
|
||
# Mark terms accepted so the trainee can use business routes (mirrors the
|
||
# demo-account helper; the HTTP /auth/setup flow is covered elsewhere).
|
||
app.extensions["user_store"].users.update(
|
||
username, must_setup=False, accepted_terms=True,
|
||
accepted_terms_at="2026-09-30T00:00:00Z",
|
||
)
|
||
r = client.post("/api/auth/login", json={"username": username, "password": "trainee-pw-123"})
|
||
assert r.status_code == 200, r.get_json()
|
||
return {"Authorization": f"Bearer {r.get_json()['token']}"}
|
||
|
||
|
||
def _start_session(client, user_h, gid, persona_id):
|
||
return client.post(f"/api/chat/{gid}/personas/{persona_id}/chat/start", headers=user_h, json={
|
||
"mode": "trainee", "scenario": "social",
|
||
})
|
||
|
||
|
||
def test_trial_quota_blocks_after_limit(quota_app):
|
||
"""Trial org (limit=3): 3 trainee sessions OK, 4th → 402."""
|
||
client = quota_app.test_client()
|
||
admin_h = _bootstrap(client)
|
||
gid = _make_group_with_personas(client, admin_h)
|
||
|
||
# Get persona IDs from the group.
|
||
r = client.get(f"/api/groups/{gid}", headers=admin_h)
|
||
personas = r.get_json()["group"]["personas"]
|
||
assert len(personas) == 15
|
||
pids = [p["id"] for p in personas]
|
||
|
||
# Create 4 trainees (each can start 1 session per persona, one-shot).
|
||
trainees = [_make_trainee(quota_app, client, admin_h, f"t{i}") for i in range(4)]
|
||
|
||
# First 3 sessions should succeed (quota=3).
|
||
for i in range(3):
|
||
r = _start_session(client, trainees[i], gid, pids[i])
|
||
assert r.status_code == 200, f"session {i} should succeed: {r.get_json()}"
|
||
|
||
# 4th session should be blocked (quota exhausted).
|
||
r = _start_session(client, trainees[3], gid, pids[3])
|
||
assert r.status_code == 402, f"session 3 should be blocked: {r.status_code} {r.get_json()}"
|
||
|
||
|
||
def test_quota_status_endpoint(quota_app):
|
||
"""GET /api/me/quota returns usage for the current org."""
|
||
client = quota_app.test_client()
|
||
admin_h = _bootstrap(client)
|
||
gid = _make_group_with_personas(client, admin_h)
|
||
|
||
r = client.get("/api/me/quota", headers=admin_h)
|
||
assert r.status_code == 200, r.get_json()
|
||
q = r.get_json()["quota"]
|
||
assert q["plan"] == "trial"
|
||
assert q["limit"] == 3
|
||
assert q["used"] == 0
|
||
assert q["remaining"] == 3
|
||
|
||
|
||
def test_paid_plan_per_seat(quota_app):
|
||
"""Paid org (limit=2/seat × 5 seats = 10): quota reflects seats."""
|
||
client = quota_app.test_client()
|
||
admin_h = _bootstrap(client)
|
||
|
||
# Bump default org to paid plan.
|
||
r = client.patch("/api/admin/orgs/org-default", headers=admin_h, json={"plan": "paid"})
|
||
assert r.status_code == 200, r.get_json()
|
||
|
||
r = client.get("/api/me/quota", headers=admin_h)
|
||
q = r.get_json()["quota"]
|
||
assert q["plan"] == "paid"
|
||
assert q["limit"] == 10 # 2 per seat × 5 seats
|
||
|
||
|
||
def test_enterprise_unlimited(quota_app):
|
||
"""Enterprise org: no quota cap."""
|
||
client = quota_app.test_client()
|
||
admin_h = _bootstrap(client)
|
||
|
||
r = client.patch("/api/admin/orgs/org-default", headers=admin_h, json={"plan": "enterprise"})
|
||
assert r.status_code == 200, r.get_json()
|
||
|
||
r = client.get("/api/me/quota", headers=admin_h)
|
||
q = r.get_json()["quota"]
|
||
assert q["plan"] == "enterprise"
|
||
assert q["limit"] is None
|
||
assert q["remaining"] is None |