统一测试辅助函数到 conftest.py,消除代码重复
This commit is contained in:
+37
-78
@@ -3,53 +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_cloud(client: TestClient, token: str, **overrides):
|
||||
data = {"cloud_type_id": "1", "is_hidden": "false"}
|
||||
data.update(overrides)
|
||||
return client.post(
|
||||
"/api/v1/clouds",
|
||||
headers=bearer(token),
|
||||
files={"image": ("cloud.png", png_bytes(), "image/png")},
|
||||
data=data,
|
||||
)
|
||||
from tests.conftest import (
|
||||
admin_token,
|
||||
bearer,
|
||||
png_bytes,
|
||||
register_and_confirm,
|
||||
upload_cloud,
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -58,7 +18,7 @@ def _upload_cloud(client: TestClient, token: str, **overrides):
|
||||
|
||||
|
||||
def test_stats(client: TestClient) -> None:
|
||||
r = client.get("/api/v1/admin/stats", headers=bearer(_admin_token(client)))
|
||||
r = client.get("/api/v1/admin/stats", headers=bearer(admin_token(client)))
|
||||
assert r.status_code == 200
|
||||
body = r.json()
|
||||
for key in ("users", "images", "today_uploads", "pending", "approved", "rejected", "hidden"):
|
||||
@@ -66,7 +26,7 @@ def test_stats(client: TestClient) -> None:
|
||||
|
||||
|
||||
def test_stats_non_admin(client: TestClient, monkeypatch) -> None:
|
||||
info = _register_and_confirm(
|
||||
info = register_and_confirm(
|
||||
client, monkeypatch,
|
||||
f"notadmin-{uuid.uuid4().hex[:8]}@example.com",
|
||||
"notadmin12", "非管理员",
|
||||
@@ -87,7 +47,7 @@ def test_stats_unauthenticated(client: TestClient) -> None:
|
||||
|
||||
|
||||
def test_list_users(client: TestClient) -> None:
|
||||
r = client.get("/api/v1/admin/users", headers=bearer(_admin_token(client)))
|
||||
r = client.get("/api/v1/admin/users", headers=bearer(admin_token(client)))
|
||||
assert r.status_code == 200
|
||||
assert r.json()["total"] >= 1
|
||||
|
||||
@@ -98,15 +58,14 @@ def test_list_users(client: TestClient) -> None:
|
||||
|
||||
|
||||
def test_update_user_role(client: TestClient, monkeypatch) -> None:
|
||||
info = _register_and_confirm(
|
||||
info = register_and_confirm(
|
||||
client, monkeypatch,
|
||||
f"promote-{uuid.uuid4().hex[:8]}@example.com",
|
||||
"promote12", "升管理员",
|
||||
)
|
||||
admin = _admin_token(client)
|
||||
r = client.patch(
|
||||
f"/api/v1/admin/users/{info['user_id']}",
|
||||
headers=bearer(admin),
|
||||
headers=bearer(admin_token(client)),
|
||||
json={"role": "admin"},
|
||||
)
|
||||
assert r.status_code == 200
|
||||
@@ -114,15 +73,15 @@ def test_update_user_role(client: TestClient, monkeypatch) -> None:
|
||||
|
||||
|
||||
def test_update_user_disable(client: TestClient, monkeypatch) -> None:
|
||||
info = _register_and_confirm(
|
||||
info = register_and_confirm(
|
||||
client, monkeypatch,
|
||||
f"disableme-{uuid.uuid4().hex[:8]}@example.com",
|
||||
"disableme", "被禁用",
|
||||
)
|
||||
admin = _admin_token(client)
|
||||
adm = admin_token(client)
|
||||
r = client.patch(
|
||||
f"/api/v1/admin/users/{info['user_id']}",
|
||||
headers=bearer(admin),
|
||||
headers=bearer(adm),
|
||||
json={"is_disabled": True},
|
||||
)
|
||||
assert r.status_code == 200
|
||||
@@ -130,18 +89,18 @@ def test_update_user_disable(client: TestClient, monkeypatch) -> None:
|
||||
|
||||
client.patch(
|
||||
f"/api/v1/admin/users/{info['user_id']}",
|
||||
headers=bearer(admin),
|
||||
headers=bearer(adm),
|
||||
json={"is_disabled": False},
|
||||
)
|
||||
|
||||
|
||||
def test_update_self_disable(client: TestClient) -> None:
|
||||
admin_token = _admin_token(client)
|
||||
me = client.get("/api/v1/auth/me", headers=bearer(admin_token))
|
||||
adm = admin_token(client)
|
||||
me = client.get("/api/v1/auth/me", headers=bearer(adm))
|
||||
admin_id = me.json()["user"]["id"]
|
||||
r = client.patch(
|
||||
f"/api/v1/admin/users/{admin_id}",
|
||||
headers=bearer(admin_token),
|
||||
headers=bearer(adm),
|
||||
json={"is_disabled": True},
|
||||
)
|
||||
assert r.status_code == 400
|
||||
@@ -149,12 +108,12 @@ def test_update_self_disable(client: TestClient) -> None:
|
||||
|
||||
|
||||
def test_update_self_demote(client: TestClient) -> None:
|
||||
admin_token = _admin_token(client)
|
||||
me = client.get("/api/v1/auth/me", headers=bearer(admin_token))
|
||||
adm = admin_token(client)
|
||||
me = client.get("/api/v1/auth/me", headers=bearer(adm))
|
||||
admin_id = me.json()["user"]["id"]
|
||||
r = client.patch(
|
||||
f"/api/v1/admin/users/{admin_id}",
|
||||
headers=bearer(admin_token),
|
||||
headers=bearer(adm),
|
||||
json={"role": "user"},
|
||||
)
|
||||
assert r.status_code == 400
|
||||
@@ -164,7 +123,7 @@ def test_update_self_demote(client: TestClient) -> None:
|
||||
def test_update_nonexistent_user(client: TestClient) -> None:
|
||||
r = client.patch(
|
||||
f"/api/v1/admin/users/{uuid.uuid4()}",
|
||||
headers=bearer(_admin_token(client)),
|
||||
headers=bearer(admin_token(client)),
|
||||
json={"role": "admin"},
|
||||
)
|
||||
assert r.status_code == 404
|
||||
@@ -176,7 +135,7 @@ def test_update_nonexistent_user(client: TestClient) -> None:
|
||||
|
||||
|
||||
def test_list_admin_clouds(client: TestClient) -> None:
|
||||
r = client.get("/api/v1/admin/clouds", headers=bearer(_admin_token(client)))
|
||||
r = client.get("/api/v1/admin/clouds", headers=bearer(admin_token(client)))
|
||||
assert r.status_code == 200
|
||||
assert "items" in r.json()
|
||||
|
||||
@@ -184,7 +143,7 @@ def test_list_admin_clouds(client: TestClient) -> None:
|
||||
def test_list_admin_clouds_filter_status(client: TestClient) -> None:
|
||||
r = client.get(
|
||||
"/api/v1/admin/clouds",
|
||||
headers=bearer(_admin_token(client)),
|
||||
headers=bearer(admin_token(client)),
|
||||
params={"status": "pending"},
|
||||
)
|
||||
assert r.status_code == 200
|
||||
@@ -195,7 +154,7 @@ def test_list_admin_clouds_filter_status(client: TestClient) -> None:
|
||||
def test_list_admin_clouds_filter_hidden(client: TestClient) -> None:
|
||||
r = client.get(
|
||||
"/api/v1/admin/clouds",
|
||||
headers=bearer(_admin_token(client)),
|
||||
headers=bearer(admin_token(client)),
|
||||
params={"is_hidden": "true"},
|
||||
)
|
||||
assert r.status_code == 200
|
||||
@@ -207,16 +166,16 @@ def test_list_admin_clouds_filter_hidden(client: TestClient) -> None:
|
||||
|
||||
|
||||
def test_update_cloud_status(client: TestClient, monkeypatch) -> None:
|
||||
info = _register_and_confirm(
|
||||
info = register_and_confirm(
|
||||
client, monkeypatch,
|
||||
f"admin-cld-{uuid.uuid4().hex[:8]}@example.com",
|
||||
"admincld", "管理员云审核",
|
||||
)
|
||||
ur = _upload_cloud(client, info["token"], cloud_type_id="1")
|
||||
ur = upload_cloud(client, info["token"], cloud_type_id="1")
|
||||
cloud_id = ur.json()["cloud"]["id"]
|
||||
r = client.patch(
|
||||
"/api/v1/admin/clouds/status",
|
||||
headers=bearer(_admin_token(client)),
|
||||
headers=bearer(admin_token(client)),
|
||||
json={"ids": [cloud_id], "status": "rejected"},
|
||||
)
|
||||
assert r.status_code == 200
|
||||
@@ -226,7 +185,7 @@ def test_update_cloud_status(client: TestClient, monkeypatch) -> None:
|
||||
def test_update_cloud_status_nonexistent(client: TestClient) -> None:
|
||||
r = client.patch(
|
||||
"/api/v1/admin/clouds/status",
|
||||
headers=bearer(_admin_token(client)),
|
||||
headers=bearer(admin_token(client)),
|
||||
json={"ids": [str(uuid.uuid4())], "status": "approved"},
|
||||
)
|
||||
assert r.status_code == 404
|
||||
@@ -238,16 +197,16 @@ def test_update_cloud_status_nonexistent(client: TestClient) -> None:
|
||||
|
||||
|
||||
def test_update_cloud_visibility(client: TestClient, monkeypatch) -> None:
|
||||
info = _register_and_confirm(
|
||||
info = register_and_confirm(
|
||||
client, monkeypatch,
|
||||
f"vis-{uuid.uuid4().hex[:8]}@example.com",
|
||||
"vispass12", "可见性测试",
|
||||
)
|
||||
ur = _upload_cloud(client, info["token"], cloud_type_id="1")
|
||||
ur = upload_cloud(client, info["token"], cloud_type_id="1")
|
||||
cloud_id = ur.json()["cloud"]["id"]
|
||||
r = client.patch(
|
||||
"/api/v1/admin/clouds/visibility",
|
||||
headers=bearer(_admin_token(client)),
|
||||
headers=bearer(admin_token(client)),
|
||||
json={"ids": [cloud_id], "is_hidden": True},
|
||||
)
|
||||
assert r.status_code == 200
|
||||
@@ -258,7 +217,7 @@ def test_update_cloud_visibility(client: TestClient, monkeypatch) -> None:
|
||||
|
||||
client.patch(
|
||||
"/api/v1/admin/clouds/visibility",
|
||||
headers=bearer(_admin_token(client)),
|
||||
headers=bearer(admin_token(client)),
|
||||
json={"ids": [cloud_id], "is_hidden": False},
|
||||
)
|
||||
|
||||
@@ -269,16 +228,16 @@ def test_update_cloud_visibility(client: TestClient, monkeypatch) -> None:
|
||||
|
||||
|
||||
def test_admin_batch_delete_clouds(client: TestClient, monkeypatch) -> None:
|
||||
info = _register_and_confirm(
|
||||
info = register_and_confirm(
|
||||
client, monkeypatch,
|
||||
f"adm-del-{uuid.uuid4().hex[:8]}@example.com",
|
||||
"admdel12", "管理员删图",
|
||||
)
|
||||
ur = _upload_cloud(client, info["token"], cloud_type_id="1")
|
||||
ur = upload_cloud(client, info["token"], cloud_type_id="1")
|
||||
cloud_id = ur.json()["cloud"]["id"]
|
||||
r = client.post(
|
||||
"/api/v1/admin/clouds/batch-delete",
|
||||
headers=bearer(_admin_token(client)),
|
||||
headers=bearer(admin_token(client)),
|
||||
json={"ids": [cloud_id]},
|
||||
)
|
||||
assert r.status_code == 200
|
||||
|
||||
Reference in New Issue
Block a user