fix(discord): route unauthorized DMs to pairing

This commit is contained in:
chengyongru
2026-07-13 12:05:04 +08:00
committed by Xubin Ren
parent 053722a2a2
commit 6d406d93c9
2 changed files with 23 additions and 1 deletions
+20
View File
@@ -362,6 +362,26 @@ async def test_on_message_accepts_allowlisted_dm() -> None:
assert handled[0]["metadata"] == {"message_id": "789", "guild_id": None, "reply_to": None}
@pytest.mark.asyncio
async def test_on_message_unauthorized_dm_sends_pairing_code(monkeypatch) -> None:
channel = DiscordChannel(DiscordConfig(enabled=True, allow_from=[]), MessageBus())
client = _FakeDiscordClient(channel, intents=None)
message = _make_message(author_id=123, channel_id=456)
client.channels[456] = message.channel
channel._client = client
channel._running = True
monkeypatch.setattr("nanobot.channels.base.is_approved", lambda _ch, _sid: False)
monkeypatch.setattr(
"nanobot.channels.base.generate_code", lambda _ch, _sid: "ABCD-EFGH"
)
await channel._on_message(message)
assert len(message.channel.sent_payloads) == 1
assert "ABCD-EFGH" in message.channel.sent_payloads[0]["content"]
assert channel._typing_tasks == {}
@pytest.mark.asyncio
async def test_on_message_accepts_when_channel_in_allow_channels() -> None:
# When allow_channels is set, messages from listed channels should be forwarded.