test_library_api.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  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. class TestLibraryAddToQueueAPI:
  215. """Integration tests for /api/v1/library/files/add-to-queue endpoint."""
  216. @pytest.fixture
  217. async def printer_factory(self, db_session):
  218. """Factory to create test printers."""
  219. _counter = [0]
  220. async def _create_printer(**kwargs):
  221. from backend.app.models.printer import Printer
  222. _counter[0] += 1
  223. counter = _counter[0]
  224. defaults = {
  225. "name": f"Test Printer {counter}",
  226. "ip_address": f"192.168.1.{100 + counter}",
  227. "serial_number": f"TESTSERIAL{counter:04d}",
  228. "access_code": "12345678",
  229. "model": "X1C",
  230. }
  231. defaults.update(kwargs)
  232. printer = Printer(**defaults)
  233. db_session.add(printer)
  234. await db_session.commit()
  235. await db_session.refresh(printer)
  236. return printer
  237. return _create_printer
  238. @pytest.fixture
  239. async def library_file_factory(self, db_session):
  240. """Factory to create test library files."""
  241. _counter = [0]
  242. async def _create_library_file(**kwargs):
  243. from backend.app.models.library import LibraryFile
  244. _counter[0] += 1
  245. counter = _counter[0]
  246. defaults = {
  247. "filename": f"test_file_{counter}.gcode.3mf",
  248. "file_path": f"/test/path/test_file_{counter}.gcode.3mf",
  249. "file_size": 1024,
  250. "file_type": "3mf",
  251. }
  252. defaults.update(kwargs)
  253. lib_file = LibraryFile(**defaults)
  254. db_session.add(lib_file)
  255. await db_session.commit()
  256. await db_session.refresh(lib_file)
  257. return lib_file
  258. return _create_library_file
  259. @pytest.mark.asyncio
  260. @pytest.mark.integration
  261. async def test_add_to_queue_file_not_found(self, async_client: AsyncClient, printer_factory, db_session):
  262. """Verify error for non-existent file."""
  263. await printer_factory()
  264. data = {"file_ids": [9999]}
  265. response = await async_client.post("/api/v1/library/files/add-to-queue", json=data)
  266. assert response.status_code == 200
  267. result = response.json()
  268. assert len(result["added"]) == 0
  269. assert len(result["errors"]) == 1
  270. assert result["errors"][0]["file_id"] == 9999
  271. @pytest.mark.asyncio
  272. @pytest.mark.integration
  273. async def test_add_non_sliced_file_to_queue_fails(
  274. self, async_client: AsyncClient, printer_factory, library_file_factory, db_session
  275. ):
  276. """Verify non-sliced file cannot be added to queue."""
  277. await printer_factory()
  278. lib_file = await library_file_factory(
  279. filename="model.stl",
  280. file_path="/test/path/model.stl",
  281. file_type="stl",
  282. )
  283. data = {"file_ids": [lib_file.id]}
  284. response = await async_client.post("/api/v1/library/files/add-to-queue", json=data)
  285. assert response.status_code == 200
  286. result = response.json()
  287. assert len(result["added"]) == 0
  288. assert len(result["errors"]) == 1
  289. assert "sliced" in result["errors"][0]["error"].lower()
  290. class TestLibraryZipExtractAPI:
  291. """Integration tests for ZIP extraction endpoint."""
  292. @pytest.mark.asyncio
  293. @pytest.mark.integration
  294. async def test_extract_zip_invalid_file_type(self, async_client: AsyncClient, db_session):
  295. """Verify non-ZIP files are rejected."""
  296. # Create a fake file that's not a ZIP
  297. files = {"file": ("test.txt", b"This is not a zip file", "text/plain")}
  298. response = await async_client.post("/api/v1/library/files/extract-zip", files=files)
  299. assert response.status_code == 400
  300. assert "ZIP" in response.json()["detail"]
  301. @pytest.mark.asyncio
  302. @pytest.mark.integration
  303. async def test_extract_zip_basic(self, async_client: AsyncClient, db_session):
  304. """Verify basic ZIP extraction works."""
  305. import io
  306. import zipfile
  307. # Create a simple ZIP file in memory
  308. zip_buffer = io.BytesIO()
  309. with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zf:
  310. zf.writestr("test1.txt", "Content of file 1")
  311. zf.writestr("test2.txt", "Content of file 2")
  312. zip_buffer.seek(0)
  313. files = {"file": ("test.zip", zip_buffer.read(), "application/zip")}
  314. response = await async_client.post("/api/v1/library/files/extract-zip", files=files)
  315. assert response.status_code == 200
  316. result = response.json()
  317. assert result["extracted"] == 2
  318. assert len(result["files"]) == 2
  319. assert len(result["errors"]) == 0
  320. @pytest.mark.asyncio
  321. @pytest.mark.integration
  322. async def test_extract_zip_with_folders(self, async_client: AsyncClient, db_session):
  323. """Verify ZIP extraction preserves folder structure."""
  324. import io
  325. import zipfile
  326. # Create a ZIP file with folder structure
  327. zip_buffer = io.BytesIO()
  328. with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zf:
  329. zf.writestr("folder1/file1.txt", "Content 1")
  330. zf.writestr("folder1/subfolder/file2.txt", "Content 2")
  331. zf.writestr("folder2/file3.txt", "Content 3")
  332. zip_buffer.seek(0)
  333. files = {"file": ("test.zip", zip_buffer.read(), "application/zip")}
  334. params = {"preserve_structure": "true"}
  335. response = await async_client.post("/api/v1/library/files/extract-zip", files=files, params=params)
  336. assert response.status_code == 200
  337. result = response.json()
  338. assert result["extracted"] == 3
  339. assert result["folders_created"] >= 3 # folder1, folder1/subfolder, folder2
  340. @pytest.mark.asyncio
  341. @pytest.mark.integration
  342. async def test_extract_zip_flat(self, async_client: AsyncClient, db_session):
  343. """Verify ZIP extraction can extract flat (no folders)."""
  344. import io
  345. import zipfile
  346. # Create a ZIP file with folder structure
  347. zip_buffer = io.BytesIO()
  348. with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zf:
  349. zf.writestr("folder/file1.txt", "Content 1")
  350. zf.writestr("folder/file2.txt", "Content 2")
  351. zip_buffer.seek(0)
  352. files = {"file": ("test.zip", zip_buffer.read(), "application/zip")}
  353. params = {"preserve_structure": "false"}
  354. response = await async_client.post("/api/v1/library/files/extract-zip", files=files, params=params)
  355. assert response.status_code == 200
  356. result = response.json()
  357. assert result["extracted"] == 2
  358. assert result["folders_created"] == 0 # No folders created when flat
  359. @pytest.mark.asyncio
  360. @pytest.mark.integration
  361. async def test_extract_zip_skips_macos_files(self, async_client: AsyncClient, db_session):
  362. """Verify ZIP extraction skips __MACOSX and hidden files."""
  363. import io
  364. import zipfile
  365. # Create a ZIP file with macOS junk files
  366. zip_buffer = io.BytesIO()
  367. with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zf:
  368. zf.writestr("real_file.txt", "Real content")
  369. zf.writestr("__MACOSX/._real_file.txt", "macOS metadata")
  370. zf.writestr(".hidden_file", "Hidden content")
  371. zip_buffer.seek(0)
  372. files = {"file": ("test.zip", zip_buffer.read(), "application/zip")}
  373. response = await async_client.post("/api/v1/library/files/extract-zip", files=files)
  374. assert response.status_code == 200
  375. result = response.json()
  376. assert result["extracted"] == 1 # Only real_file.txt
  377. assert result["files"][0]["filename"] == "real_file.txt"
  378. @pytest.mark.asyncio
  379. @pytest.mark.integration
  380. async def test_extract_zip_create_folder_from_zip(self, async_client: AsyncClient, db_session):
  381. """Verify ZIP extraction creates a folder from the ZIP filename."""
  382. import io
  383. import zipfile
  384. # Create a ZIP file with some files
  385. zip_buffer = io.BytesIO()
  386. with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zf:
  387. zf.writestr("file1.txt", "Content 1")
  388. zf.writestr("file2.txt", "Content 2")
  389. zip_buffer.seek(0)
  390. files = {"file": ("MyProject.zip", zip_buffer.read(), "application/zip")}
  391. params = {"create_folder_from_zip": "true", "preserve_structure": "false"}
  392. response = await async_client.post("/api/v1/library/files/extract-zip", files=files, params=params)
  393. assert response.status_code == 200
  394. result = response.json()
  395. assert result["extracted"] == 2
  396. assert result["folders_created"] == 1 # MyProject folder created
  397. # Verify the files are in a folder
  398. assert result["files"][0]["folder_id"] is not None
  399. assert result["files"][1]["folder_id"] is not None
  400. # Both files should be in the same folder
  401. assert result["files"][0]["folder_id"] == result["files"][1]["folder_id"]
  402. # Verify the folder was created with the right name
  403. folder_response = await async_client.get(f"/api/v1/library/folders/{result['files'][0]['folder_id']}")
  404. assert folder_response.status_code == 200
  405. folder = folder_response.json()
  406. assert folder["name"] == "MyProject"
  407. class TestLibraryStlThumbnailAPI:
  408. """Integration tests for STL thumbnail generation endpoints."""
  409. @pytest.fixture
  410. async def file_factory(self, db_session):
  411. """Factory to create test files."""
  412. _counter = [0]
  413. async def _create_file(**kwargs):
  414. from backend.app.models.library import LibraryFile
  415. _counter[0] += 1
  416. counter = _counter[0]
  417. defaults = {
  418. "filename": f"test_model_{counter}.stl",
  419. "file_path": f"/test/path/test_model_{counter}.stl",
  420. "file_size": 1024,
  421. "file_type": "stl",
  422. }
  423. defaults.update(kwargs)
  424. lib_file = LibraryFile(**defaults)
  425. db_session.add(lib_file)
  426. await db_session.commit()
  427. await db_session.refresh(lib_file)
  428. return lib_file
  429. return _create_file
  430. @pytest.mark.asyncio
  431. @pytest.mark.integration
  432. async def test_batch_generate_thumbnails_empty(self, async_client: AsyncClient, db_session):
  433. """Verify batch thumbnail generation with no files."""
  434. data = {"all_missing": True}
  435. response = await async_client.post("/api/v1/library/generate-stl-thumbnails", json=data)
  436. assert response.status_code == 200
  437. result = response.json()
  438. assert result["processed"] == 0
  439. assert result["succeeded"] == 0
  440. assert result["failed"] == 0
  441. assert result["results"] == []
  442. @pytest.mark.asyncio
  443. @pytest.mark.integration
  444. async def test_batch_generate_thumbnails_no_criteria(self, async_client: AsyncClient, db_session):
  445. """Verify batch thumbnail generation with no criteria returns empty."""
  446. data = {}
  447. response = await async_client.post("/api/v1/library/generate-stl-thumbnails", json=data)
  448. assert response.status_code == 200
  449. result = response.json()
  450. assert result["processed"] == 0
  451. @pytest.mark.asyncio
  452. @pytest.mark.integration
  453. async def test_batch_generate_thumbnails_file_not_on_disk(
  454. self, async_client: AsyncClient, file_factory, db_session
  455. ):
  456. """Verify batch thumbnail generation handles missing files gracefully."""
  457. # Create a file in DB but not on disk
  458. stl_file = await file_factory(
  459. filename="missing.stl",
  460. file_path="/nonexistent/path/missing.stl",
  461. thumbnail_path=None,
  462. )
  463. data = {"file_ids": [stl_file.id]}
  464. response = await async_client.post("/api/v1/library/generate-stl-thumbnails", json=data)
  465. assert response.status_code == 200
  466. result = response.json()
  467. assert result["processed"] == 1
  468. assert result["succeeded"] == 0
  469. assert result["failed"] == 1
  470. assert result["results"][0]["success"] is False
  471. assert "not found" in result["results"][0]["error"].lower()
  472. @pytest.mark.asyncio
  473. @pytest.mark.integration
  474. async def test_batch_generate_thumbnails_with_real_stl(self, async_client: AsyncClient, db_session):
  475. """Verify batch thumbnail generation with a real STL file."""
  476. from backend.app.models.library import LibraryFile
  477. # Create a simple ASCII STL cube
  478. stl_content = """solid cube
  479. facet normal 0 0 -1
  480. outer loop
  481. vertex 0 0 0
  482. vertex 1 0 0
  483. vertex 1 1 0
  484. endloop
  485. endfacet
  486. facet normal 0 0 1
  487. outer loop
  488. vertex 0 0 1
  489. vertex 1 1 1
  490. vertex 1 0 1
  491. endloop
  492. endfacet
  493. endsolid cube"""
  494. with tempfile.NamedTemporaryFile(suffix=".stl", delete=False, mode="w") as f:
  495. f.write(stl_content)
  496. stl_path = f.name
  497. try:
  498. # Create file in DB pointing to real STL
  499. lib_file = LibraryFile(
  500. filename="test_cube.stl",
  501. file_path=stl_path,
  502. file_size=len(stl_content),
  503. file_type="stl",
  504. thumbnail_path=None,
  505. )
  506. db_session.add(lib_file)
  507. await db_session.commit()
  508. await db_session.refresh(lib_file)
  509. data = {"file_ids": [lib_file.id]}
  510. response = await async_client.post("/api/v1/library/generate-stl-thumbnails", json=data)
  511. assert response.status_code == 200
  512. result = response.json()
  513. assert result["processed"] == 1
  514. # Result depends on whether trimesh/matplotlib are installed
  515. # Either succeeds or fails gracefully
  516. assert result["succeeded"] + result["failed"] == 1
  517. finally:
  518. import os
  519. if os.path.exists(stl_path):
  520. os.unlink(stl_path)
  521. @pytest.mark.asyncio
  522. @pytest.mark.integration
  523. async def test_upload_file_with_stl_thumbnail_param(self, async_client: AsyncClient, db_session):
  524. """Verify file upload accepts generate_stl_thumbnails parameter."""
  525. # Create a simple STL file
  526. stl_content = b"solid test\nendsolid test"
  527. files = {"file": ("test.stl", stl_content, "application/octet-stream")}
  528. params = {"generate_stl_thumbnails": "false"}
  529. response = await async_client.post("/api/v1/library/files", files=files, params=params)
  530. assert response.status_code == 200
  531. result = response.json()
  532. assert result["filename"] == "test.stl"
  533. assert result["file_type"] == "stl"
  534. # No thumbnail should be generated when disabled
  535. assert result["thumbnail_path"] is None
  536. @pytest.mark.asyncio
  537. @pytest.mark.integration
  538. async def test_extract_zip_with_stl_thumbnail_param(self, async_client: AsyncClient, db_session):
  539. """Verify ZIP extraction accepts generate_stl_thumbnails parameter."""
  540. # Create a ZIP file containing an STL
  541. stl_content = b"solid test\nendsolid test"
  542. zip_buffer = io.BytesIO()
  543. with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zf:
  544. zf.writestr("model.stl", stl_content)
  545. zip_buffer.seek(0)
  546. files = {"file": ("test.zip", zip_buffer.read(), "application/zip")}
  547. params = {"generate_stl_thumbnails": "false"}
  548. response = await async_client.post("/api/v1/library/files/extract-zip", files=files, params=params)
  549. assert response.status_code == 200
  550. result = response.json()
  551. assert result["extracted"] == 1
  552. assert result["files"][0]["filename"] == "model.stl"
  553. @pytest.mark.asyncio
  554. @pytest.mark.integration
  555. async def test_batch_generate_thumbnails_by_folder(self, async_client: AsyncClient, file_factory, db_session):
  556. """Verify batch thumbnail generation can filter by folder."""
  557. from backend.app.models.library import LibraryFolder
  558. # Create a folder
  559. folder = LibraryFolder(name="STL Folder")
  560. db_session.add(folder)
  561. await db_session.commit()
  562. await db_session.refresh(folder)
  563. # Create STL file in folder (no thumbnail)
  564. stl_in_folder = await file_factory(
  565. filename="in_folder.stl",
  566. folder_id=folder.id,
  567. thumbnail_path=None,
  568. )
  569. # Create STL file at root (no thumbnail)
  570. _stl_at_root = await file_factory(
  571. filename="at_root.stl",
  572. folder_id=None,
  573. thumbnail_path=None,
  574. )
  575. # Request thumbnails only for files in folder
  576. data = {"folder_id": folder.id, "all_missing": True}
  577. response = await async_client.post("/api/v1/library/generate-stl-thumbnails", json=data)
  578. assert response.status_code == 200
  579. result = response.json()
  580. # Should only process the file in the folder
  581. assert result["processed"] == 1
  582. assert result["results"][0]["file_id"] == stl_in_folder.id
  583. @pytest.mark.asyncio
  584. @pytest.mark.integration
  585. async def test_batch_generate_thumbnails_all_missing(self, async_client: AsyncClient, file_factory, db_session):
  586. """Verify batch thumbnail generation finds all STL files missing thumbnails."""
  587. # Create files with and without thumbnails
  588. _stl_with_thumb = await file_factory(
  589. filename="with_thumb.stl",
  590. thumbnail_path="/some/path/thumb.png",
  591. )
  592. stl_without_thumb1 = await file_factory(
  593. filename="without_thumb1.stl",
  594. thumbnail_path=None,
  595. )
  596. stl_without_thumb2 = await file_factory(
  597. filename="without_thumb2.stl",
  598. thumbnail_path=None,
  599. )
  600. data = {"all_missing": True}
  601. response = await async_client.post("/api/v1/library/generate-stl-thumbnails", json=data)
  602. assert response.status_code == 200
  603. result = response.json()
  604. # Should only process files without thumbnails
  605. assert result["processed"] == 2
  606. file_ids = {r["file_id"] for r in result["results"]}
  607. assert stl_without_thumb1.id in file_ids
  608. assert stl_without_thumb2.id in file_ids
  609. class TestLibraryPathHelpers:
  610. """Tests for path handling utilities used for backup portability."""
  611. def test_to_relative_path_converts_absolute(self):
  612. """Verify absolute paths are converted to relative paths."""
  613. from backend.app.api.routes.library import to_relative_path
  614. from backend.app.core.config import settings
  615. base_dir = str(settings.base_dir)
  616. abs_path = f"{base_dir}/archive/library/files/test.3mf"
  617. rel_path = to_relative_path(abs_path)
  618. assert not rel_path.startswith("/")
  619. assert rel_path == "archive/library/files/test.3mf"
  620. def test_to_relative_path_handles_path_object(self):
  621. """Verify Path objects are handled correctly."""
  622. from pathlib import Path
  623. from backend.app.api.routes.library import to_relative_path
  624. from backend.app.core.config import settings
  625. abs_path = Path(settings.base_dir) / "archive" / "test.3mf"
  626. rel_path = to_relative_path(abs_path)
  627. assert not rel_path.startswith("/")
  628. assert rel_path == "archive/test.3mf"
  629. def test_to_relative_path_returns_empty_for_empty_input(self):
  630. """Verify empty input returns empty string."""
  631. from backend.app.api.routes.library import to_relative_path
  632. assert to_relative_path("") == ""
  633. assert to_relative_path(None) == ""
  634. def test_to_absolute_path_converts_relative(self):
  635. """Verify relative paths are converted to absolute paths."""
  636. from backend.app.api.routes.library import to_absolute_path
  637. from backend.app.core.config import settings
  638. rel_path = "archive/library/files/test.3mf"
  639. abs_path = to_absolute_path(rel_path)
  640. assert abs_path is not None
  641. assert abs_path.is_absolute()
  642. assert str(abs_path) == f"{settings.base_dir}/archive/library/files/test.3mf"
  643. def test_to_absolute_path_handles_already_absolute(self):
  644. """Verify already absolute paths are returned as-is (for backwards compatibility)."""
  645. from backend.app.api.routes.library import to_absolute_path
  646. abs_path_str = "/data/archive/test.3mf"
  647. result = to_absolute_path(abs_path_str)
  648. assert result is not None
  649. assert str(result) == abs_path_str
  650. def test_to_absolute_path_returns_none_for_empty(self):
  651. """Verify None/empty input returns None."""
  652. from backend.app.api.routes.library import to_absolute_path
  653. assert to_absolute_path(None) is None
  654. assert to_absolute_path("") is None