补充测试覆盖:认证、云图、管理后台、资料、收藏、云类型模块共 78 个测试用例

This commit is contained in:
2026-07-26 01:16:17 +08:00
parent 77bf076ad0
commit 6b1f0d5f5f
7 changed files with 1288 additions and 0 deletions
+62
View File
@@ -0,0 +1,62 @@
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
# ---------------------------------------------------------------------------
def test_my_collections_after_upload(client: TestClient, monkeypatch) -> None:
info = _register_and_confirm(
client, monkeypatch,
f"coll-{uuid.uuid4().hex[:8]}@example.com",
"collpass1", "收藏用户",
)
r = client.get("/api/v1/collections/me", headers=bearer(info["token"]))
assert r.status_code == 200
assert len(r.json()) == 0
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"},
)
r = client.get("/api/v1/collections/me", headers=bearer(info["token"]))
assert r.status_code == 200
assert len(r.json()) == 1
assert r.json()[0]["cloud_type_id"] == 1
def test_my_collections_unauthenticated(client: TestClient) -> None:
r = client.get("/api/v1/collections/me")
assert r.status_code == 401