Skip to content

Commit 4488fdd

Browse files
authored
Remove yaml in tests for Telegram webhook bot (home-assistant#157091)
1 parent a6e0bea commit 4488fdd

File tree

7 files changed

+73
-81
lines changed

7 files changed

+73
-81
lines changed

tests/components/telegram_bot/conftest.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,20 +307,36 @@ def mock_webhooks_config_entry() -> MockConfigEntry:
307307
CONF_PLATFORM: PLATFORM_WEBHOOKS,
308308
CONF_API_KEY: "mock api key",
309309
CONF_URL: "https://test",
310-
CONF_TRUSTED_NETWORKS: ["149.154.160.0/20", "91.108.4.0/22"],
310+
CONF_TRUSTED_NETWORKS: ["127.0.0.1"],
311311
},
312312
options={ATTR_PARSER: PARSER_MD},
313313
subentries_data=[
314314
ConfigSubentryData(
315-
unique_id="1234567890",
316-
data={CONF_CHAT_ID: 1234567890},
315+
unique_id="12345678",
316+
data={CONF_CHAT_ID: 12345678},
317317
subentry_type=CONF_ALLOWED_CHAT_IDS,
318318
title="mock chat",
319319
)
320320
],
321321
)
322322

323323

324+
@pytest.fixture
325+
async def webhook_bot(
326+
hass: HomeAssistant,
327+
mock_webhooks_config_entry: MockConfigEntry,
328+
mock_register_webhook: None,
329+
mock_external_calls: None,
330+
mock_generate_secret_token: str,
331+
) -> AsyncGenerator[None]:
332+
"""Fixture for setting up a webhook telegram bot."""
333+
mock_webhooks_config_entry.add_to_hass(hass)
334+
await hass.config_entries.async_setup(mock_webhooks_config_entry.entry_id)
335+
await hass.async_block_till_done()
336+
yield
337+
await hass.async_stop()
338+
339+
324340
@pytest.fixture
325341
async def webhook_platform(
326342
hass: HomeAssistant,

tests/components/telegram_bot/snapshots/test_diagnostics.ambr

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
'data': dict({
55
'api_key': '**REDACTED**',
66
'platform': 'webhooks',
7-
'proxy_url': None,
87
'trusted_networks': list([
9-
'127.0.0.1/32',
8+
'127.0.0.1',
109
]),
1110
'url': 'https://**redacted**/',
1211
}),
1312
'options': dict({
1413
'parse_mode': 'markdown',
1514
}),
16-
'subentries_count': 2,
15+
'subentries_count': 1,
1716
})
1817
# ---
Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
11
"""Test Telegram broadcast."""
22

33
from homeassistant.core import HomeAssistant
4-
from homeassistant.setup import async_setup_component
54

5+
from tests.common import MockConfigEntry
66

7-
async def test_setup(hass: HomeAssistant, mock_external_calls: None) -> None:
7+
8+
async def test_setup(
9+
hass: HomeAssistant, mock_broadcast_config_entry: MockConfigEntry
10+
) -> None:
811
"""Test setting up Telegram broadcast."""
9-
assert await async_setup_component(
10-
hass,
11-
"telegram_bot",
12-
{
13-
"telegram_bot": {
14-
"platform": "broadcast",
15-
"api_key": "1234567890:ABC",
16-
"allowed_chat_ids": [1],
17-
}
18-
},
19-
)
12+
mock_broadcast_config_entry.add_to_hass(hass)
13+
await hass.config_entries.async_setup(mock_broadcast_config_entry.entry_id)
2014
await hass.async_block_till_done()
2115

2216
assert hass.services.has_service("telegram_bot", "send_message") is True

tests/components/telegram_bot/test_diagnostics.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
async def test_diagnostics(
1313
hass: HomeAssistant,
1414
hass_client: ClientSessionGenerator,
15-
webhook_platform,
16-
mock_external_calls: None,
17-
mock_generate_secret_token,
15+
webhook_bot,
1816
snapshot: SnapshotAssertion,
1917
) -> None:
2018
"""Test diagnostics."""

tests/components/telegram_bot/test_notify.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
@pytest.mark.freeze_time("2025-01-09T12:00:00+00:00")
2323
async def test_send_message(
2424
hass: HomeAssistant,
25-
webhook_platform: None,
25+
webhook_bot: None,
2626
) -> None:
27-
"""Test publishing ntfy message."""
27+
"""Test send message."""
2828

2929
context = Context()
3030
events = async_capture_events(hass, "telegram_sent")
@@ -35,15 +35,15 @@ async def test_send_message(
3535
return_value=Message(
3636
message_id=12345,
3737
date=datetime.now(),
38-
chat=Chat(id=123456, type=ChatType.PRIVATE),
38+
chat=Chat(id=12345678, type=ChatType.PRIVATE),
3939
)
4040
),
4141
) as mock_send_message:
4242
await hass.services.async_call(
4343
NOTIFY_DOMAIN,
4444
SERVICE_SEND_MESSAGE,
4545
{
46-
ATTR_ENTITY_ID: "notify.testbot_mock_last_name_mock_title_12345678",
46+
ATTR_ENTITY_ID: "notify.mock_title_mock_chat",
4747
ATTR_MESSAGE: "mock message",
4848
ATTR_TITLE: "mock title",
4949
},
@@ -64,7 +64,7 @@ async def test_send_message(
6464
message_thread_id=None,
6565
)
6666

67-
state = hass.states.get("notify.testbot_mock_last_name_mock_title_12345678")
67+
state = hass.states.get("notify.mock_title_mock_chat")
6868
assert state
6969
assert state.state == "2025-01-09T12:00:00+00:00"
7070

0 commit comments

Comments
 (0)