Reolink 100% test coverage (#94763)

This commit is contained in:
starkillerOG 2023-08-15 16:50:17 +02:00 committed by GitHub
parent e209f3723e
commit 92cf6ed2a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 24 deletions

View File

@ -58,18 +58,16 @@ def reolink_connect(mock_get_source_ip: None) -> Generator[MagicMock, None, None
host_mock.is_admin = True host_mock.is_admin = True
host_mock.user_level = "admin" host_mock.user_level = "admin"
host_mock.sw_version_update_required = False host_mock.sw_version_update_required = False
host_mock.hardware_version = "IPC_00000"
host_mock.sw_version = "v1.0.0.0.0.0000"
host_mock.manufacturer = "Reolink"
host_mock.model = "RLC-123"
host_mock.session_active = True
host_mock.timeout = 60 host_mock.timeout = 60
host_mock.renewtimer.return_value = 600 host_mock.renewtimer.return_value = 600
yield host_mock yield host_mock
@pytest.fixture
def reolink_ONVIF_wait() -> Generator[None, None, None]:
"""Mock reolink connection."""
with patch("homeassistant.components.reolink.host.asyncio.Event.wait", AsyncMock()):
yield
@pytest.fixture @pytest.fixture
def reolink_platforms(mock_get_source_ip: None) -> Generator[None, None, None]: def reolink_platforms(mock_get_source_ip: None) -> Generator[None, None, None]:
"""Mock reolink entry setup.""" """Mock reolink entry setup."""

View File

@ -29,9 +29,7 @@ from .conftest import (
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
pytestmark = pytest.mark.usefixtures( pytestmark = pytest.mark.usefixtures("mock_setup_entry", "reolink_connect")
"mock_setup_entry", "reolink_connect", "reolink_ONVIF_wait"
)
async def test_config_flow_manual_success(hass: HomeAssistant) -> None: async def test_config_flow_manual_success(hass: HomeAssistant) -> None:

View File

@ -1,18 +1,23 @@
"""Test the Reolink init.""" """Test the Reolink init."""
from datetime import timedelta
from typing import Any from typing import Any
from unittest.mock import AsyncMock, MagicMock, Mock, patch from unittest.mock import AsyncMock, MagicMock, Mock, patch
import pytest import pytest
from reolink_aio.exceptions import ReolinkError from reolink_aio.exceptions import ReolinkError
from homeassistant.components.reolink import const from homeassistant.components.reolink import FIRMWARE_UPDATE_INTERVAL, const
from homeassistant.config import async_process_ha_core_config from homeassistant.config import async_process_ha_core_config
from homeassistant.config_entries import ConfigEntryState from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import STATE_OFF, STATE_UNAVAILABLE, Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import issue_registry as ir from homeassistant.helpers import issue_registry as ir
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from homeassistant.util.dt import utcnow
from tests.common import MockConfigEntry from .conftest import TEST_NVR_NAME
from tests.common import MockConfigEntry, async_fire_time_changed
pytestmark = pytest.mark.usefixtures("reolink_connect", "reolink_platforms") pytestmark = pytest.mark.usefixtures("reolink_connect", "reolink_platforms")
@ -45,17 +50,11 @@ pytestmark = pytest.mark.usefixtures("reolink_connect", "reolink_platforms")
Mock(return_value=False), Mock(return_value=False),
ConfigEntryState.LOADED, ConfigEntryState.LOADED,
), ),
(
"check_new_firmware",
AsyncMock(side_effect=ReolinkError("Test error")),
ConfigEntryState.LOADED,
),
], ],
) )
async def test_failures_parametrized( async def test_failures_parametrized(
hass: HomeAssistant, hass: HomeAssistant,
reolink_connect: MagicMock, reolink_connect: MagicMock,
reolink_ONVIF_wait: MagicMock,
config_entry: MockConfigEntry, config_entry: MockConfigEntry,
attr: str, attr: str,
value: Any, value: Any,
@ -71,11 +70,36 @@ async def test_failures_parametrized(
assert config_entry.state == expected assert config_entry.state == expected
async def test_firmware_error_twice(
hass: HomeAssistant,
reolink_connect: MagicMock,
config_entry: MockConfigEntry,
) -> None:
"""Test when the firmware update fails 2 times."""
reolink_connect.check_new_firmware = AsyncMock(
side_effect=ReolinkError("Test error")
)
with patch("homeassistant.components.reolink.PLATFORMS", [Platform.UPDATE]):
assert await hass.config_entries.async_setup(config_entry.entry_id) is True
await hass.async_block_till_done()
assert config_entry.state == ConfigEntryState.LOADED
entity_id = f"{Platform.UPDATE}.{TEST_NVR_NAME}_update"
assert hass.states.is_state(entity_id, STATE_OFF)
async_fire_time_changed(
hass, utcnow() + FIRMWARE_UPDATE_INTERVAL + timedelta(minutes=1)
)
await hass.async_block_till_done()
assert hass.states.is_state(entity_id, STATE_UNAVAILABLE)
async def test_entry_reloading( async def test_entry_reloading(
hass: HomeAssistant, hass: HomeAssistant,
config_entry: MockConfigEntry, config_entry: MockConfigEntry,
reolink_connect: MagicMock, reolink_connect: MagicMock,
reolink_ONVIF_wait: MagicMock,
) -> None: ) -> None:
"""Test the entry is reloaded correctly when settings change.""" """Test the entry is reloaded correctly when settings change."""
assert await hass.config_entries.async_setup(config_entry.entry_id) assert await hass.config_entries.async_setup(config_entry.entry_id)
@ -92,7 +116,7 @@ async def test_entry_reloading(
async def test_no_repair_issue( async def test_no_repair_issue(
hass: HomeAssistant, config_entry: MockConfigEntry, reolink_ONVIF_wait: MagicMock hass: HomeAssistant, config_entry: MockConfigEntry
) -> None: ) -> None:
"""Test no repairs issue is raised when http local url is used.""" """Test no repairs issue is raised when http local url is used."""
await async_process_ha_core_config( await async_process_ha_core_config(
@ -111,7 +135,7 @@ async def test_no_repair_issue(
async def test_https_repair_issue( async def test_https_repair_issue(
hass: HomeAssistant, config_entry: MockConfigEntry, reolink_ONVIF_wait: MagicMock hass: HomeAssistant, config_entry: MockConfigEntry
) -> None: ) -> None:
"""Test repairs issue is raised when https local url is used.""" """Test repairs issue is raised when https local url is used."""
await async_process_ha_core_config( await async_process_ha_core_config(
@ -133,7 +157,7 @@ async def test_https_repair_issue(
async def test_ssl_repair_issue( async def test_ssl_repair_issue(
hass: HomeAssistant, config_entry: MockConfigEntry, reolink_ONVIF_wait: MagicMock hass: HomeAssistant, config_entry: MockConfigEntry
) -> None: ) -> None:
"""Test repairs issue is raised when global ssl certificate is used.""" """Test repairs issue is raised when global ssl certificate is used."""
assert await async_setup_component(hass, "webhook", {}) assert await async_setup_component(hass, "webhook", {})
@ -162,7 +186,6 @@ async def test_port_repair_issue(
hass: HomeAssistant, hass: HomeAssistant,
config_entry: MockConfigEntry, config_entry: MockConfigEntry,
reolink_connect: MagicMock, reolink_connect: MagicMock,
reolink_ONVIF_wait: MagicMock,
protocol: str, protocol: str,
) -> None: ) -> None:
"""Test repairs issue is raised when auto enable of ports fails.""" """Test repairs issue is raised when auto enable of ports fails."""
@ -200,7 +223,6 @@ async def test_firmware_repair_issue(
hass: HomeAssistant, hass: HomeAssistant,
config_entry: MockConfigEntry, config_entry: MockConfigEntry,
reolink_connect: MagicMock, reolink_connect: MagicMock,
reolink_ONVIF_wait: MagicMock,
) -> None: ) -> None:
"""Test firmware issue is raised when too old firmware is used.""" """Test firmware issue is raised when too old firmware is used."""
reolink_connect.sw_version_update_required = True reolink_connect.sw_version_update_required = True