94 lines
3.3 KiB
Python
94 lines
3.3 KiB
Python
import uuid
|
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
from tests.conftest import bearer, png_bytes, register_and_confirm, upload_cloud
|
|
|
|
|
|
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
|
|
|
|
|
|
def test_collection_removed_after_deleting_last_cloud(client: TestClient, monkeypatch) -> None:
|
|
info = register_and_confirm(
|
|
client, monkeypatch,
|
|
f"coll-delete-{uuid.uuid4().hex[:8]}@example.com",
|
|
"collpass1", "图鉴删除用户",
|
|
)
|
|
upload = upload_cloud(client, info["token"], cloud_type_id="1")
|
|
cloud_id = upload.json()["cloud"]["id"]
|
|
|
|
r = client.delete(f"/api/v1/clouds/{cloud_id}", headers=bearer(info["token"]))
|
|
assert r.status_code == 200
|
|
|
|
r = client.get("/api/v1/collections/me", headers=bearer(info["token"]))
|
|
assert r.status_code == 200
|
|
assert r.json() == []
|
|
|
|
r = client.get("/api/v1/profiles/me", headers=bearer(info["token"]))
|
|
assert r.status_code == 200
|
|
assert r.json()["collection_count"] == 0
|
|
|
|
|
|
def test_collection_uses_next_cloud_after_deleting_first(client: TestClient, monkeypatch) -> None:
|
|
info = register_and_confirm(
|
|
client, monkeypatch,
|
|
f"coll-replace-{uuid.uuid4().hex[:8]}@example.com",
|
|
"collpass1", "图鉴替换用户",
|
|
)
|
|
first = upload_cloud(client, info["token"], cloud_type_id="1").json()["cloud"]
|
|
second = upload_cloud(client, info["token"], cloud_type_id="1").json()["cloud"]
|
|
|
|
r = client.delete(f"/api/v1/clouds/{first['id']}", headers=bearer(info["token"]))
|
|
assert r.status_code == 200
|
|
|
|
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]["first_cloud_id"] == second["id"]
|
|
assert r.json()[0]["first_cloud"]["id"] == second["id"]
|
|
|
|
|
|
def test_batch_delete_removes_affected_collections(client: TestClient, monkeypatch) -> None:
|
|
info = register_and_confirm(
|
|
client, monkeypatch,
|
|
f"coll-batch-{uuid.uuid4().hex[:8]}@example.com",
|
|
"collpass1", "图鉴批删用户",
|
|
)
|
|
first = upload_cloud(client, info["token"], cloud_type_id="1").json()["cloud"]
|
|
second = upload_cloud(client, info["token"], cloud_type_id="2").json()["cloud"]
|
|
|
|
r = client.post(
|
|
"/api/v1/clouds/batch-delete",
|
|
headers=bearer(info["token"]),
|
|
json={"ids": [first["id"], second["id"]]},
|
|
)
|
|
assert r.status_code == 200
|
|
|
|
r = client.get("/api/v1/collections/me", headers=bearer(info["token"]))
|
|
assert r.status_code == 200
|
|
assert r.json() == []
|