test_library_api.py 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226
  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_list_files_by_project_id(self, async_client: AsyncClient, folder_factory, file_factory, db_session):
  152. """#932: project_id filter returns files across all folders linked to the project.
  153. Replaces the prior N+1 pattern where the frontend fired one request per
  154. linked folder. A single JOIN query must return every file in folders whose
  155. project_id matches, while excluding files from unlinked folders.
  156. """
  157. from backend.app.models.project import Project
  158. project = Project(name="Test Project for Files", color="#00ff00")
  159. db_session.add(project)
  160. await db_session.commit()
  161. await db_session.refresh(project)
  162. folder_a = await folder_factory(name="Folder A", project_id=project.id)
  163. folder_b = await folder_factory(name="Folder B", project_id=project.id)
  164. other_folder = await folder_factory(name="Unlinked")
  165. linked_a = await file_factory(folder_id=folder_a.id, filename="a.3mf")
  166. linked_b = await file_factory(folder_id=folder_b.id, filename="b.3mf")
  167. await file_factory(folder_id=other_folder.id, filename="unlinked.3mf")
  168. await file_factory(filename="root.3mf") # no folder → not part of any project
  169. response = await async_client.get(f"/api/v1/library/files?project_id={project.id}")
  170. assert response.status_code == 200
  171. result = response.json()
  172. ids = {f["id"] for f in result}
  173. assert ids == {linked_a.id, linked_b.id}
  174. @pytest.mark.asyncio
  175. @pytest.mark.integration
  176. async def test_list_files_folder_id_takes_precedence_over_project_id(
  177. self, async_client: AsyncClient, folder_factory, file_factory, db_session
  178. ):
  179. """When both folder_id and project_id are passed, folder_id wins.
  180. Documented precedence in list_files(): folder_id > project_id > include_root.
  181. This guards the behavior so a future refactor can't silently flip it.
  182. """
  183. from backend.app.models.project import Project
  184. project = Project(name="Precedence Project")
  185. db_session.add(project)
  186. await db_session.commit()
  187. await db_session.refresh(project)
  188. folder_linked = await folder_factory(name="Linked", project_id=project.id)
  189. folder_other = await folder_factory(name="Other")
  190. await file_factory(folder_id=folder_linked.id, filename="linked.3mf")
  191. other_file = await file_factory(folder_id=folder_other.id, filename="other.3mf")
  192. # folder_id points at a folder that is NOT in the project — must return
  193. # that folder's contents and ignore project_id entirely.
  194. response = await async_client.get(f"/api/v1/library/files?folder_id={folder_other.id}&project_id={project.id}")
  195. assert response.status_code == 200
  196. result = response.json()
  197. assert len(result) == 1
  198. assert result[0]["id"] == other_file.id
  199. @pytest.mark.asyncio
  200. @pytest.mark.integration
  201. async def test_get_file(self, async_client: AsyncClient, file_factory, db_session):
  202. """Verify single file can be retrieved."""
  203. lib_file = await file_factory(filename="test.3mf")
  204. response = await async_client.get(f"/api/v1/library/files/{lib_file.id}")
  205. assert response.status_code == 200
  206. result = response.json()
  207. assert result["id"] == lib_file.id
  208. assert result["filename"] == "test.3mf"
  209. @pytest.mark.asyncio
  210. @pytest.mark.integration
  211. async def test_get_file_not_found(self, async_client: AsyncClient, db_session):
  212. """Verify 404 for non-existent file."""
  213. response = await async_client.get("/api/v1/library/files/9999")
  214. assert response.status_code == 404
  215. @pytest.mark.asyncio
  216. @pytest.mark.integration
  217. async def test_delete_file(self, async_client: AsyncClient, file_factory, db_session):
  218. """Verify file can be deleted."""
  219. lib_file = await file_factory()
  220. response = await async_client.delete(f"/api/v1/library/files/{lib_file.id}")
  221. assert response.status_code == 200
  222. result = response.json()
  223. assert result.get("message") or result.get("success", True)
  224. @pytest.mark.asyncio
  225. @pytest.mark.integration
  226. async def test_rename_file(self, async_client: AsyncClient, file_factory, db_session):
  227. """Verify file can be renamed."""
  228. lib_file = await file_factory(filename="old_name.3mf")
  229. data = {"filename": "new_name.3mf"}
  230. response = await async_client.put(f"/api/v1/library/files/{lib_file.id}", json=data)
  231. assert response.status_code == 200
  232. result = response.json()
  233. assert result["filename"] == "new_name.3mf"
  234. @pytest.mark.asyncio
  235. @pytest.mark.integration
  236. async def test_rename_file_invalid_path_separator(self, async_client: AsyncClient, file_factory, db_session):
  237. """Verify file rename fails with a forward slash (FAT32-illegal, #1540)."""
  238. lib_file = await file_factory(filename="test.3mf")
  239. data = {"filename": "path/to/file.3mf"}
  240. response = await async_client.put(f"/api/v1/library/files/{lib_file.id}", json=data)
  241. assert response.status_code == 400
  242. assert "invalid character" in response.json()["detail"].lower()
  243. assert "/" in response.json()["detail"]
  244. @pytest.mark.asyncio
  245. @pytest.mark.integration
  246. async def test_rename_file_invalid_backslash(self, async_client: AsyncClient, file_factory, db_session):
  247. """Verify file rename fails with a backslash (FAT32-illegal, #1540)."""
  248. lib_file = await file_factory(filename="test.3mf")
  249. data = {"filename": "path\\to\\file.3mf"}
  250. response = await async_client.put(f"/api/v1/library/files/{lib_file.id}", json=data)
  251. assert response.status_code == 400
  252. assert "invalid character" in response.json()["detail"].lower()
  253. assert "\\" in response.json()["detail"]
  254. @pytest.mark.asyncio
  255. @pytest.mark.integration
  256. async def test_library_stats(self, async_client: AsyncClient, folder_factory, file_factory, db_session):
  257. """Verify library stats endpoint returns counts."""
  258. await folder_factory()
  259. await folder_factory()
  260. await file_factory()
  261. response = await async_client.get("/api/v1/library/stats")
  262. assert response.status_code == 200
  263. result = response.json()
  264. assert result["total_folders"] == 2
  265. assert result["total_files"] == 1
  266. @pytest.mark.asyncio
  267. @pytest.mark.integration
  268. async def test_file_list_includes_user_tracking_fields(self, async_client: AsyncClient, file_factory, db_session):
  269. """Verify file list response includes user tracking fields (Issue #206)."""
  270. lib_file = await file_factory(filename="test.3mf")
  271. response = await async_client.get("/api/v1/library/files?include_root=false")
  272. assert response.status_code == 200
  273. result = response.json()
  274. assert len(result) >= 1
  275. # Find our test file
  276. test_file = next((f for f in result if f["id"] == lib_file.id), None)
  277. assert test_file is not None
  278. # User tracking fields should be present (even if null)
  279. assert "created_by_id" in test_file
  280. assert "created_by_username" in test_file
  281. @pytest.mark.asyncio
  282. @pytest.mark.integration
  283. async def test_file_detail_includes_user_tracking_fields(self, async_client: AsyncClient, file_factory, db_session):
  284. """Verify file detail response includes user tracking fields (Issue #206)."""
  285. lib_file = await file_factory(filename="test_detail.3mf")
  286. response = await async_client.get(f"/api/v1/library/files/{lib_file.id}")
  287. assert response.status_code == 200
  288. result = response.json()
  289. # User tracking fields should be present (even if null)
  290. assert "created_by_id" in result
  291. assert "created_by_username" in result
  292. @pytest.mark.asyncio
  293. @pytest.mark.integration
  294. async def test_file_with_user_tracking(self, async_client: AsyncClient, db_session):
  295. """Verify file created with user shows username in response (Issue #206)."""
  296. from backend.app.models.library import LibraryFile
  297. from backend.app.models.user import User
  298. # Create a test user
  299. user = User(username="testuploader", password_hash="fakehash", role="user")
  300. db_session.add(user)
  301. await db_session.flush()
  302. # Create a file with created_by_id set
  303. lib_file = LibraryFile(
  304. filename="user_uploaded.3mf",
  305. file_path="/test/user_uploaded.3mf",
  306. file_size=2048,
  307. file_type="3mf",
  308. created_by_id=user.id,
  309. )
  310. db_session.add(lib_file)
  311. await db_session.commit()
  312. await db_session.refresh(lib_file)
  313. # Verify file detail shows username
  314. response = await async_client.get(f"/api/v1/library/files/{lib_file.id}")
  315. assert response.status_code == 200
  316. result = response.json()
  317. assert result["created_by_id"] == user.id
  318. assert result["created_by_username"] == "testuploader"
  319. # Verify file list also shows username
  320. response = await async_client.get("/api/v1/library/files?include_root=false")
  321. assert response.status_code == 200
  322. files = response.json()
  323. test_file = next((f for f in files if f["id"] == lib_file.id), None)
  324. assert test_file is not None
  325. assert test_file["created_by_id"] == user.id
  326. assert test_file["created_by_username"] == "testuploader"
  327. class TestLibraryAddToQueueAPI:
  328. """Integration tests for /api/v1/library/files/add-to-queue endpoint."""
  329. @pytest.fixture
  330. async def printer_factory(self, db_session):
  331. """Factory to create test printers."""
  332. _counter = [0]
  333. async def _create_printer(**kwargs):
  334. from backend.app.models.printer import Printer
  335. _counter[0] += 1
  336. counter = _counter[0]
  337. defaults = {
  338. "name": f"Test Printer {counter}",
  339. "ip_address": f"192.168.1.{100 + counter}",
  340. "serial_number": f"TESTSERIAL{counter:04d}",
  341. "access_code": "12345678",
  342. "model": "X1C",
  343. }
  344. defaults.update(kwargs)
  345. printer = Printer(**defaults)
  346. db_session.add(printer)
  347. await db_session.commit()
  348. await db_session.refresh(printer)
  349. return printer
  350. return _create_printer
  351. @pytest.fixture
  352. async def library_file_factory(self, db_session):
  353. """Factory to create test library files."""
  354. _counter = [0]
  355. async def _create_library_file(**kwargs):
  356. from backend.app.models.library import LibraryFile
  357. _counter[0] += 1
  358. counter = _counter[0]
  359. defaults = {
  360. "filename": f"test_file_{counter}.gcode.3mf",
  361. "file_path": f"/test/path/test_file_{counter}.gcode.3mf",
  362. "file_size": 1024,
  363. "file_type": "3mf",
  364. }
  365. defaults.update(kwargs)
  366. lib_file = LibraryFile(**defaults)
  367. db_session.add(lib_file)
  368. await db_session.commit()
  369. await db_session.refresh(lib_file)
  370. return lib_file
  371. return _create_library_file
  372. @pytest.mark.asyncio
  373. @pytest.mark.integration
  374. async def test_add_to_queue_file_not_found(self, async_client: AsyncClient, printer_factory, db_session):
  375. """Verify error for non-existent file."""
  376. await printer_factory()
  377. data = {"file_ids": [9999]}
  378. response = await async_client.post("/api/v1/library/files/add-to-queue", json=data)
  379. assert response.status_code == 200
  380. result = response.json()
  381. assert len(result["added"]) == 0
  382. assert len(result["errors"]) == 1
  383. assert result["errors"][0]["file_id"] == 9999
  384. @pytest.mark.asyncio
  385. @pytest.mark.integration
  386. async def test_add_non_sliced_file_to_queue_fails(
  387. self, async_client: AsyncClient, printer_factory, library_file_factory, db_session
  388. ):
  389. """Verify non-sliced file cannot be added to queue."""
  390. await printer_factory()
  391. lib_file = await library_file_factory(
  392. filename="model.stl",
  393. file_path="/test/path/model.stl",
  394. file_type="stl",
  395. )
  396. data = {"file_ids": [lib_file.id]}
  397. response = await async_client.post("/api/v1/library/files/add-to-queue", json=data)
  398. assert response.status_code == 200
  399. result = response.json()
  400. assert len(result["added"]) == 0
  401. assert len(result["errors"]) == 1
  402. assert "sliced" in result["errors"][0]["error"].lower()
  403. class TestLibraryZipExtractAPI:
  404. """Integration tests for ZIP extraction endpoint."""
  405. @pytest.mark.asyncio
  406. @pytest.mark.integration
  407. async def test_extract_zip_invalid_file_type(self, async_client: AsyncClient, db_session):
  408. """Verify non-ZIP files are rejected."""
  409. # Create a fake file that's not a ZIP
  410. files = {"file": ("test.txt", b"This is not a zip file", "text/plain")}
  411. response = await async_client.post("/api/v1/library/files/extract-zip", files=files)
  412. assert response.status_code == 400
  413. assert "ZIP" in response.json()["detail"]
  414. @pytest.mark.asyncio
  415. @pytest.mark.integration
  416. async def test_extract_zip_basic(self, async_client: AsyncClient, db_session):
  417. """Verify basic ZIP extraction works."""
  418. import io
  419. # Create a simple ZIP file in memory
  420. zip_buffer = io.BytesIO()
  421. with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zf:
  422. zf.writestr("test1.txt", "Content of file 1")
  423. zf.writestr("test2.txt", "Content of file 2")
  424. zip_buffer.seek(0)
  425. files = {"file": ("test.zip", zip_buffer.read(), "application/zip")}
  426. response = await async_client.post("/api/v1/library/files/extract-zip", files=files)
  427. assert response.status_code == 200
  428. result = response.json()
  429. assert result["extracted"] == 2
  430. assert len(result["files"]) == 2
  431. assert len(result["errors"]) == 0
  432. @pytest.mark.asyncio
  433. @pytest.mark.integration
  434. async def test_extract_zip_with_folders(self, async_client: AsyncClient, db_session):
  435. """Verify ZIP extraction preserves folder structure."""
  436. import io
  437. # Create a ZIP file with folder structure
  438. zip_buffer = io.BytesIO()
  439. with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zf:
  440. zf.writestr("folder1/file1.txt", "Content 1")
  441. zf.writestr("folder1/subfolder/file2.txt", "Content 2")
  442. zf.writestr("folder2/file3.txt", "Content 3")
  443. zip_buffer.seek(0)
  444. files = {"file": ("test.zip", zip_buffer.read(), "application/zip")}
  445. params = {"preserve_structure": "true"}
  446. response = await async_client.post("/api/v1/library/files/extract-zip", files=files, params=params)
  447. assert response.status_code == 200
  448. result = response.json()
  449. assert result["extracted"] == 3
  450. assert result["folders_created"] >= 3 # folder1, folder1/subfolder, folder2
  451. @pytest.mark.asyncio
  452. @pytest.mark.integration
  453. async def test_extract_zip_flat(self, async_client: AsyncClient, db_session):
  454. """Verify ZIP extraction can extract flat (no folders)."""
  455. import io
  456. # Create a ZIP file with folder structure
  457. zip_buffer = io.BytesIO()
  458. with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zf:
  459. zf.writestr("folder/file1.txt", "Content 1")
  460. zf.writestr("folder/file2.txt", "Content 2")
  461. zip_buffer.seek(0)
  462. files = {"file": ("test.zip", zip_buffer.read(), "application/zip")}
  463. params = {"preserve_structure": "false"}
  464. response = await async_client.post("/api/v1/library/files/extract-zip", files=files, params=params)
  465. assert response.status_code == 200
  466. result = response.json()
  467. assert result["extracted"] == 2
  468. assert result["folders_created"] == 0 # No folders created when flat
  469. @pytest.mark.asyncio
  470. @pytest.mark.integration
  471. async def test_extract_zip_skips_macos_files(self, async_client: AsyncClient, db_session):
  472. """Verify ZIP extraction skips __MACOSX and hidden files."""
  473. import io
  474. # Create a ZIP file with macOS junk files
  475. zip_buffer = io.BytesIO()
  476. with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zf:
  477. zf.writestr("real_file.txt", "Real content")
  478. zf.writestr("__MACOSX/._real_file.txt", "macOS metadata")
  479. zf.writestr(".hidden_file", "Hidden content")
  480. zip_buffer.seek(0)
  481. files = {"file": ("test.zip", zip_buffer.read(), "application/zip")}
  482. response = await async_client.post("/api/v1/library/files/extract-zip", files=files)
  483. assert response.status_code == 200
  484. result = response.json()
  485. assert result["extracted"] == 1 # Only real_file.txt
  486. assert result["files"][0]["filename"] == "real_file.txt"
  487. @pytest.mark.asyncio
  488. @pytest.mark.integration
  489. async def test_extract_zip_create_folder_from_zip(self, async_client: AsyncClient, db_session):
  490. """Verify ZIP extraction creates a folder from the ZIP filename."""
  491. import io
  492. # Create a ZIP file with some files
  493. zip_buffer = io.BytesIO()
  494. with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zf:
  495. zf.writestr("file1.txt", "Content 1")
  496. zf.writestr("file2.txt", "Content 2")
  497. zip_buffer.seek(0)
  498. files = {"file": ("MyProject.zip", zip_buffer.read(), "application/zip")}
  499. params = {"create_folder_from_zip": "true", "preserve_structure": "false"}
  500. response = await async_client.post("/api/v1/library/files/extract-zip", files=files, params=params)
  501. assert response.status_code == 200
  502. result = response.json()
  503. assert result["extracted"] == 2
  504. assert result["folders_created"] == 1 # MyProject folder created
  505. # Verify the files are in a folder
  506. assert result["files"][0]["folder_id"] is not None
  507. assert result["files"][1]["folder_id"] is not None
  508. # Both files should be in the same folder
  509. assert result["files"][0]["folder_id"] == result["files"][1]["folder_id"]
  510. # Verify the folder was created with the right name
  511. folder_response = await async_client.get(f"/api/v1/library/folders/{result['files'][0]['folder_id']}")
  512. assert folder_response.status_code == 200
  513. folder = folder_response.json()
  514. assert folder["name"] == "MyProject"
  515. class TestLibraryStlThumbnailAPI:
  516. """Integration tests for STL thumbnail generation endpoints."""
  517. @pytest.fixture
  518. async def file_factory(self, db_session):
  519. """Factory to create test files."""
  520. _counter = [0]
  521. async def _create_file(**kwargs):
  522. from backend.app.models.library import LibraryFile
  523. _counter[0] += 1
  524. counter = _counter[0]
  525. defaults = {
  526. "filename": f"test_model_{counter}.stl",
  527. "file_path": f"/test/path/test_model_{counter}.stl",
  528. "file_size": 1024,
  529. "file_type": "stl",
  530. }
  531. defaults.update(kwargs)
  532. lib_file = LibraryFile(**defaults)
  533. db_session.add(lib_file)
  534. await db_session.commit()
  535. await db_session.refresh(lib_file)
  536. return lib_file
  537. return _create_file
  538. @pytest.mark.asyncio
  539. @pytest.mark.integration
  540. async def test_batch_generate_thumbnails_empty(self, async_client: AsyncClient, db_session):
  541. """Verify batch thumbnail generation with no files."""
  542. data = {"all_missing": True}
  543. response = await async_client.post("/api/v1/library/generate-stl-thumbnails", json=data)
  544. assert response.status_code == 200
  545. result = response.json()
  546. assert result["processed"] == 0
  547. assert result["succeeded"] == 0
  548. assert result["failed"] == 0
  549. assert result["results"] == []
  550. @pytest.mark.asyncio
  551. @pytest.mark.integration
  552. async def test_batch_generate_thumbnails_no_criteria(self, async_client: AsyncClient, db_session):
  553. """Verify batch thumbnail generation with no criteria returns empty."""
  554. data = {}
  555. response = await async_client.post("/api/v1/library/generate-stl-thumbnails", json=data)
  556. assert response.status_code == 200
  557. result = response.json()
  558. assert result["processed"] == 0
  559. @pytest.mark.asyncio
  560. @pytest.mark.integration
  561. async def test_batch_generate_thumbnails_file_not_on_disk(
  562. self, async_client: AsyncClient, file_factory, db_session
  563. ):
  564. """Verify batch thumbnail generation handles missing files gracefully."""
  565. # Create a file in DB but not on disk
  566. stl_file = await file_factory(
  567. filename="missing.stl",
  568. file_path="/nonexistent/path/missing.stl",
  569. thumbnail_path=None,
  570. )
  571. data = {"file_ids": [stl_file.id]}
  572. response = await async_client.post("/api/v1/library/generate-stl-thumbnails", json=data)
  573. assert response.status_code == 200
  574. result = response.json()
  575. assert result["processed"] == 1
  576. assert result["succeeded"] == 0
  577. assert result["failed"] == 1
  578. assert result["results"][0]["success"] is False
  579. assert "not found" in result["results"][0]["error"].lower()
  580. @pytest.mark.asyncio
  581. @pytest.mark.integration
  582. async def test_batch_generate_thumbnails_with_real_stl(self, async_client: AsyncClient, db_session):
  583. """Verify batch thumbnail generation with a real STL file."""
  584. from backend.app.models.library import LibraryFile
  585. # Create a simple ASCII STL cube
  586. stl_content = """solid cube
  587. facet normal 0 0 -1
  588. outer loop
  589. vertex 0 0 0
  590. vertex 1 0 0
  591. vertex 1 1 0
  592. endloop
  593. endfacet
  594. facet normal 0 0 1
  595. outer loop
  596. vertex 0 0 1
  597. vertex 1 1 1
  598. vertex 1 0 1
  599. endloop
  600. endfacet
  601. endsolid cube"""
  602. with tempfile.NamedTemporaryFile(suffix=".stl", delete=False, mode="w") as f:
  603. f.write(stl_content)
  604. stl_path = f.name
  605. try:
  606. # Create file in DB pointing to real STL
  607. lib_file = LibraryFile(
  608. filename="test_cube.stl",
  609. file_path=stl_path,
  610. file_size=len(stl_content),
  611. file_type="stl",
  612. thumbnail_path=None,
  613. )
  614. db_session.add(lib_file)
  615. await db_session.commit()
  616. await db_session.refresh(lib_file)
  617. data = {"file_ids": [lib_file.id]}
  618. response = await async_client.post("/api/v1/library/generate-stl-thumbnails", json=data)
  619. assert response.status_code == 200
  620. result = response.json()
  621. assert result["processed"] == 1
  622. # Result depends on whether trimesh/matplotlib are installed
  623. # Either succeeds or fails gracefully
  624. assert result["succeeded"] + result["failed"] == 1
  625. finally:
  626. import os
  627. if os.path.exists(stl_path):
  628. os.unlink(stl_path)
  629. @pytest.mark.asyncio
  630. @pytest.mark.integration
  631. async def test_upload_file_with_stl_thumbnail_param(self, async_client: AsyncClient, db_session):
  632. """Verify file upload accepts generate_stl_thumbnails parameter."""
  633. # Create a simple STL file
  634. stl_content = b"solid test\nendsolid test"
  635. files = {"file": ("test.stl", stl_content, "application/octet-stream")}
  636. params = {"generate_stl_thumbnails": "false"}
  637. response = await async_client.post("/api/v1/library/files", files=files, params=params)
  638. assert response.status_code == 200
  639. result = response.json()
  640. assert result["filename"] == "test.stl"
  641. assert result["file_type"] == "stl"
  642. # No thumbnail should be generated when disabled
  643. assert result["thumbnail_path"] is None
  644. @pytest.mark.asyncio
  645. @pytest.mark.integration
  646. async def test_extract_zip_with_stl_thumbnail_param(self, async_client: AsyncClient, db_session):
  647. """Verify ZIP extraction accepts generate_stl_thumbnails parameter."""
  648. # Create a ZIP file containing an STL
  649. stl_content = b"solid test\nendsolid test"
  650. zip_buffer = io.BytesIO()
  651. with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zf:
  652. zf.writestr("model.stl", stl_content)
  653. zip_buffer.seek(0)
  654. files = {"file": ("test.zip", zip_buffer.read(), "application/zip")}
  655. params = {"generate_stl_thumbnails": "false"}
  656. response = await async_client.post("/api/v1/library/files/extract-zip", files=files, params=params)
  657. assert response.status_code == 200
  658. result = response.json()
  659. assert result["extracted"] == 1
  660. assert result["files"][0]["filename"] == "model.stl"
  661. @pytest.mark.asyncio
  662. @pytest.mark.integration
  663. async def test_batch_generate_thumbnails_by_folder(self, async_client: AsyncClient, file_factory, db_session):
  664. """Verify batch thumbnail generation can filter by folder."""
  665. from backend.app.models.library import LibraryFolder
  666. # Create a folder
  667. folder = LibraryFolder(name="STL Folder")
  668. db_session.add(folder)
  669. await db_session.commit()
  670. await db_session.refresh(folder)
  671. # Create STL file in folder (no thumbnail)
  672. stl_in_folder = await file_factory(
  673. filename="in_folder.stl",
  674. folder_id=folder.id,
  675. thumbnail_path=None,
  676. )
  677. # Create STL file at root (no thumbnail)
  678. _stl_at_root = await file_factory(
  679. filename="at_root.stl",
  680. folder_id=None,
  681. thumbnail_path=None,
  682. )
  683. # Request thumbnails only for files in folder
  684. data = {"folder_id": folder.id, "all_missing": True}
  685. response = await async_client.post("/api/v1/library/generate-stl-thumbnails", json=data)
  686. assert response.status_code == 200
  687. result = response.json()
  688. # Should only process the file in the folder
  689. assert result["processed"] == 1
  690. assert result["results"][0]["file_id"] == stl_in_folder.id
  691. @pytest.mark.asyncio
  692. @pytest.mark.integration
  693. async def test_batch_generate_thumbnails_all_missing(self, async_client: AsyncClient, file_factory, db_session):
  694. """Verify batch thumbnail generation finds all STL files missing thumbnails."""
  695. # Create files with and without thumbnails
  696. _stl_with_thumb = await file_factory(
  697. filename="with_thumb.stl",
  698. thumbnail_path="/some/path/thumb.png",
  699. )
  700. stl_without_thumb1 = await file_factory(
  701. filename="without_thumb1.stl",
  702. thumbnail_path=None,
  703. )
  704. stl_without_thumb2 = await file_factory(
  705. filename="without_thumb2.stl",
  706. thumbnail_path=None,
  707. )
  708. data = {"all_missing": True}
  709. response = await async_client.post("/api/v1/library/generate-stl-thumbnails", json=data)
  710. assert response.status_code == 200
  711. result = response.json()
  712. # Should only process files without thumbnails
  713. assert result["processed"] == 2
  714. file_ids = {r["file_id"] for r in result["results"]}
  715. assert stl_without_thumb1.id in file_ids
  716. assert stl_without_thumb2.id in file_ids
  717. class TestLibraryPathHelpers:
  718. """Tests for path handling utilities used for backup portability."""
  719. def test_to_relative_path_converts_absolute(self):
  720. """Verify absolute paths are converted to relative paths."""
  721. from backend.app.api.routes.library import to_relative_path
  722. from backend.app.core.config import settings
  723. base_dir = str(settings.base_dir)
  724. abs_path = f"{base_dir}/archive/library/files/test.3mf"
  725. rel_path = to_relative_path(abs_path)
  726. assert not rel_path.startswith("/")
  727. assert rel_path == "archive/library/files/test.3mf"
  728. def test_to_relative_path_handles_path_object(self):
  729. """Verify Path objects are handled correctly."""
  730. from pathlib import Path
  731. from backend.app.api.routes.library import to_relative_path
  732. from backend.app.core.config import settings
  733. abs_path = Path(settings.base_dir) / "archive" / "test.3mf"
  734. rel_path = to_relative_path(abs_path)
  735. assert not rel_path.startswith("/")
  736. assert rel_path == "archive/test.3mf"
  737. def test_to_relative_path_returns_empty_for_empty_input(self):
  738. """Verify empty input returns empty string."""
  739. from backend.app.api.routes.library import to_relative_path
  740. assert to_relative_path("") == ""
  741. assert to_relative_path(None) == ""
  742. def test_to_absolute_path_converts_relative(self):
  743. """Verify relative paths are converted to absolute paths."""
  744. from backend.app.api.routes.library import to_absolute_path
  745. from backend.app.core.config import settings
  746. rel_path = "archive/library/files/test.3mf"
  747. abs_path = to_absolute_path(rel_path)
  748. assert abs_path is not None
  749. assert abs_path.is_absolute()
  750. assert str(abs_path) == f"{settings.base_dir}/archive/library/files/test.3mf"
  751. def test_to_absolute_path_handles_already_absolute(self):
  752. """Verify already absolute paths are returned as-is (for backwards compatibility)."""
  753. from backend.app.api.routes.library import to_absolute_path
  754. abs_path_str = "/data/archive/test.3mf"
  755. result = to_absolute_path(abs_path_str)
  756. assert result is not None
  757. assert str(result) == abs_path_str
  758. def test_to_absolute_path_returns_none_for_empty(self):
  759. """Verify None/empty input returns None."""
  760. from backend.app.api.routes.library import to_absolute_path
  761. assert to_absolute_path(None) is None
  762. assert to_absolute_path("") is None
  763. class TestLibraryPermissions:
  764. """Tests for library permission enforcement."""
  765. @pytest.fixture
  766. async def auth_setup(self, db_session):
  767. """Set up auth with users of different permission levels."""
  768. from backend.app.core.auth import create_access_token, get_password_hash
  769. from backend.app.models.group import Group
  770. from backend.app.models.settings import Settings
  771. from backend.app.models.user import User
  772. # Enable auth
  773. settings = Settings(key="auth_enabled", value="true")
  774. db_session.add(settings)
  775. await db_session.commit()
  776. # Groups are auto-seeded during db init, but we need to commit them
  777. await db_session.commit()
  778. # Get groups
  779. from sqlalchemy import select
  780. admin_group = (await db_session.execute(select(Group).where(Group.name == "Administrators"))).scalar_one()
  781. operator_group = (await db_session.execute(select(Group).where(Group.name == "Operators"))).scalar_one()
  782. viewer_group = (await db_session.execute(select(Group).where(Group.name == "Viewers"))).scalar_one()
  783. password_hash = get_password_hash("password")
  784. # Create users
  785. admin_user = User(username="admin_lib", password_hash=password_hash, role="admin", is_active=True)
  786. admin_user.groups.append(admin_group)
  787. operator_user = User(username="operator_lib", password_hash=password_hash, is_active=True)
  788. operator_user.groups.append(operator_group)
  789. viewer_user = User(username="viewer_lib", password_hash=password_hash, is_active=True)
  790. viewer_user.groups.append(viewer_group)
  791. db_session.add_all([admin_user, operator_user, viewer_user])
  792. await db_session.commit()
  793. # Create tokens
  794. admin_token = create_access_token(data={"sub": admin_user.username})
  795. operator_token = create_access_token(data={"sub": operator_user.username})
  796. viewer_token = create_access_token(data={"sub": viewer_user.username})
  797. return {
  798. "admin_user": admin_user,
  799. "operator_user": operator_user,
  800. "viewer_user": viewer_user,
  801. "admin_token": admin_token,
  802. "operator_token": operator_token,
  803. "viewer_token": viewer_token,
  804. }
  805. @pytest.fixture
  806. async def test_file(self, db_session, auth_setup):
  807. """Create a test file owned by the operator user."""
  808. from backend.app.models.library import LibraryFile
  809. operator_user = auth_setup["operator_user"]
  810. lib_file = LibraryFile(
  811. filename="test.txt",
  812. file_path="data/archive/library/files/test.txt",
  813. file_type="txt",
  814. file_size=100,
  815. created_by_id=operator_user.id,
  816. )
  817. db_session.add(lib_file)
  818. await db_session.commit()
  819. await db_session.refresh(lib_file)
  820. return lib_file
  821. @pytest.mark.asyncio
  822. @pytest.mark.integration
  823. async def test_list_files_requires_library_read(self, async_client: AsyncClient, db_session, auth_setup):
  824. """Verify list_files requires library:read permission."""
  825. viewer_token = auth_setup["viewer_token"]
  826. # Viewers have library:read, should succeed
  827. response = await async_client.get("/api/v1/library/files", headers={"Authorization": f"Bearer {viewer_token}"})
  828. assert response.status_code == 200
  829. @pytest.mark.asyncio
  830. @pytest.mark.integration
  831. async def test_list_files_denied_without_permission(self, async_client: AsyncClient, db_session):
  832. """Verify list_files denied without auth when auth is enabled."""
  833. from backend.app.models.settings import Settings
  834. # Enable auth
  835. settings = Settings(key="auth_enabled", value="true")
  836. db_session.add(settings)
  837. await db_session.commit()
  838. # Request without token should fail
  839. response = await async_client.get("/api/v1/library/files")
  840. assert response.status_code == 401
  841. @pytest.mark.asyncio
  842. @pytest.mark.integration
  843. async def test_delete_file_own_by_owner(self, async_client: AsyncClient, db_session, auth_setup, test_file):
  844. """Verify operator can delete their own files."""
  845. from pathlib import Path
  846. # Create actual file on disk so delete doesn't fail
  847. from backend.app.core.config import settings as app_settings
  848. file_path = Path(app_settings.base_dir) / test_file.file_path
  849. file_path.parent.mkdir(parents=True, exist_ok=True)
  850. file_path.write_text("test content")
  851. operator_token = auth_setup["operator_token"]
  852. response = await async_client.delete(
  853. f"/api/v1/library/files/{test_file.id}", headers={"Authorization": f"Bearer {operator_token}"}
  854. )
  855. assert response.status_code == 200
  856. @pytest.mark.asyncio
  857. @pytest.mark.integration
  858. async def test_delete_file_own_denied_for_others_file(self, async_client: AsyncClient, db_session, auth_setup):
  859. """Verify operator cannot delete files owned by others."""
  860. # Create another operator user with a file
  861. from sqlalchemy import select
  862. from backend.app.core.auth import create_access_token
  863. from backend.app.models.group import Group
  864. from backend.app.models.library import LibraryFile
  865. from backend.app.models.user import User
  866. operator_group = (await db_session.execute(select(Group).where(Group.name == "Operators"))).scalar_one()
  867. from backend.app.core.auth import get_password_hash as get_pw_hash
  868. other_user = User(username="other_op", password_hash=get_pw_hash("password"), is_active=True)
  869. other_user.groups.append(operator_group)
  870. db_session.add(other_user)
  871. await db_session.commit()
  872. await db_session.refresh(other_user)
  873. # Create file owned by other user
  874. other_file = LibraryFile(
  875. filename="other.txt",
  876. file_path="data/archive/library/files/other.txt",
  877. file_type="txt",
  878. file_size=100,
  879. created_by_id=other_user.id,
  880. )
  881. db_session.add(other_file)
  882. await db_session.commit()
  883. await db_session.refresh(other_file)
  884. # Original operator should not be able to delete it
  885. operator_token = auth_setup["operator_token"]
  886. response = await async_client.delete(
  887. f"/api/v1/library/files/{other_file.id}", headers={"Authorization": f"Bearer {operator_token}"}
  888. )
  889. assert response.status_code == 403
  890. assert "your own files" in response.json()["detail"].lower()
  891. @pytest.mark.asyncio
  892. @pytest.mark.integration
  893. async def test_delete_file_admin_can_delete_any(self, async_client: AsyncClient, db_session, auth_setup):
  894. """Verify admin can delete any file."""
  895. from pathlib import Path
  896. from backend.app.core.config import settings as app_settings
  897. from backend.app.models.library import LibraryFile
  898. # Create file owned by operator
  899. operator_user = auth_setup["operator_user"]
  900. lib_file = LibraryFile(
  901. filename="admin_can_delete.txt",
  902. file_path="data/archive/library/files/admin_can_delete.txt",
  903. file_type="txt",
  904. file_size=100,
  905. created_by_id=operator_user.id,
  906. )
  907. db_session.add(lib_file)
  908. await db_session.commit()
  909. await db_session.refresh(lib_file)
  910. # Create actual file on disk
  911. file_path = Path(app_settings.base_dir) / lib_file.file_path
  912. file_path.parent.mkdir(parents=True, exist_ok=True)
  913. file_path.write_text("test content")
  914. # Admin should be able to delete it
  915. admin_token = auth_setup["admin_token"]
  916. response = await async_client.delete(
  917. f"/api/v1/library/files/{lib_file.id}", headers={"Authorization": f"Bearer {admin_token}"}
  918. )
  919. assert response.status_code == 200
  920. @pytest.mark.asyncio
  921. @pytest.mark.integration
  922. async def test_viewer_cannot_delete_files(self, async_client: AsyncClient, db_session, auth_setup, test_file):
  923. """Verify viewer cannot delete any files."""
  924. viewer_token = auth_setup["viewer_token"]
  925. response = await async_client.delete(
  926. f"/api/v1/library/files/{test_file.id}", headers={"Authorization": f"Bearer {viewer_token}"}
  927. )
  928. # Viewers don't have delete_own or delete_all permissions
  929. assert response.status_code == 403
  930. class TestPrintFileUploadValidation:
  931. """#1401: pre-flight rejection of unprintable uploads at the library +
  932. archive routes. Smoke tests the shared ``validate_print_file_upload``
  933. helper through both surfaces a user can reach with a drag-drop."""
  934. def _valid_3mf_bytes(self, name: str = "Metadata/plate_1.gcode") -> bytes:
  935. """Build a minimal-but-real zip with the gcode-3mf magic in it so
  936. the validator's ``startswith(b"PK\\x03\\x04")`` check passes."""
  937. buf = io.BytesIO()
  938. with zipfile.ZipFile(buf, "w", zipfile.ZIP_DEFLATED) as zf:
  939. zf.writestr(name, "; G-code\nG28\n")
  940. return buf.getvalue()
  941. @pytest.mark.asyncio
  942. @pytest.mark.integration
  943. async def test_library_rejects_raw_gcode_upload(self, async_client: AsyncClient, db_session):
  944. """``Foo.gcode`` direct uploads are blocked at the library route —
  945. the dispatcher would otherwise append ``.3mf`` and ship raw gcode
  946. to the printer as a fake 3MF."""
  947. files = {"file": ("plate_1.gcode", b"; raw gcode\nG28\n", "application/octet-stream")}
  948. response = await async_client.post("/api/v1/library/files", files=files)
  949. assert response.status_code == 400
  950. # Error message must name the actual remedy, not just say "invalid".
  951. assert "gcode.3mf" in response.json()["detail"]
  952. @pytest.mark.asyncio
  953. @pytest.mark.integration
  954. async def test_library_rejects_non_zip_3mf_upload(self, async_client: AsyncClient, db_session):
  955. """A ``.3mf`` upload whose body isn't a zip is rejected — covers
  956. raw gcode renamed to .3mf, corrupted downloads, etc."""
  957. files = {"file": ("model.3mf", b"; raw gcode\nG28\n", "application/octet-stream")}
  958. response = await async_client.post("/api/v1/library/files", files=files)
  959. assert response.status_code == 400
  960. assert "ZIP container" in response.json()["detail"]
  961. @pytest.mark.asyncio
  962. @pytest.mark.integration
  963. async def test_library_rejects_non_zip_gcode_3mf_upload(self, async_client: AsyncClient, db_session):
  964. """The compound-extension ``.gcode.3mf`` case is gated by the same
  965. zip-magic check — splitext returns just ``.3mf``, but the suffix
  966. match covers both."""
  967. files = {"file": ("plate_1.gcode.3mf", b"; raw gcode\nG28\n", "application/octet-stream")}
  968. response = await async_client.post("/api/v1/library/files", files=files)
  969. assert response.status_code == 400
  970. assert "ZIP container" in response.json()["detail"]
  971. @pytest.mark.asyncio
  972. @pytest.mark.integration
  973. async def test_library_accepts_valid_gcode_3mf_upload(self, async_client: AsyncClient, db_session):
  974. """A real ``.gcode.3mf`` zip uploads successfully — the existing
  975. happy path is not regressed by the new validation."""
  976. files = {
  977. "file": (
  978. "plate_1.gcode.3mf",
  979. self._valid_3mf_bytes(),
  980. "application/zip",
  981. )
  982. }
  983. response = await async_client.post("/api/v1/library/files", files=files)
  984. assert response.status_code == 200
  985. result = response.json()
  986. assert result["filename"] == "plate_1.gcode.3mf"
  987. @pytest.mark.asyncio
  988. @pytest.mark.integration
  989. async def test_library_still_accepts_non_print_extensions(self, async_client: AsyncClient, db_session):
  990. """STL / image / other non-print uploads bypass the validator
  991. entirely — Bambuddy is also a library, not just a print dispatcher."""
  992. files = {"file": ("model.stl", b"solid test\nendsolid test", "application/octet-stream")}
  993. response = await async_client.post(
  994. "/api/v1/library/files", files=files, params={"generate_stl_thumbnails": "false"}
  995. )
  996. assert response.status_code == 200
  997. @pytest.mark.asyncio
  998. @pytest.mark.integration
  999. async def test_archive_upload_rejects_non_zip(self, async_client: AsyncClient, db_session):
  1000. """``POST /archives/upload`` shares the same validator — covers the
  1001. manual archive-upload entry point too."""
  1002. files = {"file": ("model.3mf", b"; raw gcode\nG28\n", "application/octet-stream")}
  1003. response = await async_client.post("/api/v1/archives/upload", files=files)
  1004. assert response.status_code == 400
  1005. assert "ZIP container" in response.json()["detail"]
  1006. @pytest.mark.asyncio
  1007. @pytest.mark.integration
  1008. async def test_archive_bulk_upload_collects_per_file_errors(self, async_client: AsyncClient, db_session):
  1009. """The bulk-archive route reports validation failures per file and
  1010. continues processing the remaining items — one bad upload in a
  1011. 10-file drag-drop must not abort the whole batch."""
  1012. good = self._valid_3mf_bytes()
  1013. bad = b"; raw gcode\nG28\n"
  1014. # httpx multipart with a list-of-tuples preserves order + same field name.
  1015. files = [
  1016. ("files", ("good.3mf", good, "application/zip")),
  1017. ("files", ("bad.3mf", bad, "application/octet-stream")),
  1018. ]
  1019. response = await async_client.post("/api/v1/archives/upload-bulk", files=files)
  1020. assert response.status_code == 200
  1021. body = response.json()
  1022. # The bulk route's archive_print may still reject the "good" file
  1023. # downstream (no printer match, etc.) — we don't care about that
  1024. # here; what matters is the bad file lands in `errors` with the
  1025. # validator's message and the route didn't 500.
  1026. assert body["failed"] >= 1
  1027. bad_errors = [e for e in body["errors"] if e["filename"] == "bad.3mf"]
  1028. assert bad_errors, body
  1029. assert "ZIP container" in bad_errors[0]["error"]