统一测试辅助函数到 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
+28 -61
View File
@@ -3,58 +3,13 @@ import uuid
import pytest
from fastapi.testclient import TestClient
from app.routers import auth as auth_router
from tests.conftest import bearer, png_bytes
ADMIN_EMAIL = "admin@example.com"
ADMIN_PASSWORD = "admin-password-123"
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"]}
def _admin_token(client: TestClient) -> str:
r = client.post(
"/api/v1/auth/login",
json={"email": ADMIN_EMAIL, "password": ADMIN_PASSWORD},
)
return r.json()["access_token"]
def _upload_and_approve(client: TestClient, token: str) -> str:
ur = client.post(
"/api/v1/clouds",
headers=bearer(token),
files={"image": ("cloud.png", png_bytes(), "image/png")},
data={"cloud_type_id": "1", "is_hidden": "false"},
)
cloud_id = ur.json()["cloud"]["id"]
client.patch(
"/api/v1/admin/clouds/status",
headers=bearer(_admin_token(client)),
json={"ids": [cloud_id], "status": "approved"},
)
return cloud_id
from tests.conftest import (
admin_token,
approve_cloud,
bearer,
png_bytes,
register_and_confirm,
)
# ---------------------------------------------------------------------------
@@ -63,7 +18,7 @@ def _upload_and_approve(client: TestClient, token: str) -> str:
def test_get_profile(client: TestClient, monkeypatch) -> None:
info = _register_and_confirm(
info = register_and_confirm(
client, monkeypatch,
f"prof-{uuid.uuid4().hex[:8]}@example.com",
"profpass1", "资料展示",
@@ -84,7 +39,7 @@ def test_get_nonexistent_profile(client: TestClient) -> None:
def test_update_me(client: TestClient, monkeypatch) -> None:
info = _register_and_confirm(
info = register_and_confirm(
client, monkeypatch,
f"updme-{uuid.uuid4().hex[:8]}@example.com",
"updmepass", "原昵称",
@@ -99,7 +54,7 @@ def test_update_me(client: TestClient, monkeypatch) -> None:
def test_update_me_short_username(client: TestClient, monkeypatch) -> None:
info = _register_and_confirm(
info = register_and_confirm(
client, monkeypatch,
f"shortname-{uuid.uuid4().hex[:8]}@example.com",
"shortname", "短名字测试",
@@ -113,12 +68,12 @@ def test_update_me_short_username(client: TestClient, monkeypatch) -> None:
def test_update_me_duplicate_username(client: TestClient, monkeypatch) -> None:
_register_and_confirm(
register_and_confirm(
client, monkeypatch,
f"dupname-u1-{uuid.uuid4().hex[:8]}@example.com",
"pass11112", "已占用的名字",
)
info2 = _register_and_confirm(
info2 = register_and_confirm(
client, monkeypatch,
f"dupname-u2-{uuid.uuid4().hex[:8]}@example.com",
"pass22223", "第二人",
@@ -138,12 +93,18 @@ def test_update_me_duplicate_username(client: TestClient, monkeypatch) -> None:
def test_get_user_clouds_public(client: TestClient, monkeypatch) -> None:
info = _register_and_confirm(
info = register_and_confirm(
client, monkeypatch,
f"pubcloud-{uuid.uuid4().hex[:8]}@example.com",
"pubcloud1", "公开云主",
)
_upload_and_approve(client, info["token"])
ur = client.post(
"/api/v1/clouds",
headers=bearer(info["token"]),
files={"image": ("cloud.png", png_bytes(), "image/png")},
data={"cloud_type_id": "1", "is_hidden": "false"},
)
approve_cloud(client, admin_token(client), ur.json()["cloud"]["id"])
r = client.get(f"/api/v1/profiles/{info['user_id']}/clouds")
assert r.status_code == 200
assert r.json()["total"] >= 1
@@ -152,12 +113,18 @@ def test_get_user_clouds_public(client: TestClient, monkeypatch) -> None:
def test_get_user_clouds_as_owner(client: TestClient, monkeypatch) -> None:
info = _register_and_confirm(
info = register_and_confirm(
client, monkeypatch,
f"owncloud-{uuid.uuid4().hex[:8]}@example.com",
"owncloud1", "私有云主",
)
cloud_id = _upload_and_approve(client, info["token"])
ur = client.post(
"/api/v1/clouds",
headers=bearer(info["token"]),
files={"image": ("cloud.png", png_bytes(), "image/png")},
data={"cloud_type_id": "1", "is_hidden": "false"},
)
approve_cloud(client, admin_token(client), ur.json()["cloud"]["id"])
r = client.get(
f"/api/v1/profiles/{info['user_id']}/clouds",
headers=bearer(info["token"]),