172 lines
5.3 KiB
Python
172 lines
5.3 KiB
Python
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
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# get profile
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_get_profile(client: TestClient, monkeypatch) -> None:
|
|
info = _register_and_confirm(
|
|
client, monkeypatch,
|
|
f"prof-{uuid.uuid4().hex[:8]}@example.com",
|
|
"profpass1", "资料展示",
|
|
)
|
|
r = client.get(f"/api/v1/profiles/{info['user_id']}")
|
|
assert r.status_code == 200
|
|
assert r.json()["username"] == "资料展示"
|
|
|
|
|
|
def test_get_nonexistent_profile(client: TestClient) -> None:
|
|
r = client.get(f"/api/v1/profiles/{uuid.uuid4()}")
|
|
assert r.status_code == 404
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# update my profile
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_update_me(client: TestClient, monkeypatch) -> None:
|
|
info = _register_and_confirm(
|
|
client, monkeypatch,
|
|
f"updme-{uuid.uuid4().hex[:8]}@example.com",
|
|
"updmepass", "原昵称",
|
|
)
|
|
r = client.patch(
|
|
"/api/v1/profiles/me",
|
|
headers=bearer(info["token"]),
|
|
json={"username": "新昵称"},
|
|
)
|
|
assert r.status_code == 200
|
|
assert r.json()["username"] == "新昵称"
|
|
|
|
|
|
def test_update_me_short_username(client: TestClient, monkeypatch) -> None:
|
|
info = _register_and_confirm(
|
|
client, monkeypatch,
|
|
f"shortname-{uuid.uuid4().hex[:8]}@example.com",
|
|
"shortname", "短名字测试",
|
|
)
|
|
r = client.patch(
|
|
"/api/v1/profiles/me",
|
|
headers=bearer(info["token"]),
|
|
json={"username": "A"},
|
|
)
|
|
assert r.status_code == 422
|
|
|
|
|
|
def test_update_me_duplicate_username(client: TestClient, monkeypatch) -> None:
|
|
_register_and_confirm(
|
|
client, monkeypatch,
|
|
f"dupname-u1-{uuid.uuid4().hex[:8]}@example.com",
|
|
"pass11112", "已占用的名字",
|
|
)
|
|
info2 = _register_and_confirm(
|
|
client, monkeypatch,
|
|
f"dupname-u2-{uuid.uuid4().hex[:8]}@example.com",
|
|
"pass22223", "第二人",
|
|
)
|
|
r = client.patch(
|
|
"/api/v1/profiles/me",
|
|
headers=bearer(info2["token"]),
|
|
json={"username": "已占用的名字"},
|
|
)
|
|
assert r.status_code == 409
|
|
assert "昵称" in r.json()["detail"]
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# get user clouds
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_get_user_clouds_public(client: TestClient, monkeypatch) -> None:
|
|
info = _register_and_confirm(
|
|
client, monkeypatch,
|
|
f"pubcloud-{uuid.uuid4().hex[:8]}@example.com",
|
|
"pubcloud1", "公开云主",
|
|
)
|
|
_upload_and_approve(client, info["token"])
|
|
r = client.get(f"/api/v1/profiles/{info['user_id']}/clouds")
|
|
assert r.status_code == 200
|
|
assert r.json()["total"] >= 1
|
|
for item in r.json()["items"]:
|
|
assert item["status"] == "approved"
|
|
|
|
|
|
def test_get_user_clouds_as_owner(client: TestClient, monkeypatch) -> None:
|
|
info = _register_and_confirm(
|
|
client, monkeypatch,
|
|
f"owncloud-{uuid.uuid4().hex[:8]}@example.com",
|
|
"owncloud1", "私有云主",
|
|
)
|
|
cloud_id = _upload_and_approve(client, info["token"])
|
|
r = client.get(
|
|
f"/api/v1/profiles/{info['user_id']}/clouds",
|
|
headers=bearer(info["token"]),
|
|
)
|
|
assert r.status_code == 200
|
|
assert r.json()["total"] >= 1
|
|
|
|
|
|
def test_get_nonexistent_user_clouds(client: TestClient) -> None:
|
|
r = client.get(f"/api/v1/profiles/{uuid.uuid4()}/clouds")
|
|
assert r.status_code == 404
|