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
4 changed files with 106 additions and 92 deletions
|
@ -1,16 +1,6 @@
|
|||
"""Test for ViCare."""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Final
|
||||
|
||||
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",
|
||||
}
|
||||
MODULE = "homeassistant.components.vicare"
|
||||
|
||||
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."""
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
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.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.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
|
||||
|
||||
pytestmark = pytest.mark.usefixtures("mock_setup_entry")
|
||||
|
||||
async def test_form(hass: HomeAssistant) -> None:
|
||||
"""Test we get the form."""
|
||||
VALID_CONFIG = {
|
||||
CONF_USERNAME: "foo@bar.com",
|
||||
CONF_PASSWORD: "1234",
|
||||
CONF_CLIENT_ID: "5678",
|
||||
}
|
||||
|
||||
DHCP_INFO = dhcp.DhcpServiceInfo(
|
||||
ip="1.1.1.1",
|
||||
hostname="mock_hostname",
|
||||
macaddress=MOCK_MAC,
|
||||
)
|
||||
|
||||
|
||||
async def test_user_create_entry(
|
||||
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(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
||||
assert len(result["errors"]) == 0
|
||||
assert result["type"] == FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
assert result["errors"] == {}
|
||||
|
||||
# test PyViCareInvalidCredentialsError
|
||||
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_PASSWORD: "1234",
|
||||
CONF_CLIENT_ID: "5678",
|
||||
},
|
||||
)
|
||||
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:
|
||||
"""Test a flow with an invalid Vicare login."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.vicare.config_flow.vicare_login",
|
||||
f"{MODULE}.config_flow.vicare_login",
|
||||
side_effect=PyViCareInvalidCredentialsError,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_USERNAME: "foo@bar.com",
|
||||
CONF_PASSWORD: "1234",
|
||||
CONF_CLIENT_ID: "5678",
|
||||
},
|
||||
VALID_CONFIG,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
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()
|
||||
|
||||
assert result2["type"] == data_entry_flow.FlowResultType.FORM
|
||||
assert result2["step_id"] == "user"
|
||||
assert result2["errors"] == {"base": "invalid_auth"}
|
||||
assert result["type"] == FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "ViCare"
|
||||
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."""
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_DHCP},
|
||||
data=dhcp.DhcpServiceInfo(
|
||||
ip="1.1.1.1",
|
||||
hostname="mock_hostname",
|
||||
macaddress=MOCK_MAC,
|
||||
),
|
||||
context={"source": SOURCE_DHCP},
|
||||
data=DHCP_INFO,
|
||||
)
|
||||
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
||||
assert result["type"] == FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
assert result["errors"] == {}
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.vicare.config_flow.vicare_login",
|
||||
f"{MODULE}.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 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_USERNAME: "foo@bar.com",
|
||||
CONF_PASSWORD: "1234",
|
||||
CONF_CLIENT_ID: "5678",
|
||||
},
|
||||
VALID_CONFIG,
|
||||
)
|
||||
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
|
||||
assert result["type"] == FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "ViCare"
|
||||
assert result["data"] == snapshot
|
||||
mock_setup_entry.assert_called_once()
|
||||
|
||||
|
||||
async def test_dhcp_single_instance_allowed(hass: HomeAssistant) -> None:
|
||||
"""Test that configuring more than one instance is rejected."""
|
||||
mock_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data=ENTRY_CONFIG,
|
||||
data=VALID_CONFIG,
|
||||
)
|
||||
mock_entry.add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_DHCP},
|
||||
data=dhcp.DhcpServiceInfo(
|
||||
ip="1.1.1.1",
|
||||
hostname="mock_hostname",
|
||||
macaddress=MOCK_MAC,
|
||||
),
|
||||
context={"source": SOURCE_DHCP},
|
||||
data=DHCP_INFO,
|
||||
)
|
||||
assert result["type"] == data_entry_flow.FlowResultType.ABORT
|
||||
assert result["type"] == FlowResultType.ABORT
|
||||
assert result["reason"] == "single_instance_allowed"
|
||||
|
||||
|
||||
|
@ -135,12 +126,12 @@ async def test_user_input_single_instance_allowed(hass: HomeAssistant) -> None:
|
|||
mock_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
unique_id="ViCare",
|
||||
data=ENTRY_CONFIG,
|
||||
data=VALID_CONFIG,
|
||||
)
|
||||
mock_entry.add_to_hass(hass)
|
||||
|
||||
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"
|
||||
|
|
Loading…
Add table
Reference in a new issue