hass-core/tests/components/reolink/test_init.py
starkillerOG 2d3c5cf8ee
Reolink test init 100% (#89112)
* Split out reolink tests

* Bring __init__ coverage to 100%

* Improve docstrings

* Use patching and autospec=True for ReolinkHost

* Use fixture

* fix styling

* Parametrize tests

* Update tests/components/reolink/conftest.py

Co-authored-by: Franck Nijhof <frenck@frenck.nl>

* Apply suggestions from code review

Co-authored-by: Franck Nijhof <frenck@frenck.nl>

* Update test_config_flow.py

* convert to fixture

* review comments

* Update tests/components/reolink/conftest.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Update tests/components/reolink/conftest.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Update tests/components/reolink/conftest.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* fix tests

* fix imports

* Update test_init.py

* Check if host is logout on reload

---------

Co-authored-by: Franck Nijhof <frenck@frenck.nl>
Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
2023-03-07 20:29:38 +01:00

116 lines
3.6 KiB
Python

"""Test the Reolink init."""
from typing import Any
from unittest.mock import AsyncMock, MagicMock, Mock
import pytest
from reolink_aio.exceptions import ReolinkError
from homeassistant.components.reolink import const
from homeassistant.config import async_process_ha_core_config
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant
from homeassistant.helpers import issue_registry as ir
from tests.common import MockConfigEntry
pytestmark = pytest.mark.usefixtures("reolink_connect", "reolink_platforms")
@pytest.mark.parametrize(
("attr", "value", "expected"),
[
(
"is_admin",
False,
ConfigEntryState.SETUP_ERROR,
),
(
"get_host_data",
AsyncMock(side_effect=ReolinkError("Test error")),
ConfigEntryState.SETUP_RETRY,
),
(
"get_host_data",
AsyncMock(side_effect=ValueError("Test error")),
ConfigEntryState.SETUP_ERROR,
),
(
"get_states",
AsyncMock(side_effect=ReolinkError("Test error")),
ConfigEntryState.SETUP_RETRY,
),
(
"supported",
Mock(return_value=False),
ConfigEntryState.LOADED,
),
(
"check_new_firmware",
AsyncMock(side_effect=ReolinkError("Test error")),
ConfigEntryState.LOADED,
),
],
)
async def test_failures_parametrized(
hass: HomeAssistant,
reolink_connect: MagicMock,
config_entry: MockConfigEntry,
attr: str,
value: Any,
expected: ConfigEntryState,
) -> None:
"""Test outcomes when changing errors."""
setattr(reolink_connect, attr, value)
assert await hass.config_entries.async_setup(config_entry.entry_id) is (
expected == ConfigEntryState.LOADED
)
await hass.async_block_till_done()
assert config_entry.state == expected
async def test_entry_reloading(
hass: HomeAssistant, config_entry: MockConfigEntry, reolink_connect: MagicMock
) -> None:
"""Test the entry is reloaded correctly when settings change."""
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
assert reolink_connect.logout.call_count == 0
assert config_entry.title == "test_reolink_name"
hass.config_entries.async_update_entry(config_entry, title="New Name")
await hass.async_block_till_done()
assert reolink_connect.logout.call_count == 1
assert config_entry.title == "New Name"
async def test_http_no_repair_issue(
hass: HomeAssistant, config_entry: MockConfigEntry
) -> None:
"""Test no repairs issue is raised when http local url is used."""
await async_process_ha_core_config(
hass, {"country": "GB", "internal_url": "http://test_homeassistant_address"}
)
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
issue_registry = ir.async_get(hass)
assert (const.DOMAIN, "https_webhook") not in issue_registry.issues
async def test_https_repair_issue(
hass: HomeAssistant, config_entry: MockConfigEntry
) -> None:
"""Test repairs issue is raised when https local url is used."""
await async_process_ha_core_config(
hass, {"country": "GB", "internal_url": "https://test_homeassistant_address"}
)
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
issue_registry = ir.async_get(hass)
assert (const.DOMAIN, "https_webhook") in issue_registry.issues