test_spoolman_inventory_api.py 101 KB

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