fix(whatsapp): allow group ids in allowFrom (#4834)
This commit is contained in:
@@ -93,3 +93,35 @@ async def test_handle_message_group_ignores_unknown() -> None:
|
||||
|
||||
assert channel._sent == []
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_handle_message_uses_authorization_id_without_changing_sender() -> None:
|
||||
bus = MessageBus()
|
||||
channel = _DummyChannel({"allowFrom": ["group@g.us"]}, bus)
|
||||
|
||||
await channel._handle_message(
|
||||
sender_id="member-lid",
|
||||
authorization_id="group@g.us",
|
||||
chat_id="group@g.us",
|
||||
content="hello",
|
||||
)
|
||||
|
||||
msg = await bus.consume_inbound()
|
||||
assert msg.sender_id == "member-lid"
|
||||
assert msg.chat_id == "group@g.us"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_handle_message_rejects_when_authorization_id_is_not_allowed() -> None:
|
||||
bus = MessageBus()
|
||||
channel = _DummyChannel({"allowFrom": ["member-lid"]}, bus)
|
||||
|
||||
await channel._handle_message(
|
||||
sender_id="member-lid",
|
||||
authorization_id="other-group@g.us",
|
||||
chat_id="other-group@g.us",
|
||||
content="hello",
|
||||
)
|
||||
|
||||
assert bus.inbound_size == 0
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ from unittest.mock import AsyncMock, MagicMock
|
||||
import pytest
|
||||
|
||||
from nanobot.bus.events import OutboundMessage
|
||||
from nanobot.bus.queue import MessageBus
|
||||
from nanobot.channels import whatsapp as whatsapp_module
|
||||
from nanobot.channels.whatsapp import WhatsAppChannel, _legacy_bridge_config_fields, _NeonizeAPI
|
||||
|
||||
@@ -320,6 +321,50 @@ async def test_group_sender_id_uses_participant_not_group_jid() -> None:
|
||||
assert kwargs["metadata"]["participant"] == "SENDERLID@lid"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("allowed_group", ["120363000@g.us", "120363000"])
|
||||
@pytest.mark.asyncio
|
||||
async def test_group_allow_from_accepts_group_jid_or_bare_id(allowed_group: str) -> None:
|
||||
bus = MessageBus()
|
||||
ch = WhatsAppChannel({"enabled": True, "allowFrom": [allowed_group]}, bus)
|
||||
ch._started_at = 0
|
||||
|
||||
await ch._handle_neonize_message(
|
||||
SimpleNamespace(download_any=AsyncMock()),
|
||||
_event(
|
||||
message=_Proto(conversation="hi"),
|
||||
chat=_jid("120363000", "g.us"),
|
||||
sender=_jid("SENDERLID", "lid"),
|
||||
is_group=True,
|
||||
),
|
||||
)
|
||||
|
||||
assert bus.inbound_size == 1
|
||||
msg = await bus.consume_inbound()
|
||||
assert msg.sender_id == "SENDERLID"
|
||||
assert msg.chat_id == "120363000@g.us"
|
||||
assert msg.content == "hi"
|
||||
assert msg.metadata["participant"] == "SENDERLID@lid"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_group_allow_from_does_not_allow_same_participant_in_other_group() -> None:
|
||||
bus = MessageBus()
|
||||
ch = WhatsAppChannel({"enabled": True, "allowFrom": ["120363000"]}, bus)
|
||||
ch._started_at = 0
|
||||
|
||||
await ch._handle_neonize_message(
|
||||
SimpleNamespace(download_any=AsyncMock()),
|
||||
_event(
|
||||
message=_Proto(conversation="hi"),
|
||||
chat=_jid("120363999", "g.us"),
|
||||
sender=_jid("SENDERLID", "lid"),
|
||||
is_group=True,
|
||||
),
|
||||
)
|
||||
|
||||
assert bus.inbound_size == 0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_read_receipt_is_requested_once_after_dedup() -> None:
|
||||
ch = _make_channel()
|
||||
|
||||
Reference in New Issue
Block a user