test_spoolman_inventory_api.py 104 KB

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