mirror of
https://github.com/home-assistant/core.git
synced 2026-02-15 07:36:16 +00:00
Improve client mock for NextDNS tests (#155036)
Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
@@ -59,29 +59,31 @@ def mock_config_entry() -> MockConfigEntry:
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_nextdns_client() -> Generator[AsyncMock]:
|
||||
"""Mock a NextDNS client."""
|
||||
|
||||
def mock_nextdns() -> Generator[AsyncMock]:
|
||||
"""Mock the NextDns class."""
|
||||
with (
|
||||
patch("homeassistant.components.nextdns.NextDns", autospec=True) as mock_client,
|
||||
patch(
|
||||
"homeassistant.components.nextdns.config_flow.NextDns",
|
||||
new=mock_client,
|
||||
),
|
||||
patch("homeassistant.components.nextdns.NextDns", autospec=True) as mock_class,
|
||||
patch("homeassistant.components.nextdns.config_flow.NextDns", new=mock_class),
|
||||
):
|
||||
client = mock_client.create.return_value
|
||||
client.clear_logs.return_value = True
|
||||
client.connection_status.return_value = CONNECTION_STATUS
|
||||
client.get_analytics_dnssec.return_value = ANALYTICS_DNSSEC
|
||||
client.get_analytics_encryption.return_value = ANALYTICS_ENCRYPTION
|
||||
client.get_analytics_ip_versions.return_value = ANALYTICS_IP_VERSIONS
|
||||
client.get_analytics_protocols.return_value = ANALYTICS_PROTOCOLS
|
||||
client.get_analytics_status.return_value = ANALYTICS_STATUS
|
||||
client.get_profile_id = Mock(return_value="xyz12")
|
||||
client.get_profile_name = Mock(return_value="Fake Profile")
|
||||
client.get_profiles.return_value = PROFILES
|
||||
client.get_settings.return_value = SETTINGS
|
||||
client.set_setting.return_value = True
|
||||
client.profiles = [ProfileInfo(**PROFILES[0])]
|
||||
yield mock_class
|
||||
|
||||
yield client
|
||||
|
||||
@pytest.fixture
|
||||
def mock_nextdns_client(mock_nextdns: AsyncMock) -> AsyncMock:
|
||||
"""Mock a NextDNS client instance."""
|
||||
client = mock_nextdns.create.return_value
|
||||
client.clear_logs.return_value = True
|
||||
client.connection_status.return_value = CONNECTION_STATUS
|
||||
client.get_analytics_dnssec.return_value = ANALYTICS_DNSSEC
|
||||
client.get_analytics_encryption.return_value = ANALYTICS_ENCRYPTION
|
||||
client.get_analytics_ip_versions.return_value = ANALYTICS_IP_VERSIONS
|
||||
client.get_analytics_protocols.return_value = ANALYTICS_PROTOCOLS
|
||||
client.get_analytics_status.return_value = ANALYTICS_STATUS
|
||||
client.get_profile_id = Mock(return_value="xyz12")
|
||||
client.get_profile_name = Mock(return_value="Fake Profile")
|
||||
client.get_profiles.return_value = PROFILES
|
||||
client.get_settings.return_value = SETTINGS
|
||||
client.set_setting.return_value = True
|
||||
client.profiles = [ProfileInfo(**PROFILES[0])]
|
||||
|
||||
return client
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"""Define tests for the NextDNS config flow."""
|
||||
|
||||
from unittest.mock import AsyncMock, patch
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from nextdns import ApiError, InvalidApiKeyError
|
||||
import pytest
|
||||
@@ -21,6 +21,7 @@ async def test_form_create_entry(
|
||||
hass: HomeAssistant,
|
||||
mock_setup_entry: AsyncMock,
|
||||
mock_nextdns_client: AsyncMock,
|
||||
mock_nextdns: AsyncMock,
|
||||
) -> None:
|
||||
"""Test that the user step works."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
@@ -64,6 +65,7 @@ async def test_form_errors(
|
||||
hass: HomeAssistant,
|
||||
mock_setup_entry: AsyncMock,
|
||||
mock_nextdns_client: AsyncMock,
|
||||
mock_nextdns: AsyncMock,
|
||||
exc: Exception,
|
||||
base_error: str,
|
||||
) -> None:
|
||||
@@ -74,18 +76,18 @@ async def test_form_errors(
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {}
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nextdns.NextDns.create",
|
||||
side_effect=exc,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_API_KEY: "fake_api_key"},
|
||||
)
|
||||
mock_nextdns.create.side_effect = exc
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_API_KEY: "fake_api_key"},
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {"base": base_error}
|
||||
|
||||
mock_nextdns.create.side_effect = None
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_API_KEY: "fake_api_key"},
|
||||
@@ -110,6 +112,7 @@ async def test_form_already_configured(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_nextdns_client: AsyncMock,
|
||||
mock_nextdns: AsyncMock,
|
||||
) -> None:
|
||||
"""Test that errors are shown when duplicates are added."""
|
||||
await init_integration(hass, mock_config_entry)
|
||||
@@ -135,6 +138,7 @@ async def test_reauth_successful(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_nextdns_client: AsyncMock,
|
||||
mock_nextdns: AsyncMock,
|
||||
) -> None:
|
||||
"""Test starting a reauthentication flow."""
|
||||
await init_integration(hass, mock_config_entry)
|
||||
@@ -168,6 +172,7 @@ async def test_reauth_errors(
|
||||
base_error: str,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_nextdns_client: AsyncMock,
|
||||
mock_nextdns: AsyncMock,
|
||||
) -> None:
|
||||
"""Test reauthentication flow with errors."""
|
||||
await init_integration(hass, mock_config_entry)
|
||||
@@ -176,14 +181,17 @@ async def test_reauth_errors(
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "reauth_confirm"
|
||||
|
||||
with patch("homeassistant.components.nextdns.NextDns.create", side_effect=exc):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={CONF_API_KEY: "new_api_key"},
|
||||
)
|
||||
mock_nextdns.create.side_effect = exc
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={CONF_API_KEY: "new_api_key"},
|
||||
)
|
||||
|
||||
assert result["errors"] == {"base": base_error}
|
||||
|
||||
mock_nextdns.create.side_effect = None
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={CONF_API_KEY: "new_api_key"},
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"""Test init of NextDNS integration."""
|
||||
|
||||
from unittest.mock import AsyncMock, patch
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from nextdns import ApiError, InvalidApiKeyError
|
||||
import pytest
|
||||
@@ -36,15 +36,13 @@ async def test_async_setup_entry(
|
||||
async def test_config_not_ready(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_nextdns_client: AsyncMock,
|
||||
mock_nextdns: AsyncMock,
|
||||
exc: Exception,
|
||||
) -> None:
|
||||
"""Test for setup failure if the connection to the service fails."""
|
||||
with patch(
|
||||
"homeassistant.components.nextdns.NextDns.create",
|
||||
side_effect=exc,
|
||||
):
|
||||
await init_integration(hass, mock_config_entry)
|
||||
mock_nextdns.create.side_effect = exc
|
||||
|
||||
await init_integration(hass, mock_config_entry)
|
||||
|
||||
assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY
|
||||
|
||||
@@ -53,6 +51,7 @@ async def test_unload_entry(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_nextdns_client: AsyncMock,
|
||||
mock_nextdns: AsyncMock,
|
||||
) -> None:
|
||||
"""Test successful unload of entry."""
|
||||
await init_integration(hass, mock_config_entry)
|
||||
@@ -70,14 +69,12 @@ async def test_unload_entry(
|
||||
async def test_config_auth_failed(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_nextdns_client: AsyncMock,
|
||||
mock_nextdns: AsyncMock,
|
||||
) -> None:
|
||||
"""Test for setup failure if the auth fails."""
|
||||
with patch(
|
||||
"homeassistant.components.nextdns.NextDns.create",
|
||||
side_effect=InvalidApiKeyError,
|
||||
):
|
||||
await init_integration(hass, mock_config_entry)
|
||||
mock_nextdns.create.side_effect = InvalidApiKeyError
|
||||
|
||||
await init_integration(hass, mock_config_entry)
|
||||
|
||||
assert mock_config_entry.state is ConfigEntryState.SETUP_ERROR
|
||||
|
||||
|
||||
Reference in New Issue
Block a user