test_spoolman_inventory_api.py 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526
  1. """Integration tests for the Spoolman inventory proxy endpoints.
  2. These tests verify that /api/v1/spoolman/inventory/spools/* correctly
  3. translates between Spoolman's data model and Bambuddy's InventorySpool format.
  4. """
  5. from unittest.mock import AsyncMock, MagicMock, patch
  6. import pytest
  7. from fastapi import HTTPException
  8. from httpx import AsyncClient
  9. # ---------------------------------------------------------------------------
  10. # Shared fixtures
  11. # ---------------------------------------------------------------------------
  12. SAMPLE_SPOOLMAN_SPOOL = {
  13. "id": 42,
  14. "filament": {
  15. "id": 7,
  16. "name": "PLA Basic",
  17. "material": "PLA",
  18. "color_hex": "FF0000",
  19. "weight": 1000,
  20. "vendor": {"id": 3, "name": "Bambu Lab"},
  21. },
  22. "remaining_weight": 750.0,
  23. "used_weight": 250.0,
  24. "location": "Printer1 - AMS A1",
  25. "comment": "test note",
  26. "first_used": "2024-01-01T00:00:00+00:00",
  27. "last_used": "2024-02-01T00:00:00+00:00",
  28. "registered": "2024-01-01T00:00:00+00:00",
  29. "archived": False,
  30. "price": None,
  31. "extra": {"tag": '"AABBCCDDEEFF0011AABBCCDDEEFF0011"'},
  32. }
  33. @pytest.fixture
  34. async def spoolman_settings(db_session):
  35. """Create Spoolman settings in the database (enabled with URL)."""
  36. from backend.app.models.settings import Settings
  37. enabled_setting = Settings(key="spoolman_enabled", value="true")
  38. url_setting = Settings(key="spoolman_url", value="http://localhost:7912")
  39. db_session.add(enabled_setting)
  40. db_session.add(url_setting)
  41. await db_session.commit()
  42. return {"enabled": enabled_setting, "url": url_setting}
  43. @pytest.fixture
  44. def mock_spoolman_client():
  45. """Mock the Spoolman client with a sample spool."""
  46. mock_client = MagicMock()
  47. mock_client.base_url = "http://localhost:7912"
  48. mock_client.health_check = AsyncMock(return_value=True)
  49. mock_client.get_all_spools = AsyncMock(return_value=[SAMPLE_SPOOLMAN_SPOOL])
  50. mock_client.get_spool = AsyncMock(return_value=SAMPLE_SPOOLMAN_SPOOL)
  51. mock_client.create_spool = AsyncMock(return_value=SAMPLE_SPOOLMAN_SPOOL)
  52. mock_client.delete_spool = AsyncMock(return_value=True)
  53. mock_client.set_spool_archived = AsyncMock(
  54. side_effect=lambda spool_id, archived: {**SAMPLE_SPOOLMAN_SPOOL, "archived": archived}
  55. )
  56. mock_client.update_spool_full = AsyncMock(return_value=SAMPLE_SPOOLMAN_SPOOL)
  57. mock_client.merge_spool_extra = AsyncMock(return_value=SAMPLE_SPOOLMAN_SPOOL)
  58. mock_client.find_or_create_filament = AsyncMock(return_value=7)
  59. with (
  60. patch(
  61. "backend.app.api.routes.spoolman_inventory.get_spoolman_client",
  62. AsyncMock(return_value=mock_client),
  63. ),
  64. patch(
  65. "backend.app.api.routes.spoolman_inventory.init_spoolman_client",
  66. AsyncMock(return_value=mock_client),
  67. ),
  68. ):
  69. yield mock_client
  70. class TestSpoolmanInventoryMapping:
  71. """Tests for the Spoolman → InventorySpool data mapping."""
  72. @pytest.mark.asyncio
  73. @pytest.mark.integration
  74. async def test_list_spools_returns_inventory_format(
  75. self,
  76. async_client: AsyncClient,
  77. spoolman_settings,
  78. mock_spoolman_client,
  79. ):
  80. """GET /spoolman/inventory/spools returns spools in InventorySpool format."""
  81. response = await async_client.get("/api/v1/spoolman/inventory/spools")
  82. assert response.status_code == 200
  83. spools = response.json()
  84. assert isinstance(spools, list)
  85. assert len(spools) == 1
  86. spool = spools[0]
  87. assert spool["id"] == 42
  88. assert spool["material"] == "PLA"
  89. assert spool["subtype"] == "Basic"
  90. assert spool["brand"] == "Bambu Lab"
  91. assert spool["label_weight"] == 1000
  92. assert spool["weight_used"] == 250.0
  93. assert spool["note"] == "test note"
  94. assert spool["data_origin"] == "spoolman"
  95. assert spool["tag_type"] == "spoolman"
  96. # RRGGBB + FF alpha
  97. assert spool["rgba"] == "FF0000FF"
  98. # Spoolman location mapped to storage_location
  99. assert spool["storage_location"] == "Printer1 - AMS A1"
  100. # RFID tag: 32-char → tray_uuid
  101. assert spool["tray_uuid"] == "AABBCCDDEEFF0011AABBCCDDEEFF0011"
  102. assert spool["tag_uid"] is None
  103. @pytest.mark.asyncio
  104. @pytest.mark.integration
  105. async def test_get_single_spool(
  106. self,
  107. async_client: AsyncClient,
  108. spoolman_settings,
  109. mock_spoolman_client,
  110. ):
  111. """GET /spoolman/inventory/spools/{id} returns a single spool."""
  112. response = await async_client.get("/api/v1/spoolman/inventory/spools/42")
  113. assert response.status_code == 200
  114. spool = response.json()
  115. assert spool["id"] == 42
  116. assert spool["material"] == "PLA"
  117. @pytest.mark.asyncio
  118. @pytest.mark.integration
  119. async def test_list_includes_archived_when_requested(
  120. self,
  121. async_client: AsyncClient,
  122. spoolman_settings,
  123. mock_spoolman_client,
  124. ):
  125. """GET /spoolman/inventory/spools?include_archived=true calls Spoolman with allow_archived."""
  126. await async_client.get("/api/v1/spoolman/inventory/spools?include_archived=true")
  127. mock_spoolman_client.get_all_spools.assert_called_once_with(allow_archived=True)
  128. @pytest.mark.asyncio
  129. @pytest.mark.integration
  130. async def test_archived_spool_has_archived_at(
  131. self,
  132. async_client: AsyncClient,
  133. spoolman_settings,
  134. mock_spoolman_client,
  135. ):
  136. """An archived Spoolman spool maps to archived_at != None."""
  137. archived_spool = {
  138. **SAMPLE_SPOOLMAN_SPOOL,
  139. "archived": True,
  140. }
  141. mock_spoolman_client.get_all_spools.return_value = [archived_spool]
  142. response = await async_client.get("/api/v1/spoolman/inventory/spools?include_archived=true")
  143. spool = response.json()[0]
  144. assert spool["archived_at"] is not None
  145. @pytest.mark.asyncio
  146. @pytest.mark.integration
  147. async def test_malformed_spool_skipped_in_list(
  148. self,
  149. async_client: AsyncClient,
  150. spoolman_settings,
  151. mock_spoolman_client,
  152. ):
  153. """A spool with an invalid id (e.g. 0) is silently skipped; others still appear."""
  154. bad_spool = {**SAMPLE_SPOOLMAN_SPOOL, "id": 0}
  155. mock_spoolman_client.get_all_spools.return_value = [bad_spool, SAMPLE_SPOOLMAN_SPOOL]
  156. response = await async_client.get("/api/v1/spoolman/inventory/spools")
  157. assert response.status_code == 200
  158. spools = response.json()
  159. # bad_spool is dropped; the valid one survives
  160. assert len(spools) == 1
  161. assert spools[0]["id"] == 42
  162. @pytest.mark.asyncio
  163. @pytest.mark.integration
  164. async def test_list_spools_returns_503_when_spoolman_unavailable(
  165. self,
  166. async_client: AsyncClient,
  167. spoolman_settings,
  168. mock_spoolman_client,
  169. ):
  170. """GET /spoolman/inventory/spools returns 503 when Spoolman is unreachable (H10)."""
  171. from backend.app.services.spoolman import SpoolmanUnavailableError
  172. mock_spoolman_client.get_all_spools.side_effect = SpoolmanUnavailableError("down")
  173. response = await async_client.get("/api/v1/spoolman/inventory/spools")
  174. assert response.status_code == 503
  175. @pytest.mark.asyncio
  176. @pytest.mark.integration
  177. async def test_tag_uid_16char_maps_correctly(
  178. self,
  179. async_client: AsyncClient,
  180. spoolman_settings,
  181. mock_spoolman_client,
  182. ):
  183. """A 16-char tag maps to tag_uid, not tray_uuid."""
  184. spool_with_short_tag = {
  185. **SAMPLE_SPOOLMAN_SPOOL,
  186. "extra": {"tag": '"AABBCCDDEEFF0011"'},
  187. }
  188. mock_spoolman_client.get_all_spools.return_value = [spool_with_short_tag]
  189. response = await async_client.get("/api/v1/spoolman/inventory/spools")
  190. spool = response.json()[0]
  191. assert spool["tag_uid"] == "AABBCCDDEEFF0011"
  192. assert spool["tray_uuid"] is None
  193. class TestSpoolmanInventoryCRUD:
  194. """Tests for create, update, delete, archive, restore operations."""
  195. @pytest.mark.asyncio
  196. @pytest.mark.integration
  197. async def test_not_enabled_returns_400(self, async_client: AsyncClient):
  198. """All endpoints return 400 when Spoolman is not enabled."""
  199. response = await async_client.get("/api/v1/spoolman/inventory/spools")
  200. assert response.status_code == 400
  201. assert "not enabled" in response.json()["detail"].lower()
  202. @pytest.mark.asyncio
  203. @pytest.mark.integration
  204. async def test_create_spool(
  205. self,
  206. async_client: AsyncClient,
  207. spoolman_settings,
  208. mock_spoolman_client,
  209. ):
  210. """POST /spoolman/inventory/spools creates a spool via Spoolman."""
  211. payload = {
  212. "material": "PLA",
  213. "subtype": "Basic",
  214. "brand": "Bambu Lab",
  215. "rgba": "FF0000FF",
  216. "label_weight": 1000,
  217. "weight_used": 0,
  218. }
  219. response = await async_client.post("/api/v1/spoolman/inventory/spools", json=payload)
  220. assert response.status_code == 200
  221. mock_spoolman_client.find_or_create_filament.assert_called_once()
  222. mock_spoolman_client.create_spool.assert_called_once()
  223. data = response.json()
  224. assert data["material"] == "PLA"
  225. @pytest.mark.asyncio
  226. @pytest.mark.integration
  227. async def test_bulk_create_spools(
  228. self,
  229. async_client: AsyncClient,
  230. spoolman_settings,
  231. mock_spoolman_client,
  232. ):
  233. """POST /spoolman/inventory/spools/bulk creates multiple spools."""
  234. payload = {
  235. "spool": {"material": "PETG", "label_weight": 1000, "weight_used": 0},
  236. "quantity": 3,
  237. }
  238. response = await async_client.post("/api/v1/spoolman/inventory/spools/bulk", json=payload)
  239. assert response.status_code == 200
  240. assert mock_spoolman_client.create_spool.call_count == 3
  241. @pytest.mark.asyncio
  242. @pytest.mark.integration
  243. async def test_bulk_create_quantity_out_of_range_returns_422(
  244. self,
  245. async_client: AsyncClient,
  246. spoolman_settings,
  247. mock_spoolman_client,
  248. ):
  249. """Bulk create quantity outside 1-50 is rejected with 422 (not silently clamped)."""
  250. payload = {
  251. "spool": {"material": "ABS", "label_weight": 1000, "weight_used": 0},
  252. "quantity": 999,
  253. }
  254. response = await async_client.post("/api/v1/spoolman/inventory/spools/bulk", json=payload)
  255. assert response.status_code == 422
  256. @pytest.mark.asyncio
  257. @pytest.mark.integration
  258. async def test_bulk_create_quantity_zero_returns_422(
  259. self,
  260. async_client: AsyncClient,
  261. spoolman_settings,
  262. mock_spoolman_client,
  263. ):
  264. """Bulk create quantity of 0 is rejected with 422."""
  265. payload = {
  266. "spool": {"material": "ABS", "label_weight": 1000, "weight_used": 0},
  267. "quantity": 0,
  268. }
  269. response = await async_client.post("/api/v1/spoolman/inventory/spools/bulk", json=payload)
  270. assert response.status_code == 422
  271. @pytest.mark.asyncio
  272. @pytest.mark.integration
  273. async def test_update_spool(
  274. self,
  275. async_client: AsyncClient,
  276. spoolman_settings,
  277. mock_spoolman_client,
  278. ):
  279. """PATCH /spoolman/inventory/spools/{id} updates a spool."""
  280. payload = {"note": "updated note", "weight_used": 100.0}
  281. response = await async_client.patch("/api/v1/spoolman/inventory/spools/42", json=payload)
  282. assert response.status_code == 200
  283. mock_spoolman_client.update_spool_full.assert_called_once()
  284. @pytest.mark.asyncio
  285. @pytest.mark.integration
  286. async def test_update_spool_not_found(
  287. self,
  288. async_client: AsyncClient,
  289. spoolman_settings,
  290. mock_spoolman_client,
  291. ):
  292. """PATCH returns 404 when Spoolman spool does not exist."""
  293. from backend.app.services.spoolman import SpoolmanNotFoundError
  294. mock_spoolman_client.get_spool.side_effect = SpoolmanNotFoundError("spool not found")
  295. response = await async_client.patch("/api/v1/spoolman/inventory/spools/999", json={"note": "x"})
  296. assert response.status_code == 404
  297. @pytest.mark.asyncio
  298. @pytest.mark.integration
  299. async def test_delete_spool(
  300. self,
  301. async_client: AsyncClient,
  302. spoolman_settings,
  303. mock_spoolman_client,
  304. ):
  305. """DELETE /spoolman/inventory/spools/{id} deletes a spool."""
  306. response = await async_client.delete("/api/v1/spoolman/inventory/spools/42")
  307. assert response.status_code == 200
  308. assert response.json()["status"] == "deleted"
  309. mock_spoolman_client.delete_spool.assert_called_once_with(42)
  310. @pytest.mark.asyncio
  311. @pytest.mark.integration
  312. async def test_delete_spool_failure(
  313. self,
  314. async_client: AsyncClient,
  315. spoolman_settings,
  316. mock_spoolman_client,
  317. ):
  318. """DELETE returns 503 when Spoolman is unreachable."""
  319. from backend.app.services.spoolman import SpoolmanUnavailableError
  320. mock_spoolman_client.delete_spool.side_effect = SpoolmanUnavailableError("unreachable")
  321. response = await async_client.delete("/api/v1/spoolman/inventory/spools/42")
  322. assert response.status_code == 503
  323. @pytest.mark.asyncio
  324. @pytest.mark.integration
  325. async def test_delete_spool_not_found(
  326. self,
  327. async_client: AsyncClient,
  328. spoolman_settings,
  329. mock_spoolman_client,
  330. ):
  331. """DELETE returns 404 when Spoolman reports the spool does not exist."""
  332. from backend.app.services.spoolman import SpoolmanNotFoundError
  333. mock_spoolman_client.delete_spool.side_effect = SpoolmanNotFoundError("gone")
  334. response = await async_client.delete("/api/v1/spoolman/inventory/spools/42")
  335. assert response.status_code == 404
  336. @pytest.mark.asyncio
  337. @pytest.mark.integration
  338. async def test_archive_spool_not_found(
  339. self,
  340. async_client: AsyncClient,
  341. spoolman_settings,
  342. mock_spoolman_client,
  343. ):
  344. """POST /archive returns 404 when Spoolman reports the spool does not exist."""
  345. from backend.app.services.spoolman import SpoolmanNotFoundError
  346. mock_spoolman_client.set_spool_archived.side_effect = SpoolmanNotFoundError("gone")
  347. response = await async_client.post("/api/v1/spoolman/inventory/spools/42/archive")
  348. assert response.status_code == 404
  349. @pytest.mark.asyncio
  350. @pytest.mark.integration
  351. async def test_restore_spool_not_found(
  352. self,
  353. async_client: AsyncClient,
  354. spoolman_settings,
  355. mock_spoolman_client,
  356. ):
  357. """POST /restore returns 404 when Spoolman reports the spool does not exist."""
  358. from backend.app.services.spoolman import SpoolmanNotFoundError
  359. mock_spoolman_client.set_spool_archived.side_effect = SpoolmanNotFoundError("gone")
  360. response = await async_client.post("/api/v1/spoolman/inventory/spools/42/restore")
  361. assert response.status_code == 404
  362. @pytest.mark.asyncio
  363. @pytest.mark.integration
  364. async def test_archive_spool(
  365. self,
  366. async_client: AsyncClient,
  367. spoolman_settings,
  368. mock_spoolman_client,
  369. ):
  370. """POST /spoolman/inventory/spools/{id}/archive archives a spool."""
  371. response = await async_client.post("/api/v1/spoolman/inventory/spools/42/archive")
  372. assert response.status_code == 200
  373. mock_spoolman_client.set_spool_archived.assert_called_once_with(42, archived=True)
  374. @pytest.mark.asyncio
  375. @pytest.mark.integration
  376. async def test_restore_spool(
  377. self,
  378. async_client: AsyncClient,
  379. spoolman_settings,
  380. mock_spoolman_client,
  381. ):
  382. """POST /spoolman/inventory/spools/{id}/restore restores an archived spool."""
  383. response = await async_client.post("/api/v1/spoolman/inventory/spools/42/restore")
  384. assert response.status_code == 200
  385. mock_spoolman_client.set_spool_archived.assert_called_once_with(42, archived=False)
  386. @pytest.mark.asyncio
  387. @pytest.mark.integration
  388. async def test_sync_weight(
  389. self,
  390. async_client: AsyncClient,
  391. spoolman_settings,
  392. mock_spoolman_client,
  393. ):
  394. """PATCH /spoolman/inventory/spools/{id}/weight updates remaining weight."""
  395. payload = {"weight_grams": 850.0}
  396. response = await async_client.patch("/api/v1/spoolman/inventory/spools/42/weight", json=payload)
  397. assert response.status_code == 200
  398. result = response.json()
  399. assert result["status"] == "ok"
  400. # remaining = 850 - 250 core = 600; weight_used = 1000 - 600 = 400
  401. assert result["weight_used"] == 400.0
  402. mock_spoolman_client.update_spool_full.assert_called_once_with(spool_id=42, remaining_weight=600.0)
  403. @pytest.mark.asyncio
  404. @pytest.mark.integration
  405. async def test_update_spool_returns_404_on_not_found(
  406. self,
  407. async_client: AsyncClient,
  408. spoolman_settings,
  409. mock_spoolman_client,
  410. ):
  411. """PATCH returns 404 when update_spool_full raises SpoolmanNotFoundError (I2)."""
  412. from backend.app.services.spoolman import SpoolmanNotFoundError
  413. mock_spoolman_client.update_spool_full.side_effect = SpoolmanNotFoundError("gone")
  414. response = await async_client.patch("/api/v1/spoolman/inventory/spools/42", json={"note": "x"})
  415. assert response.status_code == 404
  416. @pytest.mark.asyncio
  417. @pytest.mark.integration
  418. async def test_update_spool_returns_503_on_unavailable(
  419. self,
  420. async_client: AsyncClient,
  421. spoolman_settings,
  422. mock_spoolman_client,
  423. ):
  424. """PATCH returns 503 when update_spool_full raises SpoolmanUnavailableError (I2)."""
  425. from backend.app.services.spoolman import SpoolmanUnavailableError
  426. mock_spoolman_client.update_spool_full.side_effect = SpoolmanUnavailableError("down")
  427. response = await async_client.patch("/api/v1/spoolman/inventory/spools/42", json={"note": "x"})
  428. assert response.status_code == 503
  429. @pytest.mark.asyncio
  430. @pytest.mark.integration
  431. async def test_sync_weight_returns_404_on_not_found(
  432. self,
  433. async_client: AsyncClient,
  434. spoolman_settings,
  435. mock_spoolman_client,
  436. ):
  437. """PATCH /weight returns 404 when update_spool_full raises SpoolmanNotFoundError (I2)."""
  438. from backend.app.services.spoolman import SpoolmanNotFoundError
  439. mock_spoolman_client.update_spool_full.side_effect = SpoolmanNotFoundError("gone")
  440. response = await async_client.patch("/api/v1/spoolman/inventory/spools/42/weight", json={"weight_grams": 500.0})
  441. assert response.status_code == 404
  442. @pytest.mark.asyncio
  443. @pytest.mark.integration
  444. async def test_sync_weight_returns_503_on_unavailable(
  445. self,
  446. async_client: AsyncClient,
  447. spoolman_settings,
  448. mock_spoolman_client,
  449. ):
  450. """PATCH /weight returns 503 when update_spool_full raises SpoolmanUnavailableError (I2)."""
  451. from backend.app.services.spoolman import SpoolmanUnavailableError
  452. mock_spoolman_client.update_spool_full.side_effect = SpoolmanUnavailableError("down")
  453. response = await async_client.patch("/api/v1/spoolman/inventory/spools/42/weight", json={"weight_grams": 500.0})
  454. assert response.status_code == 503
  455. class TestSpoolmanInventorySlicerFilament:
  456. """slicer_filament persistence via Spoolman extra dict.
  457. Spoolman has no native slicer_filament field — Bambuddy persists the
  458. BambuStudio preset under bambu_slicer_filament[_name] keys in the
  459. spool's extra dict and unwraps them in _map_spoolman_spool. Without
  460. this round-trip the user's slicer-preset selection on the spool form
  461. is silently dropped (#1114).
  462. """
  463. @pytest.mark.asyncio
  464. @pytest.mark.integration
  465. async def test_update_persists_slicer_filament_to_extra(
  466. self,
  467. async_client: AsyncClient,
  468. spoolman_settings,
  469. mock_spoolman_client,
  470. ):
  471. """PATCH with slicer_filament writes bambu_slicer_filament to extra.
  472. Spoolman's PATCH MERGES extra keys, so we send via merge_spool_extra
  473. not update_spool_full. Values are JSON-encoded strings.
  474. """
  475. import json as _json
  476. mock_spoolman_client.ensure_extra_field = AsyncMock(return_value=True)
  477. response = await async_client.patch(
  478. "/api/v1/spoolman/inventory/spools/42",
  479. json={
  480. "slicer_filament": "PFUSf543b298f8ea66",
  481. "slicer_filament_name": "Devil Design PLA Basic @Bambu Lab H2D 0.4 nozzle (Custom)",
  482. },
  483. )
  484. assert response.status_code == 200
  485. # Field registration is idempotent — must be called for each key
  486. ensure_calls = [c.args[0] for c in mock_spoolman_client.ensure_extra_field.call_args_list]
  487. assert "bambu_slicer_filament" in ensure_calls
  488. assert "bambu_slicer_filament_name" in ensure_calls
  489. # Values must be JSON-encoded so read-side can json.loads + .strip('"')
  490. mock_spoolman_client.merge_spool_extra.assert_called_once_with(
  491. 42,
  492. {
  493. "bambu_slicer_filament": _json.dumps("PFUSf543b298f8ea66"),
  494. "bambu_slicer_filament_name": _json.dumps("Devil Design PLA Basic @Bambu Lab H2D 0.4 nozzle (Custom)"),
  495. },
  496. )
  497. @pytest.mark.asyncio
  498. @pytest.mark.integration
  499. async def test_update_without_slicer_filament_skips_merge(
  500. self,
  501. async_client: AsyncClient,
  502. spoolman_settings,
  503. mock_spoolman_client,
  504. ):
  505. """PATCH without slicer_filament fields must not call merge_spool_extra.
  506. Avoids overwriting an existing preset with empty/null when the user
  507. just changed an unrelated field (e.g. note, weight).
  508. """
  509. response = await async_client.patch(
  510. "/api/v1/spoolman/inventory/spools/42",
  511. json={"note": "just changing the note"},
  512. )
  513. assert response.status_code == 200
  514. mock_spoolman_client.merge_spool_extra.assert_not_called()
  515. @pytest.mark.asyncio
  516. @pytest.mark.integration
  517. async def test_update_clears_slicer_filament_with_empty_string(
  518. self,
  519. async_client: AsyncClient,
  520. spoolman_settings,
  521. mock_spoolman_client,
  522. ):
  523. """Empty-string slicer_filament writes the JSON-encoded "" sentinel.
  524. The read-side strip('"') resolves it to an empty string and falls
  525. back to filament.name — matches the user-facing "clear preset" flow.
  526. """
  527. import json as _json
  528. mock_spoolman_client.ensure_extra_field = AsyncMock(return_value=True)
  529. response = await async_client.patch(
  530. "/api/v1/spoolman/inventory/spools/42",
  531. json={"slicer_filament": "", "slicer_filament_name": ""},
  532. )
  533. assert response.status_code == 200
  534. mock_spoolman_client.merge_spool_extra.assert_called_once_with(
  535. 42,
  536. {
  537. "bambu_slicer_filament": _json.dumps(""),
  538. "bambu_slicer_filament_name": _json.dumps(""),
  539. },
  540. )
  541. class TestSpoolmanInventoryCostPerKg:
  542. """Tests for the two-step cost_per_kg create path (PT-C2)."""
  543. @pytest.mark.asyncio
  544. @pytest.mark.integration
  545. async def test_create_spool_with_cost_per_kg_calls_price_update(
  546. self, async_client: AsyncClient, spoolman_settings, mock_spoolman_client
  547. ):
  548. """POST with cost_per_kg calls update_spool_full with price= after creation."""
  549. from unittest.mock import AsyncMock
  550. mock_spoolman_client.update_spool_full = AsyncMock(return_value=SAMPLE_SPOOLMAN_SPOOL)
  551. payload = {
  552. "material": "PLA",
  553. "brand": "Bambu Lab",
  554. "label_weight": 1000,
  555. "cost_per_kg": 24.99,
  556. }
  557. resp = await async_client.post("/api/v1/spoolman/inventory/spools", json=payload)
  558. assert resp.status_code == 200
  559. # update_spool_full must have been called with price=24.99
  560. calls = [
  561. c
  562. for c in mock_spoolman_client.update_spool_full.call_args_list
  563. if c.kwargs.get("price") == 24.99 or (c.args and 24.99 in c.args)
  564. ]
  565. assert len(calls) >= 1
  566. @pytest.mark.asyncio
  567. @pytest.mark.integration
  568. async def test_create_spool_without_cost_per_kg_skips_price_update(
  569. self, async_client: AsyncClient, spoolman_settings, mock_spoolman_client
  570. ):
  571. """POST without cost_per_kg does not call update_spool_full."""
  572. from unittest.mock import AsyncMock
  573. mock_spoolman_client.update_spool_full = AsyncMock(return_value=SAMPLE_SPOOLMAN_SPOOL)
  574. payload = {"material": "PLA", "brand": "Bambu Lab", "label_weight": 1000}
  575. resp = await async_client.post("/api/v1/spoolman/inventory/spools", json=payload)
  576. assert resp.status_code == 200
  577. mock_spoolman_client.update_spool_full.assert_not_called()
  578. class TestSpoolmanInventoryInputValidation:
  579. """Tests for input validation added as security hardening."""
  580. @pytest.mark.asyncio
  581. @pytest.mark.integration
  582. async def test_create_rejects_material_too_long(
  583. self,
  584. async_client: AsyncClient,
  585. spoolman_settings,
  586. mock_spoolman_client,
  587. ):
  588. """material longer than 64 chars is rejected with 422."""
  589. payload = {"material": "A" * 65, "label_weight": 1000, "weight_used": 0}
  590. response = await async_client.post("/api/v1/spoolman/inventory/spools", json=payload)
  591. assert response.status_code == 422
  592. @pytest.mark.asyncio
  593. @pytest.mark.integration
  594. async def test_create_rejects_note_too_long(
  595. self,
  596. async_client: AsyncClient,
  597. spoolman_settings,
  598. mock_spoolman_client,
  599. ):
  600. """note longer than 1000 chars is rejected with 422."""
  601. payload = {
  602. "material": "PLA",
  603. "label_weight": 1000,
  604. "weight_used": 0,
  605. "note": "x" * 1001,
  606. }
  607. response = await async_client.post("/api/v1/spoolman/inventory/spools", json=payload)
  608. assert response.status_code == 422
  609. @pytest.mark.asyncio
  610. @pytest.mark.integration
  611. async def test_create_rejects_negative_weight_used(
  612. self,
  613. async_client: AsyncClient,
  614. spoolman_settings,
  615. mock_spoolman_client,
  616. ):
  617. """Negative weight_used is rejected with 422."""
  618. payload = {"material": "PLA", "label_weight": 1000, "weight_used": -1.0}
  619. response = await async_client.post("/api/v1/spoolman/inventory/spools", json=payload)
  620. assert response.status_code == 422
  621. @pytest.mark.asyncio
  622. @pytest.mark.integration
  623. async def test_create_rejects_zero_label_weight(
  624. self,
  625. async_client: AsyncClient,
  626. spoolman_settings,
  627. mock_spoolman_client,
  628. ):
  629. """label_weight of 0 is rejected (minimum is 1)."""
  630. payload = {"material": "PLA", "label_weight": 0, "weight_used": 0}
  631. response = await async_client.post("/api/v1/spoolman/inventory/spools", json=payload)
  632. assert response.status_code == 422
  633. @pytest.mark.asyncio
  634. @pytest.mark.integration
  635. async def test_create_rejects_invalid_rgba(
  636. self,
  637. async_client: AsyncClient,
  638. spoolman_settings,
  639. mock_spoolman_client,
  640. ):
  641. """Non-hex rgba string is rejected with 422."""
  642. payload = {"material": "PLA", "label_weight": 1000, "weight_used": 0, "rgba": "GGGGGGFF"}
  643. response = await async_client.post("/api/v1/spoolman/inventory/spools", json=payload)
  644. assert response.status_code == 422
  645. @pytest.mark.asyncio
  646. @pytest.mark.integration
  647. async def test_create_accepts_valid_6char_rgba(
  648. self,
  649. async_client: AsyncClient,
  650. spoolman_settings,
  651. mock_spoolman_client,
  652. ):
  653. """A valid 6-char hex rgba is accepted."""
  654. payload = {"material": "PLA", "label_weight": 1000, "weight_used": 0, "rgba": "FF0000"}
  655. response = await async_client.post("/api/v1/spoolman/inventory/spools", json=payload)
  656. assert response.status_code == 200
  657. @pytest.mark.asyncio
  658. @pytest.mark.integration
  659. async def test_weight_update_rejects_negative_grams(
  660. self,
  661. async_client: AsyncClient,
  662. spoolman_settings,
  663. mock_spoolman_client,
  664. ):
  665. """Negative weight_grams on weight sync endpoint is rejected with 422."""
  666. response = await async_client.patch(
  667. "/api/v1/spoolman/inventory/spools/42/weight",
  668. json={"weight_grams": -50.0},
  669. )
  670. assert response.status_code == 422
  671. @pytest.mark.asyncio
  672. @pytest.mark.integration
  673. async def test_update_rejects_tag_uid_too_long(
  674. self,
  675. async_client: AsyncClient,
  676. spoolman_settings,
  677. mock_spoolman_client,
  678. ):
  679. """tag_uid longer than 30 chars is rejected with 422 (NFC UID max 10 bytes = 20 hex chars, capped at 30)."""
  680. payload = {"tag_uid": "A" * 65}
  681. response = await async_client.patch("/api/v1/spoolman/inventory/spools/42", json=payload)
  682. assert response.status_code == 422
  683. @pytest.mark.asyncio
  684. @pytest.mark.integration
  685. async def test_update_rejects_tray_uuid_too_long(
  686. self,
  687. async_client: AsyncClient,
  688. spoolman_settings,
  689. mock_spoolman_client,
  690. ):
  691. """tray_uuid longer than 32 chars is rejected with 422."""
  692. payload = {"tray_uuid": "B" * 65}
  693. response = await async_client.patch("/api/v1/spoolman/inventory/spools/42", json=payload)
  694. assert response.status_code == 422
  695. @pytest.mark.asyncio
  696. @pytest.mark.integration
  697. @pytest.mark.parametrize("uuid_len", [16, 31])
  698. async def test_update_rejects_tray_uuid_too_short(
  699. self,
  700. async_client: AsyncClient,
  701. spoolman_settings,
  702. mock_spoolman_client,
  703. uuid_len: int,
  704. ):
  705. """tray_uuid shorter than 32 chars is rejected (min_length=max_length=32)."""
  706. payload = {"tray_uuid": "A" * uuid_len}
  707. response = await async_client.patch("/api/v1/spoolman/inventory/spools/42", json=payload)
  708. assert response.status_code == 422
  709. @pytest.mark.asyncio
  710. @pytest.mark.integration
  711. async def test_update_rejects_rgba_nine_chars(
  712. self,
  713. async_client: AsyncClient,
  714. spoolman_settings,
  715. mock_spoolman_client,
  716. ):
  717. """rgba must be max 8 hex chars; 9-char value is rejected with 422."""
  718. payload = {"rgba": "FF0000FFA"} # 9 chars
  719. response = await async_client.patch("/api/v1/spoolman/inventory/spools/42", json=payload)
  720. assert response.status_code == 422
  721. @pytest.mark.asyncio
  722. @pytest.mark.integration
  723. async def test_tag_uid_below_min_length_rejected(
  724. self,
  725. async_client: AsyncClient,
  726. spoolman_settings,
  727. mock_spoolman_client,
  728. ):
  729. """tag_uid shorter than 8 hex chars is rejected with 422 (PT-I5)."""
  730. payload = {"tag_uid": "AABBCC"} # 6 chars, below min_length=8
  731. resp = await async_client.patch("/api/v1/spoolman/inventory/spools/42", json=payload)
  732. assert resp.status_code == 422
  733. @pytest.mark.asyncio
  734. @pytest.mark.integration
  735. async def test_invalid_spoolman_url_scheme_returns_400(
  736. self,
  737. async_client: AsyncClient,
  738. db_session,
  739. mock_spoolman_client,
  740. ):
  741. """A spoolman_url with a non-http(s) scheme is rejected."""
  742. from backend.app.models.settings import Settings
  743. db_session.add(Settings(key="spoolman_enabled", value="true"))
  744. db_session.add(Settings(key="spoolman_url", value="ftp://evil.internal/"))
  745. await db_session.commit()
  746. response = await async_client.get("/api/v1/spoolman/inventory/spools")
  747. assert response.status_code == 400
  748. assert "http" in response.json()["detail"].lower()
  749. @pytest.mark.asyncio
  750. @pytest.mark.integration
  751. @pytest.mark.parametrize(
  752. "evil_url",
  753. [
  754. "file:///etc/passwd",
  755. "gopher://127.0.0.1:70/",
  756. "dict://internal.corp/",
  757. "javascript:alert(1)",
  758. "http://169.254.169.254/latest/meta-data/", # AWS IMDS
  759. "http://100.100.100.200/", # Alibaba Cloud metadata
  760. "http://[fd00:ec2::254]/", # AWS IMDS IPv6
  761. "http://0.0.0.0/", # unspecified
  762. "http://224.0.0.1/", # IPv4 multicast
  763. "http://[ff02::1]/", # IPv6 multicast
  764. "http://[::ffff:169.254.169.254]/", # IPv4-mapped IPv6 IMDS bypass
  765. "http://2130706433/", # decimal-encoded 127.0.0.1
  766. "http://0x7f000001/", # hex-encoded 127.0.0.1
  767. ],
  768. )
  769. async def test_ssrf_blocked_schemes_and_addresses(
  770. self,
  771. async_client: AsyncClient,
  772. db_session,
  773. mock_spoolman_client,
  774. evil_url: str,
  775. ):
  776. """SSRF: dangerous schemes, cloud metadata IPs, multicast, unspecified,
  777. and numeric-encoded IPs must be rejected with 400. Loopback and
  778. RFC-1918 private ranges are allowed — they are legitimate Spoolman
  779. topologies for self-hosted Bambuddy deployments."""
  780. from backend.app.models.settings import Settings
  781. db_session.add(Settings(key="spoolman_enabled", value="true"))
  782. db_session.add(Settings(key="spoolman_url", value=evil_url))
  783. await db_session.commit()
  784. response = await async_client.get("/api/v1/spoolman/inventory/spools")
  785. assert response.status_code == 400, (
  786. f"Expected 400 for SSRF URL {evil_url!r} but got {response.status_code}: {response.json()}"
  787. )
  788. @pytest.mark.asyncio
  789. @pytest.mark.integration
  790. @pytest.mark.parametrize(
  791. "lan_url",
  792. [
  793. "http://127.0.0.1:7912/", # loopback
  794. "http://[::1]:7912/", # IPv6 loopback
  795. "http://192.168.1.50:7912/", # RFC-1918 /16
  796. "http://10.0.0.5:7912/", # RFC-1918 /8
  797. "http://172.20.0.3:7912/", # RFC-1918 /12
  798. ],
  799. )
  800. async def test_ssrf_allows_lan_spoolman_topologies(
  801. self,
  802. async_client: AsyncClient,
  803. db_session,
  804. mock_spoolman_client,
  805. lan_url: str,
  806. ):
  807. """Regression: Bambuddy's normal deployment is LAN-local Spoolman.
  808. Loopback and RFC-1918 private addresses must NOT be rejected as SSRF."""
  809. from backend.app.models.settings import Settings
  810. db_session.add(Settings(key="spoolman_enabled", value="true"))
  811. db_session.add(Settings(key="spoolman_url", value=lan_url))
  812. await db_session.commit()
  813. response = await async_client.get("/api/v1/spoolman/inventory/spools")
  814. assert response.status_code != 400, f"LAN URL {lan_url!r} was incorrectly blocked as SSRF: {response.json()}"
  815. @pytest.mark.asyncio
  816. @pytest.mark.integration
  817. async def test_create_rejects_storage_location_too_long(
  818. self,
  819. async_client: AsyncClient,
  820. spoolman_settings,
  821. mock_spoolman_client,
  822. ):
  823. """storage_location longer than 255 chars is rejected with 422."""
  824. payload = {
  825. "material": "PLA",
  826. "label_weight": 1000,
  827. "weight_used": 0,
  828. "storage_location": "x" * 256,
  829. }
  830. response = await async_client.post("/api/v1/spoolman/inventory/spools", json=payload)
  831. assert response.status_code == 422
  832. @pytest.mark.asyncio
  833. @pytest.mark.integration
  834. async def test_update_rejects_storage_location_too_long(
  835. self,
  836. async_client: AsyncClient,
  837. spoolman_settings,
  838. mock_spoolman_client,
  839. ):
  840. """storage_location longer than 255 chars on PATCH is rejected with 422."""
  841. payload = {"storage_location": "y" * 256}
  842. response = await async_client.patch("/api/v1/spoolman/inventory/spools/42", json=payload)
  843. assert response.status_code == 422
  844. class TestStorageLocationPassthrough:
  845. """Tests that storage_location is correctly passed to and from Spoolman."""
  846. @pytest.mark.asyncio
  847. @pytest.mark.integration
  848. async def test_list_spools_maps_spoolman_location_to_storage_location(
  849. self,
  850. async_client: AsyncClient,
  851. spoolman_settings,
  852. mock_spoolman_client,
  853. ):
  854. """Spoolman's location field is exposed as storage_location in the response."""
  855. response = await async_client.get("/api/v1/spoolman/inventory/spools")
  856. spool = response.json()[0]
  857. assert spool["storage_location"] == "Printer1 - AMS A1"
  858. @pytest.mark.asyncio
  859. @pytest.mark.integration
  860. async def test_list_spools_null_location_gives_null_storage_location(
  861. self,
  862. async_client: AsyncClient,
  863. spoolman_settings,
  864. mock_spoolman_client,
  865. ):
  866. """A Spoolman spool with no location gives null storage_location."""
  867. spool_no_loc = {**SAMPLE_SPOOLMAN_SPOOL, "location": None}
  868. mock_spoolman_client.get_all_spools.return_value = [spool_no_loc]
  869. response = await async_client.get("/api/v1/spoolman/inventory/spools")
  870. spool = response.json()[0]
  871. assert spool["storage_location"] is None
  872. @pytest.mark.asyncio
  873. @pytest.mark.integration
  874. async def test_create_passes_storage_location_to_spoolman(
  875. self,
  876. async_client: AsyncClient,
  877. spoolman_settings,
  878. mock_spoolman_client,
  879. ):
  880. """storage_location is forwarded as location when creating a Spoolman spool."""
  881. payload = {
  882. "material": "PLA",
  883. "label_weight": 1000,
  884. "weight_used": 0,
  885. "storage_location": "Shelf B",
  886. }
  887. response = await async_client.post("/api/v1/spoolman/inventory/spools", json=payload)
  888. assert response.status_code == 200
  889. mock_spoolman_client.create_spool.assert_called_once()
  890. _, kwargs = mock_spoolman_client.create_spool.call_args
  891. assert kwargs.get("location") == "Shelf B"
  892. @pytest.mark.asyncio
  893. @pytest.mark.integration
  894. async def test_update_passes_storage_location_to_spoolman(
  895. self,
  896. async_client: AsyncClient,
  897. spoolman_settings,
  898. mock_spoolman_client,
  899. ):
  900. """storage_location is forwarded as location when updating a Spoolman spool."""
  901. payload = {"storage_location": "Drawer 3"}
  902. response = await async_client.patch("/api/v1/spoolman/inventory/spools/42", json=payload)
  903. assert response.status_code == 200
  904. mock_spoolman_client.update_spool_full.assert_called_once()
  905. _, kwargs = mock_spoolman_client.update_spool_full.call_args
  906. assert kwargs.get("location") == "Drawer 3"
  907. assert kwargs.get("clear_location") is False
  908. @pytest.mark.asyncio
  909. @pytest.mark.integration
  910. async def test_update_clears_storage_location_when_null_sent(
  911. self,
  912. async_client: AsyncClient,
  913. spoolman_settings,
  914. mock_spoolman_client,
  915. ):
  916. """Explicitly sending null storage_location clears the Spoolman location."""
  917. payload = {"storage_location": None}
  918. response = await async_client.patch("/api/v1/spoolman/inventory/spools/42", json=payload)
  919. assert response.status_code == 200
  920. _, kwargs = mock_spoolman_client.update_spool_full.call_args
  921. assert kwargs.get("clear_location") is True
  922. @pytest.mark.asyncio
  923. @pytest.mark.integration
  924. async def test_update_clears_storage_location_when_empty_string_sent(
  925. self,
  926. async_client: AsyncClient,
  927. spoolman_settings,
  928. mock_spoolman_client,
  929. ):
  930. """Sending an empty string for storage_location also clears the Spoolman location."""
  931. payload = {"storage_location": ""}
  932. response = await async_client.patch("/api/v1/spoolman/inventory/spools/42", json=payload)
  933. assert response.status_code == 200
  934. _, kwargs = mock_spoolman_client.update_spool_full.call_args
  935. assert kwargs.get("clear_location") is True
  936. @pytest.mark.asyncio
  937. @pytest.mark.integration
  938. async def test_update_omitting_storage_location_does_not_write_location_to_spoolman(
  939. self,
  940. async_client: AsyncClient,
  941. spoolman_settings,
  942. mock_spoolman_client,
  943. ):
  944. """PATCH without storage_location in the payload must not touch Spoolman's location field.
  945. Regression test for the round-trip bug: opening the edit modal and saving without
  946. changing the location would previously echo the current Spoolman value back
  947. (storage_location_changed=False branch used current.get("location") instead of None).
  948. """
  949. # Payload deliberately omits storage_location — simulates saving the modal
  950. # without touching that field.
  951. payload = {"note": "just updating the note"}
  952. response = await async_client.patch("/api/v1/spoolman/inventory/spools/42", json=payload)
  953. assert response.status_code == 200
  954. mock_spoolman_client.update_spool_full.assert_called_once()
  955. _, kwargs = mock_spoolman_client.update_spool_full.call_args
  956. # location must be None so update_spool_full skips writing the field entirely
  957. assert kwargs.get("location") is None
  958. # clear_location must also be False — we are not explicitly clearing it either
  959. assert kwargs.get("clear_location") is False
  960. class TestColorNamePassthrough:
  961. """color_name is forwarded to find_or_create_filament on create and update (B6 / T5)."""
  962. @pytest.mark.asyncio
  963. @pytest.mark.integration
  964. async def test_create_passes_color_name_to_filament(
  965. self,
  966. async_client: AsyncClient,
  967. spoolman_settings,
  968. mock_spoolman_client,
  969. ):
  970. """color_name from the create payload is forwarded to find_or_create_filament."""
  971. payload = {
  972. "material": "PLA",
  973. "label_weight": 1000,
  974. "weight_used": 0,
  975. "color_name": "Bambu Green",
  976. }
  977. response = await async_client.post("/api/v1/spoolman/inventory/spools", json=payload)
  978. assert response.status_code == 200
  979. mock_spoolman_client.find_or_create_filament.assert_called_once()
  980. _, kwargs = mock_spoolman_client.find_or_create_filament.call_args
  981. assert kwargs.get("color_name") == "Bambu Green"
  982. @pytest.mark.asyncio
  983. @pytest.mark.integration
  984. async def test_update_passes_color_name_to_filament(
  985. self,
  986. async_client: AsyncClient,
  987. spoolman_settings,
  988. mock_spoolman_client,
  989. ):
  990. """color_name from the update payload is forwarded to find_or_create_filament."""
  991. payload = {"color_name": "Jade White"}
  992. response = await async_client.patch("/api/v1/spoolman/inventory/spools/42", json=payload)
  993. assert response.status_code == 200
  994. mock_spoolman_client.find_or_create_filament.assert_called_once()
  995. _, kwargs = mock_spoolman_client.find_or_create_filament.call_args
  996. assert kwargs.get("color_name") == "Jade White"
  997. @pytest.mark.asyncio
  998. @pytest.mark.integration
  999. async def test_update_omits_color_name_when_not_provided(
  1000. self,
  1001. async_client: AsyncClient,
  1002. spoolman_settings,
  1003. mock_spoolman_client,
  1004. ):
  1005. """When color_name is not in the PATCH payload, the existing filament color_name is used."""
  1006. payload = {"note": "no color_name here"}
  1007. response = await async_client.patch("/api/v1/spoolman/inventory/spools/42", json=payload)
  1008. assert response.status_code == 200
  1009. _, kwargs = mock_spoolman_client.find_or_create_filament.call_args
  1010. # color_name falls back to current filament's color_name (which is None in test fixture)
  1011. assert kwargs.get("color_name") is None
  1012. class TestSpoolmanInventoryAuth:
  1013. """Write/delete endpoints require INVENTORY_UPDATE when auth is enabled."""
  1014. @pytest.fixture
  1015. async def auth_and_spoolman_settings(self, db_session):
  1016. """Enable both Spoolman and auth."""
  1017. from backend.app.models.settings import Settings
  1018. db_session.add(Settings(key="spoolman_enabled", value="true"))
  1019. db_session.add(Settings(key="spoolman_url", value="http://localhost:7912"))
  1020. db_session.add(Settings(key="auth_enabled", value="true"))
  1021. await db_session.commit()
  1022. @pytest.mark.asyncio
  1023. @pytest.mark.integration
  1024. @pytest.mark.parametrize(
  1025. "method,path,payload",
  1026. [
  1027. ("POST", "/api/v1/spoolman/inventory/spools", {"material": "PLA", "label_weight": 1000, "weight_used": 0}),
  1028. (
  1029. "POST",
  1030. "/api/v1/spoolman/inventory/spools/bulk",
  1031. {"spool": {"material": "PLA", "label_weight": 1000, "weight_used": 0}, "quantity": 1},
  1032. ),
  1033. ("PATCH", "/api/v1/spoolman/inventory/spools/42", {"note": "x"}),
  1034. ("DELETE", "/api/v1/spoolman/inventory/spools/42", None),
  1035. ("POST", "/api/v1/spoolman/inventory/spools/42/archive", None),
  1036. ("POST", "/api/v1/spoolman/inventory/spools/42/restore", None),
  1037. ("PATCH", "/api/v1/spoolman/inventory/spools/42/weight", {"weight_grams": 100.0}),
  1038. ],
  1039. )
  1040. async def test_write_endpoints_require_auth(
  1041. self,
  1042. async_client: AsyncClient,
  1043. auth_and_spoolman_settings,
  1044. method: str,
  1045. path: str,
  1046. payload: dict | None,
  1047. ):
  1048. """All write/delete endpoints return 401 when auth is enabled and no token is provided."""
  1049. response = await async_client.request(method, path, json=payload)
  1050. assert response.status_code == 401, (
  1051. f"{method} {path} should require auth but got {response.status_code}: {response.json()}"
  1052. )
  1053. @pytest.mark.asyncio
  1054. @pytest.mark.integration
  1055. @pytest.mark.parametrize(
  1056. "method,path",
  1057. [
  1058. ("GET", "/api/v1/spoolman/inventory/spools"),
  1059. ("GET", "/api/v1/spoolman/inventory/spools/42"),
  1060. ],
  1061. )
  1062. async def test_read_endpoints_require_auth(
  1063. self,
  1064. async_client: AsyncClient,
  1065. auth_and_spoolman_settings,
  1066. method: str,
  1067. path: str,
  1068. ):
  1069. """Read endpoints also require auth when auth is enabled."""
  1070. response = await async_client.request(method, path)
  1071. assert response.status_code == 401, (
  1072. f"{method} {path} should require auth but got {response.status_code}: {response.json()}"
  1073. )
  1074. @pytest.fixture
  1075. async def viewer_token(self, db_session):
  1076. """Create a Viewer-group user (INVENTORY_READ only, no INVENTORY_UPDATE)."""
  1077. from sqlalchemy import select
  1078. from backend.app.core.auth import create_access_token, get_password_hash
  1079. from backend.app.models.group import Group
  1080. from backend.app.models.settings import Settings
  1081. from backend.app.models.user import User
  1082. db_session.add(Settings(key="spoolman_enabled", value="true"))
  1083. db_session.add(Settings(key="spoolman_url", value="http://localhost:7912"))
  1084. db_session.add(Settings(key="auth_enabled", value="true"))
  1085. await db_session.commit()
  1086. viewer_group = (await db_session.execute(select(Group).where(Group.name == "Viewers"))).scalar_one()
  1087. viewer = User(
  1088. username="sm_inv_viewer",
  1089. password_hash=get_password_hash("pw"),
  1090. is_active=True,
  1091. )
  1092. viewer.groups.append(viewer_group)
  1093. db_session.add(viewer)
  1094. await db_session.commit()
  1095. return create_access_token(data={"sub": viewer.username})
  1096. @pytest.mark.asyncio
  1097. @pytest.mark.integration
  1098. @pytest.mark.parametrize(
  1099. "method,path,payload",
  1100. [
  1101. ("POST", "/api/v1/spoolman/inventory/spools", {"material": "PLA", "label_weight": 1000, "weight_used": 0}),
  1102. (
  1103. "POST",
  1104. "/api/v1/spoolman/inventory/spools/bulk",
  1105. {"spool": {"material": "PLA", "label_weight": 1000, "weight_used": 0}, "quantity": 1},
  1106. ),
  1107. ("PATCH", "/api/v1/spoolman/inventory/spools/42", {"note": "x"}),
  1108. ("DELETE", "/api/v1/spoolman/inventory/spools/42", None),
  1109. ("POST", "/api/v1/spoolman/inventory/spools/42/archive", None),
  1110. ("POST", "/api/v1/spoolman/inventory/spools/42/restore", None),
  1111. ("PATCH", "/api/v1/spoolman/inventory/spools/42/weight", {"weight_grams": 100.0}),
  1112. ],
  1113. )
  1114. async def test_write_endpoints_return_403_for_viewer(
  1115. self,
  1116. async_client: AsyncClient,
  1117. viewer_token,
  1118. method: str,
  1119. path: str,
  1120. payload: dict | None,
  1121. ):
  1122. """Viewer-group users (INVENTORY_READ, no INVENTORY_UPDATE) get 403 on write endpoints."""
  1123. response = await async_client.request(
  1124. method,
  1125. path,
  1126. json=payload,
  1127. headers={"Authorization": f"Bearer {viewer_token}"},
  1128. )
  1129. assert response.status_code == 403, (
  1130. f"{method} {path} should return 403 for read-only user but got {response.status_code}: {response.json()}"
  1131. )
  1132. # Error body must mention the permission string so a "banned-user middleware"
  1133. # regression (generic 403 with no permission context) doesn't pass silently.
  1134. detail = response.json().get("detail", "")
  1135. assert "inventory:update" in detail, f"Expected 'inventory:update' in 403 detail but got: {detail!r}"
  1136. # ---------------------------------------------------------------------------
  1137. # Additional regression tests for second-round review items
  1138. # ---------------------------------------------------------------------------
  1139. class TestSpoolmanInventorySecurityExtras:
  1140. """Additional security/validation tests added in second review round."""
  1141. @pytest.mark.asyncio
  1142. @pytest.mark.integration
  1143. async def test_create_rejects_double_hash_rgba(
  1144. self,
  1145. async_client: AsyncClient,
  1146. spoolman_settings,
  1147. mock_spoolman_client,
  1148. ):
  1149. """SEC-3: rgba like '##FF0000' (double hash) must be rejected with 422."""
  1150. payload = {"material": "PLA", "label_weight": 1000, "weight_used": 0, "rgba": "##FF0000"}
  1151. response = await async_client.post("/api/v1/spoolman/inventory/spools", json=payload)
  1152. assert response.status_code == 422
  1153. @pytest.mark.asyncio
  1154. @pytest.mark.integration
  1155. @pytest.mark.parametrize("spool_id", [0, -1])
  1156. async def test_path_param_non_positive_spool_id_returns_422(
  1157. self,
  1158. async_client: AsyncClient,
  1159. spoolman_settings,
  1160. mock_spoolman_client,
  1161. spool_id: int,
  1162. ):
  1163. """SEC-5: /spools/0 and /spools/-1 must be rejected with 422 (Path gt=0)."""
  1164. response = await async_client.get(f"/api/v1/spoolman/inventory/spools/{spool_id}")
  1165. assert response.status_code == 422, f"Expected 422 for spool_id={spool_id} but got {response.status_code}"
  1166. @pytest.mark.asyncio
  1167. @pytest.mark.integration
  1168. @pytest.mark.parametrize(
  1169. "tag_uid,expected_status",
  1170. [
  1171. # After B1 fix: non-null tag_uid on PATCH /spools/{id} is rejected (use /tag endpoint)
  1172. ("A" * 30, 422), # non-null → 422 (use /tag endpoint instead)
  1173. ("DEADBEEF12345678", 422), # non-null → 422 regardless of length
  1174. ("A" * 31, 422), # exceeds max_length — also 422
  1175. ("A" * 32, 422), # tray_uuid-length value — also 422
  1176. ],
  1177. )
  1178. async def test_tag_uid_length_boundary(
  1179. self,
  1180. async_client: AsyncClient,
  1181. spoolman_settings,
  1182. mock_spoolman_client,
  1183. tag_uid: str,
  1184. expected_status: int,
  1185. ):
  1186. """tag_uid on PATCH /spools/{id} — all non-null values are rejected (B1 fix; use /tag endpoint)."""
  1187. payload = {"tag_uid": tag_uid}
  1188. response = await async_client.patch("/api/v1/spoolman/inventory/spools/42", json=payload)
  1189. assert response.status_code == expected_status, (
  1190. f"tag_uid len={len(tag_uid)}: expected {expected_status} but got {response.status_code}"
  1191. )
  1192. @pytest.mark.asyncio
  1193. @pytest.mark.integration
  1194. async def test_bulk_create_partial_failure_returns_207(
  1195. self,
  1196. async_client: AsyncClient,
  1197. spoolman_settings,
  1198. mock_spoolman_client,
  1199. ):
  1200. """I9: bulk create with quantity=3 where middle call fails → 207 Multi-Status."""
  1201. from backend.app.services.spoolman import SpoolmanUnavailableError
  1202. results = [SAMPLE_SPOOLMAN_SPOOL, SpoolmanUnavailableError("Spoolman down"), SAMPLE_SPOOLMAN_SPOOL]
  1203. mock_spoolman_client.create_spool.side_effect = results
  1204. payload = {
  1205. "spool": {"material": "PLA", "label_weight": 1000, "weight_used": 0},
  1206. "quantity": 3,
  1207. }
  1208. response = await async_client.post("/api/v1/spoolman/inventory/spools/bulk", json=payload)
  1209. assert response.status_code == 207, (
  1210. f"Expected 207 Multi-Status for partial failure but got {response.status_code}"
  1211. )
  1212. body = response.json()
  1213. assert isinstance(body, dict)
  1214. assert body["requested_count"] == 3
  1215. assert body["failed_count"] == 1
  1216. assert len(body["created"]) == 2
  1217. class TestTagClearPreservesExtraKeys:
  1218. """Regression test: clearing tag_uid must not wipe unrelated Spoolman extra fields."""
  1219. @pytest.mark.asyncio
  1220. @pytest.mark.integration
  1221. async def test_tag_clear_preserves_custom_extra_key(
  1222. self,
  1223. async_client: AsyncClient,
  1224. spoolman_settings,
  1225. mock_spoolman_client,
  1226. ):
  1227. """PATCH tag_uid=None clears tag without dropping unrelated extra keys.
  1228. Spoolman PATCHes the extra dict by MERGING — popping a key from the
  1229. dict and sending the rest doesn't actually clear it. The endpoint
  1230. sets tag = json.dumps("") explicitly; read-side filters strip the
  1231. wrapping quotes and treat the empty string as "no tag" (#1114).
  1232. """
  1233. import json as _json
  1234. spool_with_extra = {
  1235. **SAMPLE_SPOOLMAN_SPOOL,
  1236. "extra": {"tag": '"AABBCCDDEEFF0011AABBCCDDEEFF0011"', "custom_key": "keep_me"},
  1237. }
  1238. mock_spoolman_client.get_spool = AsyncMock(return_value=spool_with_extra)
  1239. mock_spoolman_client.update_spool_full = AsyncMock(return_value=spool_with_extra)
  1240. response = await async_client.patch(
  1241. "/api/v1/spoolman/inventory/spools/42",
  1242. json={"tag_uid": None},
  1243. )
  1244. assert response.status_code == 200
  1245. mock_spoolman_client.update_spool_full.assert_called_once()
  1246. _, kwargs = mock_spoolman_client.update_spool_full.call_args
  1247. sent_extra = kwargs.get("extra")
  1248. assert sent_extra is not None, "extra must be sent when tag is cleared"
  1249. assert sent_extra.get("tag") == _json.dumps(""), (
  1250. "tag must be set to JSON empty-string sentinel (Spoolman PATCH merges; "
  1251. "popping the key would leave the previous value in place)"
  1252. )
  1253. assert sent_extra.get("custom_key") == "keep_me", "unrelated extra keys must survive"
  1254. @pytest.mark.asyncio
  1255. @pytest.mark.integration
  1256. async def test_tag_clear_refetches_spool_inside_lock(
  1257. self,
  1258. async_client: AsyncClient,
  1259. spoolman_settings,
  1260. mock_spoolman_client,
  1261. ):
  1262. """B7: tag-clear does a fresh get_spool() re-fetch inside the lock, not the stale one.
  1263. Simulates a write that changes extra between the initial get_spool (used for
  1264. other field resolution) and the lock acquisition. The extra sent to
  1265. update_spool_full must come from the second (in-lock) fetch, not the first.
  1266. """
  1267. stale_extra = {"tag": '"AABBCCDD"', "custom_key": "stale_value"}
  1268. fresh_extra = {"tag": '"AABBCCDD"', "custom_key": "fresh_value"}
  1269. stale_spool = {**SAMPLE_SPOOLMAN_SPOOL, "extra": stale_extra}
  1270. fresh_spool = {**SAMPLE_SPOOLMAN_SPOOL, "extra": fresh_extra}
  1271. # First call returns stale; second call (inside lock) returns fresh
  1272. mock_spoolman_client.get_spool = AsyncMock(side_effect=[stale_spool, fresh_spool])
  1273. mock_spoolman_client.update_spool_full = AsyncMock(return_value=fresh_spool)
  1274. response = await async_client.patch(
  1275. "/api/v1/spoolman/inventory/spools/42",
  1276. json={"tag_uid": None, "tray_uuid": None},
  1277. )
  1278. assert response.status_code == 200
  1279. # get_spool called twice: once for field resolution, once for fresh extra fetch
  1280. assert mock_spoolman_client.get_spool.call_count == 2
  1281. import json as _json
  1282. _, kwargs = mock_spoolman_client.update_spool_full.call_args
  1283. sent_extra = kwargs.get("extra")
  1284. assert sent_extra is not None
  1285. # Tag is set to the JSON empty-string sentinel (not popped) — Spoolman
  1286. # PATCH merges, so popping the key would leave the previous value.
  1287. assert sent_extra.get("tag") == _json.dumps("")
  1288. # custom_key must come from the fresh re-fetch, not the stale first fetch
  1289. assert sent_extra.get("custom_key") == "fresh_value"
  1290. class TestSpoolmanInventorySSRFSpoolBuddyPath:
  1291. """SSRF tests for _get_spoolman_client_or_none (nfc/* and scale/ endpoints)."""
  1292. @pytest.mark.asyncio
  1293. @pytest.mark.integration
  1294. @pytest.mark.parametrize(
  1295. "evil_url",
  1296. [
  1297. "file:///etc/passwd",
  1298. "http://169.254.169.254/latest/meta-data/", # AWS IMDS
  1299. "http://0.0.0.0/", # unspecified
  1300. "http://[::ffff:169.254.169.254]/", # IPv4-mapped IMDS bypass
  1301. ],
  1302. )
  1303. async def test_nfc_tag_scanned_with_ssrf_url_ignores_spoolman(
  1304. self,
  1305. async_client: AsyncClient,
  1306. db_session,
  1307. evil_url: str,
  1308. ):
  1309. """SSRF: _get_spoolman_client_or_none silently disables Spoolman for unsafe URLs
  1310. on the SpoolBuddy NFC path (tag-scanned broadcasts unknown_tag, not 400)."""
  1311. from backend.app.models.settings import Settings
  1312. db_session.add(Settings(key="spoolman_enabled", value="true"))
  1313. db_session.add(Settings(key="spoolman_url", value=evil_url))
  1314. await db_session.commit()
  1315. from unittest.mock import AsyncMock, patch
  1316. with patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws:
  1317. mock_ws.broadcast = AsyncMock()
  1318. resp = await async_client.post(
  1319. "/api/v1/spoolbuddy/nfc/tag-scanned",
  1320. json={"device_id": "sb-ssrf", "tag_uid": "AABBCCDD"},
  1321. )
  1322. # Must not crash or proxy the SSRF URL — unknown_tag is the safe degraded response
  1323. assert resp.status_code == 200
  1324. if mock_ws.broadcast.called:
  1325. msg = mock_ws.broadcast.call_args[0][0]
  1326. assert msg["type"] == "spoolbuddy_unknown_tag"
  1327. @pytest.mark.asyncio
  1328. @pytest.mark.integration
  1329. @pytest.mark.parametrize(
  1330. "evil_url",
  1331. [
  1332. "http://169.254.169.254/latest/meta-data/", # AWS IMDS
  1333. "http://[::ffff:169.254.169.254]/", # IPv4-mapped IMDS bypass
  1334. ],
  1335. )
  1336. async def test_nfc_write_result_with_ssrf_url_degrades_gracefully(
  1337. self,
  1338. async_client: AsyncClient,
  1339. db_session,
  1340. evil_url: str,
  1341. ):
  1342. """SSRF: write-result with unsafe Spoolman URL must not proxy to the evil host.
  1343. write-result calls Spoolman to write-back the tag UID when data_origin='spoolman'.
  1344. With an SSRF URL, _get_spoolman_client_or_none returns None so the call is skipped
  1345. and the route returns 502 (tag written but link not persisted — not a server crash).
  1346. """
  1347. import json as _json
  1348. from backend.app.models.settings import Settings
  1349. from backend.app.models.spoolbuddy_device import SpoolBuddyDevice
  1350. db_session.add(Settings(key="spoolman_enabled", value="true"))
  1351. db_session.add(Settings(key="spoolman_url", value=evil_url))
  1352. # Register the device so the route doesn't 404 before reaching the SSRF guard.
  1353. db_session.add(
  1354. SpoolBuddyDevice(
  1355. device_id="sb-ssrf-wr",
  1356. hostname="sb-ssrf-wr.local",
  1357. ip_address="127.0.0.1",
  1358. pending_command="write_tag",
  1359. pending_write_payload=_json.dumps({"spool_id": 99, "ndef_data_hex": "DEAD", "data_origin": "spoolman"}),
  1360. )
  1361. )
  1362. await db_session.commit()
  1363. from unittest.mock import AsyncMock, patch
  1364. with patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws:
  1365. mock_ws.broadcast = AsyncMock()
  1366. resp = await async_client.post(
  1367. "/api/v1/spoolbuddy/nfc/write-result",
  1368. json={
  1369. "device_id": "sb-ssrf-wr",
  1370. "spool_id": 99,
  1371. "tag_uid": "AABBCCDD",
  1372. "success": True,
  1373. },
  1374. )
  1375. # 502 = tag written to NFC but Spoolman link not persisted (SSRF guard blocked it).
  1376. # Must not be 500 (crash) and must not have proxied to the evil host.
  1377. assert resp.status_code == 502
  1378. @pytest.mark.asyncio
  1379. @pytest.mark.integration
  1380. @pytest.mark.parametrize(
  1381. "evil_url",
  1382. [
  1383. "http://169.254.169.254/latest/meta-data/", # AWS IMDS
  1384. ],
  1385. )
  1386. async def test_scale_update_weight_with_ssrf_url_degrades_gracefully(
  1387. self,
  1388. async_client: AsyncClient,
  1389. db_session,
  1390. evil_url: str,
  1391. ):
  1392. """SSRF: scale weight update with unsafe Spoolman URL must not proxy to the evil host."""
  1393. from backend.app.models.settings import Settings
  1394. db_session.add(Settings(key="spoolman_enabled", value="true"))
  1395. db_session.add(Settings(key="spoolman_url", value=evil_url))
  1396. await db_session.commit()
  1397. from unittest.mock import AsyncMock, patch
  1398. with patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws:
  1399. mock_ws.broadcast = AsyncMock()
  1400. resp = await async_client.post(
  1401. "/api/v1/spoolbuddy/scale/update-spool-weight",
  1402. json={"device_id": "sb-ssrf-scale", "spool_id": 1, "weight_grams": 500.0},
  1403. )
  1404. # Must not crash or proxy to an SSRF host
  1405. assert resp.status_code in (200, 404, 422)
  1406. class TestMergeSpoolExtraPreservesKeys:
  1407. """Unit-level test for merge_spool_extra key preservation (via mocked Spoolman)."""
  1408. @pytest.mark.asyncio
  1409. @pytest.mark.integration
  1410. async def test_merge_preserves_unrelated_extra_keys(
  1411. self,
  1412. async_client: AsyncClient,
  1413. spoolman_settings,
  1414. mock_spoolman_client,
  1415. ):
  1416. """merge_spool_extra must deep-merge rather than overwrite the extra dict.
  1417. Seed extra={"custom_key": "keep_me", "tag": "old"}.
  1418. After merging {"tag": "new"}, the PATCH payload must still contain custom_key.
  1419. """
  1420. from unittest.mock import AsyncMock, patch
  1421. existing_spool = {
  1422. **SAMPLE_SPOOLMAN_SPOOL,
  1423. "extra": {"custom_key": "keep_me", "tag": '"old"'},
  1424. }
  1425. updated_spool = {**existing_spool, "extra": {"custom_key": "keep_me", "tag": '"new"'}}
  1426. mock_client = mock_spoolman_client
  1427. mock_client.get_spool = AsyncMock(return_value=existing_spool)
  1428. mock_client.update_spool_full = AsyncMock(return_value=updated_spool)
  1429. # Call merge_spool_extra directly through the service
  1430. from backend.app.services.spoolman import SpoolmanClient
  1431. client = SpoolmanClient.__new__(SpoolmanClient)
  1432. client.base_url = "http://localhost:7912"
  1433. client.api_url = "http://localhost:7912/api/v1"
  1434. client._extra_locks = {}
  1435. async def _mock_get(spool_id):
  1436. return existing_spool
  1437. async def _mock_update(spool_id, **kwargs):
  1438. # Capture what was actually sent
  1439. _mock_update.captured_extra = kwargs.get("extra")
  1440. return updated_spool
  1441. _mock_update.captured_extra = None
  1442. client.get_spool = _mock_get
  1443. client.update_spool_full = _mock_update
  1444. result = await client.merge_spool_extra(42, {"tag": '"new"'})
  1445. # The merged extra must include the unrelated key
  1446. assert _mock_update.captured_extra is not None
  1447. assert _mock_update.captured_extra.get("custom_key") == "keep_me"
  1448. assert _mock_update.captured_extra.get("tag") == '"new"'
  1449. assert result is not None
  1450. class TestGetClientValueError:
  1451. """Test the ValueError branch in _get_client when init_spoolman_client fails (Gap 5)."""
  1452. @pytest.mark.asyncio
  1453. @pytest.mark.integration
  1454. async def test_returns_400_when_init_spoolman_client_raises_value_error(
  1455. self, async_client: AsyncClient, spoolman_settings
  1456. ):
  1457. """If init_spoolman_client raises ValueError after SSRF check passes, return HTTP 400."""
  1458. with (
  1459. patch(
  1460. "backend.app.api.routes.spoolman_inventory.get_spoolman_client",
  1461. AsyncMock(return_value=None),
  1462. ),
  1463. patch(
  1464. "backend.app.api.routes.spoolman_inventory.init_spoolman_client",
  1465. AsyncMock(side_effect=ValueError("unsupported scheme")),
  1466. ),
  1467. ):
  1468. resp = await async_client.get("/api/v1/spoolman/inventory/spools")
  1469. assert resp.status_code == 400
  1470. assert "unsupported scheme" in resp.json()["detail"]
  1471. class TestBulkCreateWithPriceFailure:
  1472. """Test that bulk create handles price-update failures per C1/C8 semantics."""
  1473. @pytest.mark.asyncio
  1474. @pytest.mark.integration
  1475. async def test_bulk_create_price_503_moves_spool_to_failures(
  1476. self, async_client: AsyncClient, spoolman_settings, mock_spoolman_client
  1477. ):
  1478. """When price update fails (503), the spool goes to failures — overall returns 207 if at least one succeeds."""
  1479. from backend.app.services.spoolman import SpoolmanUnavailableError
  1480. # First price update fails (SpoolmanUnavailableError → 503), second succeeds
  1481. mock_spoolman_client.update_spool_full = AsyncMock(
  1482. side_effect=[SpoolmanUnavailableError("price server down"), SAMPLE_SPOOLMAN_SPOOL]
  1483. )
  1484. mock_spoolman_client.create_spool = AsyncMock(return_value=SAMPLE_SPOOLMAN_SPOOL)
  1485. payload = {
  1486. "spool": {
  1487. "material": "PLA",
  1488. "brand": "Bambu Lab",
  1489. "label_weight": 1000,
  1490. "cost_per_kg": 19.99,
  1491. },
  1492. "quantity": 2,
  1493. }
  1494. resp = await async_client.post("/api/v1/spoolman/inventory/spools/bulk", json=payload)
  1495. # One spool succeeded, one failed (price 503) → 207 Partial
  1496. assert resp.status_code == 207
  1497. data = resp.json()
  1498. assert len(data["created"]) == 1
  1499. assert data["failed_count"] == 1
  1500. # Both Spoolman creates were attempted
  1501. assert mock_spoolman_client.create_spool.call_count == 2
  1502. # Both price updates were attempted
  1503. assert mock_spoolman_client.update_spool_full.call_count == 2
  1504. class TestSpoolTagLinkValidation:
  1505. """NEW-B1: /spools/{id}/tag endpoint validates tag_uid length and content."""
  1506. @pytest.mark.asyncio
  1507. @pytest.mark.integration
  1508. async def test_tag_uid_6_chars_rejected(self, async_client: AsyncClient, spoolman_settings, mock_spoolman_client):
  1509. """tag_uid with 6 hex chars is rejected — minimum is 8 chars (4-byte UID)."""
  1510. resp = await async_client.patch(
  1511. "/api/v1/spoolman/inventory/spools/42/tag",
  1512. json={"tag_uid": "AABBCC"}, # 6 chars — below new minimum
  1513. )
  1514. assert resp.status_code == 422
  1515. @pytest.mark.asyncio
  1516. @pytest.mark.integration
  1517. async def test_tag_uid_all_zeros_rejected(self, async_client: AsyncClient, spoolman_settings, mock_spoolman_client):
  1518. """tag_uid that is all-zero bytes is rejected as an unwritten/blank tag."""
  1519. resp = await async_client.patch(
  1520. "/api/v1/spoolman/inventory/spools/42/tag",
  1521. json={"tag_uid": "00000000000000"}, # 14 zeros
  1522. )
  1523. assert resp.status_code == 422
  1524. @pytest.mark.asyncio
  1525. @pytest.mark.integration
  1526. async def test_tag_uid_valid_14_chars_accepted(
  1527. self, async_client: AsyncClient, spoolman_settings, mock_spoolman_client
  1528. ):
  1529. """tag_uid with 14 valid hex chars (7-byte UID) is accepted."""
  1530. # This tag is not in SAMPLE_SPOOLMAN_SPOOL so no duplicate conflict.
  1531. resp = await async_client.patch(
  1532. "/api/v1/spoolman/inventory/spools/42/tag",
  1533. json={"tag_uid": "AABBCCDD112233"}, # 14 chars, valid, not all-zeros
  1534. )
  1535. assert resp.status_code == 200
  1536. @pytest.mark.asyncio
  1537. @pytest.mark.integration
  1538. async def test_tag_uid_8_chars_accepted(self, async_client: AsyncClient, spoolman_settings, mock_spoolman_client):
  1539. """tag_uid with 8 hex chars (4-byte Bambu Lab NFC UID) is accepted after min_length fix."""
  1540. resp = await async_client.patch(
  1541. "/api/v1/spoolman/inventory/spools/42/tag",
  1542. json={"tag_uid": "2728C17B"}, # 8 chars — real Bambu Lab 4-byte hardware UID
  1543. )
  1544. assert resp.status_code == 200
  1545. @pytest.mark.asyncio
  1546. @pytest.mark.integration
  1547. async def test_tag_uid_8_zeros_rejected(self, async_client: AsyncClient, spoolman_settings, mock_spoolman_client):
  1548. """tag_uid with 8 zero chars is rejected — all-zeros validator applies at the new minimum."""
  1549. resp = await async_client.patch(
  1550. "/api/v1/spoolman/inventory/spools/42/tag",
  1551. json={"tag_uid": "00000000"}, # 8 zeros — meets min_length but is a blank/unwritten tag
  1552. )
  1553. assert resp.status_code == 422
  1554. class TestLinkTagDuplicate:
  1555. """NEW-I1: /spools/{id}/tag returns 409 when another spool already has the same tag."""
  1556. @pytest.mark.asyncio
  1557. @pytest.mark.integration
  1558. async def test_link_tag_returns_200_when_tag_not_on_another_spool(
  1559. self, async_client: AsyncClient, spoolman_settings, mock_spoolman_client
  1560. ):
  1561. """Linking a fresh tag to spool 42 returns 200 — no duplicate in Spoolman."""
  1562. resp = await async_client.patch(
  1563. "/api/v1/spoolman/inventory/spools/42/tag",
  1564. json={"tag_uid": "AABBCCDD112233"}, # not in SAMPLE_SPOOLMAN_SPOOL
  1565. )
  1566. assert resp.status_code == 200
  1567. mock_spoolman_client.update_spool_full.assert_called_once()
  1568. @pytest.mark.asyncio
  1569. @pytest.mark.integration
  1570. async def test_link_tag_returns_409_when_same_tag_on_different_spool(
  1571. self, async_client: AsyncClient, spoolman_settings, mock_spoolman_client
  1572. ):
  1573. """Linking spool 99 to a tag that spool 42 already carries must return 409."""
  1574. # SAMPLE_SPOOLMAN_SPOOL (id=42) has extra.tag = '"AABBCCDDEEFF0011AABBCCDDEEFF0011"'.
  1575. # Attempting to assign the same tag to spool 99 must be rejected.
  1576. resp = await async_client.patch(
  1577. "/api/v1/spoolman/inventory/spools/99/tag",
  1578. json={"tray_uuid": "AABBCCDDEEFF0011AABBCCDDEEFF0011"}, # 32-char tray UUID
  1579. )
  1580. assert resp.status_code == 409
  1581. detail = resp.json()["detail"]
  1582. assert "42" in str(detail)
  1583. class TestSpoolmanInventoryUpdateCoreWeight:
  1584. """core_weight is accepted for schema parity but not persisted — any value should be accepted."""
  1585. @pytest.mark.asyncio
  1586. @pytest.mark.integration
  1587. async def test_patch_core_weight_other_than_250_accepted(
  1588. self, async_client: AsyncClient, spoolman_settings, mock_spoolman_client
  1589. ):
  1590. """PATCH with core_weight != 250 is accepted (field is ignored server-side, not rejected)."""
  1591. resp = await async_client.patch(
  1592. "/api/v1/spoolman/inventory/spools/42",
  1593. json={"core_weight": 100},
  1594. )
  1595. assert resp.status_code == 200
  1596. @pytest.mark.asyncio
  1597. @pytest.mark.integration
  1598. async def test_patch_core_weight_250_explicitly_is_accepted(
  1599. self, async_client: AsyncClient, spoolman_settings, mock_spoolman_client
  1600. ):
  1601. """PATCH with core_weight=250 (the default) is valid and returns 200."""
  1602. resp = await async_client.patch(
  1603. "/api/v1/spoolman/inventory/spools/42",
  1604. json={"core_weight": 250},
  1605. )
  1606. assert resp.status_code == 200
  1607. @pytest.mark.asyncio
  1608. @pytest.mark.integration
  1609. async def test_patch_without_core_weight_is_accepted(
  1610. self, async_client: AsyncClient, spoolman_settings, mock_spoolman_client
  1611. ):
  1612. """PATCH without core_weight (omitted) must not trigger the validator — returns 200."""
  1613. resp = await async_client.patch(
  1614. "/api/v1/spoolman/inventory/spools/42",
  1615. json={"note": "no core_weight key"},
  1616. )
  1617. assert resp.status_code == 200
  1618. class TestUnlinkSpool:
  1619. """POST /spoolman/spools/{id}/unlink clears Spoolman tag without re-entrant lock deadlock.
  1620. Spoolman PATCHes the extra dict by MERGING — popping a key + sending the
  1621. rest doesn't clear the popped key. The endpoint sends the JSON empty-string
  1622. sentinel ('""') which the read-side filters strip. (#1114)
  1623. The endpoint uses merge_spool_extra (not update_spool_full directly)
  1624. because (a) merge_spool_extra owns the per-spool extra_lock for atomic
  1625. read-modify-write semantics, and (b) wrapping it in another extra_lock
  1626. would deadlock — asyncio.Lock is not re-entrant.
  1627. """
  1628. @pytest.fixture
  1629. def mock_unlink_client(self):
  1630. """Mock Spoolman client for the spoolman.py (non-inventory) route."""
  1631. spool_with_tag = {
  1632. **SAMPLE_SPOOLMAN_SPOOL,
  1633. "extra": {"tag": '"AABBCCDDEEFF0011AABBCCDDEEFF0011"', "custom": "keep"},
  1634. }
  1635. mock_client = MagicMock()
  1636. mock_client.base_url = "http://localhost:7912"
  1637. mock_client.health_check = AsyncMock(return_value=True)
  1638. mock_client.get_spool = AsyncMock(return_value=spool_with_tag)
  1639. # merge_spool_extra returns the spool with the tag cleared (and custom
  1640. # preserved) — that's what the read-side will see after the fix.
  1641. mock_client.merge_spool_extra = AsyncMock(
  1642. return_value={**spool_with_tag, "extra": {"tag": '""', "custom": "keep"}}
  1643. )
  1644. with (
  1645. patch(
  1646. "backend.app.api.routes.spoolman.get_spoolman_client",
  1647. AsyncMock(return_value=mock_client),
  1648. ),
  1649. patch(
  1650. "backend.app.api.routes.spoolman.init_spoolman_client",
  1651. AsyncMock(return_value=mock_client),
  1652. ),
  1653. ):
  1654. yield mock_client
  1655. @pytest.mark.asyncio
  1656. @pytest.mark.integration
  1657. async def test_unlink_sets_tag_to_json_empty_string(
  1658. self,
  1659. async_client: AsyncClient,
  1660. spoolman_settings,
  1661. mock_unlink_client,
  1662. ):
  1663. """Unlink calls merge_spool_extra with the JSON-empty-string sentinel.
  1664. Pre-fix the endpoint did `cur_extra.pop("tag")` then PATCHed the rest.
  1665. Spoolman silently kept the previous tag because the key wasn't in the
  1666. payload (PATCH merges). Now the endpoint sends `{"tag": '""'}` and
  1667. the read-side .strip('"') resolves it to "" → spool drops out of
  1668. get_linked_spools.
  1669. """
  1670. import json as _json
  1671. resp = await async_client.post("/api/v1/spoolman/spools/42/unlink")
  1672. assert resp.status_code == 200
  1673. mock_unlink_client.merge_spool_extra.assert_called_once_with(42, {"tag": _json.dumps("")})
  1674. @pytest.mark.asyncio
  1675. @pytest.mark.integration
  1676. async def test_unlink_preserves_other_extra_keys(
  1677. self,
  1678. async_client: AsyncClient,
  1679. spoolman_settings,
  1680. mock_unlink_client,
  1681. ):
  1682. """Unrelated extra keys must survive unlink.
  1683. merge_spool_extra is responsible for the merge (read current → merge
  1684. new fields → PATCH). The unlink endpoint only sends `{"tag": ...}`,
  1685. so any other extra key on the spool is automatically preserved by
  1686. merge_spool_extra's read-merge-write semantics.
  1687. """
  1688. resp = await async_client.post("/api/v1/spoolman/spools/42/unlink")
  1689. assert resp.status_code == 200
  1690. # The endpoint passes only the tag key — merge_spool_extra does the
  1691. # rest. We don't assert anything about `custom` on the call args
  1692. # because the route doesn't see / pass it.
  1693. _, args, _ = mock_unlink_client.merge_spool_extra.mock_calls[0]
  1694. sent_fields = args[1] if len(args) >= 2 else {}
  1695. assert sent_fields == {"tag": '""'}, "unlink should only send the tag key — merge_spool_extra does the merge"
  1696. # ---------------------------------------------------------------------------
  1697. # B1: GET /spoolman/inventory/filaments
  1698. # B2: POST /spools with spoolman_filament_id bypasses find_or_create_filament
  1699. # ---------------------------------------------------------------------------
  1700. SAMPLE_FILAMENT_DICT = {
  1701. "id": 7,
  1702. "name": "PLA Basic",
  1703. "material": "PLA",
  1704. "color_hex": "FF0000",
  1705. "color_name": "Red",
  1706. "weight": 1000,
  1707. "spool_weight": 196,
  1708. "vendor": {"id": 3, "name": "Bambu Lab"},
  1709. }
  1710. class TestListSpoolmanFilaments:
  1711. """Tests for GET /api/v1/spoolman/inventory/filaments (B1)."""
  1712. @pytest.mark.asyncio
  1713. @pytest.mark.integration
  1714. async def test_list_filaments_disabled_returns_400(self, async_client: AsyncClient):
  1715. """Without Spoolman enabled the endpoint returns 400."""
  1716. resp = await async_client.get("/api/v1/spoolman/inventory/filaments")
  1717. assert resp.status_code == 400
  1718. @pytest.mark.asyncio
  1719. @pytest.mark.integration
  1720. async def test_list_filaments_unreachable_returns_503(self, async_client: AsyncClient, spoolman_settings):
  1721. """503 is returned when _get_client raises HTTPException(503)."""
  1722. with patch(
  1723. "backend.app.api.routes.spoolman_inventory._get_client",
  1724. AsyncMock(side_effect=HTTPException(status_code=503, detail="Spoolman server is not reachable")),
  1725. ):
  1726. resp = await async_client.get("/api/v1/spoolman/inventory/filaments")
  1727. assert resp.status_code == 503
  1728. @pytest.mark.asyncio
  1729. @pytest.mark.integration
  1730. async def test_list_filaments_success(self, async_client: AsyncClient, spoolman_settings):
  1731. """Success path returns normalised filament list including spool_weight."""
  1732. mock_client = MagicMock()
  1733. mock_client.get_filaments = AsyncMock(return_value=[SAMPLE_FILAMENT_DICT])
  1734. with patch(
  1735. "backend.app.api.routes.spoolman_inventory._get_client",
  1736. AsyncMock(return_value=mock_client),
  1737. ):
  1738. resp = await async_client.get("/api/v1/spoolman/inventory/filaments")
  1739. assert resp.status_code == 200
  1740. data = resp.json()
  1741. assert isinstance(data, list)
  1742. assert len(data) == 1
  1743. entry = data[0]
  1744. assert entry["id"] == 7
  1745. assert entry["material"] == "PLA"
  1746. assert entry["spool_weight"] == 196
  1747. assert entry["vendor"]["name"] == "Bambu Lab"
  1748. class TestCreateSpoolWithFilamentId:
  1749. """Tests for POST /api/v1/spoolman/inventory/spools with spoolman_filament_id (B2)."""
  1750. @pytest.mark.asyncio
  1751. @pytest.mark.integration
  1752. async def test_create_with_filament_id_skips_find_or_create(self, async_client: AsyncClient, spoolman_settings):
  1753. """When spoolman_filament_id is provided, find_or_create_filament must NOT be called."""
  1754. mock_client = MagicMock()
  1755. mock_client.find_or_create_filament = AsyncMock(return_value=7)
  1756. mock_client.create_spool = AsyncMock(return_value=SAMPLE_SPOOLMAN_SPOOL)
  1757. mock_client.update_spool_full = AsyncMock(return_value=SAMPLE_SPOOLMAN_SPOOL)
  1758. with patch(
  1759. "backend.app.api.routes.spoolman_inventory._get_client",
  1760. AsyncMock(return_value=mock_client),
  1761. ):
  1762. resp = await async_client.post(
  1763. "/api/v1/spoolman/inventory/spools",
  1764. json={"spoolman_filament_id": 7},
  1765. )
  1766. assert resp.status_code == 200
  1767. mock_client.find_or_create_filament.assert_not_called()
  1768. mock_client.create_spool.assert_called_once()
  1769. _, kwargs = mock_client.create_spool.call_args
  1770. assert kwargs.get("filament_id") == 7
  1771. @pytest.mark.asyncio
  1772. @pytest.mark.integration
  1773. async def test_create_with_invalid_filament_id_returns_404(self, async_client: AsyncClient, spoolman_settings):
  1774. """An invalid spoolman_filament_id (not in Spoolman) must return 404."""
  1775. from backend.app.services.spoolman import SpoolmanNotFoundError
  1776. mock_client = MagicMock()
  1777. mock_client.create_spool = AsyncMock(side_effect=SpoolmanNotFoundError("filament not found"))
  1778. with patch(
  1779. "backend.app.api.routes.spoolman_inventory._get_client",
  1780. AsyncMock(return_value=mock_client),
  1781. ):
  1782. resp = await async_client.post(
  1783. "/api/v1/spoolman/inventory/spools",
  1784. json={"spoolman_filament_id": 9999},
  1785. )
  1786. assert resp.status_code == 404
  1787. assert "9999" in resp.json()["detail"]
  1788. # ---------------------------------------------------------------------------
  1789. # WICHTIG-12: Additional edge-case tests
  1790. # ---------------------------------------------------------------------------
  1791. class TestBulkCreateWithFilamentId:
  1792. """Bulk create with spoolman_filament_id skips find_or_create_filament."""
  1793. @pytest.mark.asyncio
  1794. @pytest.mark.integration
  1795. async def test_bulk_create_with_filament_id_skips_find_or_create(
  1796. self, async_client: AsyncClient, spoolman_settings
  1797. ):
  1798. """Bulk POST with spoolman_filament_id must NOT call find_or_create_filament."""
  1799. mock_client = MagicMock()
  1800. mock_client.find_or_create_filament = AsyncMock(return_value=7)
  1801. mock_client.create_spool = AsyncMock(return_value=SAMPLE_SPOOLMAN_SPOOL)
  1802. mock_client.update_spool_full = AsyncMock(return_value=SAMPLE_SPOOLMAN_SPOOL)
  1803. with patch(
  1804. "backend.app.api.routes.spoolman_inventory._get_client",
  1805. AsyncMock(return_value=mock_client),
  1806. ):
  1807. resp = await async_client.post(
  1808. "/api/v1/spoolman/inventory/spools/bulk",
  1809. json={"spool": {"spoolman_filament_id": 7}, "quantity": 2},
  1810. )
  1811. assert resp.status_code == 200
  1812. mock_client.find_or_create_filament.assert_not_called()
  1813. assert mock_client.create_spool.call_count == 2
  1814. for call in mock_client.create_spool.call_args_list:
  1815. _, kwargs = call
  1816. assert kwargs.get("filament_id") == 7
  1817. class TestCreateSpoolValidation:
  1818. """Validation edge cases for SpoolmanInventoryCreate."""
  1819. @pytest.mark.asyncio
  1820. @pytest.mark.integration
  1821. async def test_create_spool_filament_id_zero_returns_422(self, async_client: AsyncClient, spoolman_settings):
  1822. """spoolman_filament_id=0 must fail Field(gt=0) validation → 422."""
  1823. resp = await async_client.post(
  1824. "/api/v1/spoolman/inventory/spools",
  1825. json={"spoolman_filament_id": 0},
  1826. )
  1827. assert resp.status_code == 422
  1828. @pytest.mark.asyncio
  1829. @pytest.mark.integration
  1830. async def test_create_spool_without_material_or_filament_id_returns_422(
  1831. self, async_client: AsyncClient, spoolman_settings
  1832. ):
  1833. """Neither material nor spoolman_filament_id → model_validator must reject → 422."""
  1834. resp = await async_client.post(
  1835. "/api/v1/spoolman/inventory/spools",
  1836. json={"label_weight": 1000},
  1837. )
  1838. assert resp.status_code == 422
  1839. class TestNormalizeFilament:
  1840. """Unit-style tests for _normalize_filament helper (imported directly)."""
  1841. def test_normalize_filament_null_vendor(self):
  1842. from backend.app.api.routes.spoolman_inventory import _normalize_filament
  1843. result = _normalize_filament({"id": 5, "name": "PLA", "vendor": None})
  1844. assert result is not None
  1845. assert result["vendor"] is None
  1846. def test_normalize_filament_null_id_returns_none(self):
  1847. from backend.app.api.routes.spoolman_inventory import _normalize_filament
  1848. result = _normalize_filament({"id": None, "name": "PLA"})
  1849. assert result is None
  1850. def test_normalize_filament_zero_id_returns_none(self):
  1851. from backend.app.api.routes.spoolman_inventory import _normalize_filament
  1852. result = _normalize_filament({"id": 0, "name": "PLA"})
  1853. assert result is None
  1854. # ---------------------------------------------------------------------------
  1855. # F1: TestTranslateSpoolmanErrors — 502/404/503 paths through _translate_spoolman_errors
  1856. # ---------------------------------------------------------------------------
  1857. class TestTranslateSpoolmanErrors:
  1858. """F1: _translate_spoolman_errors() maps Spoolman exceptions to HTTP codes."""
  1859. @pytest.mark.asyncio
  1860. @pytest.mark.integration
  1861. async def test_spoolman_not_found_returns_404(
  1862. self, async_client: AsyncClient, spoolman_settings, mock_spoolman_client
  1863. ):
  1864. """SpoolmanNotFoundError from get_spool → 404."""
  1865. from backend.app.services.spoolman import SpoolmanNotFoundError
  1866. mock_spoolman_client.get_spool.side_effect = SpoolmanNotFoundError("spool 999 not found")
  1867. resp = await async_client.get("/api/v1/spoolman/inventory/spools/999")
  1868. assert resp.status_code == 404
  1869. @pytest.mark.asyncio
  1870. @pytest.mark.integration
  1871. async def test_spoolman_unavailable_returns_503(
  1872. self, async_client: AsyncClient, spoolman_settings, mock_spoolman_client
  1873. ):
  1874. """SpoolmanUnavailableError from get_spool → 503."""
  1875. from backend.app.services.spoolman import SpoolmanUnavailableError
  1876. mock_spoolman_client.get_spool.side_effect = SpoolmanUnavailableError("network error")
  1877. resp = await async_client.get("/api/v1/spoolman/inventory/spools/42")
  1878. assert resp.status_code == 503
  1879. @pytest.mark.asyncio
  1880. @pytest.mark.integration
  1881. async def test_spoolman_client_error_returns_502_with_upstream_status(
  1882. self, async_client: AsyncClient, spoolman_settings, mock_spoolman_client
  1883. ):
  1884. """SpoolmanClientError from get_spool → 502 with upstream_status in body."""
  1885. from backend.app.services.spoolman import SpoolmanClientError
  1886. mock_spoolman_client.get_spool.side_effect = SpoolmanClientError("Spoolman rejected", 422, "filament not found")
  1887. resp = await async_client.get("/api/v1/spoolman/inventory/spools/42")
  1888. assert resp.status_code == 502
  1889. body = resp.json()
  1890. assert body["detail"]["upstream_status"] == 422
  1891. assert body["detail"]["upstream_body"] == "filament not found"
  1892. # ---------------------------------------------------------------------------
  1893. # F2: _get_client health_check returns False → 503
  1894. # ---------------------------------------------------------------------------
  1895. class TestGetClientHealthCheckFalse:
  1896. """F2: _get_client raises 503 when health_check() returns False."""
  1897. @pytest.mark.asyncio
  1898. @pytest.mark.integration
  1899. async def test_returns_503_when_health_check_returns_false(
  1900. self, async_client: AsyncClient, spoolman_settings, mock_spoolman_client
  1901. ):
  1902. """health_check() → False should produce 503 on any inventory call."""
  1903. import time
  1904. import backend.app.api.routes.spoolman_inventory as inv_module
  1905. mock_spoolman_client.health_check = AsyncMock(return_value=False)
  1906. # Clear the TTL cache so health_check is actually called
  1907. inv_module._health_check_cache.clear()
  1908. resp = await async_client.get("/api/v1/spoolman/inventory/spools")
  1909. assert resp.status_code == 503
  1910. # ---------------------------------------------------------------------------
  1911. # F3: SpoolTagLinkRequest both fields null → 422
  1912. # ---------------------------------------------------------------------------
  1913. class TestSpoolTagLinkBothNull:
  1914. """F3: /spools/{id}/tag with both tag_uid and tray_uuid null → 422."""
  1915. @pytest.mark.asyncio
  1916. @pytest.mark.integration
  1917. async def test_both_null_returns_422(self, async_client: AsyncClient, spoolman_settings, mock_spoolman_client):
  1918. """Sending {} (both fields absent) → at_least_one validator → 422."""
  1919. resp = await async_client.patch(
  1920. "/api/v1/spoolman/inventory/spools/42/tag",
  1921. json={},
  1922. )
  1923. assert resp.status_code == 422
  1924. @pytest.mark.asyncio
  1925. @pytest.mark.integration
  1926. async def test_both_explicitly_null_returns_422(
  1927. self, async_client: AsyncClient, spoolman_settings, mock_spoolman_client
  1928. ):
  1929. """Sending {tag_uid: null, tray_uuid: null} → at_least_one validator → 422."""
  1930. resp = await async_client.patch(
  1931. "/api/v1/spoolman/inventory/spools/42/tag",
  1932. json={"tag_uid": None, "tray_uuid": None},
  1933. )
  1934. assert resp.status_code == 422
  1935. # ---------------------------------------------------------------------------
  1936. # F5: RBAC lists — missing endpoints
  1937. # ---------------------------------------------------------------------------
  1938. class TestSpoolmanInventoryAuthExtended:
  1939. """F5: Additional endpoints in RBAC auth/403 parametrize lists."""
  1940. @pytest.fixture
  1941. async def auth_and_spoolman_settings(self, db_session):
  1942. from backend.app.models.settings import Settings
  1943. db_session.add(Settings(key="spoolman_enabled", value="true"))
  1944. db_session.add(Settings(key="spoolman_url", value="http://localhost:7912"))
  1945. db_session.add(Settings(key="auth_enabled", value="true"))
  1946. await db_session.commit()
  1947. @pytest.mark.asyncio
  1948. @pytest.mark.integration
  1949. @pytest.mark.parametrize(
  1950. "method,path,payload",
  1951. [
  1952. ("PATCH", "/api/v1/spoolman/inventory/spools/42/tag", {"tag_uid": "AABBCCDDEE112233"}),
  1953. ("POST", "/api/v1/spoolman/inventory/sync-ams-weights", {"printer_id": 1, "ams_data": []}),
  1954. ("PATCH", "/api/v1/spoolman/inventory/filaments/7", {"spool_weight": 196.0}),
  1955. ],
  1956. )
  1957. async def test_extended_write_endpoints_require_auth(
  1958. self,
  1959. async_client: AsyncClient,
  1960. auth_and_spoolman_settings,
  1961. method: str,
  1962. path: str,
  1963. payload: dict | None,
  1964. ):
  1965. """Additional write endpoints return 401 when auth is enabled and no token is provided."""
  1966. resp = await async_client.request(method, path, json=payload)
  1967. assert resp.status_code == 401, f"{method} {path} should require auth but got {resp.status_code}: {resp.json()}"
  1968. @pytest.mark.asyncio
  1969. @pytest.mark.integration
  1970. @pytest.mark.parametrize(
  1971. "method,path",
  1972. [
  1973. ("GET", "/api/v1/spoolman/inventory/filaments"),
  1974. ],
  1975. )
  1976. async def test_extended_read_endpoints_require_auth(
  1977. self,
  1978. async_client: AsyncClient,
  1979. auth_and_spoolman_settings,
  1980. method: str,
  1981. path: str,
  1982. ):
  1983. """Additional read endpoints return 401 when auth is enabled and no token is provided."""
  1984. resp = await async_client.request(method, path)
  1985. assert resp.status_code == 401, f"{method} {path} should require auth but got {resp.status_code}: {resp.json()}"
  1986. # ---------------------------------------------------------------------------
  1987. # F8: _normalize_filament negative ID returns None
  1988. # ---------------------------------------------------------------------------
  1989. class TestNormalizeFilamentNegativeId:
  1990. """F8: _normalize_filament with negative id → None (was only checking == 0)."""
  1991. def test_normalize_filament_negative_id_returns_none(self):
  1992. from backend.app.api.routes.spoolman_inventory import _normalize_filament
  1993. result = _normalize_filament({"id": -1, "name": "PLA"})
  1994. assert result is None
  1995. def test_normalize_filament_large_negative_id_returns_none(self):
  1996. from backend.app.api.routes.spoolman_inventory import _normalize_filament
  1997. result = _normalize_filament({"id": -999, "name": "PLA"})
  1998. assert result is None
  1999. # ---------------------------------------------------------------------------
  2000. # F9: weight_used > label_weight cross-field validator integration test
  2001. # ---------------------------------------------------------------------------
  2002. class TestCreateSpoolWeightValidation:
  2003. """F9: SpoolmanInventoryCreate.validate_weight_consistency cross-field validator."""
  2004. @pytest.mark.asyncio
  2005. @pytest.mark.integration
  2006. async def test_weight_used_exceeds_label_weight_returns_422(self, async_client: AsyncClient, spoolman_settings):
  2007. """weight_used > label_weight → cross-field validator → 422."""
  2008. resp = await async_client.post(
  2009. "/api/v1/spoolman/inventory/spools",
  2010. json={"material": "PLA", "label_weight": 500, "weight_used": 600},
  2011. )
  2012. assert resp.status_code == 422
  2013. @pytest.mark.asyncio
  2014. @pytest.mark.integration
  2015. async def test_weight_used_equals_label_weight_accepted(
  2016. self, async_client: AsyncClient, spoolman_settings, mock_spoolman_client
  2017. ):
  2018. """weight_used == label_weight is exactly at the boundary → should pass (201)."""
  2019. resp = await async_client.post(
  2020. "/api/v1/spoolman/inventory/spools",
  2021. json={"material": "PLA", "label_weight": 1000, "weight_used": 1000},
  2022. )
  2023. # 201 or 200 (spool created)
  2024. assert resp.status_code in (200, 201)
  2025. @pytest.mark.asyncio
  2026. @pytest.mark.integration
  2027. async def test_create_spool_with_non_default_core_weight_accepted(
  2028. self, async_client: AsyncClient, spoolman_settings, mock_spoolman_client
  2029. ):
  2030. """A3: core_weight != 250 must no longer be rejected → 201."""
  2031. resp = await async_client.post(
  2032. "/api/v1/spoolman/inventory/spools",
  2033. json={"material": "PLA", "label_weight": 1000, "weight_used": 0, "core_weight": 196},
  2034. )
  2035. assert resp.status_code in (200, 201)
  2036. @pytest.mark.asyncio
  2037. @pytest.mark.integration
  2038. async def test_update_spool_with_non_default_core_weight_accepted(
  2039. self, async_client: AsyncClient, spoolman_settings, mock_spoolman_client
  2040. ):
  2041. """A3: PATCH with core_weight != 250 must no longer return 422."""
  2042. resp = await async_client.patch(
  2043. "/api/v1/spoolman/inventory/spools/42",
  2044. json={"core_weight": 300},
  2045. )
  2046. assert resp.status_code == 200
  2047. # ---------------------------------------------------------------------------
  2048. # P8-T1: /slot-assignments/all enriches with printer_name + ams_label
  2049. # ---------------------------------------------------------------------------
  2050. class TestGetAllSlotAssignmentsEnriched:
  2051. """P8-T1: /slot-assignments/all enriches with printer_name + ams_label.
  2052. Regression for InventoryPage LOCATION column showing '-' for Spoolman
  2053. spools because the endpoint only returned 4 raw fields without the
  2054. printer_name + ams_label needed by the UI.
  2055. """
  2056. @pytest.mark.asyncio
  2057. @pytest.mark.integration
  2058. async def test_returns_printer_name_for_existing_printer(
  2059. self, async_client: AsyncClient, db_session, spoolman_settings
  2060. ):
  2061. """printer_name is enriched from the joined Printer relationship."""
  2062. from backend.app.models.printer import Printer
  2063. from backend.app.models.spoolman_slot_assignment import SpoolmanSlotAssignment
  2064. db_session.add(
  2065. Printer(
  2066. id=1,
  2067. name="Sully",
  2068. model="X1C",
  2069. serial_number="SN1",
  2070. ip_address="1.2.3.4",
  2071. access_code="",
  2072. )
  2073. )
  2074. db_session.add(
  2075. SpoolmanSlotAssignment(
  2076. printer_id=1,
  2077. ams_id=0,
  2078. tray_id=2,
  2079. spoolman_spool_id=216,
  2080. )
  2081. )
  2082. await db_session.commit()
  2083. with patch("backend.app.api.routes.spoolman_inventory.printer_manager") as mock_pm:
  2084. mock_pm.get_all_statuses.return_value = {}
  2085. resp = await async_client.get("/api/v1/spoolman/inventory/slot-assignments/all")
  2086. assert resp.status_code == 200
  2087. data = resp.json()
  2088. assert len(data) == 1
  2089. assert data[0]["printer_name"] == "Sully"
  2090. assert data[0]["spoolman_spool_id"] == 216
  2091. assert data[0]["ams_id"] == 0
  2092. assert data[0]["tray_id"] == 2
  2093. assert data[0]["ams_label"] is None
  2094. @pytest.mark.asyncio
  2095. @pytest.mark.integration
  2096. async def test_returns_ams_label_when_label_configured(
  2097. self, async_client: AsyncClient, db_session, spoolman_settings
  2098. ):
  2099. """ams_label is enriched from AmsLabel via printer MQTT serial map."""
  2100. from backend.app.models.ams_label import AmsLabel
  2101. from backend.app.models.printer import Printer
  2102. from backend.app.models.spoolman_slot_assignment import SpoolmanSlotAssignment
  2103. db_session.add(
  2104. Printer(
  2105. id=1,
  2106. name="Sully",
  2107. model="X1C",
  2108. serial_number="SN1",
  2109. ip_address="1.2.3.4",
  2110. access_code="",
  2111. )
  2112. )
  2113. db_session.add(AmsLabel(ams_serial_number="ABC123", label="Top Shelf"))
  2114. db_session.add(
  2115. SpoolmanSlotAssignment(
  2116. printer_id=1,
  2117. ams_id=0,
  2118. tray_id=2,
  2119. spoolman_spool_id=216,
  2120. )
  2121. )
  2122. await db_session.commit()
  2123. mock_state = MagicMock(raw_data={"ams": [{"id": 0, "sn": "ABC123"}]})
  2124. with patch("backend.app.api.routes.spoolman_inventory.printer_manager") as mock_pm:
  2125. mock_pm.get_all_statuses.return_value = {1: mock_state}
  2126. resp = await async_client.get("/api/v1/spoolman/inventory/slot-assignments/all")
  2127. assert resp.status_code == 200
  2128. assert resp.json()[0]["ams_label"] == "Top Shelf"
  2129. @pytest.mark.asyncio
  2130. @pytest.mark.integration
  2131. async def test_synthetic_ams_label_fallback(self, async_client: AsyncClient, db_session, spoolman_settings):
  2132. """Falls back to synthetic 'p{pid}a{ams_id}' key when no MQTT serial available."""
  2133. from backend.app.models.ams_label import AmsLabel
  2134. from backend.app.models.printer import Printer
  2135. from backend.app.models.spoolman_slot_assignment import SpoolmanSlotAssignment
  2136. db_session.add(
  2137. Printer(
  2138. id=1,
  2139. name="Sully",
  2140. model="X1C",
  2141. serial_number="SN1",
  2142. ip_address="1.2.3.4",
  2143. access_code="",
  2144. )
  2145. )
  2146. db_session.add(AmsLabel(ams_serial_number="p1a0", label="Synthetic Label"))
  2147. db_session.add(
  2148. SpoolmanSlotAssignment(
  2149. printer_id=1,
  2150. ams_id=0,
  2151. tray_id=2,
  2152. spoolman_spool_id=216,
  2153. )
  2154. )
  2155. await db_session.commit()
  2156. with patch("backend.app.api.routes.spoolman_inventory.printer_manager") as mock_pm:
  2157. mock_pm.get_all_statuses.return_value = {} # No live state -> synthetic key
  2158. resp = await async_client.get("/api/v1/spoolman/inventory/slot-assignments/all")
  2159. assert resp.json()[0]["ams_label"] == "Synthetic Label"
  2160. @pytest.mark.asyncio
  2161. @pytest.mark.integration
  2162. async def test_filter_by_printer_id_still_works(self, async_client: AsyncClient, db_session, spoolman_settings):
  2163. """Regression: ?printer_id=N still filters and enriches."""
  2164. from backend.app.models.printer import Printer
  2165. from backend.app.models.spoolman_slot_assignment import SpoolmanSlotAssignment
  2166. for pid in (1, 2):
  2167. db_session.add(
  2168. Printer(
  2169. id=pid,
  2170. name=f"P{pid}",
  2171. model="X1C",
  2172. serial_number=f"SN{pid}",
  2173. ip_address=f"1.2.3.{pid}",
  2174. access_code="",
  2175. )
  2176. )
  2177. db_session.add(
  2178. SpoolmanSlotAssignment(
  2179. printer_id=pid,
  2180. ams_id=0,
  2181. tray_id=0,
  2182. spoolman_spool_id=200 + pid,
  2183. )
  2184. )
  2185. await db_session.commit()
  2186. with patch("backend.app.api.routes.spoolman_inventory.printer_manager") as mock_pm:
  2187. mock_pm.get_all_statuses.return_value = {}
  2188. resp = await async_client.get("/api/v1/spoolman/inventory/slot-assignments/all?printer_id=1")
  2189. data = resp.json()
  2190. assert len(data) == 1
  2191. assert data[0]["printer_id"] == 1
  2192. assert data[0]["printer_name"] == "P1"
  2193. assert data[0]["spoolman_spool_id"] == 201