core/tests/components/telegram_bot/conftest.py

229 lines
6.0 KiB
Python
Raw Normal View History

"""Tests for the telegram_bot integration."""
from unittest.mock import patch
import pytest
Bump python-telegram-bot package to 21.0.1 (#110297) * Bump python-telegram-bot package version to the latest. * PySocks is no longer required as python-telegram-bot doesn't use urllib3 anymore. * Fix moved ParseMode import * Update filters import to new structure. * Refactor removed Request objects to HTTPXRequest objects. * Update to support asyncc functions * Update timeout to new kwarg connect_timeout is the most obvious option based on current param description, but this may need changing. * Compatibility typo. * Make methods async and use Bot client async natively * Type needs to be Optional That's what the source types are from the library Also handle new possibility of None value * Add socks support version of the library * Refactor load_data function Update to be async friendly Refactor to use httpx instead of requests. * Refactor Dispatcher references to Application This is the newer model of the same class. * Make more stuff async-friendly. * Update tests to refactor Dispatcher usage out. * Remove import and reference directly * Refactor typing method * Use async_fire now we have async support * Fix some over complicate inheritance. * Add the polling test telegram_text event fired back in. * Add extra context to comment * Handler should also be async * Use underscores instead of camelCase Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Renamed kwarg. * Refactor current timeout param to be read timeout Reading the old version of the library code I believe this matches the existing functionality best * Combine unload methods into one listener * Fix test by stopping HA as part of fixture * Add new fixture to mock stop_polling call Use this in all polling tests. * No longer need to check if application is running It was to stop a test failing. * Make sure the updater is started in tests Mock external call methods Remove stop_polling mock. * Use cleaner references to patched methods * Improve test by letting the library create the Update object * Mock component tear down methods to be async * Bump mypy cache version * Update dependency to install from git Allows use as a custom component in 2024.3 Allows us to track mypy issue resolution. * Update manifest and requirements for new python-telegram-bot release. * Remove pytest filterwarnings entry for old version of python-telegram-bot library. --------- Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2024-03-08 07:56:26 +00:00
from telegram import User
from homeassistant.components.telegram_bot import (
CONF_ALLOWED_CHAT_IDS,
CONF_TRUSTED_NETWORKS,
DOMAIN,
)
Bump python-telegram-bot package to 21.0.1 (#110297) * Bump python-telegram-bot package version to the latest. * PySocks is no longer required as python-telegram-bot doesn't use urllib3 anymore. * Fix moved ParseMode import * Update filters import to new structure. * Refactor removed Request objects to HTTPXRequest objects. * Update to support asyncc functions * Update timeout to new kwarg connect_timeout is the most obvious option based on current param description, but this may need changing. * Compatibility typo. * Make methods async and use Bot client async natively * Type needs to be Optional That's what the source types are from the library Also handle new possibility of None value * Add socks support version of the library * Refactor load_data function Update to be async friendly Refactor to use httpx instead of requests. * Refactor Dispatcher references to Application This is the newer model of the same class. * Make more stuff async-friendly. * Update tests to refactor Dispatcher usage out. * Remove import and reference directly * Refactor typing method * Use async_fire now we have async support * Fix some over complicate inheritance. * Add the polling test telegram_text event fired back in. * Add extra context to comment * Handler should also be async * Use underscores instead of camelCase Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Renamed kwarg. * Refactor current timeout param to be read timeout Reading the old version of the library code I believe this matches the existing functionality best * Combine unload methods into one listener * Fix test by stopping HA as part of fixture * Add new fixture to mock stop_polling call Use this in all polling tests. * No longer need to check if application is running It was to stop a test failing. * Make sure the updater is started in tests Mock external call methods Remove stop_polling mock. * Use cleaner references to patched methods * Improve test by letting the library create the Update object * Mock component tear down methods to be async * Bump mypy cache version * Update dependency to install from git Allows use as a custom component in 2024.3 Allows us to track mypy issue resolution. * Update manifest and requirements for new python-telegram-bot release. * Remove pytest filterwarnings entry for old version of python-telegram-bot library. --------- Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2024-03-08 07:56:26 +00:00
from homeassistant.const import (
CONF_API_KEY,
CONF_PLATFORM,
CONF_URL,
EVENT_HOMEASSISTANT_START,
)
from homeassistant.setup import async_setup_component
@pytest.fixture
def config_webhooks():
"""Fixture for a webhooks platform configuration."""
return {
DOMAIN: [
{
CONF_PLATFORM: "webhooks",
CONF_URL: "https://test",
CONF_TRUSTED_NETWORKS: ["127.0.0.1"],
CONF_API_KEY: "1234567890:ABC",
CONF_ALLOWED_CHAT_IDS: [
# "me"
12345678,
# Some chat
-123456789,
],
}
]
}
@pytest.fixture
def config_polling():
"""Fixture for a polling platform configuration."""
return {
DOMAIN: [
{
CONF_PLATFORM: "polling",
CONF_API_KEY: "1234567890:ABC",
CONF_ALLOWED_CHAT_IDS: [
# "me"
12345678,
# Some chat
-123456789,
],
}
]
}
@pytest.fixture
def mock_register_webhook():
"""Mock calls made by telegram_bot when (de)registering webhook."""
with patch(
"homeassistant.components.telegram_bot.webhooks.PushBot.register_webhook",
return_value=True,
), patch(
"homeassistant.components.telegram_bot.webhooks.PushBot.deregister_webhook",
return_value=True,
):
yield
Bump python-telegram-bot package to 21.0.1 (#110297) * Bump python-telegram-bot package version to the latest. * PySocks is no longer required as python-telegram-bot doesn't use urllib3 anymore. * Fix moved ParseMode import * Update filters import to new structure. * Refactor removed Request objects to HTTPXRequest objects. * Update to support asyncc functions * Update timeout to new kwarg connect_timeout is the most obvious option based on current param description, but this may need changing. * Compatibility typo. * Make methods async and use Bot client async natively * Type needs to be Optional That's what the source types are from the library Also handle new possibility of None value * Add socks support version of the library * Refactor load_data function Update to be async friendly Refactor to use httpx instead of requests. * Refactor Dispatcher references to Application This is the newer model of the same class. * Make more stuff async-friendly. * Update tests to refactor Dispatcher usage out. * Remove import and reference directly * Refactor typing method * Use async_fire now we have async support * Fix some over complicate inheritance. * Add the polling test telegram_text event fired back in. * Add extra context to comment * Handler should also be async * Use underscores instead of camelCase Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Renamed kwarg. * Refactor current timeout param to be read timeout Reading the old version of the library code I believe this matches the existing functionality best * Combine unload methods into one listener * Fix test by stopping HA as part of fixture * Add new fixture to mock stop_polling call Use this in all polling tests. * No longer need to check if application is running It was to stop a test failing. * Make sure the updater is started in tests Mock external call methods Remove stop_polling mock. * Use cleaner references to patched methods * Improve test by letting the library create the Update object * Mock component tear down methods to be async * Bump mypy cache version * Update dependency to install from git Allows use as a custom component in 2024.3 Allows us to track mypy issue resolution. * Update manifest and requirements for new python-telegram-bot release. * Remove pytest filterwarnings entry for old version of python-telegram-bot library. --------- Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2024-03-08 07:56:26 +00:00
@pytest.fixture
def mock_external_calls():
"""Mock calls that make calls to the live Telegram API."""
test_user = User(123456, "Testbot", True)
with patch(
"telegram.Bot.get_me",
return_value=test_user,
), patch(
"telegram.Bot._bot_user",
test_user,
), patch(
"telegram.Bot.bot",
test_user,
), patch("telegram.ext.Updater._bootstrap"):
yield
@pytest.fixture
def mock_generate_secret_token():
"""Mock secret token generated for webhook."""
mock_secret_token = "DEADBEEF12345678DEADBEEF87654321"
with patch(
"homeassistant.components.telegram_bot.webhooks.secrets.choice",
side_effect=mock_secret_token,
):
yield mock_secret_token
@pytest.fixture
def incorrect_secret_token():
"""Mock incorrect secret token."""
return "AAAABBBBCCCCDDDDEEEEFFFF00009999"
@pytest.fixture
def update_message_command():
"""Fixture for mocking an incoming update of type message/command."""
return {
"update_id": 1,
"message": {
"message_id": 1,
"from": {
"id": 12345678,
"is_bot": False,
"first_name": "Firstname",
"username": "some_username",
"language_code": "en",
},
"chat": {
"id": -123456789,
"title": "SomeChat",
"type": "group",
"all_members_are_administrators": True,
},
"date": 1644518189,
"text": "/command",
"entities": [
{
"type": "bot_command",
"offset": 0,
"length": 7,
}
],
},
}
@pytest.fixture
def update_message_text():
"""Fixture for mocking an incoming update of type message/text."""
return {
"update_id": 1,
"message": {
"message_id": 1,
"date": 1441645532,
"from": {
"id": 12345678,
"is_bot": False,
"last_name": "Test Lastname",
"first_name": "Test Firstname",
"username": "Testusername",
},
"chat": {
"last_name": "Test Lastname",
"id": 1111111,
"type": "private",
"first_name": "Test Firstname",
"username": "Testusername",
},
"text": "HELLO",
},
}
@pytest.fixture
def unauthorized_update_message_text(update_message_text):
"""Fixture for mocking an incoming update of type message/text that is not in our `allowed_chat_ids`."""
update_message_text["message"]["from"]["id"] = 1234
update_message_text["message"]["chat"]["id"] = 1234
return update_message_text
@pytest.fixture
def update_callback_query():
"""Fixture for mocking an incoming update of type callback_query."""
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",
},
"chat_instance": "aaa111",
"data": "Data from button callback",
"inline_message_id": "1234csdbsk4839",
},
}
@pytest.fixture
async def webhook_platform(
Bump python-telegram-bot package to 21.0.1 (#110297) * Bump python-telegram-bot package version to the latest. * PySocks is no longer required as python-telegram-bot doesn't use urllib3 anymore. * Fix moved ParseMode import * Update filters import to new structure. * Refactor removed Request objects to HTTPXRequest objects. * Update to support asyncc functions * Update timeout to new kwarg connect_timeout is the most obvious option based on current param description, but this may need changing. * Compatibility typo. * Make methods async and use Bot client async natively * Type needs to be Optional That's what the source types are from the library Also handle new possibility of None value * Add socks support version of the library * Refactor load_data function Update to be async friendly Refactor to use httpx instead of requests. * Refactor Dispatcher references to Application This is the newer model of the same class. * Make more stuff async-friendly. * Update tests to refactor Dispatcher usage out. * Remove import and reference directly * Refactor typing method * Use async_fire now we have async support * Fix some over complicate inheritance. * Add the polling test telegram_text event fired back in. * Add extra context to comment * Handler should also be async * Use underscores instead of camelCase Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Renamed kwarg. * Refactor current timeout param to be read timeout Reading the old version of the library code I believe this matches the existing functionality best * Combine unload methods into one listener * Fix test by stopping HA as part of fixture * Add new fixture to mock stop_polling call Use this in all polling tests. * No longer need to check if application is running It was to stop a test failing. * Make sure the updater is started in tests Mock external call methods Remove stop_polling mock. * Use cleaner references to patched methods * Improve test by letting the library create the Update object * Mock component tear down methods to be async * Bump mypy cache version * Update dependency to install from git Allows use as a custom component in 2024.3 Allows us to track mypy issue resolution. * Update manifest and requirements for new python-telegram-bot release. * Remove pytest filterwarnings entry for old version of python-telegram-bot library. --------- Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2024-03-08 07:56:26 +00:00
hass,
config_webhooks,
mock_register_webhook,
mock_external_calls,
mock_generate_secret_token,
):
"""Fixture for setting up the webhooks platform using appropriate config and mocks."""
await async_setup_component(
hass,
DOMAIN,
config_webhooks,
)
await hass.async_block_till_done()
Bump python-telegram-bot package to 21.0.1 (#110297) * Bump python-telegram-bot package version to the latest. * PySocks is no longer required as python-telegram-bot doesn't use urllib3 anymore. * Fix moved ParseMode import * Update filters import to new structure. * Refactor removed Request objects to HTTPXRequest objects. * Update to support asyncc functions * Update timeout to new kwarg connect_timeout is the most obvious option based on current param description, but this may need changing. * Compatibility typo. * Make methods async and use Bot client async natively * Type needs to be Optional That's what the source types are from the library Also handle new possibility of None value * Add socks support version of the library * Refactor load_data function Update to be async friendly Refactor to use httpx instead of requests. * Refactor Dispatcher references to Application This is the newer model of the same class. * Make more stuff async-friendly. * Update tests to refactor Dispatcher usage out. * Remove import and reference directly * Refactor typing method * Use async_fire now we have async support * Fix some over complicate inheritance. * Add the polling test telegram_text event fired back in. * Add extra context to comment * Handler should also be async * Use underscores instead of camelCase Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Renamed kwarg. * Refactor current timeout param to be read timeout Reading the old version of the library code I believe this matches the existing functionality best * Combine unload methods into one listener * Fix test by stopping HA as part of fixture * Add new fixture to mock stop_polling call Use this in all polling tests. * No longer need to check if application is running It was to stop a test failing. * Make sure the updater is started in tests Mock external call methods Remove stop_polling mock. * Use cleaner references to patched methods * Improve test by letting the library create the Update object * Mock component tear down methods to be async * Bump mypy cache version * Update dependency to install from git Allows use as a custom component in 2024.3 Allows us to track mypy issue resolution. * Update manifest and requirements for new python-telegram-bot release. * Remove pytest filterwarnings entry for old version of python-telegram-bot library. --------- Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2024-03-08 07:56:26 +00:00
yield
await hass.async_stop()
@pytest.fixture
Bump python-telegram-bot package to 21.0.1 (#110297) * Bump python-telegram-bot package version to the latest. * PySocks is no longer required as python-telegram-bot doesn't use urllib3 anymore. * Fix moved ParseMode import * Update filters import to new structure. * Refactor removed Request objects to HTTPXRequest objects. * Update to support asyncc functions * Update timeout to new kwarg connect_timeout is the most obvious option based on current param description, but this may need changing. * Compatibility typo. * Make methods async and use Bot client async natively * Type needs to be Optional That's what the source types are from the library Also handle new possibility of None value * Add socks support version of the library * Refactor load_data function Update to be async friendly Refactor to use httpx instead of requests. * Refactor Dispatcher references to Application This is the newer model of the same class. * Make more stuff async-friendly. * Update tests to refactor Dispatcher usage out. * Remove import and reference directly * Refactor typing method * Use async_fire now we have async support * Fix some over complicate inheritance. * Add the polling test telegram_text event fired back in. * Add extra context to comment * Handler should also be async * Use underscores instead of camelCase Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Renamed kwarg. * Refactor current timeout param to be read timeout Reading the old version of the library code I believe this matches the existing functionality best * Combine unload methods into one listener * Fix test by stopping HA as part of fixture * Add new fixture to mock stop_polling call Use this in all polling tests. * No longer need to check if application is running It was to stop a test failing. * Make sure the updater is started in tests Mock external call methods Remove stop_polling mock. * Use cleaner references to patched methods * Improve test by letting the library create the Update object * Mock component tear down methods to be async * Bump mypy cache version * Update dependency to install from git Allows use as a custom component in 2024.3 Allows us to track mypy issue resolution. * Update manifest and requirements for new python-telegram-bot release. * Remove pytest filterwarnings entry for old version of python-telegram-bot library. --------- Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2024-03-08 07:56:26 +00:00
async def polling_platform(hass, config_polling, mock_external_calls):
"""Fixture for setting up the polling platform using appropriate config and mocks."""
await async_setup_component(
hass,
DOMAIN,
config_polling,
)
Bump python-telegram-bot package to 21.0.1 (#110297) * Bump python-telegram-bot package version to the latest. * PySocks is no longer required as python-telegram-bot doesn't use urllib3 anymore. * Fix moved ParseMode import * Update filters import to new structure. * Refactor removed Request objects to HTTPXRequest objects. * Update to support asyncc functions * Update timeout to new kwarg connect_timeout is the most obvious option based on current param description, but this may need changing. * Compatibility typo. * Make methods async and use Bot client async natively * Type needs to be Optional That's what the source types are from the library Also handle new possibility of None value * Add socks support version of the library * Refactor load_data function Update to be async friendly Refactor to use httpx instead of requests. * Refactor Dispatcher references to Application This is the newer model of the same class. * Make more stuff async-friendly. * Update tests to refactor Dispatcher usage out. * Remove import and reference directly * Refactor typing method * Use async_fire now we have async support * Fix some over complicate inheritance. * Add the polling test telegram_text event fired back in. * Add extra context to comment * Handler should also be async * Use underscores instead of camelCase Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Renamed kwarg. * Refactor current timeout param to be read timeout Reading the old version of the library code I believe this matches the existing functionality best * Combine unload methods into one listener * Fix test by stopping HA as part of fixture * Add new fixture to mock stop_polling call Use this in all polling tests. * No longer need to check if application is running It was to stop a test failing. * Make sure the updater is started in tests Mock external call methods Remove stop_polling mock. * Use cleaner references to patched methods * Improve test by letting the library create the Update object * Mock component tear down methods to be async * Bump mypy cache version * Update dependency to install from git Allows use as a custom component in 2024.3 Allows us to track mypy issue resolution. * Update manifest and requirements for new python-telegram-bot release. * Remove pytest filterwarnings entry for old version of python-telegram-bot library. --------- Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2024-03-08 07:56:26 +00:00
# Fire this event to start polling
hass.bus.fire(EVENT_HOMEASSISTANT_START)
await hass.async_block_till_done()