fix(webui): refresh pairing prompts on gateway changes (#6401)
This commit is contained in:
1 parent
9e9e22d331
commit
8af6313c56
26 files changed
+630
-82
No files matched your search
@@ -70,7 +70,7 @@ def test_is_allowed_pairing_fallback(monkeypatch) -> None:
|
||||
async def test_handle_message_dm_sends_pairing_code(monkeypatch) -> None:
|
||||
channel = _DummyChannel({"allowFrom": []}, MessageBus())
|
||||
monkeypatch.setattr(
|
||||
"nanobot.channels.base.generate_code", lambda _ch, sid: "ABCD-EFGH"
|
||||
"nanobot.channels.base.generate_code", lambda _ch, sid, **_kwargs: "ABCD-EFGH"
|
||||
)
|
||||
|
||||
await channel._handle_message(
|
||||
@@ -168,3 +168,20 @@ async def test_handle_message_rejects_when_authorization_id_is_not_allowed() ->
|
||||
|
||||
assert bus.inbound_size == 0
|
||||
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_dm_publishes_pairing_changes_without_a_file_watcher(monkeypatch, tmp_path) -> None:
|
||||
from nanobot.pairing import store
|
||||
from nanobot.pairing.events import PairingChanged
|
||||
|
||||
monkeypatch.setattr(store, "_store_path", lambda: tmp_path / "pairing.json")
|
||||
bus = MessageBus()
|
||||
observed = []
|
||||
bus.subscribe(lambda _event: observed.append(store.list_pending()), PairingChanged)
|
||||
channel = _DummyChannel({"allowFrom": []}, bus)
|
||||
await channel._handle_message("alice", "dm", "hello", is_dm=True)
|
||||
await bus.drain()
|
||||
assert observed and observed[-1][0]["delivery_status"] == "sent"
|
||||
assert observed[-1][0]["code"] in channel._sent[0].content
|
||||
assert not store.is_approved("dummy", "alice")
|
||||
@@ -0,0 +1,31 @@
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
from nanobot.bus.events import InboundMessage
|
||||
from nanobot.bus.queue import MessageBus
|
||||
from nanobot.command.builtin import cmd_pairing
|
||||
from nanobot.command.router import CommandContext
|
||||
from nanobot.pairing import store
|
||||
from nanobot.pairing.events import PairingChanged
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_pairing_command_notifies_after_persistence_without_a_file_watcher(tmp_path, monkeypatch):
|
||||
monkeypatch.setattr(store, "_store_path", lambda: tmp_path / "pairing.json")
|
||||
code = store.generate_code("telegram", "alice")
|
||||
bus = MessageBus()
|
||||
approved = []
|
||||
bus.subscribe(lambda _event: approved.append(store.is_approved("telegram", "alice")), PairingChanged)
|
||||
loop = MagicMock(bus=bus)
|
||||
msg = InboundMessage(channel="cli", sender_id="owner", chat_id="direct", content=f"/pairing approve {code}")
|
||||
ctx = CommandContext(msg=msg, session=None, key=msg.session_key, raw=msg.content, args=f"approve {code}", loop=loop)
|
||||
result = await cmd_pairing(ctx)
|
||||
await bus.drain()
|
||||
assert "Approved" in result.content and approved == [True]
|
||||
ctx.args = "approve invalid"
|
||||
await cmd_pairing(ctx)
|
||||
ctx.args = "list"
|
||||
await cmd_pairing(ctx)
|
||||
await bus.drain()
|
||||
assert approved == [True]
|
||||
@@ -264,7 +264,7 @@ class TestPairingCommandDispatch:
|
||||
) -> None:
|
||||
monkeypatch.setattr(
|
||||
"nanobot.pairing.store.approve_code",
|
||||
lambda code: ("telegram", "123") if code == "ABCD-EFGH" else None,
|
||||
lambda code, *, on_change=None: ("telegram", "123") if code == "ABCD-EFGH" else None,
|
||||
)
|
||||
fake_msg.content = "/pairing approve ABCD-EFGH"
|
||||
ctx = CommandContext(
|
||||
@@ -286,7 +286,7 @@ class TestPairingCommandDispatch:
|
||||
) -> None:
|
||||
monkeypatch.setattr(
|
||||
"nanobot.pairing.store.revoke",
|
||||
lambda ch, sid: sid == "123",
|
||||
lambda ch, sid, *, on_change=None: sid == "123",
|
||||
)
|
||||
fake_msg.content = "/pairing revoke 123"
|
||||
ctx = CommandContext(
|
||||
|
||||
@@ -441,3 +441,75 @@ class TestTransientReadFailure:
|
||||
|
||||
assert "unavailable" in reply.lower()
|
||||
assert store.approve_code(code) == ("telegram", "123")
|
||||
|
||||
|
||||
class TestChangeNotifications:
|
||||
def test_changes_are_committed_and_unlocked_before_notification(self):
|
||||
snapshots = []
|
||||
|
||||
def changed():
|
||||
unlocked = store._LOCK.acquire(blocking=False)
|
||||
if unlocked:
|
||||
store._LOCK.release()
|
||||
snapshots.append(store.list_pending())
|
||||
|
||||
code = store.generate_code("telegram", "alice", on_change=changed)
|
||||
assert len(snapshots) == 1 and snapshots[-1][0]["code"] == code
|
||||
assert store.generate_code("telegram", "alice", on_change=changed) == code
|
||||
assert len(snapshots) == 1
|
||||
store.record_delivery(code, "sending", on_change=changed)
|
||||
assert snapshots[-1][0]["delivery_status"] == "sending"
|
||||
store.record_delivery(code, "sending", on_change=changed)
|
||||
assert len(snapshots) == 2
|
||||
store.record_delivery(code, "sent", on_change=changed)
|
||||
store.record_delivery(code, "failed", on_change=changed)
|
||||
assert len(snapshots) == 3 and snapshots[-1][0]["delivery_status"] == "sent"
|
||||
assert store.approve_code(code, on_change=changed) == ("telegram", "alice")
|
||||
assert snapshots[-1] == [] and len(snapshots) == 4
|
||||
assert store.approve_code(code, on_change=changed) is None
|
||||
assert not store.deny_code(code, on_change=changed)
|
||||
assert len(snapshots) == 4
|
||||
|
||||
def test_commands_notify_only_after_changes(self):
|
||||
changes = []
|
||||
|
||||
def changed():
|
||||
changes.append(json.loads(store._store_path().read_text()))
|
||||
|
||||
code = store.generate_code("telegram", "alice")
|
||||
assert "Approved" in store.handle_pairing_command("cli", f"approve {code}", on_change=changed)
|
||||
assert changes[-1]["approved"] == {"telegram": ["alice"]}
|
||||
store.handle_pairing_command("cli", "list", on_change=changed)
|
||||
store.handle_pairing_command("cli", "approve invalid", on_change=changed)
|
||||
assert len(changes) == 1
|
||||
store.handle_pairing_command("cli", "revoke telegram alice", on_change=changed)
|
||||
store.handle_pairing_command("cli", "revoke telegram alice", on_change=changed)
|
||||
code = store.generate_code("telegram", "bob")
|
||||
store.handle_pairing_command("cli", f"deny {code}", on_change=changed)
|
||||
code = store.generate_code("telegram", "carol")
|
||||
store.handle_pairing_command("cli", f"approve {code}", on_change=changed)
|
||||
store.handle_pairing_command("cli", "revoke telegram carol", on_change=changed)
|
||||
assert len(changes) == 5 and changes[-1] == {"approved": {}, "pending": {}}
|
||||
|
||||
def test_failed_persistence_does_not_notify(self, monkeypatch):
|
||||
code = store.generate_code("telegram", "alice")
|
||||
before = store._store_path().read_bytes()
|
||||
changes = []
|
||||
|
||||
def failed_write(_data):
|
||||
raise OSError("fixture disk failure")
|
||||
|
||||
monkeypatch.setattr(store, "_save", failed_write)
|
||||
with pytest.raises(OSError, match="disk failure"):
|
||||
store.generate_code("telegram", "bob", on_change=lambda: changes.append(True))
|
||||
store.record_delivery(code, "sent", on_change=lambda: changes.append(True))
|
||||
assert changes == [] and store._store_path().read_bytes() == before
|
||||
|
||||
def test_notification_failure_does_not_report_persisted_mutation_as_failure(self):
|
||||
def failed_notify():
|
||||
raise RuntimeError("fixture observer failure")
|
||||
|
||||
code = store.generate_code("telegram", "alice", on_change=failed_notify)
|
||||
assert store.list_pending()[0]["code"] == code
|
||||
assert store.approve_code(code, on_change=failed_notify) == ("telegram", "alice")
|
||||
assert store.is_approved("telegram", "alice")
|
||||
Reference in new issue
Block a user