test_printer_ip_change_syncs_vps.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. """A printer's new IP reaches the proxy virtual printers aimed at it.
  2. Proxy VPs forward to the IP they were started with. The sync that restarts
  3. them on a changed target IP ran only when a virtual printer was saved, so
  4. editing the printer's IP left them forwarding to the old address until then.
  5. """
  6. from unittest.mock import AsyncMock, patch
  7. import pytest
  8. from httpx import AsyncClient
  9. @pytest.fixture
  10. def vp_sync():
  11. with (
  12. patch("backend.app.api.routes.printers.printer_manager.disconnect_printer"),
  13. patch("backend.app.api.routes.printers.printer_manager.connect_printer", new=AsyncMock()),
  14. patch(
  15. "backend.app.services.virtual_printer.virtual_printer_manager.sync_from_db",
  16. new_callable=AsyncMock,
  17. ) as sync,
  18. ):
  19. yield sync
  20. @pytest.mark.asyncio
  21. @pytest.mark.integration
  22. async def test_ip_change_syncs_virtual_printers(async_client: AsyncClient, printer_factory, vp_sync):
  23. printer = await printer_factory(ip_address="192.168.1.50")
  24. response = await async_client.patch(f"/api/v1/printers/{printer.id}", json={"ip_address": "192.168.1.77"})
  25. assert response.status_code == 200
  26. vp_sync.assert_awaited_once()
  27. @pytest.mark.asyncio
  28. @pytest.mark.integration
  29. async def test_unchanged_ip_does_not_sync(async_client: AsyncClient, printer_factory, vp_sync):
  30. printer = await printer_factory(ip_address="192.168.1.50")
  31. response = await async_client.patch(
  32. f"/api/v1/printers/{printer.id}", json={"name": "Renamed", "ip_address": "192.168.1.50"}
  33. )
  34. assert response.status_code == 200
  35. vp_sync.assert_not_awaited()
  36. @pytest.mark.asyncio
  37. @pytest.mark.integration
  38. async def test_a_failed_sync_does_not_fail_the_update(async_client: AsyncClient, printer_factory, vp_sync):
  39. printer = await printer_factory(ip_address="192.168.1.50")
  40. vp_sync.side_effect = RuntimeError("sync broke")
  41. response = await async_client.patch(f"/api/v1/printers/{printer.id}", json={"ip_address": "192.168.1.77"})
  42. assert response.status_code == 200
  43. assert response.json()["ip_address"] == "192.168.1.77"