新增管理员接口 GET /profiles/{user_id}/stats,返回用户全部/公开上传数和收藏数
This commit is contained in:
@@ -136,3 +136,67 @@ def test_get_user_clouds_as_owner(client: TestClient, monkeypatch) -> None:
|
||||
def test_get_nonexistent_user_clouds(client: TestClient) -> None:
|
||||
r = client.get(f"/api/v1/profiles/{uuid.uuid4()}/clouds")
|
||||
assert r.status_code == 404
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# stats (admin only)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_get_user_stats_admin(client: TestClient, monkeypatch) -> None:
|
||||
info = register_and_confirm(
|
||||
client, monkeypatch,
|
||||
f"stats-{uuid.uuid4().hex[:8]}@example.com",
|
||||
"statspass", "统计用户",
|
||||
)
|
||||
# upload one cloud and approve it, upload another pending
|
||||
ur1 = 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), ur1.json()["cloud"]["id"])
|
||||
client.post(
|
||||
"/api/v1/clouds",
|
||||
headers=bearer(info["token"]),
|
||||
files={"image": ("cloud.png", png_bytes(), "image/png")},
|
||||
data={"cloud_type_id": "2", "is_hidden": "false"},
|
||||
)
|
||||
|
||||
r = client.get(
|
||||
f"/api/v1/profiles/{info['user_id']}/stats",
|
||||
headers=bearer(admin_token(client)),
|
||||
)
|
||||
assert r.status_code == 200
|
||||
body = r.json()
|
||||
assert body["cloud_count"] == 2
|
||||
assert body["public_cloud_count"] == 1
|
||||
assert body["collection_count"] == 2
|
||||
assert body["username"] == "统计用户"
|
||||
|
||||
|
||||
def test_get_user_stats_non_admin(client: TestClient, monkeypatch) -> None:
|
||||
info = register_and_confirm(
|
||||
client, monkeypatch,
|
||||
f"stats-na-{uuid.uuid4().hex[:8]}@example.com",
|
||||
"statsnap", "非管理员统计",
|
||||
)
|
||||
r = client.get(
|
||||
f"/api/v1/profiles/{info['user_id']}/stats",
|
||||
headers=bearer(info["token"]),
|
||||
)
|
||||
assert r.status_code == 403
|
||||
|
||||
|
||||
def test_get_user_stats_unauthenticated(client: TestClient) -> None:
|
||||
r = client.get(f"/api/v1/profiles/{uuid.uuid4()}/stats")
|
||||
assert r.status_code == 401
|
||||
|
||||
|
||||
def test_get_user_stats_nonexistent(client: TestClient) -> None:
|
||||
r = client.get(
|
||||
f"/api/v1/profiles/{uuid.uuid4()}/stats",
|
||||
headers=bearer(admin_token(client)),
|
||||
)
|
||||
assert r.status_code == 404
|
||||
|
||||
Reference in New Issue
Block a user