统一测试辅助函数到 conftest.py,消除代码重复

This commit is contained in:
2026-07-26 01:23:12 +08:00
parent 6b1f0d5f5f
commit 12d15878da
7 changed files with 276 additions and 421 deletions
+2 -32
View File
@@ -1,42 +1,12 @@
import uuid
import pytest
from fastapi.testclient import TestClient
from app.routers import auth as auth_router
from tests.conftest import bearer, png_bytes
def _capture_email(monkeypatch) -> dict[str, str]:
captured: dict[str, str] = {}
async def capture(recipient: str, token: str) -> None:
captured["token"] = token
monkeypatch.setattr(auth_router, "send_confirmation_email", capture)
return captured
def _register_and_confirm(client, monkeypatch, email, password, username) -> dict[str, str]:
captured = _capture_email(monkeypatch)
r = client.post(
"/api/v1/auth/register",
json={"email": email, "password": password, "username": username},
)
assert r.status_code == 201
client.post("/api/v1/auth/confirm-email", json={"token": captured["token"]})
lr = client.post("/api/v1/auth/login", json={"email": email, "password": password})
body = lr.json()
return {"token": body["access_token"], "user_id": body["user"]["id"]}
# ---------------------------------------------------------------------------
# my collections
# ---------------------------------------------------------------------------
from tests.conftest import bearer, png_bytes, register_and_confirm
def test_my_collections_after_upload(client: TestClient, monkeypatch) -> None:
info = _register_and_confirm(
info = register_and_confirm(
client, monkeypatch,
f"coll-{uuid.uuid4().hex[:8]}@example.com",
"collpass1", "收藏用户",