test_firmware_check_setting.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. """The "Check printer firmware" setting is enforced by the API, not only the UI.
  2. The printers page asked before its settings had loaded, so with the check
  3. switched off Bambuddy still went out to bambulab.com once per printer on every
  4. page load. Off has to mean no request at all.
  5. """
  6. from unittest.mock import AsyncMock, MagicMock, patch
  7. import pytest
  8. from httpx import AsyncClient
  9. from backend.app.models.settings import Settings
  10. UPDATE = {
  11. "update_available": True,
  12. "latest_version": "01.09.00.00",
  13. "download_url": "https://example.com/fw.bin",
  14. "release_notes": None,
  15. "available_versions": [],
  16. }
  17. def _service() -> MagicMock:
  18. service = MagicMock()
  19. service.check_for_update = AsyncMock(return_value=UPDATE)
  20. return service
  21. async def _switch_off(db) -> None:
  22. db.add(Settings(key="check_printer_firmware", value="false"))
  23. await db.commit()
  24. @pytest.mark.integration
  25. class TestFirmwareCheckSetting:
  26. @pytest.mark.asyncio
  27. async def test_on_by_default(self, async_client: AsyncClient, printer_factory):
  28. printer = await printer_factory()
  29. service = _service()
  30. with patch("backend.app.api.routes.firmware.get_firmware_service", return_value=service):
  31. r = await async_client.get(f"/api/v1/firmware/updates/{printer.id}")
  32. assert r.status_code == 200, r.text
  33. assert r.json()["update_available"] is True
  34. service.check_for_update.assert_awaited_once()
  35. @pytest.mark.asyncio
  36. async def test_off_asks_nobody(self, async_client: AsyncClient, db_session, printer_factory):
  37. printer = await printer_factory(name="Garage")
  38. await _switch_off(db_session)
  39. service = _service()
  40. with patch("backend.app.api.routes.firmware.get_firmware_service", return_value=service):
  41. r = await async_client.get(f"/api/v1/firmware/updates/{printer.id}")
  42. assert r.status_code == 200, r.text
  43. body = r.json()
  44. assert body["printer_id"] == printer.id and body["printer_name"] == "Garage"
  45. assert body["update_available"] is False and body["latest_version"] is None
  46. service.check_for_update.assert_not_called()
  47. @pytest.mark.asyncio
  48. async def test_off_still_404s_an_unknown_printer(self, async_client: AsyncClient, db_session):
  49. await _switch_off(db_session)
  50. assert (await async_client.get("/api/v1/firmware/updates/9999")).status_code == 404
  51. @pytest.mark.asyncio
  52. async def test_off_covers_the_all_printers_check(self, async_client: AsyncClient, db_session, printer_factory):
  53. await printer_factory()
  54. await printer_factory()
  55. await _switch_off(db_session)
  56. service = _service()
  57. with patch("backend.app.api.routes.firmware.get_firmware_service", return_value=service):
  58. r = await async_client.get("/api/v1/firmware/updates")
  59. assert r.status_code == 200, r.text
  60. body = r.json()
  61. assert body["updates_available"] == 0
  62. assert len(body["updates"]) == 2
  63. assert all(u["update_available"] is False for u in body["updates"])
  64. service.check_for_update.assert_not_called()