b9865be55f
Final-review hardening: - discover_printer falls back to direct IP reachability when printer_mac is empty (restores legacy behavior; avoids permanent unreachable on misconfig). Keeps the MAC check when a MAC IS set so we never print to a wrong DHCP tenant. - bridge subprocess decodes child stdout as utf-8 explicitly, so a LANG=C systemd locale can't UnicodeDecodeError on the success checkmark and mis-report error. - add tests for both no-MAC fallback paths (5 passed). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
61 lines
2.7 KiB
Python
61 lines
2.7 KiB
Python
import os, sys
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
import pos_briefing as pb
|
|
|
|
|
|
def test_cache_hit_returns_cached_ip(monkeypatch, tmp_path):
|
|
cache = tmp_path / "last_ip"
|
|
cache.write_text("192.168.0.50")
|
|
monkeypatch.setattr(pb, "CACHE_FILE", cache)
|
|
monkeypatch.setattr(pb, "PRINTER_MAC", "50:57:9c:eb:0f:07")
|
|
monkeypatch.setattr(pb, "_mac_of", lambda ip: "50:57:9c:eb:0f:07" if ip == "192.168.0.50" else None)
|
|
monkeypatch.setattr(pb, "_port_open", lambda ip, port, timeout=3: True)
|
|
assert pb.discover_printer() == "192.168.0.50"
|
|
|
|
|
|
def test_sweep_finds_printer_on_cache_miss(monkeypatch, tmp_path):
|
|
cache = tmp_path / "last_ip"
|
|
monkeypatch.setattr(pb, "CACHE_FILE", cache)
|
|
monkeypatch.setattr(pb, "PRINTER_IP", "192.168.0.99") # stale seed
|
|
monkeypatch.setattr(pb, "PRINTER_MAC", "50:57:9c:eb:0f:07")
|
|
monkeypatch.setattr(pb, "PRINTER_SUBNET", "192.168.0")
|
|
monkeypatch.setattr(pb, "_mac_of", lambda ip: None) # seed/cache miss
|
|
monkeypatch.setattr(pb, "_sweep_arp", lambda: {"50:57:9c:eb:0f:07": "192.168.0.184"})
|
|
monkeypatch.setattr(pb, "_port_open", lambda ip, port, timeout=3: True)
|
|
assert pb.discover_printer() == "192.168.0.184"
|
|
assert cache.read_text().strip() == "192.168.0.184"
|
|
|
|
|
|
def test_not_found_raises(monkeypatch, tmp_path):
|
|
monkeypatch.setattr(pb, "CACHE_FILE", tmp_path / "last_ip")
|
|
monkeypatch.setattr(pb, "PRINTER_IP", "")
|
|
monkeypatch.setattr(pb, "PRINTER_MAC", "50:57:9c:eb:0f:07")
|
|
monkeypatch.setattr(pb, "_mac_of", lambda ip: None)
|
|
monkeypatch.setattr(pb, "_sweep_arp", lambda: {})
|
|
try:
|
|
pb.discover_printer()
|
|
assert False, "expected RuntimeError"
|
|
except RuntimeError as e:
|
|
assert "printer-not-found" in str(e)
|
|
|
|
|
|
def test_no_mac_falls_back_to_direct_ip(monkeypatch, tmp_path):
|
|
# When printer_mac is unconfigured, a reachable seed IP is used directly.
|
|
monkeypatch.setattr(pb, "CACHE_FILE", tmp_path / "last_ip")
|
|
monkeypatch.setattr(pb, "PRINTER_IP", "192.168.0.184")
|
|
monkeypatch.setattr(pb, "PRINTER_MAC", "")
|
|
monkeypatch.setattr(pb, "_port_open", lambda ip, port, timeout=3: ip == "192.168.0.184")
|
|
assert pb.discover_printer() == "192.168.0.184"
|
|
|
|
|
|
def test_no_mac_unreachable_raises(monkeypatch, tmp_path):
|
|
monkeypatch.setattr(pb, "CACHE_FILE", tmp_path / "last_ip")
|
|
monkeypatch.setattr(pb, "PRINTER_IP", "192.168.0.184")
|
|
monkeypatch.setattr(pb, "PRINTER_MAC", "")
|
|
monkeypatch.setattr(pb, "_port_open", lambda ip, port, timeout=3: False)
|
|
try:
|
|
pb.discover_printer()
|
|
assert False, "expected RuntimeError"
|
|
except RuntimeError as e:
|
|
assert "no printer_mac" in str(e)
|