test_library_trash_api.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. """Integration tests for the library trash bin + admin purge (#1008)."""
  2. from datetime import datetime, timedelta, timezone
  3. import pytest
  4. from httpx import AsyncClient
  5. @pytest.fixture
  6. async def file_factory(db_session):
  7. """Factory for LibraryFile rows with sensible defaults."""
  8. _counter = [0]
  9. async def _create_file(**kwargs):
  10. from backend.app.models.library import LibraryFile
  11. _counter[0] += 1
  12. counter = _counter[0]
  13. defaults = {
  14. "filename": f"trash_test_{counter}.3mf",
  15. "file_path": f"/test/library/trash_test_{counter}.3mf",
  16. "file_size": 1024 * counter,
  17. "file_type": "3mf",
  18. }
  19. defaults.update(kwargs)
  20. lib_file = LibraryFile(**defaults)
  21. db_session.add(lib_file)
  22. await db_session.commit()
  23. await db_session.refresh(lib_file)
  24. return lib_file
  25. return _create_file
  26. @pytest.mark.asyncio
  27. @pytest.mark.integration
  28. async def test_delete_file_moves_to_trash(async_client: AsyncClient, file_factory, db_session):
  29. """DELETE /library/files/{id} soft-deletes (managed) files into trash."""
  30. from backend.app.models.library import LibraryFile
  31. f = await file_factory()
  32. response = await async_client.delete(f"/api/v1/library/files/{f.id}")
  33. assert response.status_code == 200
  34. body = response.json()
  35. assert body["trashed"] is True
  36. # Row still exists with deleted_at stamped
  37. await db_session.refresh(f)
  38. assert f.deleted_at is not None
  39. # Normal listing hides it
  40. list_resp = await async_client.get("/api/v1/library/files")
  41. assert list_resp.status_code == 200
  42. ids = [row["id"] for row in list_resp.json()]
  43. assert f.id not in ids
  44. # Trash listing surfaces it
  45. trash_resp = await async_client.get("/api/v1/library/trash")
  46. assert trash_resp.status_code == 200
  47. payload = trash_resp.json()
  48. trashed_ids = [item["id"] for item in payload["items"]]
  49. assert f.id in trashed_ids
  50. assert payload["total"] >= 1
  51. assert payload["retention_days"] >= 1
  52. # Row's file_type is preserved in the original table (sanity check on the filter)
  53. row = await db_session.get(LibraryFile, f.id)
  54. assert row is not None
  55. assert row.file_type == "3mf"
  56. @pytest.mark.asyncio
  57. @pytest.mark.integration
  58. async def test_delete_external_file_hard_deletes(async_client: AsyncClient, file_factory, db_session):
  59. """External files skip the trash — DB row is dropped directly."""
  60. from backend.app.models.library import LibraryFile
  61. f = await file_factory(is_external=True)
  62. file_id = f.id
  63. response = await async_client.delete(f"/api/v1/library/files/{file_id}")
  64. assert response.status_code == 200
  65. assert response.json()["trashed"] is False
  66. # The route commits in its own session; expire ours so get() re-reads.
  67. db_session.expire_all()
  68. missing = await db_session.get(LibraryFile, file_id)
  69. assert missing is None
  70. @pytest.mark.asyncio
  71. @pytest.mark.integration
  72. async def test_restore_from_trash(async_client: AsyncClient, file_factory, db_session):
  73. """Restoring a trashed file clears deleted_at and makes it visible again."""
  74. f = await file_factory()
  75. await async_client.delete(f"/api/v1/library/files/{f.id}")
  76. resp = await async_client.post(f"/api/v1/library/trash/{f.id}/restore")
  77. assert resp.status_code == 200
  78. await db_session.refresh(f)
  79. assert f.deleted_at is None
  80. list_resp = await async_client.get("/api/v1/library/files")
  81. ids = [row["id"] for row in list_resp.json()]
  82. assert f.id in ids
  83. @pytest.mark.asyncio
  84. @pytest.mark.integration
  85. async def test_hard_delete_from_trash(async_client: AsyncClient, file_factory, db_session):
  86. """Hard-delete from trash removes the DB row immediately."""
  87. from backend.app.models.library import LibraryFile
  88. f = await file_factory()
  89. file_id = f.id
  90. await async_client.delete(f"/api/v1/library/files/{file_id}")
  91. resp = await async_client.delete(f"/api/v1/library/trash/{file_id}")
  92. assert resp.status_code == 200
  93. db_session.expire_all()
  94. row = await db_session.get(LibraryFile, file_id)
  95. assert row is None
  96. @pytest.mark.asyncio
  97. @pytest.mark.integration
  98. async def test_empty_trash(async_client: AsyncClient, file_factory, db_session):
  99. """Empty-trash hard-deletes every trashed row in the caller's scope."""
  100. from sqlalchemy import func, select
  101. from backend.app.models.library import LibraryFile
  102. for _ in range(3):
  103. f = await file_factory()
  104. await async_client.delete(f"/api/v1/library/files/{f.id}")
  105. resp = await async_client.delete("/api/v1/library/trash")
  106. assert resp.status_code == 200
  107. assert resp.json()["deleted"] >= 3
  108. count = await db_session.execute(select(func.count(LibraryFile.id)).where(LibraryFile.deleted_at.isnot(None)))
  109. assert (count.scalar() or 0) == 0
  110. @pytest.mark.asyncio
  111. @pytest.mark.integration
  112. async def test_hard_delete_rejects_active_file(async_client: AsyncClient, file_factory):
  113. """Trash endpoints 404 for files that aren't actually trashed."""
  114. f = await file_factory()
  115. resp = await async_client.delete(f"/api/v1/library/trash/{f.id}")
  116. assert resp.status_code == 404
  117. resp = await async_client.post(f"/api/v1/library/trash/{f.id}/restore")
  118. assert resp.status_code == 404
  119. @pytest.mark.asyncio
  120. @pytest.mark.integration
  121. async def test_purge_preview_counts_old_files(async_client: AsyncClient, file_factory, db_session):
  122. """Preview counts only files past the threshold and returns total size + samples."""
  123. old_cutoff = datetime.now(timezone.utc) - timedelta(days=120)
  124. old1 = await file_factory(file_size=5000)
  125. old2 = await file_factory(file_size=7000)
  126. # A young file whose created_at stays near "now" — must not be counted.
  127. await file_factory(file_size=3000)
  128. # Stamp created_at into the past so the never-printed branch matches.
  129. for row, ts in ((old1, old_cutoff), (old2, old_cutoff)):
  130. row.created_at = ts
  131. await db_session.commit()
  132. resp = await async_client.get(
  133. "/api/v1/library/purge/preview",
  134. params={"older_than_days": 90, "include_never_printed": True},
  135. )
  136. assert resp.status_code == 200
  137. body = resp.json()
  138. assert body["count"] == 2
  139. assert body["total_bytes"] == 12000
  140. assert body["older_than_days"] == 90
  141. assert len(body["sample_filenames"]) == 2
  142. @pytest.mark.asyncio
  143. @pytest.mark.integration
  144. async def test_purge_excludes_never_printed_when_requested(async_client: AsyncClient, file_factory, db_session):
  145. """With include_never_printed=False, only files with last_printed_at are eligible."""
  146. long_ago = datetime.now(timezone.utc) - timedelta(days=200)
  147. recently_printed = await file_factory()
  148. recently_printed.last_printed_at = long_ago
  149. never_printed = await file_factory()
  150. never_printed.created_at = long_ago
  151. await db_session.commit()
  152. # Exclude never-printed → only 1 match
  153. resp = await async_client.get(
  154. "/api/v1/library/purge/preview",
  155. params={"older_than_days": 90, "include_never_printed": False},
  156. )
  157. assert resp.json()["count"] == 1
  158. # Include → 2 matches
  159. resp = await async_client.get(
  160. "/api/v1/library/purge/preview",
  161. params={"older_than_days": 90, "include_never_printed": True},
  162. )
  163. assert resp.json()["count"] == 2
  164. @pytest.mark.asyncio
  165. @pytest.mark.integration
  166. async def test_purge_execute_moves_to_trash(async_client: AsyncClient, file_factory, db_session):
  167. """POST /library/purge moves matching files into trash (deleted_at stamped)."""
  168. long_ago = datetime.now(timezone.utc) - timedelta(days=200)
  169. f = await file_factory()
  170. f.created_at = long_ago
  171. await db_session.commit()
  172. resp = await async_client.post(
  173. "/api/v1/library/purge",
  174. json={"older_than_days": 90, "include_never_printed": True},
  175. )
  176. assert resp.status_code == 200
  177. assert resp.json()["moved_to_trash"] >= 1
  178. await db_session.refresh(f)
  179. assert f.deleted_at is not None
  180. @pytest.mark.asyncio
  181. @pytest.mark.integration
  182. async def test_purge_skips_external_files(async_client: AsyncClient, file_factory, db_session):
  183. """External files are never eligible for purge, regardless of age."""
  184. long_ago = datetime.now(timezone.utc) - timedelta(days=300)
  185. ext = await file_factory(is_external=True)
  186. ext.created_at = long_ago
  187. await db_session.commit()
  188. resp = await async_client.get(
  189. "/api/v1/library/purge/preview",
  190. params={"older_than_days": 90, "include_never_printed": True},
  191. )
  192. assert resp.json()["count"] == 0
  193. @pytest.mark.asyncio
  194. @pytest.mark.integration
  195. async def test_trash_settings_roundtrip(async_client: AsyncClient):
  196. """Retention setting persists and is clamped to [MIN, MAX]."""
  197. resp = await async_client.get("/api/v1/library/trash/settings")
  198. assert resp.status_code == 200
  199. default = resp.json()["retention_days"]
  200. assert 1 <= default <= 365
  201. resp = await async_client.put("/api/v1/library/trash/settings", json={"retention_days": 60})
  202. assert resp.status_code == 200
  203. assert resp.json()["retention_days"] == 60
  204. resp = await async_client.get("/api/v1/library/trash/settings")
  205. assert resp.json()["retention_days"] == 60
  206. @pytest.mark.asyncio
  207. @pytest.mark.integration
  208. async def test_trash_settings_rejects_out_of_range(async_client: AsyncClient):
  209. """retention_days must fall within the clamped range."""
  210. resp = await async_client.put("/api/v1/library/trash/settings", json={"retention_days": 0})
  211. assert resp.status_code == 422 # Pydantic ge=1 trip
  212. resp = await async_client.put("/api/v1/library/trash/settings", json={"retention_days": 9999})
  213. assert resp.status_code == 422
  214. @pytest.mark.asyncio
  215. @pytest.mark.integration
  216. async def test_sweeper_hard_deletes_past_retention(db_session):
  217. """The background sweeper clears rows whose deleted_at is older than retention."""
  218. from backend.app.models.library import LibraryFile
  219. from backend.app.services.library_trash import library_trash_service
  220. # Retention = 30 days; stamp one row 40 days ago, one 5 days ago.
  221. await library_trash_service.set_retention_days(db_session, 30)
  222. fresh = LibraryFile(
  223. filename="fresh.3mf",
  224. file_path="/test/library/fresh.3mf",
  225. file_size=1024,
  226. file_type="3mf",
  227. deleted_at=datetime.now(timezone.utc) - timedelta(days=5),
  228. )
  229. stale = LibraryFile(
  230. filename="stale.3mf",
  231. file_path="/test/library/stale.3mf",
  232. file_size=2048,
  233. file_type="3mf",
  234. deleted_at=datetime.now(timezone.utc) - timedelta(days=40),
  235. )
  236. db_session.add_all([fresh, stale])
  237. await db_session.commit()
  238. stale_id = stale.id
  239. fresh_id = fresh.id
  240. deleted = await library_trash_service._sweep(db_session)
  241. assert deleted >= 1
  242. # The sweeper commits in its own session; expire ours so get() re-reads.
  243. db_session.expire_all()
  244. remaining = await db_session.get(LibraryFile, stale_id)
  245. assert remaining is None
  246. still_there = await db_session.get(LibraryFile, fresh_id)
  247. assert still_there is not None
  248. @pytest.mark.asyncio
  249. @pytest.mark.integration
  250. async def test_auto_purge_settings_roundtrip(async_client: AsyncClient):
  251. """Auto-purge fields on /library/trash/settings round-trip correctly."""
  252. resp = await async_client.put(
  253. "/api/v1/library/trash/settings",
  254. json={
  255. "retention_days": 30,
  256. "auto_purge_enabled": True,
  257. "auto_purge_days": 120,
  258. "auto_purge_include_never_printed": False,
  259. },
  260. )
  261. assert resp.status_code == 200
  262. body = resp.json()
  263. assert body["auto_purge_enabled"] is True
  264. assert body["auto_purge_days"] == 120
  265. assert body["auto_purge_include_never_printed"] is False
  266. # GET surfaces the same saved values
  267. resp = await async_client.get("/api/v1/library/trash/settings")
  268. got = resp.json()
  269. assert got["auto_purge_enabled"] is True
  270. assert got["auto_purge_days"] == 120
  271. assert got["auto_purge_include_never_printed"] is False
  272. @pytest.mark.asyncio
  273. @pytest.mark.integration
  274. async def test_auto_purge_runs_when_enabled_and_throttles_by_24h(file_factory, db_session):
  275. """The scheduler loop's auto-purge branch runs once, then the 24h throttle blocks."""
  276. from backend.app.services.library_trash import library_trash_service
  277. long_ago = datetime.now(timezone.utc) - timedelta(days=200)
  278. f = await file_factory()
  279. f.created_at = long_ago
  280. await db_session.commit()
  281. # Enable auto-purge with a 90-day threshold
  282. await library_trash_service.set_auto_purge_settings(db_session, enabled=True, days=90, include_never_printed=True)
  283. moved = await library_trash_service._maybe_run_auto_purge(db_session)
  284. assert moved >= 1
  285. db_session.expire_all()
  286. await db_session.refresh(f)
  287. assert f.deleted_at is not None
  288. # Second invocation within 24h should be throttled — no additional rows moved.
  289. long_ago2 = datetime.now(timezone.utc) - timedelta(days=200)
  290. f2 = await file_factory()
  291. f2.created_at = long_ago2
  292. await db_session.commit()
  293. moved_again = await library_trash_service._maybe_run_auto_purge(db_session)
  294. assert moved_again == 0
  295. @pytest.mark.asyncio
  296. @pytest.mark.integration
  297. async def test_auto_purge_skipped_when_disabled(file_factory, db_session):
  298. """If the toggle is off, old files stay put even when everything else matches."""
  299. from backend.app.services.library_trash import library_trash_service
  300. long_ago = datetime.now(timezone.utc) - timedelta(days=200)
  301. f = await file_factory()
  302. f.created_at = long_ago
  303. await db_session.commit()
  304. await library_trash_service.set_auto_purge_settings(db_session, enabled=False, days=90, include_never_printed=True)
  305. moved = await library_trash_service._maybe_run_auto_purge(db_session)
  306. assert moved == 0
  307. db_session.expire_all()
  308. await db_session.refresh(f)
  309. assert f.deleted_at is None
  310. @pytest.mark.asyncio
  311. @pytest.mark.integration
  312. async def test_trashed_file_hidden_from_makerworld_dedupe(async_client: AsyncClient, file_factory, db_session):
  313. """MakerWorld 'already imported' dedupe must not match trashed rows."""
  314. from sqlalchemy import select
  315. from backend.app.models.library import LibraryFile
  316. f = await file_factory(source_type="makerworld", source_url="https://makerworld.com/en/models/99#profileId-1")
  317. # Trash it.
  318. await async_client.delete(f"/api/v1/library/files/{f.id}")
  319. # The dedupe query used by the makerworld helper is `source_url == X AND deleted_at IS NULL`.
  320. result = await db_session.execute(
  321. LibraryFile.active().where(LibraryFile.source_url == "https://makerworld.com/en/models/99#profileId-1")
  322. )
  323. assert result.scalar_one_or_none() is None
  324. # Direct lookup WITHOUT the active filter still sees the row.
  325. direct = await db_session.execute(
  326. select(LibraryFile).where(LibraryFile.source_url == "https://makerworld.com/en/models/99#profileId-1")
  327. )
  328. assert direct.scalar_one_or_none() is not None