test_panda_breath_api.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. """Panda Breath routes (#1466): the command goes to the heater bound to the
  2. printer in the path, found by its serial number, never by a caller's id."""
  3. import json
  4. from unittest.mock import MagicMock, patch
  5. import pytest
  6. from httpx import AsyncClient
  7. from backend.app.services.panda_breath import panda_breath_service
  8. @pytest.fixture
  9. def bound(printer_factory):
  10. """A printer with a Panda Breath bound to it, on a connected broker."""
  11. created = []
  12. async def _make(**kwargs):
  13. printer = await printer_factory(name="P", **kwargs)
  14. created.append(printer)
  15. with patch("backend.app.services.printer_manager.printer_manager") as pm:
  16. pm.printer_id_for_serial.return_value = None
  17. panda_breath_service.apply_message(
  18. "panda_breath/AABBCC/state",
  19. json.dumps({"chamber_temp": 40, "work_on": "ON", "printer_sn": printer.serial_number}),
  20. )
  21. return printer
  22. client = MagicMock()
  23. client.publish.return_value = MagicMock(rc=0)
  24. saved = (panda_breath_service.enabled, panda_breath_service.client, panda_breath_service.connected)
  25. panda_breath_service.enabled = True
  26. panda_breath_service.client = client
  27. panda_breath_service.connected = True
  28. yield _make, client
  29. panda_breath_service.enabled, panda_breath_service.client, panda_breath_service.connected = saved
  30. panda_breath_service.devices.clear()
  31. class TestPandaBreathCommand:
  32. @pytest.mark.asyncio
  33. @pytest.mark.integration
  34. @pytest.mark.parametrize(
  35. ("body", "key", "value"),
  36. [
  37. ({"work_on": False}, "work_on", "OFF"),
  38. ({"work_on": True}, "work_on", "ON"),
  39. ({"mode": "filament drying"}, "mode", "filament drying"),
  40. ({"target_temp": 55}, "target_temp", 55),
  41. ],
  42. )
  43. async def test_sends_the_change_to_the_bound_device(self, async_client: AsyncClient, bound, body, key, value):
  44. make, client = bound
  45. printer = await make()
  46. response = await async_client.post(f"/api/v1/printers/{printer.id}/panda-breath", json=body)
  47. assert response.status_code == 200
  48. client.publish.assert_called_once_with(
  49. "panda_breath/AABBCC/command", json.dumps({key: value}), qos=1, retain=False
  50. )
  51. @pytest.mark.asyncio
  52. @pytest.mark.integration
  53. @pytest.mark.parametrize("body", [{}, {"work_on": True, "mode": "auto mode"}])
  54. async def test_exactly_one_change(self, async_client: AsyncClient, bound, body):
  55. make, client = bound
  56. printer = await make()
  57. response = await async_client.post(f"/api/v1/printers/{printer.id}/panda-breath", json=body)
  58. assert response.status_code == 400
  59. client.publish.assert_not_called()
  60. @pytest.mark.asyncio
  61. @pytest.mark.integration
  62. @pytest.mark.parametrize("body", [{"mode": "turbo"}, {"target_temp": 61}, {"target_temp": -1}])
  63. async def test_values_the_firmware_does_not_take_are_refused(self, async_client: AsyncClient, bound, body):
  64. make, client = bound
  65. printer = await make()
  66. response = await async_client.post(f"/api/v1/printers/{printer.id}/panda-breath", json=body)
  67. assert response.status_code == 422
  68. client.publish.assert_not_called()
  69. @pytest.mark.asyncio
  70. @pytest.mark.integration
  71. async def test_printer_without_a_device(self, async_client: AsyncClient, bound, printer_factory):
  72. make, client = bound
  73. await make()
  74. other = await printer_factory(name="Other")
  75. response = await async_client.post(f"/api/v1/printers/{other.id}/panda-breath", json={"work_on": False})
  76. assert response.status_code == 404
  77. client.publish.assert_not_called()
  78. @pytest.mark.asyncio
  79. @pytest.mark.integration
  80. async def test_unknown_printer(self, async_client: AsyncClient, bound):
  81. response = await async_client.post("/api/v1/printers/99999/panda-breath", json={"work_on": False})
  82. assert response.status_code == 404
  83. @pytest.mark.asyncio
  84. @pytest.mark.integration
  85. async def test_broker_down(self, async_client: AsyncClient, bound):
  86. make, client = bound
  87. printer = await make()
  88. panda_breath_service.connected = False
  89. response = await async_client.post(f"/api/v1/printers/{printer.id}/panda-breath", json={"work_on": False})
  90. assert response.status_code == 503
  91. client.publish.assert_not_called()
  92. class TestPandaBreathInStatus:
  93. @pytest.mark.asyncio
  94. @pytest.mark.integration
  95. async def test_status_of_a_disconnected_printer_carries_it(self, async_client: AsyncClient, bound):
  96. """The heater runs with the printer off, so the offline card still gets it,
  97. matched by the stored serial number (the printer has no live connection)."""
  98. make, _ = bound
  99. printer = await make()
  100. response = await async_client.get(f"/api/v1/printers/{printer.id}/status")
  101. assert response.status_code == 200
  102. body = response.json()
  103. assert body["connected"] is False
  104. assert body["panda_breath"]["device_id"] == "AABBCC"
  105. assert body["panda_breath"]["chamber_temp"] == 40.0
  106. @pytest.mark.asyncio
  107. @pytest.mark.integration
  108. async def test_settings_status_names_the_printer(self, async_client: AsyncClient, bound):
  109. make, _ = bound
  110. printer = await make()
  111. response = await async_client.get("/api/v1/settings/panda-breath/status")
  112. assert response.status_code == 200
  113. [row] = response.json()["devices"]
  114. assert (row["device_id"], row["printer_id"], row["printer_name"]) == ("AABBCC", printer.id, "P")
  115. @pytest.mark.asyncio
  116. @pytest.mark.integration
  117. async def test_settings_status_hides_printers_out_of_scope(self, async_client: AsyncClient, bound):
  118. from backend.app.core.auth import get_printer_scope_if_auth_enabled
  119. from backend.app.core.printer_scope import PrinterScope
  120. from backend.app.main import app
  121. make, _ = bound
  122. printer = await make()
  123. app.dependency_overrides[get_printer_scope_if_auth_enabled] = lambda: PrinterScope(frozenset({printer.id + 1}))
  124. try:
  125. response = await async_client.get("/api/v1/settings/panda-breath/status")
  126. finally:
  127. app.dependency_overrides.pop(get_printer_scope_if_auth_enabled, None)
  128. [row] = response.json()["devices"]
  129. assert row["bound"] is True
  130. assert row["printer_name"] is None
  131. class TestPandaBreathSetting:
  132. @pytest.mark.asyncio
  133. @pytest.mark.integration
  134. async def test_saving_the_switch_reconfigures_the_service(self, async_client: AsyncClient):
  135. with patch("backend.app.services.panda_breath.panda_breath_service.configure") as configure:
  136. response = await async_client.put("/api/v1/settings/", json={"panda_breath_enabled": True})
  137. assert response.status_code == 200
  138. assert response.json()["panda_breath_enabled"] is True
  139. assert configure.call_args.args[0]["panda_breath_enabled"] is True