From 32221a1ec4664b48d0a7e1dc6af78c21cfb36bba Mon Sep 17 00:00:00 2001 From: hanwg Date: Wed, 25 Mar 2026 16:45:44 +0800 Subject: [PATCH] Fix open sockets in tests for Telegram bot (#166451) --- .../telegram_bot/test_config_flow.py | 17 +++++++++++------ .../telegram_bot/test_telegram_bot.py | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/tests/components/telegram_bot/test_config_flow.py b/tests/components/telegram_bot/test_config_flow.py index dbf96ff2379..5db2e92edbf 100644 --- a/tests/components/telegram_bot/test_config_flow.py +++ b/tests/components/telegram_bot/test_config_flow.py @@ -298,7 +298,12 @@ async def test_reconfigure_flow_logout_failed( assert mock_broadcast_config_entry.data[CONF_API_ENDPOINT] == "http://mock2" -async def test_create_entry(hass: HomeAssistant) -> None: +async def test_create_entry( + hass: HomeAssistant, + mock_register_webhook: None, + mock_external_calls: None, + mock_generate_secret_token: str, +) -> None: """Test user flow.""" # test: no input @@ -334,10 +339,9 @@ async def test_create_entry(hass: HomeAssistant) -> None: # test: telegram error with patch( - "homeassistant.components.telegram_bot.config_flow.Bot.get_me", - ) as mock_bot: - mock_bot.side_effect = NetworkError("mock network error") - + "homeassistant.components.telegram_bot.bot.Bot.get_me", + side_effect=NetworkError("mock network error"), + ) as mock_get_me: result = await hass.config_entries.flow.async_configure( result["flow_id"], { @@ -350,6 +354,7 @@ async def test_create_entry(hass: HomeAssistant) -> None: ) await hass.async_block_till_done() + mock_get_me.assert_called_once() assert result["step_id"] == "user" assert result["type"] is FlowResultType.FORM assert result["errors"]["base"] == "telegram_error" @@ -389,7 +394,7 @@ async def test_create_entry(hass: HomeAssistant) -> None: await hass.async_block_till_done() assert result["type"] is FlowResultType.CREATE_ENTRY - assert result["title"] == "Testbot" + assert result["title"] == "Testbot mock last name" assert result["data"][CONF_PLATFORM] == PLATFORM_WEBHOOKS assert result["data"][CONF_API_KEY] == "mock api key" assert result["data"][CONF_PROXY_URL] == "https://proxy" diff --git a/tests/components/telegram_bot/test_telegram_bot.py b/tests/components/telegram_bot/test_telegram_bot.py index 43bb81c644a..bd04afed6e7 100644 --- a/tests/components/telegram_bot/test_telegram_bot.py +++ b/tests/components/telegram_bot/test_telegram_bot.py @@ -90,6 +90,7 @@ from homeassistant.components.telegram_bot.const import ( SERVICE_SEND_VOICE, ) from homeassistant.components.telegram_bot.webhooks import TELEGRAM_WEBHOOK_URL +from homeassistant.config_entries import ConfigEntryState from homeassistant.const import ( ATTR_DOMAIN, ATTR_ENTITY_ID, @@ -132,6 +133,23 @@ async def test_polling_platform_init( assert hass.services.has_service(DOMAIN, SERVICE_SEND_MESSAGE) is True +async def test_polling_platform_init_failed( + hass: HomeAssistant, + mock_polling_config_entry: MockConfigEntry, +) -> None: + """Test failed initialization of the polling platform.""" + with patch( + "homeassistant.components.telegram_bot.bot.Bot.get_me", + side_effect=NetworkError("mock network error"), + ) as mock_get_me: + mock_polling_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(mock_polling_config_entry.entry_id) + await hass.async_block_till_done() + + mock_get_me.assert_called_once() + assert mock_polling_config_entry.state == ConfigEntryState.SETUP_RETRY + + @pytest.mark.parametrize( ("service", "input"), [