test_spoolman_inventory_api.py 108 KB

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