test_labels.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. """Integration tests for the spool-label routes (#809).
  2. Covers both ``POST /inventory/labels`` (local DB) and ``POST /spoolman/labels``
  3. (Spoolman-backed). The renderer itself has its own unit tests; these tests
  4. focus on auth, request validation, mode gating, and the wiring between route
  5. and renderer.
  6. """
  7. from __future__ import annotations
  8. from unittest.mock import AsyncMock, MagicMock, patch
  9. import pytest
  10. from httpx import AsyncClient
  11. from sqlalchemy.ext.asyncio import AsyncSession
  12. from backend.app.models.spool import Spool
  13. @pytest.fixture
  14. async def spool_factory(db_session: AsyncSession):
  15. """Factory to create test spools."""
  16. _counter = [0]
  17. async def _create_spool(**kwargs):
  18. _counter[0] += 1
  19. defaults = {
  20. "material": "PLA",
  21. "subtype": "Basic",
  22. "brand": "Polymaker",
  23. "color_name": f"Test {_counter[0]}",
  24. "rgba": "FF8800FF",
  25. "label_weight": 1000,
  26. "weight_used": 0,
  27. }
  28. defaults.update(kwargs)
  29. spool = Spool(**defaults)
  30. db_session.add(spool)
  31. await db_session.commit()
  32. await db_session.refresh(spool)
  33. return spool
  34. return _create_spool
  35. # ── /inventory/labels (local DB) ─────────────────────────────────────────────
  36. class TestLocalInventoryLabels:
  37. @pytest.mark.asyncio
  38. @pytest.mark.integration
  39. async def test_renders_pdf_for_local_spools(self, async_client: AsyncClient, spool_factory):
  40. s1 = await spool_factory()
  41. s2 = await spool_factory(material="PETG", brand="Sunlu")
  42. resp = await async_client.post(
  43. "/api/v1/inventory/labels",
  44. json={"spool_ids": [s1.id, s2.id], "template": "box_62x29"},
  45. )
  46. assert resp.status_code == 200
  47. assert resp.headers["content-type"] == "application/pdf"
  48. assert resp.content.startswith(b"%PDF")
  49. assert int(resp.headers["content-length"]) == len(resp.content)
  50. @pytest.mark.asyncio
  51. @pytest.mark.integration
  52. async def test_all_four_templates_succeed(self, async_client: AsyncClient, spool_factory):
  53. s = await spool_factory()
  54. for template in (
  55. "ams_holder_74x33",
  56. "ams_holder_75x55",
  57. "box_62x29",
  58. "avery_5160",
  59. "avery_l7160",
  60. ):
  61. resp = await async_client.post(
  62. "/api/v1/inventory/labels",
  63. json={"spool_ids": [s.id], "template": template},
  64. )
  65. assert resp.status_code == 200, f"{template} returned {resp.status_code}: {resp.text}"
  66. assert resp.content.startswith(b"%PDF")
  67. @pytest.mark.asyncio
  68. @pytest.mark.integration
  69. async def test_unknown_template_rejected(self, async_client: AsyncClient, spool_factory):
  70. s = await spool_factory()
  71. resp = await async_client.post(
  72. "/api/v1/inventory/labels",
  73. json={"spool_ids": [s.id], "template": "totally_made_up"},
  74. )
  75. # Pydantic Literal validation → 422
  76. assert resp.status_code in (400, 422)
  77. @pytest.mark.asyncio
  78. @pytest.mark.integration
  79. async def test_empty_spool_ids_rejected(self, async_client: AsyncClient):
  80. resp = await async_client.post(
  81. "/api/v1/inventory/labels",
  82. json={"spool_ids": [], "template": "box_62x29"},
  83. )
  84. assert resp.status_code == 422
  85. @pytest.mark.asyncio
  86. @pytest.mark.integration
  87. async def test_unknown_spool_id_returns_404(self, async_client: AsyncClient, spool_factory):
  88. s = await spool_factory()
  89. resp = await async_client.post(
  90. "/api/v1/inventory/labels",
  91. json={"spool_ids": [s.id, 99999], "template": "ams_holder_74x33"},
  92. )
  93. assert resp.status_code == 404
  94. assert "99999" in resp.text
  95. @pytest.mark.asyncio
  96. @pytest.mark.integration
  97. async def test_preserves_request_order(self, async_client: AsyncClient, spool_factory):
  98. """Caller's `spool_ids` order should match the on-screen list — important
  99. for Avery sheet layouts where users curate the layout via filtering."""
  100. s1 = await spool_factory()
  101. s2 = await spool_factory()
  102. s3 = await spool_factory()
  103. # Reverse order; assert the route doesn't sort them. We can't peek
  104. # inside the PDF for assertion, but we can call render_labels directly
  105. # under the same patches and compare bytes deterministically.
  106. from backend.app.api.routes import labels as labels_module
  107. captured = {}
  108. original = labels_module.render_labels
  109. def _capture(template, data_list, **kwargs):
  110. captured["ids"] = [d.spool_id for d in data_list]
  111. return original(template, data_list, **kwargs)
  112. with patch.object(labels_module, "render_labels", side_effect=_capture):
  113. resp = await async_client.post(
  114. "/api/v1/inventory/labels",
  115. json={"spool_ids": [s3.id, s1.id, s2.id], "template": "avery_l7160"},
  116. )
  117. assert resp.status_code == 200
  118. assert captured["ids"] == [s3.id, s1.id, s2.id]
  119. @pytest.mark.asyncio
  120. @pytest.mark.integration
  121. async def test_forwards_sheet_starting_position(self, async_client: AsyncClient, spool_factory):
  122. spool = await spool_factory()
  123. from backend.app.api.routes import labels as labels_module
  124. captured = {}
  125. original = labels_module.render_labels
  126. def _capture(template, data_list, **kwargs):
  127. captured["starting_position"] = kwargs["starting_position"]
  128. return original(template, data_list, **kwargs)
  129. with patch.object(labels_module, "render_labels", side_effect=_capture):
  130. resp = await async_client.post(
  131. "/api/v1/inventory/labels",
  132. json={"spool_ids": [spool.id], "template": "avery_5160", "starting_position": 8},
  133. )
  134. assert resp.status_code == 200
  135. assert captured["starting_position"] == 8
  136. # ── /spoolman/labels (Spoolman-backed) ───────────────────────────────────────
  137. class TestSpoolmanLabels:
  138. @pytest.mark.asyncio
  139. @pytest.mark.integration
  140. async def test_returns_400_when_spoolman_disabled(self, async_client: AsyncClient):
  141. # Default state in tests: spoolman_enabled is unset / "false"
  142. resp = await async_client.post(
  143. "/api/v1/spoolman/labels",
  144. json={"spool_ids": [1], "template": "box_62x29"},
  145. )
  146. assert resp.status_code == 400
  147. assert "Spoolman" in resp.text
  148. @pytest.mark.asyncio
  149. @pytest.mark.integration
  150. async def test_returns_503_when_spoolman_unreachable(self, async_client: AsyncClient, db_session: AsyncSession):
  151. from backend.app.models.settings import Settings
  152. db_session.add(Settings(key="spoolman_enabled", value="true"))
  153. await db_session.commit()
  154. with patch("backend.app.api.routes.labels.get_spoolman_client", AsyncMock(return_value=None)):
  155. resp = await async_client.post(
  156. "/api/v1/spoolman/labels",
  157. json={"spool_ids": [1], "template": "box_62x29"},
  158. )
  159. assert resp.status_code == 503
  160. @pytest.mark.asyncio
  161. @pytest.mark.integration
  162. async def test_renders_pdf_from_spoolman_data(self, async_client: AsyncClient, db_session: AsyncSession):
  163. from backend.app.models.settings import Settings
  164. db_session.add(Settings(key="spoolman_enabled", value="true"))
  165. await db_session.commit()
  166. spoolman_spool = {
  167. "id": 42,
  168. "filament": {
  169. "name": "PolyTerra Sapphire Blue",
  170. "material": "PLA",
  171. "color_hex": "0033AA",
  172. "vendor": {"name": "Polymaker"},
  173. },
  174. "location": "Shelf 5, slot C",
  175. }
  176. mock_client = MagicMock()
  177. mock_client.is_connected = True
  178. mock_client.get_spools = AsyncMock(return_value=[spoolman_spool])
  179. with patch(
  180. "backend.app.api.routes.labels.get_spoolman_client",
  181. AsyncMock(return_value=mock_client),
  182. ):
  183. resp = await async_client.post(
  184. "/api/v1/spoolman/labels",
  185. json={"spool_ids": [42], "template": "avery_l7160"},
  186. )
  187. assert resp.status_code == 200
  188. assert resp.headers["content-type"] == "application/pdf"
  189. assert resp.content.startswith(b"%PDF")
  190. @pytest.mark.asyncio
  191. @pytest.mark.integration
  192. async def test_returns_404_when_spool_missing_from_spoolman(
  193. self, async_client: AsyncClient, db_session: AsyncSession
  194. ):
  195. from backend.app.models.settings import Settings
  196. db_session.add(Settings(key="spoolman_enabled", value="true"))
  197. await db_session.commit()
  198. mock_client = MagicMock()
  199. mock_client.is_connected = True
  200. mock_client.get_spools = AsyncMock(return_value=[{"id": 1, "filament": {"name": "X", "material": "PLA"}}])
  201. with patch(
  202. "backend.app.api.routes.labels.get_spoolman_client",
  203. AsyncMock(return_value=mock_client),
  204. ):
  205. resp = await async_client.post(
  206. "/api/v1/spoolman/labels",
  207. json={"spool_ids": [99], "template": "box_62x29"},
  208. )
  209. assert resp.status_code == 404
  210. assert "99" in resp.text
  211. # ── Validation cross-cutting ─────────────────────────────────────────────────
  212. class TestValidation:
  213. @pytest.mark.asyncio
  214. @pytest.mark.integration
  215. @pytest.mark.parametrize(
  216. ("template", "starting_position"),
  217. (("avery_5160", 0), ("avery_5160", 31), ("avery_l7160", 22), ("box_62x29", 2)),
  218. )
  219. async def test_invalid_starting_position_rejected(
  220. self,
  221. async_client: AsyncClient,
  222. template: str,
  223. starting_position: int,
  224. ):
  225. resp = await async_client.post(
  226. "/api/v1/inventory/labels",
  227. json={"spool_ids": [1], "template": template, "starting_position": starting_position},
  228. )
  229. assert resp.status_code == 422
  230. @pytest.mark.asyncio
  231. @pytest.mark.integration
  232. async def test_request_body_size_capped(self, async_client: AsyncClient):
  233. """spool_ids is bounded to MAX_LABELS_PER_REQUEST so a runaway client
  234. can't flood the renderer."""
  235. from backend.app.api.routes.labels import MAX_LABELS_PER_REQUEST
  236. resp = await async_client.post(
  237. "/api/v1/inventory/labels",
  238. json={
  239. "spool_ids": list(range(1, MAX_LABELS_PER_REQUEST + 2)),
  240. "template": "box_62x29",
  241. },
  242. )
  243. assert resp.status_code == 422