mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-05-19 16:12:30 +00:00
/pairing is now a first-class built-in command dispatched through CommandRouter, just like /status, /model, /dream, etc. Benefits: - WebUI automatically shows /pairing in the slash command palette (because builtin_command_palette() feeds /api/commands). - All channels (Telegram, Discord, WebSocket, etc.) use the same dispatch path for /pairing; no more channel-level interception. - The command still only works for already-authorised users because is_allowed() gates message ingestion before the bus. Changes: - Add handle_pairing_command() to nanobot.pairing.store — pure function callable from CLI, CommandRouter, and tests. - Add cmd_pairing to nanobot.command.builtin and register in BUILTIN_COMMAND_SPECS + register_builtin_commands(). - Remove BaseChannel._handle_pairing_command() and the /pairing interception logic from _handle_message(). - Clean up unused pairing imports from base.py. - Add unit tests for handle_pairing_command and cmd_pairing dispatch.
88 lines
2.5 KiB
Python
88 lines
2.5 KiB
Python
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
from nanobot.bus.events import OutboundMessage
|
|
from nanobot.bus.queue import MessageBus
|
|
from nanobot.channels.base import BaseChannel
|
|
|
|
|
|
class _DummyChannel(BaseChannel):
|
|
name = "dummy"
|
|
_sent: list[OutboundMessage]
|
|
|
|
def __init__(self, config, bus):
|
|
super().__init__(config, bus)
|
|
self._sent = []
|
|
|
|
async def start(self) -> None:
|
|
return None
|
|
|
|
async def stop(self) -> None:
|
|
return None
|
|
|
|
async def send(self, msg: OutboundMessage) -> None:
|
|
self._sent.append(msg)
|
|
|
|
|
|
def test_is_allowed_requires_exact_match() -> None:
|
|
channel = _DummyChannel(SimpleNamespace(allow_from=["allow@email.com"]), MessageBus())
|
|
|
|
assert channel.is_allowed("allow@email.com") is True
|
|
assert channel.is_allowed("attacker|allow@email.com") is False
|
|
|
|
|
|
def test_is_allowed_supports_dict_allow_from_alias() -> None:
|
|
channel = _DummyChannel({"allowFrom": ["alice"]}, MessageBus())
|
|
|
|
assert channel.is_allowed("alice") is True
|
|
|
|
|
|
def test_is_allowed_denies_empty_dict_allow_from() -> None:
|
|
channel = _DummyChannel({"allow_from": []}, MessageBus())
|
|
|
|
assert channel.is_allowed("alice") is False
|
|
|
|
|
|
def test_is_allowed_star_allows_all() -> None:
|
|
channel = _DummyChannel({"allowFrom": ["*"]}, MessageBus())
|
|
assert channel.is_allowed("anyone") is True
|
|
|
|
|
|
def test_is_allowed_pairing_fallback(monkeypatch) -> None:
|
|
channel = _DummyChannel({"allowFrom": []}, MessageBus())
|
|
monkeypatch.setattr(
|
|
"nanobot.channels.base.is_approved", lambda _ch, sid: sid == "paired"
|
|
)
|
|
assert channel.is_allowed("paired") is True
|
|
assert channel.is_allowed("unknown") is False
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
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"
|
|
)
|
|
|
|
await channel._handle_message(
|
|
sender_id="stranger", chat_id="chat1", content="hello", is_dm=True
|
|
)
|
|
|
|
assert len(channel._sent) == 1
|
|
msg = channel._sent[0]
|
|
assert "ABCD-EFGH" in msg.content
|
|
assert msg.metadata.get("_pairing_code") == "ABCD-EFGH"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_handle_message_group_ignores_unknown() -> None:
|
|
channel = _DummyChannel({"allowFrom": []}, MessageBus())
|
|
|
|
await channel._handle_message(
|
|
sender_id="stranger", chat_id="chat1", content="hello", is_dm=False
|
|
)
|
|
|
|
assert channel._sent == []
|
|
|