Add callback inline keyboard tests for Telegram bot (#163328)

This commit is contained in:
hanwg
2026-02-19 17:50:51 +01:00
committed by GitHub
parent 865ec96429
commit 05abe7efe0
2 changed files with 70 additions and 2 deletions
+27
View File
@@ -250,6 +250,33 @@ def update_callback_query():
}
@pytest.fixture
def update_callback_inline_keyboard():
"""Fixture for mocking an incoming update of type callback_query from inline keyboard button."""
return {
"update_id": 1,
"callback_query": {
"id": "4382bfdwdsb323b2d9",
"from": {
"id": 12345678,
"type": "private",
"is_bot": False,
"last_name": "Test Lastname",
"first_name": "Test Firstname",
"username": "Testusername",
},
"message": {
"message_id": 101,
"chat": {"id": 987654321, "type": "private"},
"date": 1708181000,
"text": "command",
},
"chat_instance": "aaa111",
"data": "/command arg1 arg2",
},
}
@pytest.fixture
def mock_broadcast_config_entry() -> MockConfigEntry:
"""Return the default mocked config entry."""
@@ -646,14 +646,14 @@ async def test_webhook_endpoint_generates_telegram_command_event(
assert isinstance(events[0].context, Context)
async def test_webhook_endpoint_generates_telegram_callback_event(
async def test_webhook_callback_inline_query(
hass: HomeAssistant,
webhook_bot,
hass_client: ClientSessionGenerator,
update_callback_query,
mock_generate_secret_token,
) -> None:
"""POST to the configured webhook endpoint and assert fired `telegram_callback` event."""
"""Test callback query triggered by inline query."""
client = await hass_client()
events = async_capture_events(hass, "telegram_callback")
@@ -673,6 +673,47 @@ async def test_webhook_endpoint_generates_telegram_callback_event(
assert isinstance(events[0].context, Context)
async def test_webhook_callback_inline_keyboard(
hass: HomeAssistant,
webhook_bot: None,
hass_client: ClientSessionGenerator,
update_callback_inline_keyboard,
mock_generate_secret_token,
) -> None:
"""Test callback query triggered by inline keyboard button."""
client = await hass_client()
events = async_capture_events(hass, "telegram_callback")
response = await client.post(
f"{TELEGRAM_WEBHOOK_URL}_123456",
json=update_callback_inline_keyboard,
headers={"X-Telegram-Bot-Api-Secret-Token": mock_generate_secret_token},
)
assert response.status == 200
assert (await response.read()).decode("utf-8") == ""
# Make sure event has fired
await hass.async_block_till_done()
assert len(events) == 1
assert (
events[0].data["chat_id"]
== update_callback_inline_keyboard["callback_query"]["message"]["chat"]["id"]
)
expected_message = {
**update_callback_inline_keyboard["callback_query"]["message"],
"delete_chat_photo": False,
"group_chat_created": False,
"supergroup_chat_created": False,
"channel_chat_created": False,
}
assert events[0].data["message"] == expected_message
assert events[0].data["data"] == "/command arg1 arg2"
assert events[0].data["command"] == "/command"
assert events[0].data["args"] == ["arg1", "arg2"]
assert isinstance(events[0].context, Context)
@pytest.mark.parametrize(
("attachment_type"),
[