mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Refactor vicare config_flow tests (#90568)
* Refactor vicare config_flow tests * Address review comments * Remove unused parameters
This commit is contained in:
parent
8cbe394028
commit
ea32cc5d92
@ -1,16 +1,6 @@
|
|||||||
"""Test for ViCare."""
|
"""Test for ViCare."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import Final
|
MODULE = "homeassistant.components.vicare"
|
||||||
|
|
||||||
from homeassistant.components.vicare.const import CONF_HEATING_TYPE
|
|
||||||
from homeassistant.const import CONF_CLIENT_ID, CONF_PASSWORD, CONF_USERNAME
|
|
||||||
|
|
||||||
ENTRY_CONFIG: Final[dict[str, str]] = {
|
|
||||||
CONF_USERNAME: "foo@bar.com",
|
|
||||||
CONF_PASSWORD: "1234",
|
|
||||||
CONF_CLIENT_ID: "5678",
|
|
||||||
CONF_HEATING_TYPE: "auto",
|
|
||||||
}
|
|
||||||
|
|
||||||
MOCK_MAC = "B874241B7B9"
|
MOCK_MAC = "B874241B7B9"
|
||||||
|
16
tests/components/vicare/conftest.py
Normal file
16
tests/components/vicare/conftest.py
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
"""Fixtures for ViCare integration tests."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Generator
|
||||||
|
from unittest.mock import AsyncMock, patch
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from . import MODULE
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_setup_entry() -> Generator[AsyncMock, None, None]:
|
||||||
|
"""Mock setting up a config entry."""
|
||||||
|
with patch(f"{MODULE}.async_setup_entry", return_value=True) as mock_setup_entry:
|
||||||
|
yield mock_setup_entry
|
17
tests/components/vicare/snapshots/test_config_flow.ambr
Normal file
17
tests/components/vicare/snapshots/test_config_flow.ambr
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
# serializer version: 1
|
||||||
|
# name: test_form_dhcp
|
||||||
|
dict({
|
||||||
|
'client_id': '5678',
|
||||||
|
'heating_type': 'auto',
|
||||||
|
'password': '1234',
|
||||||
|
'username': 'foo@bar.com',
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_user_create_entry
|
||||||
|
dict({
|
||||||
|
'client_id': '5678',
|
||||||
|
'heating_type': 'auto',
|
||||||
|
'password': '1234',
|
||||||
|
'username': 'foo@bar.com',
|
||||||
|
})
|
||||||
|
# ---
|
@ -1,132 +1,123 @@
|
|||||||
"""Test the ViCare config flow."""
|
"""Test the ViCare config flow."""
|
||||||
from unittest.mock import patch
|
from unittest.mock import AsyncMock, patch
|
||||||
|
|
||||||
from PyViCare.PyViCareUtils import PyViCareInvalidCredentialsError
|
from PyViCare.PyViCareUtils import PyViCareInvalidCredentialsError
|
||||||
|
import pytest
|
||||||
|
from syrupy.assertion import SnapshotAssertion
|
||||||
|
|
||||||
from homeassistant import config_entries, data_entry_flow
|
|
||||||
from homeassistant.components import dhcp
|
from homeassistant.components import dhcp
|
||||||
from homeassistant.components.vicare.const import DOMAIN
|
from homeassistant.components.vicare.const import DOMAIN
|
||||||
|
from homeassistant.config_entries import SOURCE_DHCP, SOURCE_USER
|
||||||
from homeassistant.const import CONF_CLIENT_ID, CONF_PASSWORD, CONF_USERNAME
|
from homeassistant.const import CONF_CLIENT_ID, CONF_PASSWORD, CONF_USERNAME
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.data_entry_flow import FlowResultType
|
||||||
|
|
||||||
from . import ENTRY_CONFIG, MOCK_MAC
|
from . import MOCK_MAC, MODULE
|
||||||
|
|
||||||
from tests.common import MockConfigEntry
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
|
pytestmark = pytest.mark.usefixtures("mock_setup_entry")
|
||||||
|
|
||||||
async def test_form(hass: HomeAssistant) -> None:
|
VALID_CONFIG = {
|
||||||
"""Test we get the form."""
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
|
||||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
||||||
)
|
|
||||||
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
|
||||||
assert len(result["errors"]) == 0
|
|
||||||
|
|
||||||
with patch(
|
|
||||||
"homeassistant.components.vicare.config_flow.vicare_login",
|
|
||||||
return_value=None,
|
|
||||||
), patch(
|
|
||||||
"homeassistant.components.vicare.async_setup_entry",
|
|
||||||
return_value=True,
|
|
||||||
) as mock_setup_entry:
|
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
|
||||||
result["flow_id"],
|
|
||||||
{
|
|
||||||
CONF_USERNAME: "foo@bar.com",
|
CONF_USERNAME: "foo@bar.com",
|
||||||
CONF_PASSWORD: "1234",
|
CONF_PASSWORD: "1234",
|
||||||
CONF_CLIENT_ID: "5678",
|
CONF_CLIENT_ID: "5678",
|
||||||
},
|
}
|
||||||
|
|
||||||
|
DHCP_INFO = dhcp.DhcpServiceInfo(
|
||||||
|
ip="1.1.1.1",
|
||||||
|
hostname="mock_hostname",
|
||||||
|
macaddress=MOCK_MAC,
|
||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
assert result2["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
|
||||||
assert result2["title"] == "ViCare"
|
|
||||||
assert result2["data"] == ENTRY_CONFIG
|
|
||||||
assert len(mock_setup_entry.mock_calls) == 1
|
|
||||||
|
|
||||||
|
|
||||||
async def test_invalid_login(hass: HomeAssistant) -> None:
|
async def test_user_create_entry(
|
||||||
"""Test a flow with an invalid Vicare login."""
|
hass: HomeAssistant, mock_setup_entry: AsyncMock, snapshot: SnapshotAssertion
|
||||||
|
) -> None:
|
||||||
|
"""Test that the user step works."""
|
||||||
|
# start user flow
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
DOMAIN, context={"source": SOURCE_USER}
|
||||||
)
|
)
|
||||||
|
assert result["type"] == FlowResultType.FORM
|
||||||
|
assert result["step_id"] == "user"
|
||||||
|
assert result["errors"] == {}
|
||||||
|
|
||||||
|
# test PyViCareInvalidCredentialsError
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.vicare.config_flow.vicare_login",
|
f"{MODULE}.config_flow.vicare_login",
|
||||||
side_effect=PyViCareInvalidCredentialsError,
|
side_effect=PyViCareInvalidCredentialsError,
|
||||||
):
|
):
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
result = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
{
|
VALID_CONFIG,
|
||||||
CONF_USERNAME: "foo@bar.com",
|
)
|
||||||
CONF_PASSWORD: "1234",
|
await hass.async_block_till_done()
|
||||||
CONF_CLIENT_ID: "5678",
|
assert result["type"] == FlowResultType.FORM
|
||||||
},
|
assert result["step_id"] == "user"
|
||||||
|
assert result["errors"] == {"base": "invalid_auth"}
|
||||||
|
|
||||||
|
# test success
|
||||||
|
with patch(
|
||||||
|
f"{MODULE}.config_flow.vicare_login",
|
||||||
|
return_value=None,
|
||||||
|
) as mock_setup_entry:
|
||||||
|
result = await hass.config_entries.flow.async_configure(
|
||||||
|
result["flow_id"],
|
||||||
|
VALID_CONFIG,
|
||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert result2["type"] == data_entry_flow.FlowResultType.FORM
|
assert result["type"] == FlowResultType.CREATE_ENTRY
|
||||||
assert result2["step_id"] == "user"
|
assert result["title"] == "ViCare"
|
||||||
assert result2["errors"] == {"base": "invalid_auth"}
|
assert result["data"] == snapshot
|
||||||
|
mock_setup_entry.assert_called_once()
|
||||||
|
|
||||||
|
|
||||||
async def test_form_dhcp(hass: HomeAssistant) -> None:
|
async def test_form_dhcp(
|
||||||
|
hass: HomeAssistant, mock_setup_entry: AsyncMock, snapshot: SnapshotAssertion
|
||||||
|
) -> None:
|
||||||
"""Test we can setup from dhcp."""
|
"""Test we can setup from dhcp."""
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
context={"source": config_entries.SOURCE_DHCP},
|
context={"source": SOURCE_DHCP},
|
||||||
data=dhcp.DhcpServiceInfo(
|
data=DHCP_INFO,
|
||||||
ip="1.1.1.1",
|
|
||||||
hostname="mock_hostname",
|
|
||||||
macaddress=MOCK_MAC,
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
assert result["type"] == FlowResultType.FORM
|
||||||
assert result["step_id"] == "user"
|
assert result["step_id"] == "user"
|
||||||
assert result["errors"] == {}
|
assert result["errors"] == {}
|
||||||
|
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.vicare.config_flow.vicare_login",
|
f"{MODULE}.config_flow.vicare_login",
|
||||||
return_value=None,
|
return_value=None,
|
||||||
), patch(
|
):
|
||||||
"homeassistant.components.vicare.async_setup_entry",
|
result = await hass.config_entries.flow.async_configure(
|
||||||
return_value=True,
|
|
||||||
) as mock_setup_entry:
|
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
{
|
VALID_CONFIG,
|
||||||
CONF_USERNAME: "foo@bar.com",
|
|
||||||
CONF_PASSWORD: "1234",
|
|
||||||
CONF_CLIENT_ID: "5678",
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert result2["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
assert result["type"] == FlowResultType.CREATE_ENTRY
|
||||||
assert result2["title"] == "ViCare"
|
assert result["title"] == "ViCare"
|
||||||
assert result2["data"] == ENTRY_CONFIG
|
assert result["data"] == snapshot
|
||||||
assert len(mock_setup_entry.mock_calls) == 1
|
mock_setup_entry.assert_called_once()
|
||||||
|
|
||||||
|
|
||||||
async def test_dhcp_single_instance_allowed(hass: HomeAssistant) -> None:
|
async def test_dhcp_single_instance_allowed(hass: HomeAssistant) -> None:
|
||||||
"""Test that configuring more than one instance is rejected."""
|
"""Test that configuring more than one instance is rejected."""
|
||||||
mock_entry = MockConfigEntry(
|
mock_entry = MockConfigEntry(
|
||||||
domain=DOMAIN,
|
domain=DOMAIN,
|
||||||
data=ENTRY_CONFIG,
|
data=VALID_CONFIG,
|
||||||
)
|
)
|
||||||
mock_entry.add_to_hass(hass)
|
mock_entry.add_to_hass(hass)
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
context={"source": config_entries.SOURCE_DHCP},
|
context={"source": SOURCE_DHCP},
|
||||||
data=dhcp.DhcpServiceInfo(
|
data=DHCP_INFO,
|
||||||
ip="1.1.1.1",
|
|
||||||
hostname="mock_hostname",
|
|
||||||
macaddress=MOCK_MAC,
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
assert result["type"] == data_entry_flow.FlowResultType.ABORT
|
assert result["type"] == FlowResultType.ABORT
|
||||||
assert result["reason"] == "single_instance_allowed"
|
assert result["reason"] == "single_instance_allowed"
|
||||||
|
|
||||||
|
|
||||||
@ -135,12 +126,12 @@ async def test_user_input_single_instance_allowed(hass: HomeAssistant) -> None:
|
|||||||
mock_entry = MockConfigEntry(
|
mock_entry = MockConfigEntry(
|
||||||
domain=DOMAIN,
|
domain=DOMAIN,
|
||||||
unique_id="ViCare",
|
unique_id="ViCare",
|
||||||
data=ENTRY_CONFIG,
|
data=VALID_CONFIG,
|
||||||
)
|
)
|
||||||
mock_entry.add_to_hass(hass)
|
mock_entry.add_to_hass(hass)
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
DOMAIN, context={"source": SOURCE_USER}
|
||||||
)
|
)
|
||||||
assert result["type"] == data_entry_flow.FlowResultType.ABORT
|
assert result["type"] == FlowResultType.ABORT
|
||||||
assert result["reason"] == "single_instance_allowed"
|
assert result["reason"] == "single_instance_allowed"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user