test_external_folders_api.py 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  1. """Integration tests for External Folder API endpoints."""
  2. import os
  3. import tempfile
  4. from pathlib import Path
  5. import pytest
  6. from httpx import AsyncClient
  7. @pytest.fixture(autouse=True)
  8. def _enable_external_roots(monkeypatch, tmp_path):
  9. """Permit pytest's ``tmp_path`` tree as a valid external root.
  10. After the GHSA-r2qv I1 fix, external folders are opt-in via the
  11. ``BAMBUDDY_EXTERNAL_ROOTS`` env var (empty by default → feature
  12. disabled). The test suite's external dirs live under pytest's
  13. per-session ``tmp_path`` root, which is a subtree of the OS tmp
  14. dir, so allowlisting the parent of ``tmp_path`` lets every test
  15. folder fixture pass the new validator. Autouse so individual tests
  16. don't have to know the env var exists.
  17. """
  18. monkeypatch.setenv("BAMBUDDY_EXTERNAL_ROOTS", str(tmp_path.parent))
  19. class TestExternalFolderCreation:
  20. """Tests for POST /library/folders/external."""
  21. @pytest.fixture
  22. def external_dir(self, tmp_path):
  23. """Create a temporary directory to act as an external folder."""
  24. ext_dir = tmp_path / "nas_share"
  25. ext_dir.mkdir()
  26. # Add some test files
  27. (ext_dir / "benchy.3mf").write_bytes(b"fake3mf")
  28. (ext_dir / "bracket.stl").write_bytes(b"fakestl")
  29. (ext_dir / "print.gcode").write_text("G28\nG1 X10 Y10")
  30. (ext_dir / "readme.txt").write_text("not a print file")
  31. (ext_dir / ".hidden.3mf").write_bytes(b"hidden")
  32. return ext_dir
  33. @pytest.fixture
  34. def nested_external_dir(self, external_dir):
  35. """Create a nested subdirectory in the external folder."""
  36. sub = external_dir / "subfolder"
  37. sub.mkdir()
  38. (sub / "nested_part.stl").write_bytes(b"nestedstl")
  39. return external_dir
  40. @pytest.mark.asyncio
  41. @pytest.mark.integration
  42. async def test_create_external_folder(self, async_client: AsyncClient, db_session, external_dir):
  43. """Verify external folder can be created with valid path."""
  44. data = {
  45. "name": "NAS Prints",
  46. "external_path": str(external_dir),
  47. "readonly": True,
  48. "show_hidden": False,
  49. }
  50. response = await async_client.post("/api/v1/library/folders/external", json=data)
  51. assert response.status_code == 200
  52. result = response.json()
  53. assert result["name"] == "NAS Prints"
  54. assert result["is_external"] is True
  55. assert result["external_readonly"] is True
  56. assert result["external_show_hidden"] is False
  57. assert result["external_path"] == str(external_dir.resolve())
  58. @pytest.mark.asyncio
  59. @pytest.mark.integration
  60. async def test_create_external_folder_nonexistent_path(self, async_client: AsyncClient, db_session, tmp_path):
  61. """Verify 400 for non-existent path within an allowed root.
  62. After GHSA-r2qv I1 the allowlist check runs before the existence
  63. check, so the test path must be inside ``BAMBUDDY_EXTERNAL_ROOTS``
  64. (= ``tmp_path.parent`` per ``_enable_external_roots``) to actually
  65. exercise the existence branch rather than the allowlist branch.
  66. """
  67. bad_path = tmp_path / "nonexistent" / "subdir"
  68. data = {
  69. "name": "Bad Path",
  70. "external_path": str(bad_path),
  71. }
  72. response = await async_client.post("/api/v1/library/folders/external", json=data)
  73. assert response.status_code == 400
  74. assert "does not exist" in response.json()["detail"]
  75. @pytest.mark.asyncio
  76. @pytest.mark.integration
  77. async def test_create_external_folder_outside_allowlist_blocked(self, async_client: AsyncClient, db_session):
  78. """Paths outside ``BAMBUDDY_EXTERNAL_ROOTS`` are rejected (GHSA-r2qv I1).
  79. Prior behaviour was a denylist (``/proc``, ``/sys``, ``/dev``, etc);
  80. anything not enumerated passed, including ``/data`` containing
  81. other users' archives. The allowlist replacement defaults to the
  82. empty set; this test confirms that a path outside the (tmp-path)
  83. allowlist set up by ``_enable_external_roots`` is rejected.
  84. ``/proc`` is the canonical example of a system directory that
  85. any operator allowlist would never legitimately include.
  86. """
  87. data = {
  88. "name": "System",
  89. "external_path": "/proc",
  90. }
  91. response = await async_client.post("/api/v1/library/folders/external", json=data)
  92. assert response.status_code == 400
  93. assert "not within an allowed external root" in response.json()["detail"].lower()
  94. @pytest.mark.asyncio
  95. @pytest.mark.integration
  96. async def test_create_external_folder_file_not_dir(self, async_client: AsyncClient, db_session, tmp_path):
  97. """Verify 400 when path is a file, not directory."""
  98. file_path = tmp_path / "not_a_dir.txt"
  99. file_path.write_text("hello")
  100. data = {
  101. "name": "Not A Dir",
  102. "external_path": str(file_path),
  103. }
  104. response = await async_client.post("/api/v1/library/folders/external", json=data)
  105. assert response.status_code == 400
  106. assert "not a directory" in response.json()["detail"].lower()
  107. @pytest.mark.asyncio
  108. @pytest.mark.integration
  109. async def test_create_external_folder_duplicate_path(self, async_client: AsyncClient, db_session, external_dir):
  110. """Verify 409 when same path already linked."""
  111. data = {
  112. "name": "First",
  113. "external_path": str(external_dir),
  114. }
  115. response = await async_client.post("/api/v1/library/folders/external", json=data)
  116. assert response.status_code == 200
  117. data["name"] = "Duplicate"
  118. response = await async_client.post("/api/v1/library/folders/external", json=data)
  119. assert response.status_code == 409
  120. assert "already exists" in response.json()["detail"]
  121. @pytest.mark.asyncio
  122. @pytest.mark.integration
  123. async def test_external_folder_appears_in_tree(self, async_client: AsyncClient, db_session, external_dir):
  124. """Verify external folder shows up in folder tree with external fields."""
  125. data = {
  126. "name": "My NAS",
  127. "external_path": str(external_dir),
  128. "readonly": True,
  129. }
  130. await async_client.post("/api/v1/library/folders/external", json=data)
  131. response = await async_client.get("/api/v1/library/folders")
  132. assert response.status_code == 200
  133. folders = response.json()
  134. ext_folder = next((f for f in folders if f["name"] == "My NAS"), None)
  135. assert ext_folder is not None
  136. assert ext_folder["is_external"] is True
  137. assert ext_folder["external_readonly"] is True
  138. def find_folder_in_tree(folders: list, name: str) -> dict | None:
  139. """Recursively search a folder tree for a folder by name."""
  140. for f in folders:
  141. if f["name"] == name:
  142. return f
  143. result = find_folder_in_tree(f.get("children", []), name)
  144. if result:
  145. return result
  146. return None
  147. def collect_folder_names(folders: list) -> list[str]:
  148. """Recursively collect all folder names from a tree."""
  149. names = []
  150. for f in folders:
  151. names.append(f["name"])
  152. names.extend(collect_folder_names(f.get("children", [])))
  153. return names
  154. class TestExternalFolderScan:
  155. """Tests for POST /library/folders/{id}/scan."""
  156. @pytest.fixture
  157. def external_dir(self, tmp_path):
  158. """Create a temporary directory with test files."""
  159. ext_dir = tmp_path / "prints"
  160. ext_dir.mkdir()
  161. (ext_dir / "benchy.3mf").write_bytes(b"fake3mf")
  162. (ext_dir / "bracket.stl").write_bytes(b"fakestl")
  163. (ext_dir / "print.gcode").write_text("G28\nG1 X10 Y10")
  164. (ext_dir / "readme.txt").write_text("not a print file")
  165. (ext_dir / ".hidden.3mf").write_bytes(b"hidden")
  166. sub = ext_dir / "subfolder"
  167. sub.mkdir()
  168. (sub / "nested.stl").write_bytes(b"nested")
  169. return ext_dir
  170. @pytest.fixture
  171. async def external_folder(self, async_client, db_session, external_dir):
  172. """Create an external folder via API."""
  173. data = {
  174. "name": "Scan Test",
  175. "external_path": str(external_dir),
  176. "readonly": True,
  177. "show_hidden": False,
  178. }
  179. response = await async_client.post("/api/v1/library/folders/external", json=data)
  180. return response.json()
  181. @pytest.mark.asyncio
  182. @pytest.mark.integration
  183. async def test_scan_discovers_files(self, async_client: AsyncClient, db_session, external_folder):
  184. """Verify scan discovers supported files and creates subfolders."""
  185. response = await async_client.post(f"/api/v1/library/folders/{external_folder['id']}/scan")
  186. assert response.status_code == 200
  187. result = response.json()
  188. # Should find: benchy.3mf, bracket.stl, print.gcode (root) + subfolder/nested.stl
  189. # Should skip: readme.txt (unsupported), .hidden.3mf (hidden)
  190. assert result["added"] == 4
  191. assert result["removed"] == 0
  192. # Root folder should have 3 files (nested.stl is in subfolder)
  193. response = await async_client.get(f"/api/v1/library/files?folder_id={external_folder['id']}")
  194. root_files = response.json()
  195. assert len(root_files) == 3
  196. root_filenames = {f["filename"] for f in root_files}
  197. assert root_filenames == {"benchy.3mf", "bracket.stl", "print.gcode"}
  198. # Subfolder should exist in the tree and contain nested.stl
  199. response = await async_client.get("/api/v1/library/folders")
  200. folders = response.json()
  201. subfolder = find_folder_in_tree(folders, "subfolder")
  202. assert subfolder is not None
  203. assert subfolder["is_external"] is True
  204. assert subfolder["parent_id"] == external_folder["id"]
  205. response = await async_client.get(f"/api/v1/library/files?folder_id={subfolder['id']}")
  206. sub_files = response.json()
  207. assert len(sub_files) == 1
  208. assert sub_files[0]["filename"] == "nested.stl"
  209. @pytest.mark.asyncio
  210. @pytest.mark.integration
  211. async def test_scan_skips_hidden_files(self, async_client: AsyncClient, db_session, external_folder):
  212. """Verify hidden files are skipped by default."""
  213. await async_client.post(f"/api/v1/library/folders/{external_folder['id']}/scan")
  214. # List files in root folder
  215. response = await async_client.get(f"/api/v1/library/files?folder_id={external_folder['id']}")
  216. assert response.status_code == 200
  217. files = response.json()
  218. filenames = [f["filename"] for f in files]
  219. assert ".hidden.3mf" not in filenames
  220. @pytest.mark.asyncio
  221. @pytest.mark.integration
  222. async def test_scan_shows_hidden_when_enabled(self, async_client: AsyncClient, db_session, external_dir):
  223. """Verify hidden files found when show_hidden=True."""
  224. data = {
  225. "name": "Show Hidden Test",
  226. "external_path": str(external_dir),
  227. "show_hidden": True,
  228. }
  229. response = await async_client.post("/api/v1/library/folders/external", json=data)
  230. folder = response.json()
  231. response = await async_client.post(f"/api/v1/library/folders/{folder['id']}/scan")
  232. result = response.json()
  233. # Now should also find .hidden.3mf → 5 total
  234. assert result["added"] == 5
  235. @pytest.mark.asyncio
  236. @pytest.mark.integration
  237. async def test_scan_idempotent(self, async_client: AsyncClient, db_session, external_folder):
  238. """Verify scanning twice doesn't duplicate files."""
  239. response1 = await async_client.post(f"/api/v1/library/folders/{external_folder['id']}/scan")
  240. assert response1.json()["added"] == 4
  241. response2 = await async_client.post(f"/api/v1/library/folders/{external_folder['id']}/scan")
  242. assert response2.json()["added"] == 0
  243. assert response2.json()["removed"] == 0
  244. @pytest.mark.asyncio
  245. @pytest.mark.integration
  246. async def test_scan_removes_deleted_files(
  247. self, async_client: AsyncClient, db_session, external_folder, external_dir
  248. ):
  249. """Verify scan removes entries for files no longer on disk."""
  250. await async_client.post(f"/api/v1/library/folders/{external_folder['id']}/scan")
  251. # Delete a file from disk
  252. (external_dir / "bracket.stl").unlink()
  253. response = await async_client.post(f"/api/v1/library/folders/{external_folder['id']}/scan")
  254. result = response.json()
  255. assert result["removed"] == 1
  256. assert result["added"] == 0
  257. @pytest.mark.asyncio
  258. @pytest.mark.integration
  259. async def test_scan_indexes_pre_existing_markdown(
  260. self, async_client: AsyncClient, db_session, external_folder, external_dir
  261. ):
  262. """Scan should index a README.md already on disk (#2520 item 1).
  263. Markdown dropped into the folder by external tools (not the Upload
  264. dialog) must be picked up so the Folder Readme panel can show it.
  265. """
  266. (external_dir / "README.md").write_text("# Fishing Floats\n\nDescription.")
  267. response = await async_client.post(f"/api/v1/library/folders/{external_folder['id']}/scan")
  268. assert response.status_code == 200
  269. # 4 supported files from the fixture + the new README.md
  270. assert response.json()["added"] == 5
  271. response = await async_client.get(f"/api/v1/library/files?folder_id={external_folder['id']}")
  272. root_filenames = {f["filename"] for f in response.json()}
  273. assert "README.md" in root_filenames
  274. # Readme panel can now resolve it.
  275. response = await async_client.get(f"/api/v1/library/folders/{external_folder['id']}/readme")
  276. assert response.status_code == 200
  277. assert response.json()["filename"] == "README.md"
  278. assert "Fishing Floats" in response.json()["content"]
  279. @pytest.mark.asyncio
  280. @pytest.mark.integration
  281. async def test_scan_preserves_uploaded_markdown(self, async_client: AsyncClient, db_session, tmp_path):
  282. """Scanning must not delete an uploaded README.md (#2520 destructive-cleanup bug).
  283. Before the fix, .md was absent from _SCANNABLE_EXTENSIONS, so an
  284. uploaded markdown record was never re-found during the walk and the
  285. cleanup pass purged it — the Readme panel then 404'd and hid.
  286. """
  287. import io
  288. writable_dir = tmp_path / "writable"
  289. writable_dir.mkdir()
  290. response = await async_client.post(
  291. "/api/v1/library/folders/external",
  292. json={"name": "Writable", "external_path": str(writable_dir), "readonly": False},
  293. )
  294. folder = response.json()
  295. upload = await async_client.post(
  296. f"/api/v1/library/files?folder_id={folder['id']}",
  297. files={"file": ("README.md", io.BytesIO(b"# Model\n\nHello"), "text/markdown")},
  298. )
  299. assert upload.status_code in (200, 201)
  300. # Panel works before the scan.
  301. readme = await async_client.get(f"/api/v1/library/folders/{folder['id']}/readme")
  302. assert readme.status_code == 200
  303. # The scan that used to nuke the record.
  304. scan = await async_client.post(f"/api/v1/library/folders/{folder['id']}/scan")
  305. assert scan.status_code == 200
  306. assert scan.json()["removed"] == 0
  307. # Record and panel survive.
  308. readme = await async_client.get(f"/api/v1/library/folders/{folder['id']}/readme")
  309. assert readme.status_code == 200
  310. assert readme.json()["filename"] == "README.md"
  311. @pytest.mark.asyncio
  312. @pytest.mark.integration
  313. async def test_scan_preserves_non_scannable_file_on_disk(self, async_client: AsyncClient, db_session, tmp_path):
  314. """Cleanup must gate on disk presence, not scannable-extension membership (#2520).
  315. Any uploaded file whose extension is outside _SCANNABLE_EXTENSIONS
  316. (here a .txt) stays on disk, so its DB record must survive a scan
  317. rather than being treated as deleted.
  318. """
  319. import io
  320. writable_dir = tmp_path / "writable_txt"
  321. writable_dir.mkdir()
  322. response = await async_client.post(
  323. "/api/v1/library/folders/external",
  324. json={"name": "Writable Txt", "external_path": str(writable_dir), "readonly": False},
  325. )
  326. folder = response.json()
  327. upload = await async_client.post(
  328. f"/api/v1/library/files?folder_id={folder['id']}",
  329. files={"file": ("notes.txt", io.BytesIO(b"keep me"), "text/plain")},
  330. )
  331. assert upload.status_code in (200, 201)
  332. scan = await async_client.post(f"/api/v1/library/folders/{folder['id']}/scan")
  333. assert scan.status_code == 200
  334. assert scan.json()["removed"] == 0
  335. files = await async_client.get(f"/api/v1/library/files?folder_id={folder['id']}")
  336. assert "notes.txt" in {f["filename"] for f in files.json()}
  337. @pytest.mark.asyncio
  338. @pytest.mark.integration
  339. async def test_scan_non_external_folder_fails(self, async_client: AsyncClient, db_session):
  340. """Verify scan fails on regular (non-external) folder."""
  341. # Create a regular folder
  342. data = {"name": "Regular Folder"}
  343. response = await async_client.post("/api/v1/library/folders", json=data)
  344. folder = response.json()
  345. response = await async_client.post(f"/api/v1/library/folders/{folder['id']}/scan")
  346. assert response.status_code == 400
  347. assert "not an external" in response.json()["detail"].lower()
  348. @pytest.mark.asyncio
  349. @pytest.mark.integration
  350. async def test_scan_files_marked_external(self, async_client: AsyncClient, db_session, external_folder):
  351. """Verify scanned files have is_external=True in root and subfolders."""
  352. await async_client.post(f"/api/v1/library/folders/{external_folder['id']}/scan")
  353. # Check root folder files
  354. response = await async_client.get(f"/api/v1/library/files?folder_id={external_folder['id']}")
  355. files = response.json()
  356. assert len(files) > 0
  357. for f in files:
  358. assert f["is_external"] is True
  359. # Check subfolder files
  360. response = await async_client.get("/api/v1/library/folders")
  361. folders = response.json()
  362. subfolder = find_folder_in_tree(folders, "subfolder")
  363. assert subfolder is not None
  364. response = await async_client.get(f"/api/v1/library/files?folder_id={subfolder['id']}")
  365. sub_files = response.json()
  366. for f in sub_files:
  367. assert f["is_external"] is True
  368. @pytest.mark.asyncio
  369. @pytest.mark.integration
  370. async def test_scan_creates_nested_subfolders(self, async_client: AsyncClient, db_session, external_dir):
  371. """Verify deeply nested directories create correct folder hierarchy."""
  372. # Create nested structure: deep/nested/dir/model.stl
  373. deep = external_dir / "deep" / "nested" / "dir"
  374. deep.mkdir(parents=True)
  375. (deep / "model.stl").write_bytes(b"deepstl")
  376. data = {
  377. "name": "Nested Test",
  378. "external_path": str(external_dir),
  379. "readonly": True,
  380. "show_hidden": False,
  381. }
  382. response = await async_client.post("/api/v1/library/folders/external", json=data)
  383. root = response.json()
  384. response = await async_client.post(f"/api/v1/library/folders/{root['id']}/scan")
  385. assert response.status_code == 200
  386. # Verify folder chain: root -> deep -> nested -> dir
  387. response = await async_client.get("/api/v1/library/folders")
  388. all_folders = response.json()
  389. deep = find_folder_in_tree(all_folders, "deep")
  390. assert deep is not None
  391. assert deep["parent_id"] == root["id"]
  392. assert deep["is_external"] is True
  393. nested = find_folder_in_tree(all_folders, "nested")
  394. assert nested is not None
  395. assert nested["parent_id"] == deep["id"]
  396. dir_folder = find_folder_in_tree(all_folders, "dir")
  397. assert dir_folder is not None
  398. assert dir_folder["parent_id"] == nested["id"]
  399. # model.stl should be in the "dir" folder
  400. response = await async_client.get(f"/api/v1/library/files?folder_id={dir_folder['id']}")
  401. files = response.json()
  402. assert len(files) == 1
  403. assert files[0]["filename"] == "model.stl"
  404. @pytest.mark.asyncio
  405. @pytest.mark.integration
  406. async def test_scan_skips_hidden_directories(self, async_client: AsyncClient, db_session, external_dir):
  407. """Verify hidden directories are skipped when show_hidden=False."""
  408. hidden_dir = external_dir / ".hidden_dir"
  409. hidden_dir.mkdir()
  410. (hidden_dir / "secret.stl").write_bytes(b"secret")
  411. data = {
  412. "name": "Hidden Dir Test",
  413. "external_path": str(external_dir),
  414. "readonly": True,
  415. "show_hidden": False,
  416. }
  417. response = await async_client.post("/api/v1/library/folders/external", json=data)
  418. root = response.json()
  419. response = await async_client.post(f"/api/v1/library/folders/{root['id']}/scan")
  420. result = response.json()
  421. # Should find 4 files (root 3 + subfolder/nested.stl) but NOT .hidden_dir/secret.stl
  422. assert result["added"] == 4
  423. # No ".hidden_dir" folder should be created
  424. response = await async_client.get("/api/v1/library/folders")
  425. folder_names = collect_folder_names(response.json())
  426. assert ".hidden_dir" not in folder_names
  427. @pytest.mark.asyncio
  428. @pytest.mark.integration
  429. async def test_scan_removes_deleted_subfolder(
  430. self, async_client: AsyncClient, db_session, external_folder, external_dir
  431. ):
  432. """Verify scan removes empty subfolder entries when directory deleted from disk."""
  433. await async_client.post(f"/api/v1/library/folders/{external_folder['id']}/scan")
  434. # Verify subfolder exists
  435. response = await async_client.get("/api/v1/library/folders")
  436. subfolder = find_folder_in_tree(response.json(), "subfolder")
  437. assert subfolder is not None
  438. # Delete the subfolder from disk
  439. import shutil
  440. shutil.rmtree(external_dir / "subfolder")
  441. # Re-scan
  442. response = await async_client.post(f"/api/v1/library/folders/{external_folder['id']}/scan")
  443. result = response.json()
  444. assert result["removed"] == 1 # nested.stl removed
  445. # Subfolder should be cleaned up (empty + directory gone)
  446. response = await async_client.get("/api/v1/library/folders")
  447. subfolder = find_folder_in_tree(response.json(), "subfolder")
  448. assert subfolder is None
  449. @pytest.mark.asyncio
  450. @pytest.mark.integration
  451. async def test_scan_subfolder_inherits_readonly(
  452. self, async_client: AsyncClient, db_session, external_folder, external_dir
  453. ):
  454. """Verify created subfolders inherit external_readonly from parent."""
  455. await async_client.post(f"/api/v1/library/folders/{external_folder['id']}/scan")
  456. response = await async_client.get("/api/v1/library/folders")
  457. subfolder = find_folder_in_tree(response.json(), "subfolder")
  458. assert subfolder is not None
  459. assert subfolder["external_readonly"] is True
  460. class TestExternalFolderProtections:
  461. """Tests for read-only protections on external folders."""
  462. @pytest.fixture
  463. def external_dir(self, tmp_path):
  464. ext_dir = tmp_path / "readonly_share"
  465. ext_dir.mkdir()
  466. (ext_dir / "test.stl").write_bytes(b"fakestl")
  467. return ext_dir
  468. @pytest.fixture
  469. async def readonly_folder(self, async_client, db_session, external_dir):
  470. """Create a read-only external folder with files scanned."""
  471. data = {
  472. "name": "Read Only",
  473. "external_path": str(external_dir),
  474. "readonly": True,
  475. }
  476. response = await async_client.post("/api/v1/library/folders/external", json=data)
  477. folder = response.json()
  478. await async_client.post(f"/api/v1/library/folders/{folder['id']}/scan")
  479. return folder
  480. @pytest.mark.asyncio
  481. @pytest.mark.integration
  482. async def test_upload_to_readonly_folder_blocked(self, async_client: AsyncClient, db_session, readonly_folder):
  483. """Verify uploads to read-only external folders are blocked."""
  484. import io
  485. file_content = io.BytesIO(b"test content")
  486. response = await async_client.post(
  487. f"/api/v1/library/files?folder_id={readonly_folder['id']}",
  488. files={"file": ("test.gcode", file_content, "application/octet-stream")},
  489. )
  490. assert response.status_code == 403
  491. assert "read-only" in response.json()["detail"].lower()
  492. @pytest.mark.asyncio
  493. @pytest.mark.integration
  494. async def test_move_to_readonly_folder_blocked(self, async_client: AsyncClient, db_session, readonly_folder):
  495. """Verify moving files to read-only external folder is blocked."""
  496. from backend.app.models.library import LibraryFile
  497. # Create a regular file
  498. lib_file = LibraryFile(
  499. filename="regular.3mf",
  500. file_path="/test/regular.3mf",
  501. file_size=1024,
  502. file_type="3mf",
  503. )
  504. db_session.add(lib_file)
  505. await db_session.commit()
  506. await db_session.refresh(lib_file)
  507. data = {"file_ids": [lib_file.id], "folder_id": readonly_folder["id"]}
  508. response = await async_client.post("/api/v1/library/files/move", json=data)
  509. assert response.status_code == 403
  510. assert "read-only" in response.json()["detail"].lower()
  511. @pytest.mark.asyncio
  512. @pytest.mark.integration
  513. async def test_external_files_cannot_be_moved_out(self, async_client: AsyncClient, db_session, readonly_folder):
  514. """Verify external files can't be moved to other folders."""
  515. # Get the external file ID
  516. response = await async_client.get(f"/api/v1/library/files?folder_id={readonly_folder['id']}")
  517. files = response.json()
  518. assert len(files) > 0
  519. ext_file_id = files[0]["id"]
  520. # Try to move to root
  521. data = {"file_ids": [ext_file_id], "folder_id": None}
  522. response = await async_client.post("/api/v1/library/files/move", json=data)
  523. assert response.status_code == 200
  524. # File should be skipped, not moved
  525. result = response.json()
  526. assert result["moved"] == 0
  527. @pytest.mark.asyncio
  528. @pytest.mark.integration
  529. async def test_delete_external_file_removes_db_only(
  530. self, async_client: AsyncClient, db_session, readonly_folder, external_dir
  531. ):
  532. """Verify deleting an external file only removes DB entry, not the file on disk."""
  533. response = await async_client.get(f"/api/v1/library/files?folder_id={readonly_folder['id']}")
  534. files = response.json()
  535. ext_file_id = files[0]["id"]
  536. ext_filename = files[0]["filename"]
  537. # Delete via API
  538. response = await async_client.delete(f"/api/v1/library/files/{ext_file_id}")
  539. assert response.status_code == 200
  540. # File should still exist on disk
  541. assert (external_dir / ext_filename).exists()
  542. @pytest.mark.asyncio
  543. @pytest.mark.integration
  544. async def test_delete_external_folder_preserves_files(
  545. self, async_client: AsyncClient, db_session, readonly_folder, external_dir
  546. ):
  547. """Verify deleting an external folder doesn't delete files from disk."""
  548. response = await async_client.delete(f"/api/v1/library/folders/{readonly_folder['id']}")
  549. assert response.status_code == 200
  550. # Files should still exist on disk
  551. assert (external_dir / "test.stl").exists()
  552. @pytest.mark.asyncio
  553. @pytest.mark.integration
  554. async def test_zip_to_readonly_folder_blocked(self, async_client: AsyncClient, db_session, readonly_folder):
  555. """Verify ZIP extraction to read-only external folder is blocked."""
  556. import io
  557. import zipfile
  558. # Create a minimal zip
  559. buf = io.BytesIO()
  560. with zipfile.ZipFile(buf, "w") as zf:
  561. zf.writestr("test.stl", b"fakestl")
  562. buf.seek(0)
  563. response = await async_client.post(
  564. f"/api/v1/library/files/extract-zip?folder_id={readonly_folder['id']}",
  565. files={"file": ("test.zip", buf, "application/zip")},
  566. )
  567. assert response.status_code == 403
  568. assert "read-only" in response.json()["detail"].lower()
  569. class TestExternalFolderWritableUpload:
  570. """Tests for upload write-through to writable external folders (#1112).
  571. Before the fix, uploads to writable external folders silently landed in the
  572. internal library dir while the DB row pointed at the external folder —
  573. files were invisible when the mount was viewed from another machine.
  574. """
  575. @pytest.fixture
  576. def external_dir(self, tmp_path):
  577. ext_dir = tmp_path / "writable_share"
  578. ext_dir.mkdir()
  579. return ext_dir
  580. @pytest.fixture
  581. async def writable_folder(self, async_client, db_session, external_dir):
  582. data = {
  583. "name": "Writable NAS",
  584. "external_path": str(external_dir),
  585. "readonly": False,
  586. }
  587. response = await async_client.post("/api/v1/library/folders/external", json=data)
  588. assert response.status_code == 200
  589. return response.json()
  590. @pytest.mark.asyncio
  591. @pytest.mark.integration
  592. async def test_upload_lands_on_external_mount(
  593. self, async_client: AsyncClient, db_session, writable_folder, external_dir
  594. ):
  595. """Bytes are written to ``<external_path>/<filename>``, not the internal library dir."""
  596. import io
  597. content = b"hello-external-world"
  598. response = await async_client.post(
  599. f"/api/v1/library/files?folder_id={writable_folder['id']}",
  600. files={"file": ("upload.stl", io.BytesIO(content), "application/octet-stream")},
  601. )
  602. assert response.status_code == 200, response.text
  603. on_disk = external_dir / "upload.stl"
  604. assert on_disk.exists(), "file must be written to the external mount"
  605. assert on_disk.read_bytes() == content
  606. @pytest.mark.asyncio
  607. @pytest.mark.integration
  608. async def test_upload_persists_correct_db_shape(
  609. self, async_client: AsyncClient, db_session, writable_folder, external_dir
  610. ):
  611. """DB row must have ``is_external=True`` and ``file_path`` = absolute external path,
  612. so scan-dedupe and deletion behaviour match scanned files."""
  613. import io
  614. import zipfile
  615. from backend.app.models.library import LibraryFile
  616. # #1401 hardened the library upload route to reject .3mf files that
  617. # aren't valid ZIP containers. This test asserts external-folder
  618. # DB shape, not the upload validator, so feed it a minimal real zip
  619. # rather than placeholder bytes.
  620. zip_buf = io.BytesIO()
  621. with zipfile.ZipFile(zip_buf, "w", zipfile.ZIP_DEFLATED) as zf:
  622. zf.writestr("placeholder.txt", "")
  623. zip_buf.seek(0)
  624. response = await async_client.post(
  625. f"/api/v1/library/files?folder_id={writable_folder['id']}",
  626. files={"file": ("model.3mf", zip_buf, "application/octet-stream")},
  627. )
  628. assert response.status_code == 200
  629. file_id = response.json()["id"]
  630. row = await db_session.get(LibraryFile, file_id)
  631. await db_session.refresh(row)
  632. assert row.is_external is True
  633. assert row.file_path == str((external_dir / "model.3mf").resolve())
  634. @pytest.mark.asyncio
  635. @pytest.mark.integration
  636. async def test_upload_filename_collision_returns_409(
  637. self, async_client: AsyncClient, db_session, writable_folder, external_dir
  638. ):
  639. """Re-uploading a filename that already exists on the mount must 409,
  640. not silently overwrite — matches scan's treatment of external files as
  641. externally-owned bytes."""
  642. import io
  643. (external_dir / "already.stl").write_bytes(b"prior")
  644. response = await async_client.post(
  645. f"/api/v1/library/files?folder_id={writable_folder['id']}",
  646. files={"file": ("already.stl", io.BytesIO(b"new"), "application/octet-stream")},
  647. )
  648. assert response.status_code == 409
  649. assert (external_dir / "already.stl").read_bytes() == b"prior"
  650. @pytest.mark.asyncio
  651. @pytest.mark.integration
  652. async def test_upload_to_missing_external_path_returns_400(
  653. self, async_client: AsyncClient, db_session, writable_folder, external_dir
  654. ):
  655. """If the external mount has gone away between folder-create and
  656. upload, fail loud rather than silently misroute to internal storage."""
  657. import io
  658. import shutil
  659. shutil.rmtree(external_dir)
  660. response = await async_client.post(
  661. f"/api/v1/library/files?folder_id={writable_folder['id']}",
  662. files={"file": ("x.stl", io.BytesIO(b"x"), "application/octet-stream")},
  663. )
  664. assert response.status_code == 400
  665. assert "not accessible" in response.json()["detail"].lower()
  666. @pytest.mark.asyncio
  667. @pytest.mark.integration
  668. async def test_upload_rejects_path_traversal_filename(
  669. self, async_client: AsyncClient, db_session, writable_folder, external_dir
  670. ):
  671. """A malicious filename like ``../escape.stl`` must not write outside
  672. the external folder. Defence-in-depth — FastAPI already strips these
  673. on parse, but the resolve-and-relative_to guard is the final gate."""
  674. import io
  675. response = await async_client.post(
  676. f"/api/v1/library/files?folder_id={writable_folder['id']}",
  677. files={"file": ("../escape.stl", io.BytesIO(b"x"), "application/octet-stream")},
  678. )
  679. # Either a 400 from our traversal guard or a 200 with basename-stripped
  680. # filename inside the external dir — both prove nothing escaped.
  681. if response.status_code == 200:
  682. assert not (external_dir.parent / "escape.stl").exists()
  683. assert (external_dir / "escape.stl").exists() or (external_dir / "..escape.stl").exists()
  684. else:
  685. assert response.status_code in (400, 422)
  686. assert not (external_dir.parent / "escape.stl").exists()
  687. @pytest.mark.asyncio
  688. @pytest.mark.integration
  689. async def test_zip_to_writable_external_folder_rejected(
  690. self, async_client: AsyncClient, db_session, writable_folder
  691. ):
  692. """Extract-zip into writable external folders isn't supported (nested
  693. subfolder creation on the mount is a separate design). Users are
  694. pointed at the Scan flow instead."""
  695. import io
  696. import zipfile
  697. buf = io.BytesIO()
  698. with zipfile.ZipFile(buf, "w") as zf:
  699. zf.writestr("a/b/c.stl", b"x")
  700. buf.seek(0)
  701. response = await async_client.post(
  702. f"/api/v1/library/files/extract-zip?folder_id={writable_folder['id']}",
  703. files={"file": ("test.zip", buf, "application/zip")},
  704. )
  705. assert response.status_code == 400
  706. assert "scan" in response.json()["detail"].lower()
  707. @pytest.mark.asyncio
  708. @pytest.mark.integration
  709. async def test_non_external_upload_unchanged(self, async_client: AsyncClient, db_session):
  710. """Uploads with no folder_id (root) keep the existing internal-storage behaviour."""
  711. import io
  712. from backend.app.models.library import LibraryFile
  713. response = await async_client.post(
  714. "/api/v1/library/files",
  715. files={"file": ("root.stl", io.BytesIO(b"x"), "application/octet-stream")},
  716. )
  717. assert response.status_code == 200
  718. file_id = response.json()["id"]
  719. row = await db_session.get(LibraryFile, file_id)
  720. await db_session.refresh(row)
  721. assert row.is_external is False
  722. # Internal storage: file_path is UUID-scoped, stored as a relative path.
  723. assert not row.file_path.startswith("/")
  724. class TestCrossBoundaryMove:
  725. """#1112 follow-up: moving files between managed and external folders
  726. must physically relocate the bytes, not just shuffle the DB ``folder_id``.
  727. Pre-fix symptom (reported by @Carter3DP after testing 0.2.4b1): a file
  728. moved from a managed folder to a NAS-backed external folder showed up
  729. in Bambuddy's UI under the external folder but was never written to
  730. the NAS — so the SMB mount and Bambuddy disagreed about what was
  731. actually there.
  732. """
  733. @pytest.fixture
  734. def external_dir(self, tmp_path):
  735. ext_dir = tmp_path / "writable_share"
  736. ext_dir.mkdir()
  737. return ext_dir
  738. @pytest.fixture
  739. async def writable_folder(self, async_client, db_session, external_dir):
  740. data = {"name": "Writable NAS", "external_path": str(external_dir), "readonly": False}
  741. response = await async_client.post("/api/v1/library/folders/external", json=data)
  742. assert response.status_code == 200
  743. return response.json()
  744. @pytest.fixture
  745. async def readonly_folder(self, async_client, db_session, tmp_path):
  746. ro_dir = tmp_path / "ro_share"
  747. ro_dir.mkdir()
  748. (ro_dir / "stranded.gcode").write_text("G28")
  749. data = {"name": "Read-only NAS", "external_path": str(ro_dir), "readonly": True}
  750. response = await async_client.post("/api/v1/library/folders/external", json=data)
  751. assert response.status_code == 200
  752. # Populate via scan so the file gets a DB row with is_external=True.
  753. scan = await async_client.post(f"/api/v1/library/folders/{response.json()['id']}/scan")
  754. assert scan.status_code == 200
  755. return response.json()
  756. @pytest.mark.asyncio
  757. @pytest.mark.integration
  758. async def test_managed_to_external_relocates_bytes(
  759. self, async_client: AsyncClient, db_session, writable_folder, external_dir
  760. ):
  761. """The actual #1112 fix: managed → external must write the bytes
  762. to the NAS mount AND drop them from internal storage. Pre-fix the
  763. DB row flipped to the new folder but the bytes stayed put."""
  764. import io
  765. from backend.app.api.routes.library import to_absolute_path
  766. from backend.app.models.library import LibraryFile
  767. upload = await async_client.post(
  768. "/api/v1/library/files",
  769. files={"file": ("ship_me.stl", io.BytesIO(b"original-bytes"), "application/octet-stream")},
  770. )
  771. assert upload.status_code == 200
  772. file_id = upload.json()["id"]
  773. # Snapshot the pre-move on-disk path so we can verify it's gone after.
  774. pre = await db_session.get(LibraryFile, file_id)
  775. await db_session.refresh(pre)
  776. managed_disk_path = to_absolute_path(pre.file_path)
  777. assert managed_disk_path is not None and managed_disk_path.exists()
  778. response = await async_client.post(
  779. "/api/v1/library/files/move",
  780. json={"file_ids": [file_id], "folder_id": writable_folder["id"]},
  781. )
  782. assert response.status_code == 200, response.text
  783. body = response.json()
  784. assert body["moved"] == 1
  785. assert body["skipped"] == 0
  786. # Bytes are on the NAS mount.
  787. on_nas = external_dir / "ship_me.stl"
  788. assert on_nas.exists()
  789. assert on_nas.read_bytes() == b"original-bytes"
  790. # Internal copy is gone.
  791. assert not managed_disk_path.exists(), "managed source must be removed after the move"
  792. # DB row matches reality.
  793. await db_session.refresh(pre)
  794. assert pre.is_external is True
  795. assert pre.folder_id == writable_folder["id"]
  796. assert pre.file_path == str(on_nas.resolve())
  797. @pytest.mark.asyncio
  798. @pytest.mark.integration
  799. async def test_external_to_managed_relocates_bytes(
  800. self, async_client: AsyncClient, db_session, writable_folder, external_dir
  801. ):
  802. """Symmetric direction: external → managed copies the bytes into
  803. internal storage with a UUID name, deletes the source on the
  804. mount, and recomputes the file hash (since scan stores
  805. ``file_hash=None`` for external rows)."""
  806. import io
  807. from backend.app.models.library import LibraryFile
  808. # Plant a file on the writable mount and let upload give it a row.
  809. upload = await async_client.post(
  810. f"/api/v1/library/files?folder_id={writable_folder['id']}",
  811. files={"file": ("relocate_me.stl", io.BytesIO(b"nas-bytes"), "application/octet-stream")},
  812. )
  813. assert upload.status_code == 200
  814. file_id = upload.json()["id"]
  815. ext_disk = external_dir / "relocate_me.stl"
  816. assert ext_disk.exists()
  817. response = await async_client.post(
  818. "/api/v1/library/files/move",
  819. json={"file_ids": [file_id], "folder_id": None},
  820. )
  821. assert response.status_code == 200
  822. assert response.json()["moved"] == 1
  823. db_session.expire_all()
  824. row = await db_session.get(LibraryFile, file_id)
  825. assert row.is_external is False
  826. assert row.folder_id is None
  827. assert not row.file_path.startswith("/"), "managed file_path must be relative"
  828. assert not ext_disk.exists(), "external source must be removed after the move"
  829. # Hash filled in for the now-managed row so future dedup works.
  830. assert row.file_hash is not None and len(row.file_hash) == 64
  831. @pytest.mark.asyncio
  832. @pytest.mark.integration
  833. async def test_managed_to_external_collision_skips_with_reason(
  834. self, async_client: AsyncClient, db_session, writable_folder, external_dir
  835. ):
  836. """A name collision on the target external mount must skip the
  837. move with a structured reason — not silently overwrite a file
  838. that's already on the NAS."""
  839. import io
  840. # Pre-existing file on the mount with the same name as the upload.
  841. (external_dir / "duplicate.stl").write_bytes(b"pre-existing")
  842. upload = await async_client.post(
  843. "/api/v1/library/files",
  844. files={"file": ("duplicate.stl", io.BytesIO(b"new-bytes"), "application/octet-stream")},
  845. )
  846. assert upload.status_code == 200
  847. file_id = upload.json()["id"]
  848. response = await async_client.post(
  849. "/api/v1/library/files/move",
  850. json={"file_ids": [file_id], "folder_id": writable_folder["id"]},
  851. )
  852. assert response.status_code == 200
  853. body = response.json()
  854. assert body["moved"] == 0
  855. assert body["skipped"] == 1
  856. reasons = body["skipped_reasons"]
  857. assert len(reasons) == 1
  858. assert reasons[0]["file_id"] == file_id
  859. assert reasons[0]["code"] == "name_collision"
  860. # Pre-existing target file is intact.
  861. assert (external_dir / "duplicate.stl").read_bytes() == b"pre-existing"
  862. @pytest.mark.asyncio
  863. @pytest.mark.integration
  864. async def test_external_readonly_source_skips(self, async_client: AsyncClient, db_session, readonly_folder):
  865. """A read-only mount allows reading but not deletes, and a move
  866. is semantically a delete on the source. Skip with
  867. ``source_readonly`` so the file isn't duplicated by half-moving."""
  868. listing = await async_client.get(f"/api/v1/library/files?folder_id={readonly_folder['id']}")
  869. assert listing.status_code == 200
  870. ext_file_id = listing.json()[0]["id"]
  871. response = await async_client.post(
  872. "/api/v1/library/files/move",
  873. json={"file_ids": [ext_file_id], "folder_id": None},
  874. )
  875. assert response.status_code == 200
  876. body = response.json()
  877. assert body["moved"] == 0
  878. assert body["skipped"] == 1
  879. assert body["skipped_reasons"][0]["code"] == "source_readonly"
  880. @pytest.mark.asyncio
  881. @pytest.mark.integration
  882. async def test_managed_to_managed_remains_db_only(self, async_client: AsyncClient, db_session):
  883. """Same-boundary moves (managed → managed) keep the existing
  884. DB-only fast path — no shutil.copy, no UUID rename. The original
  885. file_path stays the same, only ``folder_id`` changes."""
  886. import io
  887. from backend.app.models.library import LibraryFile
  888. sub = await async_client.post(
  889. "/api/v1/library/folders",
  890. json={"name": "subfolder", "parent_id": None},
  891. )
  892. assert sub.status_code == 200
  893. target_id = sub.json()["id"]
  894. upload = await async_client.post(
  895. "/api/v1/library/files",
  896. files={"file": ("part.stl", io.BytesIO(b"x"), "application/octet-stream")},
  897. )
  898. assert upload.status_code == 200
  899. file_id = upload.json()["id"]
  900. pre = await db_session.get(LibraryFile, file_id)
  901. await db_session.refresh(pre)
  902. original_path = pre.file_path
  903. response = await async_client.post(
  904. "/api/v1/library/files/move",
  905. json={"file_ids": [file_id], "folder_id": target_id},
  906. )
  907. assert response.status_code == 200
  908. assert response.json()["moved"] == 1
  909. db_session.expire_all()
  910. post = await db_session.get(LibraryFile, file_id)
  911. assert post.folder_id == target_id
  912. assert post.is_external is False
  913. assert post.file_path == original_path # bytes never moved
  914. @pytest.mark.asyncio
  915. @pytest.mark.integration
  916. async def test_skipped_reasons_field_present_even_when_empty(self, async_client: AsyncClient, db_session):
  917. """Backwards-compatible response shape: ``skipped_reasons`` is
  918. always present (empty list when nothing skipped) so frontend
  919. code can treat it as the source of truth without optional-chain
  920. gymnastics."""
  921. import io
  922. upload = await async_client.post(
  923. "/api/v1/library/files",
  924. files={"file": ("trivial.stl", io.BytesIO(b"x"), "application/octet-stream")},
  925. )
  926. assert upload.status_code == 200
  927. file_id = upload.json()["id"]
  928. response = await async_client.post(
  929. "/api/v1/library/files/move",
  930. json={"file_ids": [file_id], "folder_id": None},
  931. )
  932. assert response.status_code == 200
  933. body = response.json()
  934. assert "skipped_reasons" in body
  935. assert body["skipped_reasons"] == []