hass-core/tests/components/mikrotik/test_init.py
Joost Lekkerkerker 6bb4e7d62c
Bump ruff to 0.3.4 (#112690)
Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com>
Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
Co-authored-by: J. Nick Koston <nick@koston.org>
2024-03-26 00:02:16 +01:00

87 lines
2.4 KiB
Python

"""Test Mikrotik setup process."""
from unittest.mock import MagicMock, patch
from librouteros.exceptions import ConnectionClosed, LibRouterosError
import pytest
from homeassistant.components import mikrotik
from homeassistant.components.mikrotik.const import DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant
from . import MOCK_DATA
from tests.common import MockConfigEntry
@pytest.fixture(autouse=True)
def mock_api():
"""Mock api."""
with (
patch("librouteros.create_transport"),
patch("librouteros.Api.readResponse") as mock_api,
):
yield mock_api
async def test_successful_config_entry(hass: HomeAssistant) -> None:
"""Test config entry successful setup."""
entry = MockConfigEntry(
domain=mikrotik.DOMAIN,
data=MOCK_DATA,
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
assert entry.state == ConfigEntryState.LOADED
async def test_hub_connection_error(hass: HomeAssistant, mock_api: MagicMock) -> None:
"""Test setup fails due to connection error."""
entry = MockConfigEntry(
domain=mikrotik.DOMAIN,
data=MOCK_DATA,
)
entry.add_to_hass(hass)
mock_api.side_effect = ConnectionClosed
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert entry.state == ConfigEntryState.SETUP_RETRY
async def test_hub_authentication_error(
hass: HomeAssistant, mock_api: MagicMock
) -> None:
"""Test setup fails due to authentication error."""
entry = MockConfigEntry(
domain=mikrotik.DOMAIN,
data=MOCK_DATA,
)
entry.add_to_hass(hass)
mock_api.side_effect = LibRouterosError("invalid user name or password")
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert entry.state == ConfigEntryState.SETUP_ERROR
async def test_unload_entry(hass: HomeAssistant) -> None:
"""Test unloading an entry."""
entry = MockConfigEntry(
domain=mikrotik.DOMAIN,
data=MOCK_DATA,
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert await hass.config_entries.async_unload(entry.entry_id)
await hass.async_block_till_done()
assert entry.state == ConfigEntryState.NOT_LOADED
assert entry.entry_id not in hass.data[DOMAIN]