mirror of
https://github.com/home-assistant/core.git
synced 2025-07-07 21:37:07 +00:00
Refactor EZVIZ config flow tests (#136434)
This commit is contained in:
parent
28951096a8
commit
fb04c256a8
@ -1,102 +1,13 @@
|
|||||||
"""Tests for the EZVIZ integration."""
|
"""Tests for the EZVIZ integration."""
|
||||||
|
|
||||||
from unittest.mock import _patch, patch
|
|
||||||
|
|
||||||
from homeassistant.components.ezviz.const import (
|
|
||||||
ATTR_SERIAL,
|
|
||||||
ATTR_TYPE_CAMERA,
|
|
||||||
ATTR_TYPE_CLOUD,
|
|
||||||
CONF_FFMPEG_ARGUMENTS,
|
|
||||||
CONF_RFSESSION_ID,
|
|
||||||
CONF_SESSION_ID,
|
|
||||||
DEFAULT_FFMPEG_ARGUMENTS,
|
|
||||||
DEFAULT_TIMEOUT,
|
|
||||||
DOMAIN,
|
|
||||||
)
|
|
||||||
from homeassistant.const import (
|
|
||||||
CONF_IP_ADDRESS,
|
|
||||||
CONF_PASSWORD,
|
|
||||||
CONF_TIMEOUT,
|
|
||||||
CONF_TYPE,
|
|
||||||
CONF_URL,
|
|
||||||
CONF_USERNAME,
|
|
||||||
)
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from tests.common import MockConfigEntry
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
ENTRY_CONFIG = {
|
|
||||||
CONF_SESSION_ID: "test-username",
|
|
||||||
CONF_RFSESSION_ID: "test-password",
|
|
||||||
CONF_URL: "apiieu.ezvizlife.com",
|
|
||||||
CONF_TYPE: ATTR_TYPE_CLOUD,
|
|
||||||
}
|
|
||||||
|
|
||||||
ENTRY_OPTIONS = {
|
async def setup_integration(hass: HomeAssistant, entry: MockConfigEntry) -> None:
|
||||||
CONF_FFMPEG_ARGUMENTS: DEFAULT_FFMPEG_ARGUMENTS,
|
|
||||||
CONF_TIMEOUT: DEFAULT_TIMEOUT,
|
|
||||||
}
|
|
||||||
|
|
||||||
USER_INPUT_VALIDATE = {
|
|
||||||
CONF_USERNAME: "test-username",
|
|
||||||
CONF_PASSWORD: "test-password",
|
|
||||||
CONF_URL: "apiieu.ezvizlife.com",
|
|
||||||
}
|
|
||||||
|
|
||||||
USER_INPUT = {
|
|
||||||
CONF_USERNAME: "test-username",
|
|
||||||
CONF_PASSWORD: "test-password",
|
|
||||||
CONF_URL: "apiieu.ezvizlife.com",
|
|
||||||
CONF_TYPE: ATTR_TYPE_CLOUD,
|
|
||||||
}
|
|
||||||
|
|
||||||
USER_INPUT_CAMERA_VALIDATE = {
|
|
||||||
ATTR_SERIAL: "C666666",
|
|
||||||
CONF_PASSWORD: "test-password",
|
|
||||||
CONF_USERNAME: "test-username",
|
|
||||||
}
|
|
||||||
|
|
||||||
USER_INPUT_CAMERA = {
|
|
||||||
CONF_PASSWORD: "test-password",
|
|
||||||
CONF_USERNAME: "test-username",
|
|
||||||
CONF_TYPE: ATTR_TYPE_CAMERA,
|
|
||||||
}
|
|
||||||
|
|
||||||
DISCOVERY_INFO = {
|
|
||||||
ATTR_SERIAL: "C666666",
|
|
||||||
CONF_USERNAME: None,
|
|
||||||
CONF_PASSWORD: None,
|
|
||||||
CONF_IP_ADDRESS: "127.0.0.1",
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST = {
|
|
||||||
CONF_USERNAME: None,
|
|
||||||
CONF_PASSWORD: None,
|
|
||||||
CONF_IP_ADDRESS: "127.0.0.1",
|
|
||||||
}
|
|
||||||
|
|
||||||
API_LOGIN_RETURN_VALIDATE = {
|
|
||||||
CONF_SESSION_ID: "fake_token",
|
|
||||||
CONF_RFSESSION_ID: "fake_rf_token",
|
|
||||||
CONF_URL: "apiieu.ezvizlife.com",
|
|
||||||
CONF_TYPE: ATTR_TYPE_CLOUD,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def patch_async_setup_entry() -> _patch:
|
|
||||||
"""Patch async_setup_entry."""
|
|
||||||
return patch(
|
|
||||||
"homeassistant.components.ezviz.async_setup_entry",
|
|
||||||
return_value=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def init_integration(hass: HomeAssistant) -> MockConfigEntry:
|
|
||||||
"""Set up the EZVIZ integration in Home Assistant."""
|
"""Set up the EZVIZ integration in Home Assistant."""
|
||||||
entry = MockConfigEntry(domain=DOMAIN, data=ENTRY_CONFIG, options=ENTRY_OPTIONS)
|
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
await hass.config_entries.async_setup(entry.entry_id)
|
await hass.config_entries.async_setup(entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
return entry
|
|
||||||
|
@ -1,19 +1,30 @@
|
|||||||
"""Define pytest.fixtures available for all tests."""
|
"""Define pytest.fixtures available for all tests."""
|
||||||
|
|
||||||
from collections.abc import Generator
|
from collections.abc import Generator
|
||||||
from unittest.mock import MagicMock, patch
|
from unittest.mock import AsyncMock, MagicMock, patch
|
||||||
|
|
||||||
from pyezviz import EzvizClient
|
|
||||||
from pyezviz.test_cam_rtsp import TestRTSPAuth
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
from homeassistant.components.ezviz import (
|
||||||
|
ATTR_TYPE_CAMERA,
|
||||||
|
ATTR_TYPE_CLOUD,
|
||||||
|
CONF_RFSESSION_ID,
|
||||||
|
CONF_SESSION_ID,
|
||||||
|
DOMAIN,
|
||||||
|
)
|
||||||
|
from homeassistant.const import CONF_PASSWORD, CONF_TYPE, CONF_URL, CONF_USERNAME
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
ezviz_login_token_return = {
|
from tests.common import MockConfigEntry
|
||||||
"session_id": "fake_token",
|
|
||||||
"rf_session_id": "fake_rf_token",
|
|
||||||
"api_url": "apiieu.ezvizlife.com",
|
@pytest.fixture
|
||||||
}
|
def mock_setup_entry() -> Generator[AsyncMock]:
|
||||||
|
"""Mock setting up a config entry."""
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.ezviz.async_setup_entry", return_value=True
|
||||||
|
) as setup_entry_mock:
|
||||||
|
yield setup_entry_mock
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
@pytest.fixture(autouse=True)
|
||||||
@ -23,40 +34,67 @@ def mock_ffmpeg(hass: HomeAssistant) -> None:
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def ezviz_test_rtsp_config_flow() -> Generator[MagicMock]:
|
def mock_config_entry() -> MockConfigEntry:
|
||||||
|
"""Return the default mocked config entry."""
|
||||||
|
return MockConfigEntry(
|
||||||
|
domain=DOMAIN,
|
||||||
|
unique_id="test-username",
|
||||||
|
title="test-username",
|
||||||
|
data={
|
||||||
|
CONF_SESSION_ID: "test-username",
|
||||||
|
CONF_RFSESSION_ID: "test-password",
|
||||||
|
CONF_URL: "apiieu.ezvizlife.com",
|
||||||
|
CONF_TYPE: ATTR_TYPE_CLOUD,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_camera_config_entry() -> MockConfigEntry:
|
||||||
|
"""Return the default mocked config entry."""
|
||||||
|
return MockConfigEntry(
|
||||||
|
domain=DOMAIN,
|
||||||
|
unique_id="C666666",
|
||||||
|
title="Camera 1",
|
||||||
|
data={
|
||||||
|
CONF_TYPE: ATTR_TYPE_CAMERA,
|
||||||
|
CONF_USERNAME: "test-username",
|
||||||
|
CONF_PASSWORD: "test-password",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_ezviz_client() -> Generator[AsyncMock]:
|
||||||
|
"""Mock the EzvizAPI for easier testing."""
|
||||||
|
with (
|
||||||
|
patch(
|
||||||
|
"homeassistant.components.ezviz.EzvizClient", autospec=True
|
||||||
|
) as mock_ezviz,
|
||||||
|
patch("homeassistant.components.ezviz.config_flow.EzvizClient", new=mock_ezviz),
|
||||||
|
):
|
||||||
|
instance = mock_ezviz.return_value
|
||||||
|
|
||||||
|
instance.login.return_value = {
|
||||||
|
"session_id": "fake_token",
|
||||||
|
"rf_session_id": "fake_rf_token",
|
||||||
|
"api_url": "apiieu.ezvizlife.com",
|
||||||
|
}
|
||||||
|
instance.get_detection_sensibility.return_value = True
|
||||||
|
|
||||||
|
yield instance
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_test_rtsp_auth() -> Generator[MagicMock]:
|
||||||
"""Mock the EzvizApi for easier testing."""
|
"""Mock the EzvizApi for easier testing."""
|
||||||
with (
|
with (
|
||||||
patch.object(TestRTSPAuth, "main", return_value=True),
|
|
||||||
patch(
|
patch(
|
||||||
"homeassistant.components.ezviz.config_flow.TestRTSPAuth"
|
"homeassistant.components.ezviz.config_flow.TestRTSPAuth"
|
||||||
) as mock_ezviz_test_rtsp,
|
) as mock_ezviz_test_rtsp,
|
||||||
):
|
):
|
||||||
instance = mock_ezviz_test_rtsp.return_value = TestRTSPAuth(
|
instance = mock_ezviz_test_rtsp.return_value
|
||||||
"test-ip",
|
|
||||||
"test-username",
|
|
||||||
"test-password",
|
|
||||||
)
|
|
||||||
|
|
||||||
instance.main = MagicMock(return_value=True)
|
instance.main.return_value = True
|
||||||
|
|
||||||
yield mock_ezviz_test_rtsp
|
yield instance
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def ezviz_config_flow() -> Generator[MagicMock]:
|
|
||||||
"""Mock the EzvizAPI for easier config flow testing."""
|
|
||||||
with (
|
|
||||||
patch.object(EzvizClient, "login", return_value=True),
|
|
||||||
patch("homeassistant.components.ezviz.config_flow.EzvizClient") as mock_ezviz,
|
|
||||||
):
|
|
||||||
instance = mock_ezviz.return_value = EzvizClient(
|
|
||||||
"test-username",
|
|
||||||
"test-password",
|
|
||||||
"local.host",
|
|
||||||
"1",
|
|
||||||
)
|
|
||||||
|
|
||||||
instance.login = MagicMock(return_value=ezviz_login_token_return)
|
|
||||||
instance.get_detection_sensibility = MagicMock(return_value=True)
|
|
||||||
|
|
||||||
yield mock_ezviz
|
|
||||||
|
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user