test_spoolman_inventory_api.py 102 KB

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