test_plate_clear_per_printer_2860.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. """A printer's own "Require plate-clear confirmation" through the API (#2860).
  2. The value lives on the printer row and is cached for the scheduler, WLED and
  3. the notification; the cache must follow every write, and a deleted printer's
  4. override must not pass to a later printer given the same id.
  5. """
  6. from unittest.mock import patch
  7. import pytest
  8. from httpx import AsyncClient
  9. from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
  10. from backend.app.services import plate_clear
  11. from backend.app.services.printer_manager import PrinterManager
  12. @pytest.fixture(autouse=True)
  13. def overrides():
  14. plate_clear.load_overrides({})
  15. yield plate_clear
  16. plate_clear.load_overrides({})
  17. @pytest.mark.asyncio
  18. @pytest.mark.integration
  19. async def test_new_printer_follows_the_setting(async_client: AsyncClient, printer_factory):
  20. printer = await printer_factory()
  21. response = await async_client.get(f"/api/v1/printers/{printer.id}")
  22. assert response.status_code == 200
  23. assert response.json()["require_plate_clear"] is None
  24. @pytest.mark.asyncio
  25. @pytest.mark.integration
  26. async def test_update_round_trips_and_reaches_the_cache(async_client: AsyncClient, printer_factory):
  27. printer = await printer_factory()
  28. response = await async_client.patch(f"/api/v1/printers/{printer.id}", json={"require_plate_clear": False})
  29. assert response.status_code == 200
  30. assert response.json()["require_plate_clear"] is False
  31. assert plate_clear.applies(printer.id, True) is False
  32. response = await async_client.patch(f"/api/v1/printers/{printer.id}", json={"require_plate_clear": True})
  33. assert response.json()["require_plate_clear"] is True
  34. assert plate_clear.applies(printer.id, False) is True
  35. @pytest.mark.asyncio
  36. @pytest.mark.integration
  37. async def test_null_returns_to_the_setting(async_client: AsyncClient, printer_factory):
  38. printer = await printer_factory()
  39. await async_client.patch(f"/api/v1/printers/{printer.id}", json={"require_plate_clear": False})
  40. response = await async_client.patch(f"/api/v1/printers/{printer.id}", json={"require_plate_clear": None})
  41. assert response.status_code == 200
  42. assert response.json()["require_plate_clear"] is None
  43. assert plate_clear.applies(printer.id, True) is True
  44. @pytest.mark.asyncio
  45. @pytest.mark.integration
  46. async def test_other_updates_leave_it_alone(async_client: AsyncClient, printer_factory):
  47. printer = await printer_factory()
  48. await async_client.patch(f"/api/v1/printers/{printer.id}", json={"require_plate_clear": False})
  49. response = await async_client.patch(f"/api/v1/printers/{printer.id}", json={"name": "Renamed"})
  50. assert response.json()["require_plate_clear"] is False
  51. assert plate_clear.applies(printer.id, True) is False
  52. @pytest.mark.asyncio
  53. @pytest.mark.integration
  54. async def test_delete_drops_the_override(async_client: AsyncClient, printer_factory):
  55. printer = await printer_factory()
  56. await async_client.patch(f"/api/v1/printers/{printer.id}", json={"require_plate_clear": False})
  57. response = await async_client.delete(f"/api/v1/printers/{printer.id}")
  58. assert response.status_code == 200
  59. assert plate_clear.applies(printer.id, True) is True
  60. @pytest.mark.asyncio
  61. @pytest.mark.integration
  62. async def test_a_failed_delete_keeps_the_override(async_client: AsyncClient, printer_factory):
  63. """The override goes only once the printer row is gone: a delete that
  64. fails before its commit leaves the printer, and its setting, as they were."""
  65. printer = await printer_factory()
  66. await async_client.patch(f"/api/v1/printers/{printer.id}", json={"require_plate_clear": False})
  67. with (
  68. patch(
  69. "backend.app.core.printer_scope.drop_printers_from_api_keys",
  70. side_effect=RuntimeError("boom"),
  71. ),
  72. pytest.raises(RuntimeError),
  73. ):
  74. await async_client.delete(f"/api/v1/printers/{printer.id}")
  75. assert plate_clear.applies(printer.id, True) is False
  76. @pytest.mark.asyncio
  77. @pytest.mark.integration
  78. async def test_startup_loads_the_overrides(test_engine, printer_factory, overrides):
  79. on = await printer_factory(require_plate_clear=True)
  80. off = await printer_factory(require_plate_clear=False)
  81. following = await printer_factory()
  82. overrides.set_override(following.id, False) # stale: must be replaced
  83. session_maker = async_sessionmaker(test_engine, class_=AsyncSession, expire_on_commit=False)
  84. with patch("backend.app.core.database.async_session", session_maker):
  85. await PrinterManager().load_awaiting_plate_clear_from_db()
  86. assert plate_clear.applies(on.id, False) is True
  87. assert plate_clear.applies(off.id, True) is False
  88. assert plate_clear.applies(following.id, True) is True
  89. @pytest.mark.asyncio
  90. @pytest.mark.integration
  91. async def test_update_refreshes_the_wled_preset(async_client: AsyncClient, printer_factory):
  92. """A printer's lights follow a changed override at once, not at its next
  93. status push, which on an idle printer can be a while."""
  94. printer = await printer_factory(is_active=True)
  95. state = object()
  96. with (
  97. patch("backend.app.api.routes.printers.printer_manager.get_status", return_value=state),
  98. patch("backend.app.api.routes.printers.wled_manager.handle_status") as handle_status,
  99. patch("backend.app.api.routes.printers.wled_manager.configure_printer") as configure,
  100. ):
  101. response = await async_client.patch(f"/api/v1/printers/{printer.id}", json={"require_plate_clear": False})
  102. assert response.status_code == 200
  103. assert handle_status.call_args.args[:2] == (printer.id, state)
  104. # The WLED config itself did not change, so its runtime is left alone.
  105. configure.assert_not_called()