P0-1 SqlCredentialStore/save_draft 由自提交改 flush,端点/服务统一 commit (新增 CredentialStore.commit() 统一提交点;token 刷新落库显式提交); 补多凭据一请求中途失败整体回滚集成测试。 P0-2 启动校验 _fernet(enc_key) 快速失败 + catch-all Exception → ErrorEnvelope; credential_enc_key 改 SecretStr。 P0-3 run_job 异常分类:AppError 存 code+message,其余存通用文案不泄 str(exc)。 P0-4 评审/正文 SSE 失败先发 error 事件,尾部 commit 包 try/except。 P1-4 max_version 加 FOR UPDATE 行锁消除 TOCTOU。 P1-5 scan_overdue 谓词下推 + 批量 UPDATE RETURNING。 P1-10 移除 OAuth user_code 日志。 P2 provider_deps 改调网关 build_adapter;accept_service Committable Protocol; CORS 白名单收窄;request_id 安全字符集白名单;stdlib 日志接管;读端点 404 校验; httpx timeout;测试用合法 Fernet key;类型化响应模型(JobResponse/DimensionEntry/ ReviewConflictView/selling_points)+路由 ErrorEnvelope responses(供 codegen)。
89 lines
2.7 KiB
Python
89 lines
2.7 KiB
Python
"""T5.5 POST /projects/:id/rules 端点(内存替身,无 DB/无网络)。
|
||
|
||
覆盖:
|
||
- 201 + 回显 level/content + 端点 commit;
|
||
- 非法 level → 422(Pydantic Literal 校验,FastAPI 422);
|
||
- 空 content → 422。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import uuid
|
||
|
||
import httpx
|
||
import pytest
|
||
from cryptography.fernet import Fernet
|
||
from fakes_projects import FakeSession
|
||
from ww_core.domain.rule_repo import RuleWriteView
|
||
|
||
|
||
class _FakeRuleWriteRepo:
|
||
def __init__(self) -> None:
|
||
self.rows: list[RuleWriteView] = []
|
||
|
||
async def create(self, project_id: uuid.UUID, *, level: str, content: str) -> RuleWriteView:
|
||
view = RuleWriteView(project_id=project_id, level=level, content=content)
|
||
self.rows.append(view)
|
||
return view
|
||
|
||
|
||
def _make_client() -> tuple[httpx.AsyncClient, _FakeRuleWriteRepo, FakeSession]:
|
||
import os
|
||
|
||
os.environ.setdefault("CREDENTIAL_ENC_KEY", Fernet.generate_key().decode())
|
||
from ww_api.main import create_app
|
||
from ww_api.services.project_deps import get_rule_write_repo
|
||
from ww_db import get_session
|
||
|
||
repo = _FakeRuleWriteRepo()
|
||
session = FakeSession()
|
||
|
||
app = create_app()
|
||
app.dependency_overrides[get_rule_write_repo] = lambda: repo
|
||
app.dependency_overrides[get_session] = lambda: session
|
||
transport = httpx.ASGITransport(app=app)
|
||
client = httpx.AsyncClient(transport=transport, base_url="http://test")
|
||
return client, repo, session
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_create_rule_returns_201_and_commits() -> None:
|
||
client, repo, session = _make_client()
|
||
pid = uuid.uuid4()
|
||
async with client:
|
||
resp = await client.post(
|
||
f"/projects/{pid}/rules",
|
||
json={"level": "project", "content": "主角不许中途复活"},
|
||
)
|
||
assert resp.status_code == 201
|
||
body = resp.json()
|
||
assert body["level"] == "project"
|
||
assert body["content"] == "主角不许中途复活"
|
||
assert session.commits == 1
|
||
assert len(repo.rows) == 1
|
||
assert repo.rows[0].project_id == pid
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_create_rule_invalid_level_returns_422() -> None:
|
||
client, _repo, _session = _make_client()
|
||
pid = uuid.uuid4()
|
||
async with client:
|
||
resp = await client.post(
|
||
f"/projects/{pid}/rules",
|
||
json={"level": "nonsense", "content": "x"},
|
||
)
|
||
assert resp.status_code == 422
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_create_rule_empty_content_returns_422() -> None:
|
||
client, _repo, _session = _make_client()
|
||
pid = uuid.uuid4()
|
||
async with client:
|
||
resp = await client.post(
|
||
f"/projects/{pid}/rules",
|
||
json={"level": "global", "content": ""},
|
||
)
|
||
assert resp.status_code == 422
|