新增头像接口:POST /profiles/me/avatar 上传、DELETE /profiles/me/avatar 删除

This commit is contained in:
2026-07-26 02:14:22 +08:00
parent 49df3f067a
commit 8149505f1b
3 changed files with 154 additions and 1 deletions
+84
View File
@@ -200,3 +200,87 @@ def test_get_user_stats_nonexistent(client: TestClient) -> None:
headers=bearer(admin_token(client)),
)
assert r.status_code == 404
# ---------------------------------------------------------------------------
# avatar
# ---------------------------------------------------------------------------
def test_upload_avatar(client: TestClient, monkeypatch) -> None:
info = register_and_confirm(
client, monkeypatch,
f"avatar-{uuid.uuid4().hex[:8]}@example.com",
"avatarp12", "头像用户",
)
r = client.post(
"/api/v1/profiles/me/avatar",
headers=bearer(info["token"]),
files={"image": ("avatar.png", png_bytes(), "image/png")},
)
assert r.status_code == 200
body = r.json()
assert body["avatar_url"] is not None
assert "avatars/" in body["avatar_url"]
def test_replace_avatar(client: TestClient, monkeypatch) -> None:
info = register_and_confirm(
client, monkeypatch,
f"avat2-{uuid.uuid4().hex[:8]}@example.com",
"avat2p12", "换头像",
)
r1 = client.post(
"/api/v1/profiles/me/avatar",
headers=bearer(info["token"]),
files={"image": ("avatar.png", png_bytes(), "image/png")},
)
assert r1.status_code == 200
assert r1.json()["avatar_url"] is not None
r2 = client.post(
"/api/v1/profiles/me/avatar",
headers=bearer(info["token"]),
files={"image": ("avatar2.png", png_bytes(), "image/png")},
)
assert r2.status_code == 200
assert r2.json()["avatar_url"] is not None
def test_delete_avatar(client: TestClient, monkeypatch) -> None:
info = register_and_confirm(
client, monkeypatch,
f"avat3-{uuid.uuid4().hex[:8]}@example.com",
"avat3p12", "删头像",
)
client.post(
"/api/v1/profiles/me/avatar",
headers=bearer(info["token"]),
files={"image": ("avatar.png", png_bytes(), "image/png")},
)
r = client.delete("/api/v1/profiles/me/avatar", headers=bearer(info["token"]))
assert r.status_code == 200
assert r.json()["avatar_url"] is None
def test_delete_avatar_not_found(client: TestClient, monkeypatch) -> None:
info = register_and_confirm(
client, monkeypatch,
f"avat4-{uuid.uuid4().hex[:8]}@example.com",
"avat4p12", "没头像删",
)
r = client.delete("/api/v1/profiles/me/avatar", headers=bearer(info["token"]))
assert r.status_code == 404
def test_upload_avatar_unauthenticated(client: TestClient) -> None:
r = client.post(
"/api/v1/profiles/me/avatar",
files={"image": ("avatar.png", png_bytes(), "image/png")},
)
assert r.status_code == 401
def test_delete_avatar_unauthenticated(client: TestClient) -> None:
r = client.delete("/api/v1/profiles/me/avatar")
assert r.status_code == 401