cmvr-visual/backend/tests/test_tokens_quotas.py
zhanghao 88cf14d794 feat: consolidate story studio and editable automatic production
Restructure backend and frontend modules; add story, library, and editor schema migrations.

Support editable automatic production, manual confirmation modes, account and user lifecycle management, compact container scrolling, consistent forms, and per-task result previews.

Update inference and TTS scheduling, deployment guidance, and regression coverage; remove obsolete standalone pages and temporary acceptance artifacts.
2026-11-14 14:07:26 +08:00

100 lines
3.9 KiB
Python

import httpx
import pytest
@pytest.mark.asyncio
async def test_token_scope_owner_revoke_and_no_admin(system, other_client):
made = await other_client.post(
"/api/auth/tokens", json={"name": "reader", "scopes": ["jobs:read", "assets:read"]}
)
assert made.status_code == 201
token = made.json()["token"]
async with httpx.AsyncClient(
transport=httpx.ASGITransport(app=system.app),
base_url="http://test",
headers={"Authorization": "Bearer " + token},
) as machine:
assert (await machine.get("/api/jobs")).json()["total"] == 0
assert (await machine.get("/api/auth/users")).status_code == 403
assert (
await machine.post(
"/api/jobs", json={"task_type": "math.solve", "input": {"prompt": "x"}}
)
).status_code == 403
await other_client.delete("/api/auth/tokens/" + made.json()["record"]["id"])
assert (await machine.get("/api/jobs")).status_code == 401
from app.models import ApiToken
from sqlalchemy import select
async with system.app.state.db.session() as session:
row = await session.scalar(select(ApiToken))
assert row.token_hash != token and not hasattr(row, "token")
@pytest.mark.asyncio
async def test_quota_replay_does_not_charge_twice(system):
system.settings.max_owner_jobs = 1
body = {"task_type": "image.generate", "input": {"prompt": "x"}}
one = await system.client.post("/api/jobs", json=body, headers={"Idempotency-Key": "one"})
assert one.status_code == 202
assert (
await system.client.post("/api/jobs", json=body, headers={"Idempotency-Key": "one"})
).status_code == 202
assert (
await system.client.post("/api/jobs", json=body, headers={"Idempotency-Key": "two"})
).status_code == 429
@pytest.mark.asyncio
async def test_workflow_read_only_token_can_recover_own_workflow_submission(system):
from app.models import Submission
actor = (await system.client.get("/api/auth/me")).json()["user"]
async with system.app.state.db.session() as session:
session.add(
Submission(
owner_id=actor["id"],
operation="workflows.run",
key="run-key",
payload_schema=2,
body_hash="x",
request_json={},
resource_json={"run_id": "known"},
)
)
await session.commit()
made = await system.client.post(
"/api/auth/tokens", json={"name": "workflow-reader", "scopes": ["workflows:read"]}
)
async with httpx.AsyncClient(
transport=httpx.ASGITransport(app=system.app),
base_url="http://test",
headers={"Authorization": "Bearer " + made.json()["token"]},
) as machine:
r = await machine.get("/api/submissions/run-key?operation=workflows.run")
assert r.status_code == 200 and r.json()["run_id"] == "known"
assert (
await machine.get("/api/submissions/run-key?operation=jobs.create")
).status_code == 403
@pytest.mark.asyncio
async def test_storage_budget_includes_own_tmp_sibling(system, monkeypatch):
from types import SimpleNamespace
from fastapi import HTTPException
from app.services import quotas
actor = (await system.client.get("/api/auth/me")).json()["user"]
temporary = system.settings.asset_root.parent / "tmp"
temporary.mkdir(exist_ok=True)
(temporary / "encoded.part").write_bytes(b"abc")
system.settings.mode = "production"
system.settings.max_data_bytes = 2
monkeypatch.setattr(
quotas.shutil, "disk_usage", lambda path: SimpleNamespace(total=10**12, free=10**12)
)
async with system.app.state.db.session() as session:
with pytest.raises(HTTPException) as caught:
await quotas.check_storage_budget(session, system.settings, actor["id"])
assert caught.value.detail["code"] == "storage_insufficient"