test_library_api.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. """Integration tests for Library API endpoints."""
  2. import io
  3. import tempfile
  4. import zipfile
  5. from pathlib import Path
  6. import pytest
  7. from httpx import AsyncClient
  8. class TestLibraryFoldersAPI:
  9. """Integration tests for library folders endpoints."""
  10. @pytest.fixture
  11. async def folder_factory(self, db_session):
  12. """Factory to create test folders."""
  13. _counter = [0]
  14. async def _create_folder(**kwargs):
  15. from backend.app.models.library import LibraryFolder
  16. _counter[0] += 1
  17. counter = _counter[0]
  18. defaults = {
  19. "name": f"Test Folder {counter}",
  20. }
  21. defaults.update(kwargs)
  22. folder = LibraryFolder(**defaults)
  23. db_session.add(folder)
  24. await db_session.commit()
  25. await db_session.refresh(folder)
  26. return folder
  27. return _create_folder
  28. @pytest.mark.asyncio
  29. @pytest.mark.integration
  30. async def test_list_folders_empty(self, async_client: AsyncClient, db_session):
  31. """Verify empty folder list returns empty array."""
  32. response = await async_client.get("/api/v1/library/folders")
  33. assert response.status_code == 200
  34. assert response.json() == []
  35. @pytest.mark.asyncio
  36. @pytest.mark.integration
  37. async def test_create_folder(self, async_client: AsyncClient, db_session):
  38. """Verify folder can be created."""
  39. data = {"name": "New Folder"}
  40. response = await async_client.post("/api/v1/library/folders", json=data)
  41. assert response.status_code == 200
  42. result = response.json()
  43. assert result["name"] == "New Folder"
  44. assert result["id"] is not None
  45. @pytest.mark.asyncio
  46. @pytest.mark.integration
  47. async def test_create_nested_folder(self, async_client: AsyncClient, folder_factory, db_session):
  48. """Verify nested folder can be created."""
  49. parent = await folder_factory(name="Parent")
  50. data = {"name": "Child", "parent_id": parent.id}
  51. response = await async_client.post("/api/v1/library/folders", json=data)
  52. assert response.status_code == 200
  53. result = response.json()
  54. assert result["name"] == "Child"
  55. assert result["parent_id"] == parent.id
  56. @pytest.mark.asyncio
  57. @pytest.mark.integration
  58. async def test_get_folder(self, async_client: AsyncClient, folder_factory, db_session):
  59. """Verify single folder can be retrieved."""
  60. folder = await folder_factory(name="Test Folder")
  61. response = await async_client.get(f"/api/v1/library/folders/{folder.id}")
  62. assert response.status_code == 200
  63. result = response.json()
  64. assert result["id"] == folder.id
  65. assert result["name"] == "Test Folder"
  66. @pytest.mark.asyncio
  67. @pytest.mark.integration
  68. async def test_get_folder_not_found(self, async_client: AsyncClient, db_session):
  69. """Verify 404 for non-existent folder."""
  70. response = await async_client.get("/api/v1/library/folders/9999")
  71. assert response.status_code == 404
  72. @pytest.mark.asyncio
  73. @pytest.mark.integration
  74. async def test_update_folder(self, async_client: AsyncClient, folder_factory, db_session):
  75. """Verify folder can be updated."""
  76. folder = await folder_factory(name="Old Name")
  77. data = {"name": "New Name"}
  78. response = await async_client.put(f"/api/v1/library/folders/{folder.id}", json=data)
  79. assert response.status_code == 200
  80. result = response.json()
  81. assert result["name"] == "New Name"
  82. @pytest.mark.asyncio
  83. @pytest.mark.integration
  84. async def test_delete_folder(self, async_client: AsyncClient, folder_factory, db_session):
  85. """Verify folder can be deleted."""
  86. folder = await folder_factory()
  87. response = await async_client.delete(f"/api/v1/library/folders/{folder.id}")
  88. assert response.status_code == 200
  89. result = response.json()
  90. assert result.get("message") or result.get("success", True)
  91. class TestLibraryFilesAPI:
  92. """Integration tests for library files endpoints."""
  93. @pytest.fixture
  94. async def folder_factory(self, db_session):
  95. """Factory to create test folders."""
  96. _counter = [0]
  97. async def _create_folder(**kwargs):
  98. from backend.app.models.library import LibraryFolder
  99. _counter[0] += 1
  100. counter = _counter[0]
  101. defaults = {"name": f"Test Folder {counter}"}
  102. defaults.update(kwargs)
  103. folder = LibraryFolder(**defaults)
  104. db_session.add(folder)
  105. await db_session.commit()
  106. await db_session.refresh(folder)
  107. return folder
  108. return _create_folder
  109. @pytest.fixture
  110. async def file_factory(self, db_session):
  111. """Factory to create test files."""
  112. _counter = [0]
  113. async def _create_file(**kwargs):
  114. from backend.app.models.library import LibraryFile
  115. _counter[0] += 1
  116. counter = _counter[0]
  117. defaults = {
  118. "filename": f"test_file_{counter}.3mf",
  119. "file_path": f"/test/path/test_file_{counter}.3mf",
  120. "file_size": 1024,
  121. "file_type": "3mf",
  122. }
  123. defaults.update(kwargs)
  124. lib_file = LibraryFile(**defaults)
  125. db_session.add(lib_file)
  126. await db_session.commit()
  127. await db_session.refresh(lib_file)
  128. return lib_file
  129. return _create_file
  130. @pytest.mark.asyncio
  131. @pytest.mark.integration
  132. async def test_list_files_empty(self, async_client: AsyncClient, db_session):
  133. """Verify empty file list returns empty array."""
  134. response = await async_client.get("/api/v1/library/files")
  135. assert response.status_code == 200
  136. assert response.json() == []
  137. @pytest.mark.asyncio
  138. @pytest.mark.integration
  139. async def test_list_files_in_folder(self, async_client: AsyncClient, folder_factory, file_factory, db_session):
  140. """Verify files can be filtered by folder."""
  141. folder = await folder_factory()
  142. file1 = await file_factory(folder_id=folder.id)
  143. await file_factory() # File in root (no folder)
  144. response = await async_client.get(f"/api/v1/library/files?folder_id={folder.id}")
  145. assert response.status_code == 200
  146. result = response.json()
  147. assert len(result) == 1
  148. assert result[0]["id"] == file1.id
  149. @pytest.mark.asyncio
  150. @pytest.mark.integration
  151. async def test_get_file(self, async_client: AsyncClient, file_factory, db_session):
  152. """Verify single file can be retrieved."""
  153. lib_file = await file_factory(filename="test.3mf")
  154. response = await async_client.get(f"/api/v1/library/files/{lib_file.id}")
  155. assert response.status_code == 200
  156. result = response.json()
  157. assert result["id"] == lib_file.id
  158. assert result["filename"] == "test.3mf"
  159. @pytest.mark.asyncio
  160. @pytest.mark.integration
  161. async def test_get_file_not_found(self, async_client: AsyncClient, db_session):
  162. """Verify 404 for non-existent file."""
  163. response = await async_client.get("/api/v1/library/files/9999")
  164. assert response.status_code == 404
  165. @pytest.mark.asyncio
  166. @pytest.mark.integration
  167. async def test_delete_file(self, async_client: AsyncClient, file_factory, db_session):
  168. """Verify file can be deleted."""
  169. lib_file = await file_factory()
  170. response = await async_client.delete(f"/api/v1/library/files/{lib_file.id}")
  171. assert response.status_code == 200
  172. result = response.json()
  173. assert result.get("message") or result.get("success", True)
  174. @pytest.mark.asyncio
  175. @pytest.mark.integration
  176. async def test_rename_file(self, async_client: AsyncClient, file_factory, db_session):
  177. """Verify file can be renamed."""
  178. lib_file = await file_factory(filename="old_name.3mf")
  179. data = {"filename": "new_name.3mf"}
  180. response = await async_client.put(f"/api/v1/library/files/{lib_file.id}", json=data)
  181. assert response.status_code == 200
  182. result = response.json()
  183. assert result["filename"] == "new_name.3mf"
  184. @pytest.mark.asyncio
  185. @pytest.mark.integration
  186. async def test_rename_file_invalid_path_separator(self, async_client: AsyncClient, file_factory, db_session):
  187. """Verify file rename fails with path separators."""
  188. lib_file = await file_factory(filename="test.3mf")
  189. data = {"filename": "path/to/file.3mf"}
  190. response = await async_client.put(f"/api/v1/library/files/{lib_file.id}", json=data)
  191. assert response.status_code == 400
  192. assert "path separator" in response.json()["detail"].lower()
  193. @pytest.mark.asyncio
  194. @pytest.mark.integration
  195. async def test_rename_file_invalid_backslash(self, async_client: AsyncClient, file_factory, db_session):
  196. """Verify file rename fails with backslash."""
  197. lib_file = await file_factory(filename="test.3mf")
  198. data = {"filename": "path\\to\\file.3mf"}
  199. response = await async_client.put(f"/api/v1/library/files/{lib_file.id}", json=data)
  200. assert response.status_code == 400
  201. assert "path separator" in response.json()["detail"].lower()
  202. @pytest.mark.asyncio
  203. @pytest.mark.integration
  204. async def test_library_stats(self, async_client: AsyncClient, folder_factory, file_factory, db_session):
  205. """Verify library stats endpoint returns counts."""
  206. await folder_factory()
  207. await folder_factory()
  208. await file_factory()
  209. response = await async_client.get("/api/v1/library/stats")
  210. assert response.status_code == 200
  211. result = response.json()
  212. assert result["total_folders"] == 2
  213. assert result["total_files"] == 1
  214. @pytest.mark.asyncio
  215. @pytest.mark.integration
  216. async def test_file_list_includes_user_tracking_fields(self, async_client: AsyncClient, file_factory, db_session):
  217. """Verify file list response includes user tracking fields (Issue #206)."""
  218. lib_file = await file_factory(filename="test.3mf")
  219. response = await async_client.get("/api/v1/library/files?include_root=false")
  220. assert response.status_code == 200
  221. result = response.json()
  222. assert len(result) >= 1
  223. # Find our test file
  224. test_file = next((f for f in result if f["id"] == lib_file.id), None)
  225. assert test_file is not None
  226. # User tracking fields should be present (even if null)
  227. assert "created_by_id" in test_file
  228. assert "created_by_username" in test_file
  229. @pytest.mark.asyncio
  230. @pytest.mark.integration
  231. async def test_file_detail_includes_user_tracking_fields(self, async_client: AsyncClient, file_factory, db_session):
  232. """Verify file detail response includes user tracking fields (Issue #206)."""
  233. lib_file = await file_factory(filename="test_detail.3mf")
  234. response = await async_client.get(f"/api/v1/library/files/{lib_file.id}")
  235. assert response.status_code == 200
  236. result = response.json()
  237. # User tracking fields should be present (even if null)
  238. assert "created_by_id" in result
  239. assert "created_by_username" in result
  240. @pytest.mark.asyncio
  241. @pytest.mark.integration
  242. async def test_file_with_user_tracking(self, async_client: AsyncClient, db_session):
  243. """Verify file created with user shows username in response (Issue #206)."""
  244. from backend.app.models.library import LibraryFile
  245. from backend.app.models.user import User
  246. # Create a test user
  247. user = User(username="testuploader", password_hash="fakehash", role="user")
  248. db_session.add(user)
  249. await db_session.flush()
  250. # Create a file with created_by_id set
  251. lib_file = LibraryFile(
  252. filename="user_uploaded.3mf",
  253. file_path="/test/user_uploaded.3mf",
  254. file_size=2048,
  255. file_type="3mf",
  256. created_by_id=user.id,
  257. )
  258. db_session.add(lib_file)
  259. await db_session.commit()
  260. await db_session.refresh(lib_file)
  261. # Verify file detail shows username
  262. response = await async_client.get(f"/api/v1/library/files/{lib_file.id}")
  263. assert response.status_code == 200
  264. result = response.json()
  265. assert result["created_by_id"] == user.id
  266. assert result["created_by_username"] == "testuploader"
  267. # Verify file list also shows username
  268. response = await async_client.get("/api/v1/library/files?include_root=false")
  269. assert response.status_code == 200
  270. files = response.json()
  271. test_file = next((f for f in files if f["id"] == lib_file.id), None)
  272. assert test_file is not None
  273. assert test_file["created_by_id"] == user.id
  274. assert test_file["created_by_username"] == "testuploader"
  275. class TestLibraryAddToQueueAPI:
  276. """Integration tests for /api/v1/library/files/add-to-queue endpoint."""
  277. @pytest.fixture
  278. async def printer_factory(self, db_session):
  279. """Factory to create test printers."""
  280. _counter = [0]
  281. async def _create_printer(**kwargs):
  282. from backend.app.models.printer import Printer
  283. _counter[0] += 1
  284. counter = _counter[0]
  285. defaults = {
  286. "name": f"Test Printer {counter}",
  287. "ip_address": f"192.168.1.{100 + counter}",
  288. "serial_number": f"TESTSERIAL{counter:04d}",
  289. "access_code": "12345678",
  290. "model": "X1C",
  291. }
  292. defaults.update(kwargs)
  293. printer = Printer(**defaults)
  294. db_session.add(printer)
  295. await db_session.commit()
  296. await db_session.refresh(printer)
  297. return printer
  298. return _create_printer
  299. @pytest.fixture
  300. async def library_file_factory(self, db_session):
  301. """Factory to create test library files."""
  302. _counter = [0]
  303. async def _create_library_file(**kwargs):
  304. from backend.app.models.library import LibraryFile
  305. _counter[0] += 1
  306. counter = _counter[0]
  307. defaults = {
  308. "filename": f"test_file_{counter}.gcode.3mf",
  309. "file_path": f"/test/path/test_file_{counter}.gcode.3mf",
  310. "file_size": 1024,
  311. "file_type": "3mf",
  312. }
  313. defaults.update(kwargs)
  314. lib_file = LibraryFile(**defaults)
  315. db_session.add(lib_file)
  316. await db_session.commit()
  317. await db_session.refresh(lib_file)
  318. return lib_file
  319. return _create_library_file
  320. @pytest.mark.asyncio
  321. @pytest.mark.integration
  322. async def test_add_to_queue_file_not_found(self, async_client: AsyncClient, printer_factory, db_session):
  323. """Verify error for non-existent file."""
  324. await printer_factory()
  325. data = {"file_ids": [9999]}
  326. response = await async_client.post("/api/v1/library/files/add-to-queue", json=data)
  327. assert response.status_code == 200
  328. result = response.json()
  329. assert len(result["added"]) == 0
  330. assert len(result["errors"]) == 1
  331. assert result["errors"][0]["file_id"] == 9999
  332. @pytest.mark.asyncio
  333. @pytest.mark.integration
  334. async def test_add_non_sliced_file_to_queue_fails(
  335. self, async_client: AsyncClient, printer_factory, library_file_factory, db_session
  336. ):
  337. """Verify non-sliced file cannot be added to queue."""
  338. await printer_factory()
  339. lib_file = await library_file_factory(
  340. filename="model.stl",
  341. file_path="/test/path/model.stl",
  342. file_type="stl",
  343. )
  344. data = {"file_ids": [lib_file.id]}
  345. response = await async_client.post("/api/v1/library/files/add-to-queue", json=data)
  346. assert response.status_code == 200
  347. result = response.json()
  348. assert len(result["added"]) == 0
  349. assert len(result["errors"]) == 1
  350. assert "sliced" in result["errors"][0]["error"].lower()
  351. class TestLibraryZipExtractAPI:
  352. """Integration tests for ZIP extraction endpoint."""
  353. @pytest.mark.asyncio
  354. @pytest.mark.integration
  355. async def test_extract_zip_invalid_file_type(self, async_client: AsyncClient, db_session):
  356. """Verify non-ZIP files are rejected."""
  357. # Create a fake file that's not a ZIP
  358. files = {"file": ("test.txt", b"This is not a zip file", "text/plain")}
  359. response = await async_client.post("/api/v1/library/files/extract-zip", files=files)
  360. assert response.status_code == 400
  361. assert "ZIP" in response.json()["detail"]
  362. @pytest.mark.asyncio
  363. @pytest.mark.integration
  364. async def test_extract_zip_basic(self, async_client: AsyncClient, db_session):
  365. """Verify basic ZIP extraction works."""
  366. import io
  367. import zipfile
  368. # Create a simple ZIP file in memory
  369. zip_buffer = io.BytesIO()
  370. with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zf:
  371. zf.writestr("test1.txt", "Content of file 1")
  372. zf.writestr("test2.txt", "Content of file 2")
  373. zip_buffer.seek(0)
  374. files = {"file": ("test.zip", zip_buffer.read(), "application/zip")}
  375. response = await async_client.post("/api/v1/library/files/extract-zip", files=files)
  376. assert response.status_code == 200
  377. result = response.json()
  378. assert result["extracted"] == 2
  379. assert len(result["files"]) == 2
  380. assert len(result["errors"]) == 0
  381. @pytest.mark.asyncio
  382. @pytest.mark.integration
  383. async def test_extract_zip_with_folders(self, async_client: AsyncClient, db_session):
  384. """Verify ZIP extraction preserves folder structure."""
  385. import io
  386. import zipfile
  387. # Create a ZIP file with folder structure
  388. zip_buffer = io.BytesIO()
  389. with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zf:
  390. zf.writestr("folder1/file1.txt", "Content 1")
  391. zf.writestr("folder1/subfolder/file2.txt", "Content 2")
  392. zf.writestr("folder2/file3.txt", "Content 3")
  393. zip_buffer.seek(0)
  394. files = {"file": ("test.zip", zip_buffer.read(), "application/zip")}
  395. params = {"preserve_structure": "true"}
  396. response = await async_client.post("/api/v1/library/files/extract-zip", files=files, params=params)
  397. assert response.status_code == 200
  398. result = response.json()
  399. assert result["extracted"] == 3
  400. assert result["folders_created"] >= 3 # folder1, folder1/subfolder, folder2
  401. @pytest.mark.asyncio
  402. @pytest.mark.integration
  403. async def test_extract_zip_flat(self, async_client: AsyncClient, db_session):
  404. """Verify ZIP extraction can extract flat (no folders)."""
  405. import io
  406. import zipfile
  407. # Create a ZIP file with folder structure
  408. zip_buffer = io.BytesIO()
  409. with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zf:
  410. zf.writestr("folder/file1.txt", "Content 1")
  411. zf.writestr("folder/file2.txt", "Content 2")
  412. zip_buffer.seek(0)
  413. files = {"file": ("test.zip", zip_buffer.read(), "application/zip")}
  414. params = {"preserve_structure": "false"}
  415. response = await async_client.post("/api/v1/library/files/extract-zip", files=files, params=params)
  416. assert response.status_code == 200
  417. result = response.json()
  418. assert result["extracted"] == 2
  419. assert result["folders_created"] == 0 # No folders created when flat
  420. @pytest.mark.asyncio
  421. @pytest.mark.integration
  422. async def test_extract_zip_skips_macos_files(self, async_client: AsyncClient, db_session):
  423. """Verify ZIP extraction skips __MACOSX and hidden files."""
  424. import io
  425. import zipfile
  426. # Create a ZIP file with macOS junk files
  427. zip_buffer = io.BytesIO()
  428. with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zf:
  429. zf.writestr("real_file.txt", "Real content")
  430. zf.writestr("__MACOSX/._real_file.txt", "macOS metadata")
  431. zf.writestr(".hidden_file", "Hidden content")
  432. zip_buffer.seek(0)
  433. files = {"file": ("test.zip", zip_buffer.read(), "application/zip")}
  434. response = await async_client.post("/api/v1/library/files/extract-zip", files=files)
  435. assert response.status_code == 200
  436. result = response.json()
  437. assert result["extracted"] == 1 # Only real_file.txt
  438. assert result["files"][0]["filename"] == "real_file.txt"
  439. @pytest.mark.asyncio
  440. @pytest.mark.integration
  441. async def test_extract_zip_create_folder_from_zip(self, async_client: AsyncClient, db_session):
  442. """Verify ZIP extraction creates a folder from the ZIP filename."""
  443. import io
  444. import zipfile
  445. # Create a ZIP file with some files
  446. zip_buffer = io.BytesIO()
  447. with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zf:
  448. zf.writestr("file1.txt", "Content 1")
  449. zf.writestr("file2.txt", "Content 2")
  450. zip_buffer.seek(0)
  451. files = {"file": ("MyProject.zip", zip_buffer.read(), "application/zip")}
  452. params = {"create_folder_from_zip": "true", "preserve_structure": "false"}
  453. response = await async_client.post("/api/v1/library/files/extract-zip", files=files, params=params)
  454. assert response.status_code == 200
  455. result = response.json()
  456. assert result["extracted"] == 2
  457. assert result["folders_created"] == 1 # MyProject folder created
  458. # Verify the files are in a folder
  459. assert result["files"][0]["folder_id"] is not None
  460. assert result["files"][1]["folder_id"] is not None
  461. # Both files should be in the same folder
  462. assert result["files"][0]["folder_id"] == result["files"][1]["folder_id"]
  463. # Verify the folder was created with the right name
  464. folder_response = await async_client.get(f"/api/v1/library/folders/{result['files'][0]['folder_id']}")
  465. assert folder_response.status_code == 200
  466. folder = folder_response.json()
  467. assert folder["name"] == "MyProject"
  468. class TestLibraryStlThumbnailAPI:
  469. """Integration tests for STL thumbnail generation endpoints."""
  470. @pytest.fixture
  471. async def file_factory(self, db_session):
  472. """Factory to create test files."""
  473. _counter = [0]
  474. async def _create_file(**kwargs):
  475. from backend.app.models.library import LibraryFile
  476. _counter[0] += 1
  477. counter = _counter[0]
  478. defaults = {
  479. "filename": f"test_model_{counter}.stl",
  480. "file_path": f"/test/path/test_model_{counter}.stl",
  481. "file_size": 1024,
  482. "file_type": "stl",
  483. }
  484. defaults.update(kwargs)
  485. lib_file = LibraryFile(**defaults)
  486. db_session.add(lib_file)
  487. await db_session.commit()
  488. await db_session.refresh(lib_file)
  489. return lib_file
  490. return _create_file
  491. @pytest.mark.asyncio
  492. @pytest.mark.integration
  493. async def test_batch_generate_thumbnails_empty(self, async_client: AsyncClient, db_session):
  494. """Verify batch thumbnail generation with no files."""
  495. data = {"all_missing": True}
  496. response = await async_client.post("/api/v1/library/generate-stl-thumbnails", json=data)
  497. assert response.status_code == 200
  498. result = response.json()
  499. assert result["processed"] == 0
  500. assert result["succeeded"] == 0
  501. assert result["failed"] == 0
  502. assert result["results"] == []
  503. @pytest.mark.asyncio
  504. @pytest.mark.integration
  505. async def test_batch_generate_thumbnails_no_criteria(self, async_client: AsyncClient, db_session):
  506. """Verify batch thumbnail generation with no criteria returns empty."""
  507. data = {}
  508. response = await async_client.post("/api/v1/library/generate-stl-thumbnails", json=data)
  509. assert response.status_code == 200
  510. result = response.json()
  511. assert result["processed"] == 0
  512. @pytest.mark.asyncio
  513. @pytest.mark.integration
  514. async def test_batch_generate_thumbnails_file_not_on_disk(
  515. self, async_client: AsyncClient, file_factory, db_session
  516. ):
  517. """Verify batch thumbnail generation handles missing files gracefully."""
  518. # Create a file in DB but not on disk
  519. stl_file = await file_factory(
  520. filename="missing.stl",
  521. file_path="/nonexistent/path/missing.stl",
  522. thumbnail_path=None,
  523. )
  524. data = {"file_ids": [stl_file.id]}
  525. response = await async_client.post("/api/v1/library/generate-stl-thumbnails", json=data)
  526. assert response.status_code == 200
  527. result = response.json()
  528. assert result["processed"] == 1
  529. assert result["succeeded"] == 0
  530. assert result["failed"] == 1
  531. assert result["results"][0]["success"] is False
  532. assert "not found" in result["results"][0]["error"].lower()
  533. @pytest.mark.asyncio
  534. @pytest.mark.integration
  535. async def test_batch_generate_thumbnails_with_real_stl(self, async_client: AsyncClient, db_session):
  536. """Verify batch thumbnail generation with a real STL file."""
  537. from backend.app.models.library import LibraryFile
  538. # Create a simple ASCII STL cube
  539. stl_content = """solid cube
  540. facet normal 0 0 -1
  541. outer loop
  542. vertex 0 0 0
  543. vertex 1 0 0
  544. vertex 1 1 0
  545. endloop
  546. endfacet
  547. facet normal 0 0 1
  548. outer loop
  549. vertex 0 0 1
  550. vertex 1 1 1
  551. vertex 1 0 1
  552. endloop
  553. endfacet
  554. endsolid cube"""
  555. with tempfile.NamedTemporaryFile(suffix=".stl", delete=False, mode="w") as f:
  556. f.write(stl_content)
  557. stl_path = f.name
  558. try:
  559. # Create file in DB pointing to real STL
  560. lib_file = LibraryFile(
  561. filename="test_cube.stl",
  562. file_path=stl_path,
  563. file_size=len(stl_content),
  564. file_type="stl",
  565. thumbnail_path=None,
  566. )
  567. db_session.add(lib_file)
  568. await db_session.commit()
  569. await db_session.refresh(lib_file)
  570. data = {"file_ids": [lib_file.id]}
  571. response = await async_client.post("/api/v1/library/generate-stl-thumbnails", json=data)
  572. assert response.status_code == 200
  573. result = response.json()
  574. assert result["processed"] == 1
  575. # Result depends on whether trimesh/matplotlib are installed
  576. # Either succeeds or fails gracefully
  577. assert result["succeeded"] + result["failed"] == 1
  578. finally:
  579. import os
  580. if os.path.exists(stl_path):
  581. os.unlink(stl_path)
  582. @pytest.mark.asyncio
  583. @pytest.mark.integration
  584. async def test_upload_file_with_stl_thumbnail_param(self, async_client: AsyncClient, db_session):
  585. """Verify file upload accepts generate_stl_thumbnails parameter."""
  586. # Create a simple STL file
  587. stl_content = b"solid test\nendsolid test"
  588. files = {"file": ("test.stl", stl_content, "application/octet-stream")}
  589. params = {"generate_stl_thumbnails": "false"}
  590. response = await async_client.post("/api/v1/library/files", files=files, params=params)
  591. assert response.status_code == 200
  592. result = response.json()
  593. assert result["filename"] == "test.stl"
  594. assert result["file_type"] == "stl"
  595. # No thumbnail should be generated when disabled
  596. assert result["thumbnail_path"] is None
  597. @pytest.mark.asyncio
  598. @pytest.mark.integration
  599. async def test_extract_zip_with_stl_thumbnail_param(self, async_client: AsyncClient, db_session):
  600. """Verify ZIP extraction accepts generate_stl_thumbnails parameter."""
  601. # Create a ZIP file containing an STL
  602. stl_content = b"solid test\nendsolid test"
  603. zip_buffer = io.BytesIO()
  604. with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zf:
  605. zf.writestr("model.stl", stl_content)
  606. zip_buffer.seek(0)
  607. files = {"file": ("test.zip", zip_buffer.read(), "application/zip")}
  608. params = {"generate_stl_thumbnails": "false"}
  609. response = await async_client.post("/api/v1/library/files/extract-zip", files=files, params=params)
  610. assert response.status_code == 200
  611. result = response.json()
  612. assert result["extracted"] == 1
  613. assert result["files"][0]["filename"] == "model.stl"
  614. @pytest.mark.asyncio
  615. @pytest.mark.integration
  616. async def test_batch_generate_thumbnails_by_folder(self, async_client: AsyncClient, file_factory, db_session):
  617. """Verify batch thumbnail generation can filter by folder."""
  618. from backend.app.models.library import LibraryFolder
  619. # Create a folder
  620. folder = LibraryFolder(name="STL Folder")
  621. db_session.add(folder)
  622. await db_session.commit()
  623. await db_session.refresh(folder)
  624. # Create STL file in folder (no thumbnail)
  625. stl_in_folder = await file_factory(
  626. filename="in_folder.stl",
  627. folder_id=folder.id,
  628. thumbnail_path=None,
  629. )
  630. # Create STL file at root (no thumbnail)
  631. _stl_at_root = await file_factory(
  632. filename="at_root.stl",
  633. folder_id=None,
  634. thumbnail_path=None,
  635. )
  636. # Request thumbnails only for files in folder
  637. data = {"folder_id": folder.id, "all_missing": True}
  638. response = await async_client.post("/api/v1/library/generate-stl-thumbnails", json=data)
  639. assert response.status_code == 200
  640. result = response.json()
  641. # Should only process the file in the folder
  642. assert result["processed"] == 1
  643. assert result["results"][0]["file_id"] == stl_in_folder.id
  644. @pytest.mark.asyncio
  645. @pytest.mark.integration
  646. async def test_batch_generate_thumbnails_all_missing(self, async_client: AsyncClient, file_factory, db_session):
  647. """Verify batch thumbnail generation finds all STL files missing thumbnails."""
  648. # Create files with and without thumbnails
  649. _stl_with_thumb = await file_factory(
  650. filename="with_thumb.stl",
  651. thumbnail_path="/some/path/thumb.png",
  652. )
  653. stl_without_thumb1 = await file_factory(
  654. filename="without_thumb1.stl",
  655. thumbnail_path=None,
  656. )
  657. stl_without_thumb2 = await file_factory(
  658. filename="without_thumb2.stl",
  659. thumbnail_path=None,
  660. )
  661. data = {"all_missing": True}
  662. response = await async_client.post("/api/v1/library/generate-stl-thumbnails", json=data)
  663. assert response.status_code == 200
  664. result = response.json()
  665. # Should only process files without thumbnails
  666. assert result["processed"] == 2
  667. file_ids = {r["file_id"] for r in result["results"]}
  668. assert stl_without_thumb1.id in file_ids
  669. assert stl_without_thumb2.id in file_ids
  670. class TestLibraryPathHelpers:
  671. """Tests for path handling utilities used for backup portability."""
  672. def test_to_relative_path_converts_absolute(self):
  673. """Verify absolute paths are converted to relative paths."""
  674. from backend.app.api.routes.library import to_relative_path
  675. from backend.app.core.config import settings
  676. base_dir = str(settings.base_dir)
  677. abs_path = f"{base_dir}/archive/library/files/test.3mf"
  678. rel_path = to_relative_path(abs_path)
  679. assert not rel_path.startswith("/")
  680. assert rel_path == "archive/library/files/test.3mf"
  681. def test_to_relative_path_handles_path_object(self):
  682. """Verify Path objects are handled correctly."""
  683. from pathlib import Path
  684. from backend.app.api.routes.library import to_relative_path
  685. from backend.app.core.config import settings
  686. abs_path = Path(settings.base_dir) / "archive" / "test.3mf"
  687. rel_path = to_relative_path(abs_path)
  688. assert not rel_path.startswith("/")
  689. assert rel_path == "archive/test.3mf"
  690. def test_to_relative_path_returns_empty_for_empty_input(self):
  691. """Verify empty input returns empty string."""
  692. from backend.app.api.routes.library import to_relative_path
  693. assert to_relative_path("") == ""
  694. assert to_relative_path(None) == ""
  695. def test_to_absolute_path_converts_relative(self):
  696. """Verify relative paths are converted to absolute paths."""
  697. from backend.app.api.routes.library import to_absolute_path
  698. from backend.app.core.config import settings
  699. rel_path = "archive/library/files/test.3mf"
  700. abs_path = to_absolute_path(rel_path)
  701. assert abs_path is not None
  702. assert abs_path.is_absolute()
  703. assert str(abs_path) == f"{settings.base_dir}/archive/library/files/test.3mf"
  704. def test_to_absolute_path_handles_already_absolute(self):
  705. """Verify already absolute paths are returned as-is (for backwards compatibility)."""
  706. from backend.app.api.routes.library import to_absolute_path
  707. abs_path_str = "/data/archive/test.3mf"
  708. result = to_absolute_path(abs_path_str)
  709. assert result is not None
  710. assert str(result) == abs_path_str
  711. def test_to_absolute_path_returns_none_for_empty(self):
  712. """Verify None/empty input returns None."""
  713. from backend.app.api.routes.library import to_absolute_path
  714. assert to_absolute_path(None) is None
  715. assert to_absolute_path("") is None