test_labels.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 ("ams_30x15", "box_62x29", "avery_5160", "avery_l7160"):
  55. resp = await async_client.post(
  56. "/api/v1/inventory/labels",
  57. json={"spool_ids": [s.id], "template": template},
  58. )
  59. assert resp.status_code == 200, f"{template} returned {resp.status_code}: {resp.text}"
  60. assert resp.content.startswith(b"%PDF")
  61. @pytest.mark.asyncio
  62. @pytest.mark.integration
  63. async def test_unknown_template_rejected(self, async_client: AsyncClient, spool_factory):
  64. s = await spool_factory()
  65. resp = await async_client.post(
  66. "/api/v1/inventory/labels",
  67. json={"spool_ids": [s.id], "template": "totally_made_up"},
  68. )
  69. # Pydantic Literal validation → 422
  70. assert resp.status_code in (400, 422)
  71. @pytest.mark.asyncio
  72. @pytest.mark.integration
  73. async def test_empty_spool_ids_rejected(self, async_client: AsyncClient):
  74. resp = await async_client.post(
  75. "/api/v1/inventory/labels",
  76. json={"spool_ids": [], "template": "box_62x29"},
  77. )
  78. assert resp.status_code == 422
  79. @pytest.mark.asyncio
  80. @pytest.mark.integration
  81. async def test_unknown_spool_id_returns_404(self, async_client: AsyncClient, spool_factory):
  82. s = await spool_factory()
  83. resp = await async_client.post(
  84. "/api/v1/inventory/labels",
  85. json={"spool_ids": [s.id, 99999], "template": "ams_30x15"},
  86. )
  87. assert resp.status_code == 404
  88. assert "99999" in resp.text
  89. @pytest.mark.asyncio
  90. @pytest.mark.integration
  91. async def test_preserves_request_order(self, async_client: AsyncClient, spool_factory):
  92. """Caller's `spool_ids` order should match the on-screen list — important
  93. for Avery sheet layouts where users curate the layout via filtering."""
  94. s1 = await spool_factory()
  95. s2 = await spool_factory()
  96. s3 = await spool_factory()
  97. # Reverse order; assert the route doesn't sort them. We can't peek
  98. # inside the PDF for assertion, but we can call render_labels directly
  99. # under the same patches and compare bytes deterministically.
  100. from backend.app.api.routes import labels as labels_module
  101. captured = {}
  102. original = labels_module.render_labels
  103. def _capture(template, data_list):
  104. captured["ids"] = [d.spool_id for d in data_list]
  105. return original(template, data_list)
  106. with patch.object(labels_module, "render_labels", side_effect=_capture):
  107. resp = await async_client.post(
  108. "/api/v1/inventory/labels",
  109. json={"spool_ids": [s3.id, s1.id, s2.id], "template": "avery_l7160"},
  110. )
  111. assert resp.status_code == 200
  112. assert captured["ids"] == [s3.id, s1.id, s2.id]
  113. # ── /spoolman/labels (Spoolman-backed) ───────────────────────────────────────
  114. class TestSpoolmanLabels:
  115. @pytest.mark.asyncio
  116. @pytest.mark.integration
  117. async def test_returns_400_when_spoolman_disabled(self, async_client: AsyncClient):
  118. # Default state in tests: spoolman_enabled is unset / "false"
  119. resp = await async_client.post(
  120. "/api/v1/spoolman/labels",
  121. json={"spool_ids": [1], "template": "box_62x29"},
  122. )
  123. assert resp.status_code == 400
  124. assert "Spoolman" in resp.text
  125. @pytest.mark.asyncio
  126. @pytest.mark.integration
  127. async def test_returns_503_when_spoolman_unreachable(self, async_client: AsyncClient, db_session: AsyncSession):
  128. from backend.app.models.settings import Settings
  129. db_session.add(Settings(key="spoolman_enabled", value="true"))
  130. await db_session.commit()
  131. with patch("backend.app.api.routes.labels.get_spoolman_client", AsyncMock(return_value=None)):
  132. resp = await async_client.post(
  133. "/api/v1/spoolman/labels",
  134. json={"spool_ids": [1], "template": "box_62x29"},
  135. )
  136. assert resp.status_code == 503
  137. @pytest.mark.asyncio
  138. @pytest.mark.integration
  139. async def test_renders_pdf_from_spoolman_data(self, async_client: AsyncClient, db_session: AsyncSession):
  140. from backend.app.models.settings import Settings
  141. db_session.add(Settings(key="spoolman_enabled", value="true"))
  142. await db_session.commit()
  143. spoolman_spool = {
  144. "id": 42,
  145. "filament": {
  146. "name": "PolyTerra Sapphire Blue",
  147. "material": "PLA",
  148. "color_hex": "0033AA",
  149. "vendor": {"name": "Polymaker"},
  150. },
  151. "location": "Shelf 5, slot C",
  152. }
  153. mock_client = MagicMock()
  154. mock_client.is_connected = True
  155. mock_client.get_spools = AsyncMock(return_value=[spoolman_spool])
  156. with patch(
  157. "backend.app.api.routes.labels.get_spoolman_client",
  158. AsyncMock(return_value=mock_client),
  159. ):
  160. resp = await async_client.post(
  161. "/api/v1/spoolman/labels",
  162. json={"spool_ids": [42], "template": "avery_l7160"},
  163. )
  164. assert resp.status_code == 200
  165. assert resp.headers["content-type"] == "application/pdf"
  166. assert resp.content.startswith(b"%PDF")
  167. @pytest.mark.asyncio
  168. @pytest.mark.integration
  169. async def test_returns_404_when_spool_missing_from_spoolman(
  170. self, async_client: AsyncClient, db_session: AsyncSession
  171. ):
  172. from backend.app.models.settings import Settings
  173. db_session.add(Settings(key="spoolman_enabled", value="true"))
  174. await db_session.commit()
  175. mock_client = MagicMock()
  176. mock_client.is_connected = True
  177. mock_client.get_spools = AsyncMock(return_value=[{"id": 1, "filament": {"name": "X", "material": "PLA"}}])
  178. with patch(
  179. "backend.app.api.routes.labels.get_spoolman_client",
  180. AsyncMock(return_value=mock_client),
  181. ):
  182. resp = await async_client.post(
  183. "/api/v1/spoolman/labels",
  184. json={"spool_ids": [99], "template": "box_62x29"},
  185. )
  186. assert resp.status_code == 404
  187. assert "99" in resp.text
  188. # ── Validation cross-cutting ─────────────────────────────────────────────────
  189. class TestValidation:
  190. @pytest.mark.asyncio
  191. @pytest.mark.integration
  192. async def test_request_body_size_capped(self, async_client: AsyncClient):
  193. """spool_ids is bounded to MAX_LABELS_PER_REQUEST so a runaway client
  194. can't flood the renderer."""
  195. from backend.app.api.routes.labels import MAX_LABELS_PER_REQUEST
  196. resp = await async_client.post(
  197. "/api/v1/inventory/labels",
  198. json={
  199. "spool_ids": list(range(1, MAX_LABELS_PER_REQUEST + 2)),
  200. "template": "box_62x29",
  201. },
  202. )
  203. assert resp.status_code == 422