fix: preserve countdown dates and restore ids
ci / gitleaks (push) Successful in 7s
ci / docker (push) Successful in 3m19s

This commit is contained in:
2026-09-09 11:45:26 +08:00
parent 010bfd3653
commit e163f6f368
2 changed files with 100 additions and 10 deletions
+77
View File
@@ -84,6 +84,35 @@ def test_lunar_countdown_crud_validation_and_display(client):
assert create_countdown(client, calendar_mode="solar", lunar_month=8, lunar_day=4).status_code == 422
def test_editing_only_lunar_countdown_title_preserves_original_lunar_year(client):
boot(client)
created = create_countdown(
client,
title="农历纪念日",
event_date="2001-01-01",
calendar_mode="lunar",
lunar_month=12,
lunar_day=20,
)
assert created.status_code == 201
before = created.json()
assert before["event_date"] == "2002-02-01"
assert before["lunar_year"] == 2001
updated = client.patch(
f"/api/v1/countdowns/{before['id']}",
json={"title": "只修改标题"},
)
assert updated.status_code == 200
after = updated.json()
assert after["title"] == "只修改标题"
assert after["event_date"] == before["event_date"]
assert after["lunar_year"] == before["lunar_year"]
assert after["lunar_month"] == before["lunar_month"]
assert after["lunar_day"] == before["lunar_day"]
def test_countdown_crud_single_pin_archive_restore_and_purge(client):
boot(client)
first = create_countdown(client).json()
@@ -188,3 +217,51 @@ def test_countdowns_backup_replace_and_merge_round_trip(client):
assert [item["title"] for item in all_active].count("周年") == 1
assert sum(item["pinned"] for item in all_active) == 1
assert active["title"] == "周年"
def test_countdown_backup_merge_remaps_ids_owned_by_another_user(client):
boot(client)
original = create_countdown(client, title="跨账号纪念日").json()
exported = client.get("/api/v1/export").json()
client.post("/api/v1/auth/logout")
from backend.auth import hash_password
from backend.db import get_db
from backend.models import TaskList, User
async def add_other_user():
db_gen = get_db()
db = await anext(db_gen)
try:
other = User(username="other", password_hash=hash_password("correct horse battery staple"))
db.add(other)
await db.flush()
db.add(TaskList(user_id=other.id, name="收集箱", is_inbox=True))
await db.commit()
finally:
await db_gen.aclose()
client.portal.call(add_other_user)
assert client.post(
"/api/v1/auth/login",
json={"username": "other", "password": "correct horse battery staple"},
).status_code == 200
restored = client.post("/api/v1/restore", params={"mode": "merge"}, json=exported)
assert restored.status_code == 200
items = client.get("/api/v1/countdowns").json()
assert len(items) == 1
assert items[0]["title"] == "跨账号纪念日"
assert items[0]["id"] != original["id"]
merged_again = client.post("/api/v1/restore", params={"mode": "merge"}, json=exported)
assert merged_again.status_code == 200
assert len(client.get("/api/v1/countdowns").json()) == 1
exported_by_other = client.get("/api/v1/export").json()
restored_again = client.post("/api/v1/restore", params={"mode": "replace"}, json=exported_by_other)
assert restored_again.status_code == 200
replaced = client.get("/api/v1/countdowns").json()
assert len(replaced) == 1
assert replaced[0]["id"] == items[0]["id"]