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