core/tests/components/sonarr/test_init.py

80 lines
2.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
from sonarr import SonarrAccessRestricted, SonarrError
2021-01-01 21:31:56 +00:00
from homeassistant.components.sonarr.const import DOMAIN
from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntryState
from homeassistant.const import CONF_SOURCE
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-01-31 22:59:18 +00:00
mock_sonarr.update.side_effect = SonarrError
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-01-31 22:59:18 +00:00
mock_sonarr.update.side_effect = SonarrAccessRestricted
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]