| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- """Panda Breath routes (#1466): the command goes to the heater bound to the
- printer in the path, found by its serial number, never by a caller's id."""
- import json
- from unittest.mock import MagicMock, patch
- import pytest
- from httpx import AsyncClient
- from backend.app.services.panda_breath import panda_breath_service
- @pytest.fixture
- def bound(printer_factory):
- """A printer with a Panda Breath bound to it, on a connected broker."""
- created = []
- async def _make(**kwargs):
- printer = await printer_factory(name="P", **kwargs)
- created.append(printer)
- with patch("backend.app.services.printer_manager.printer_manager") as pm:
- pm.printer_id_for_serial.return_value = None
- panda_breath_service.apply_message(
- "panda_breath/AABBCC/state",
- json.dumps({"chamber_temp": 40, "work_on": "ON", "printer_sn": printer.serial_number}),
- )
- return printer
- client = MagicMock()
- client.publish.return_value = MagicMock(rc=0)
- saved = (panda_breath_service.enabled, panda_breath_service.client, panda_breath_service.connected)
- panda_breath_service.enabled = True
- panda_breath_service.client = client
- panda_breath_service.connected = True
- yield _make, client
- panda_breath_service.enabled, panda_breath_service.client, panda_breath_service.connected = saved
- panda_breath_service.devices.clear()
- class TestPandaBreathCommand:
- @pytest.mark.asyncio
- @pytest.mark.integration
- @pytest.mark.parametrize(
- ("body", "key", "value"),
- [
- ({"work_on": False}, "work_on", "OFF"),
- ({"work_on": True}, "work_on", "ON"),
- ({"mode": "filament drying"}, "mode", "filament drying"),
- ({"target_temp": 55}, "target_temp", 55),
- ],
- )
- async def test_sends_the_change_to_the_bound_device(self, async_client: AsyncClient, bound, body, key, value):
- make, client = bound
- printer = await make()
- response = await async_client.post(f"/api/v1/printers/{printer.id}/panda-breath", json=body)
- assert response.status_code == 200
- client.publish.assert_called_once_with(
- "panda_breath/AABBCC/command", json.dumps({key: value}), qos=1, retain=False
- )
- @pytest.mark.asyncio
- @pytest.mark.integration
- @pytest.mark.parametrize("body", [{}, {"work_on": True, "mode": "auto mode"}])
- async def test_exactly_one_change(self, async_client: AsyncClient, bound, body):
- make, client = bound
- printer = await make()
- response = await async_client.post(f"/api/v1/printers/{printer.id}/panda-breath", json=body)
- assert response.status_code == 400
- client.publish.assert_not_called()
- @pytest.mark.asyncio
- @pytest.mark.integration
- @pytest.mark.parametrize("body", [{"mode": "turbo"}, {"target_temp": 61}, {"target_temp": -1}])
- async def test_values_the_firmware_does_not_take_are_refused(self, async_client: AsyncClient, bound, body):
- make, client = bound
- printer = await make()
- response = await async_client.post(f"/api/v1/printers/{printer.id}/panda-breath", json=body)
- assert response.status_code == 422
- client.publish.assert_not_called()
- @pytest.mark.asyncio
- @pytest.mark.integration
- async def test_printer_without_a_device(self, async_client: AsyncClient, bound, printer_factory):
- make, client = bound
- await make()
- other = await printer_factory(name="Other")
- response = await async_client.post(f"/api/v1/printers/{other.id}/panda-breath", json={"work_on": False})
- assert response.status_code == 404
- client.publish.assert_not_called()
- @pytest.mark.asyncio
- @pytest.mark.integration
- async def test_unknown_printer(self, async_client: AsyncClient, bound):
- response = await async_client.post("/api/v1/printers/99999/panda-breath", json={"work_on": False})
- assert response.status_code == 404
- @pytest.mark.asyncio
- @pytest.mark.integration
- async def test_broker_down(self, async_client: AsyncClient, bound):
- make, client = bound
- printer = await make()
- panda_breath_service.connected = False
- response = await async_client.post(f"/api/v1/printers/{printer.id}/panda-breath", json={"work_on": False})
- assert response.status_code == 503
- client.publish.assert_not_called()
- class TestPandaBreathInStatus:
- @pytest.mark.asyncio
- @pytest.mark.integration
- async def test_status_of_a_disconnected_printer_carries_it(self, async_client: AsyncClient, bound):
- """The heater runs with the printer off, so the offline card still gets it,
- matched by the stored serial number (the printer has no live connection)."""
- make, _ = bound
- printer = await make()
- response = await async_client.get(f"/api/v1/printers/{printer.id}/status")
- assert response.status_code == 200
- body = response.json()
- assert body["connected"] is False
- assert body["panda_breath"]["device_id"] == "AABBCC"
- assert body["panda_breath"]["chamber_temp"] == 40.0
- @pytest.mark.asyncio
- @pytest.mark.integration
- async def test_settings_status_names_the_printer(self, async_client: AsyncClient, bound):
- make, _ = bound
- printer = await make()
- response = await async_client.get("/api/v1/settings/panda-breath/status")
- assert response.status_code == 200
- [row] = response.json()["devices"]
- assert (row["device_id"], row["printer_id"], row["printer_name"]) == ("AABBCC", printer.id, "P")
- @pytest.mark.asyncio
- @pytest.mark.integration
- async def test_settings_status_hides_printers_out_of_scope(self, async_client: AsyncClient, bound):
- from backend.app.core.auth import get_printer_scope_if_auth_enabled
- from backend.app.core.printer_scope import PrinterScope
- from backend.app.main import app
- make, _ = bound
- printer = await make()
- app.dependency_overrides[get_printer_scope_if_auth_enabled] = lambda: PrinterScope(frozenset({printer.id + 1}))
- try:
- response = await async_client.get("/api/v1/settings/panda-breath/status")
- finally:
- app.dependency_overrides.pop(get_printer_scope_if_auth_enabled, None)
- [row] = response.json()["devices"]
- assert row["bound"] is True
- assert row["printer_name"] is None
- class TestPandaBreathSetting:
- @pytest.mark.asyncio
- @pytest.mark.integration
- async def test_saving_the_switch_reconfigures_the_service(self, async_client: AsyncClient):
- with patch("backend.app.services.panda_breath.panda_breath_service.configure") as configure:
- response = await async_client.put("/api/v1/settings/", json={"panda_breath_enabled": True})
- assert response.status_code == 200
- assert response.json()["panda_breath_enabled"] is True
- assert configure.call_args.args[0]["panda_breath_enabled"] is True
|