core/tests/components/sonarr/test_init.py

119 lines
3.6 KiB
Python
Raw Normal View History

"""Tests for the Sonsrr integration."""
2022-01-31 22:59:18 +00:00
from unittest.mock import MagicMock, patch
2022-02-22 17:33:10 +00:00
from aiopyarr import ArrAuthenticationException, ArrException
2021-01-01 21:31:56 +00:00
2022-02-22 17:33:10 +00:00
from homeassistant.components.sonarr.const import CONF_BASE_PATH, DOMAIN
from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntryState
2022-02-22 17:33:10 +00:00
from homeassistant.const import (
CONF_API_KEY,
CONF_HOST,
CONF_PORT,
CONF_SOURCE,
CONF_SSL,
CONF_URL,
CONF_VERIFY_SSL,
)
from homeassistant.core import HomeAssistant
2022-01-31 22:59:18 +00:00
from tests.common import MockConfigEntry
async def test_config_entry_not_ready(
2022-01-31 22:59:18 +00:00
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_sonarr: MagicMock,
) -> None:
"""Test the configuration entry not ready."""
2022-02-22 17:33:10 +00:00
mock_sonarr.async_get_system_status.side_effect = ArrException
2022-01-31 22:59:18 +00:00
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY
async def test_config_entry_reauth(
2022-01-31 22:59:18 +00:00
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_sonarr: MagicMock,
) -> None:
"""Test the configuration entry needing to be re-authenticated."""
2022-02-22 17:33:10 +00:00
mock_sonarr.async_get_system_status.side_effect = ArrAuthenticationException
2022-01-31 22:59:18 +00:00
with patch.object(hass.config_entries.flow, "async_init") as mock_flow_init:
2022-01-31 22:59:18 +00:00
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
2022-01-31 22:59:18 +00:00
assert mock_config_entry.state is ConfigEntryState.SETUP_ERROR
mock_flow_init.assert_called_once_with(
DOMAIN,
context={
CONF_SOURCE: SOURCE_REAUTH,
2022-01-31 22:59:18 +00:00
"entry_id": mock_config_entry.entry_id,
"unique_id": mock_config_entry.unique_id,
"title_placeholders": {"name": mock_config_entry.title},
},
2022-01-31 22:59:18 +00:00
data=mock_config_entry.data,
)
async def test_unload_config_entry(
2022-01-31 22:59:18 +00:00
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_sonarr: MagicMock,
) -> None:
"""Test the configuration entry unloading."""
2022-01-31 22:59:18 +00:00
mock_config_entry.add_to_hass(hass)
2020-08-20 10:21:58 +00:00
with patch(
2020-08-27 11:56:20 +00:00
"homeassistant.components.sonarr.sensor.async_setup_entry",
return_value=True,
2020-08-20 10:21:58 +00:00
):
2022-01-31 22:59:18 +00:00
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert hass.data[DOMAIN]
2022-01-31 22:59:18 +00:00
assert mock_config_entry.state is ConfigEntryState.LOADED
assert mock_config_entry.entry_id in hass.data[DOMAIN]
2022-01-31 22:59:18 +00:00
await hass.config_entries.async_unload(mock_config_entry.entry_id)
await hass.async_block_till_done()
2022-01-31 22:59:18 +00:00
assert mock_config_entry.state is ConfigEntryState.NOT_LOADED
assert mock_config_entry.entry_id not in hass.data[DOMAIN]
2022-02-22 17:33:10 +00:00
async def test_migrate_config_entry(hass: HomeAssistant) -> None:
2022-02-22 17:33:10 +00:00
"""Test successful migration of entry data."""
legacy_config = {
CONF_API_KEY: "MOCK_API_KEY",
CONF_HOST: "1.2.3.4",
CONF_PORT: 8989,
CONF_SSL: False,
CONF_VERIFY_SSL: False,
CONF_BASE_PATH: "/base/",
}
entry = MockConfigEntry(domain=DOMAIN, data=legacy_config)
assert entry.data == legacy_config
assert entry.version == 1
assert not entry.unique_id
await entry.async_migrate(hass)
assert entry.data == {
CONF_API_KEY: "MOCK_API_KEY",
CONF_HOST: "1.2.3.4",
CONF_PORT: 8989,
CONF_SSL: False,
CONF_VERIFY_SSL: False,
CONF_BASE_PATH: "/base/",
CONF_URL: "http://1.2.3.4:8989/base",
}
assert entry.version == 2
assert not entry.unique_id