test_support_vp_serial_redaction_3526.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. """A virtual printer's serial is redacted in the support package (#3526).
  2. Its serial is not stored whole -- it is the model's prefix plus the VP's
  3. ``serial_suffix`` -- and the A2L's ``26A19A`` and the other newer prefixes are
  4. past what the generic serial pattern matches, so the certificate log lines
  5. carried it unredacted.
  6. """
  7. import pytest
  8. from backend.app.models.virtual_printer import VirtualPrinter
  9. from backend.app.services.log_reader import collect_sensitive_strings, sanitize_log_content
  10. @pytest.mark.asyncio
  11. @pytest.mark.parametrize("model", ["N9", "N7", "O1D", "N6", "O1C2", None])
  12. async def test_the_virtual_printer_serial_is_redacted(db_session, model):
  13. from backend.app.services.virtual_printer.manager import DEFAULT_VIRTUAL_PRINTER_MODEL, _get_serial_for_model
  14. db_session.add(VirtualPrinter(name="Shop VP", model=model, serial_suffix="491800007"))
  15. await db_session.commit()
  16. serial = _get_serial_for_model(model or DEFAULT_VIRTUAL_PRINTER_MODEL, "491800007")
  17. sensitive = await collect_sensitive_strings(db_session)
  18. line = f"Generating certificates for virtual printer (serial: {serial})... Printer: CN={serial}"
  19. assert sanitize_log_content(line, sensitive) == (
  20. "Generating certificates for virtual printer (serial: [SERIAL])... Printer: CN=[SERIAL]"
  21. )
  22. @pytest.mark.asyncio
  23. async def test_the_serial_from_before_a_model_change_is_redacted(db_session):
  24. """The log may predate the VP's current model."""
  25. from backend.app.services.virtual_printer.manager import _get_serial_for_model
  26. db_session.add(VirtualPrinter(name="Shop VP", model="N9", serial_suffix="491800007"))
  27. await db_session.commit()
  28. sensitive = await collect_sensitive_strings(db_session)
  29. old_serial = _get_serial_for_model("N7", "491800007")
  30. assert sanitize_log_content(f"CN={old_serial}", sensitive) == "CN=[SERIAL]"