test_printer_schema.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. """Serial-number normalization on the printer schema (#1465).
  2. Bambu serial numbers are uppercase alphanumeric and the MQTT report topic
  3. ``device/<serial>/report`` is case-sensitive. A serial entered in the wrong
  4. case connects and subscribes without error but never receives a message, so
  5. the schema normalizes it on input.
  6. """
  7. import pytest
  8. from pydantic import ValidationError
  9. from backend.app.schemas.printer import PrinterCreate, WLEDConfig, WLEDPresets
  10. def _make(serial: str) -> PrinterCreate:
  11. return PrinterCreate(
  12. name="Test Printer",
  13. serial_number=serial,
  14. ip_address="192.168.1.50",
  15. access_code="12345678",
  16. )
  17. def test_serial_number_uppercased():
  18. assert _make("01p00a3b1234567").serial_number == "01P00A3B1234567"
  19. def test_serial_number_whitespace_stripped():
  20. assert _make(" 01P00A3B1234567 ").serial_number == "01P00A3B1234567"
  21. def test_serial_number_stripped_and_uppercased():
  22. assert _make(" 31b8c0ca1234567 ").serial_number == "31B8C0CA1234567"
  23. def test_already_normalized_serial_unchanged():
  24. assert _make("31B8C0CA1234567").serial_number == "31B8C0CA1234567"
  25. def test_blank_serial_number_rejected():
  26. with pytest.raises(ValidationError):
  27. _make(" ")
  28. def test_wled_config_normalizes_url_and_accepts_optional_mappings():
  29. config = WLEDConfig(
  30. enabled=True,
  31. base_url=" http://wled.local/ ",
  32. presets=WLEDPresets(printing=3, paused=None),
  33. finished_timeout_seconds=120,
  34. )
  35. assert config.base_url == "http://wled.local"
  36. assert config.presets.printing == 3
  37. assert config.presets.paused is None
  38. @pytest.mark.parametrize("preset", [0, 251])
  39. def test_wled_preset_outside_supported_range_rejected(preset):
  40. with pytest.raises(ValidationError):
  41. WLEDPresets(idle=preset)
  42. def test_enabled_wled_requires_url():
  43. with pytest.raises(ValidationError):
  44. WLEDConfig(enabled=True)
  45. @pytest.mark.parametrize(
  46. "url",
  47. [
  48. "file:///etc/passwd",
  49. "http://169.254.169.254/latest/meta-data",
  50. "http://user:password@wled.local",
  51. ],
  52. )
  53. def test_wled_url_uses_lan_service_safety_policy(url):
  54. with pytest.raises(ValidationError):
  55. WLEDConfig(base_url=url)
  56. def test_wled_finished_timeout_is_bounded():
  57. with pytest.raises(ValidationError):
  58. WLEDConfig(finished_timeout_seconds=86401)