feat(service): pass a qr code with a login prompt or approval wait

This commit is contained in:
imSp4rky
2026-11-26 13:27:50 -07:00
parent 251d1e779a
commit 291417db4e
28 changed files with 386 additions and 94 deletions
+28
View File
@@ -0,0 +1,28 @@
import segno
from unshackle.core import console
from unshackle.core.console import qr_text, show_qr
TOP = {"\u2588": True, "\u2580": True, "\u2584": False, " ": False}
BOTTOM = {"\u2588": True, "\u2580": False, "\u2584": True, " ": False}
def test_each_line_holds_two_module_rows_of_the_code() -> None:
data = "https://login.example/scan?code=EXAMPLE"
rows = [[bool(v) for v in row] for row in segno.make_qr(data).matrix_iter(border=2)]
unpacked = []
for line in qr_text(data, indent=0).plain.splitlines():
unpacked += [[TOP[c] for c in line], [BOTTOM[c] for c in line]]
assert unpacked[: len(rows)] == rows
assert not any(cell for row in unpacked[len(rows) :] for cell in row)
def test_short_text_is_a_full_qr_code_not_a_micro_one() -> None:
assert len(qr_text("12345", indent=0).plain.splitlines()) * 2 >= 21 + 4
def test_text_longer_than_a_qr_code_holds_draws_nothing(monkeypatch) -> None:
printed: list = []
monkeypatch.setattr(console.console, "print", lambda *a, **k: printed.extend(a))
show_qr("\u20ac" * 1500)
assert printed == []
+13 -1
View File
@@ -49,6 +49,18 @@ def test_logs_the_message_for_a_remote_session(caplog):
assert "enter code" in caplog.messages
@pytest.mark.parametrize("remote", [False, True])
def test_draws_the_qr_code_here_only_without_a_remote_session(monkeypatch, caplog, remote):
drawn = []
monkeypatch.setattr("unshackle.core.service.show_qr", drawn.append)
with caplog.at_level(logging.INFO, logger="test"):
Service.wait_for_approval(
_service(InputBridge() if remote else None), "scan", lambda: "token", timeout=5, interval=0, qr="EXAMPLE"
)
assert drawn == ([] if remote else ["EXAMPLE"])
assert [getattr(r, "qr", None) for r in caplog.records] == ["EXAMPLE"]
def test_a_request_with_no_user_fails_at_once():
calls = []
with pytest.raises(RuntimeError, match="needs the user"):
@@ -74,7 +86,7 @@ def test_no_user_message_is_an_authentication_error():
)
def test_shows_the_message_as_a_notice_and_clears_it(monkeypatch, check, expectation):
notices = []
monkeypatch.setattr(console, "_notice_handler", notices.append)
monkeypatch.setattr(console, "_notice_handler", lambda message, qr: notices.append(message))
with expectation:
Service.wait_for_approval(_service(), "enter code", check, timeout=0.05, interval=0.01)
assert notices == ["enter code", None]