Improve Syncthru config flow tests (#142618)

This commit is contained in:
Joost Lekkerkerker 2025-04-10 21:16:12 +02:00 committed by GitHub
parent 8f73c53d26
commit 6fafafbed0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 86 additions and 22 deletions

View File

@ -12,6 +12,16 @@ from homeassistant.const import CONF_NAME, CONF_URL
from tests.common import MockConfigEntry, load_json_object_fixture from tests.common import MockConfigEntry, load_json_object_fixture
@pytest.fixture
def mock_setup_entry() -> Generator[AsyncMock]:
"""Override async_setup_entry."""
with patch(
"homeassistant.components.syncthru.async_setup_entry",
return_value=True,
) as mock_setup_entry:
yield mock_setup_entry
@pytest.fixture @pytest.fixture
def mock_syncthru() -> Generator[AsyncMock]: def mock_syncthru() -> Generator[AsyncMock]:
"""Mock the SyncThru class.""" """Mock the SyncThru class."""

View File

@ -1,11 +1,12 @@
"""Tests for syncthru config flow.""" """Tests for syncthru config flow."""
from unittest.mock import AsyncMock, patch from unittest.mock import AsyncMock
from pysyncthru import SyncThruAPINotSupported from pysyncthru import SyncThruAPINotSupported
from homeassistant import config_entries from homeassistant import config_entries
from homeassistant.components.syncthru.const import DOMAIN from homeassistant.components.syncthru.const import DOMAIN
from homeassistant.config_entries import SOURCE_SSDP, SOURCE_USER
from homeassistant.const import CONF_NAME, CONF_URL from homeassistant.const import CONF_NAME, CONF_URL
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType from homeassistant.data_entry_flow import FlowResultType
@ -26,15 +27,26 @@ FIXTURE_USER_INPUT = {
} }
async def test_show_setup_form(hass: HomeAssistant, mock_syncthru: AsyncMock) -> None: async def test_full_flow(
"""Test that the setup form is served.""" hass: HomeAssistant, mock_syncthru: AsyncMock, mock_setup_entry: AsyncMock
) -> None:
"""Test the full flow."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}, data=None DOMAIN, context={"source": SOURCE_USER}
) )
assert result["type"] is FlowResultType.FORM assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user" assert result["step_id"] == "user"
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input=FIXTURE_USER_INPUT,
)
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["data"] == FIXTURE_USER_INPUT
assert result["result"].unique_id is None
async def test_already_configured_by_url( async def test_already_configured_by_url(
hass: HomeAssistant, mock_syncthru: AsyncMock hass: HomeAssistant, mock_syncthru: AsyncMock
@ -82,39 +94,40 @@ async def test_unknown_state(hass: HomeAssistant, mock_syncthru: AsyncMock) -> N
mock_syncthru.is_unknown_state.return_value = True mock_syncthru.is_unknown_state.return_value = True
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
context={"source": config_entries.SOURCE_USER}, context={"source": SOURCE_USER},
data=FIXTURE_USER_INPUT, )
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
assert not result["errors"]
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input=FIXTURE_USER_INPUT,
) )
assert result["type"] is FlowResultType.FORM assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user" assert result["step_id"] == "user"
assert result["errors"] == {CONF_URL: "unknown_state"} assert result["errors"] == {CONF_URL: "unknown_state"}
mock_syncthru.is_unknown_state.return_value = False
async def test_success(hass: HomeAssistant, mock_syncthru: AsyncMock) -> None: result = await hass.config_entries.flow.async_configure(
"""Test successful flow provides entry creation data.""" result["flow_id"],
user_input=FIXTURE_USER_INPUT,
with patch(
"homeassistant.components.syncthru.async_setup_entry", return_value=True
) as mock_setup_entry:
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_USER},
data=FIXTURE_USER_INPUT,
) )
assert result["type"] is FlowResultType.CREATE_ENTRY assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["data"][CONF_URL] == FIXTURE_USER_INPUT[CONF_URL]
assert len(mock_setup_entry.mock_calls) == 1
async def test_ssdp(hass: HomeAssistant, mock_syncthru: AsyncMock) -> None: async def test_ssdp(
hass: HomeAssistant, mock_syncthru: AsyncMock, mock_setup_entry: AsyncMock
) -> None:
"""Test SSDP discovery initiates config properly.""" """Test SSDP discovery initiates config properly."""
url = "http://192.168.1.2/" url = "http://192.168.1.2/"
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
context={"source": config_entries.SOURCE_SSDP}, context={"source": SOURCE_SSDP},
data=SsdpServiceInfo( data=SsdpServiceInfo(
ssdp_usn="mock_usn", ssdp_usn="mock_usn",
ssdp_st="mock_st", ssdp_st="mock_st",
@ -135,3 +148,44 @@ async def test_ssdp(hass: HomeAssistant, mock_syncthru: AsyncMock) -> None:
for k in result["data_schema"].schema: for k in result["data_schema"].schema:
if k == CONF_URL: if k == CONF_URL:
assert k.default() == url assert k.default() == url
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={CONF_URL: url, CONF_NAME: "Printer"},
)
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["data"] == {CONF_URL: url, CONF_NAME: "Printer"}
assert result["result"].unique_id == "uuid:XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
async def test_ssdp_already_configured(
hass: HomeAssistant, mock_syncthru: AsyncMock, mock_config_entry: MockConfigEntry
) -> None:
"""Test SSDP discovery initiates config properly."""
mock_config_entry.add_to_hass(hass)
hass.config_entries.async_update_entry(
mock_config_entry, unique_id="uuid:XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
)
url = "http://192.168.1.2/"
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_SSDP},
data=SsdpServiceInfo(
ssdp_usn="mock_usn",
ssdp_st="mock_st",
ssdp_location="http://192.168.1.2:5200/Printer.xml",
upnp={
ATTR_UPNP_DEVICE_TYPE: "urn:schemas-upnp-org:device:Printer:1",
ATTR_UPNP_MANUFACTURER: "Samsung Electronics",
ATTR_UPNP_PRESENTATION_URL: url,
ATTR_UPNP_SERIAL: "00000000",
ATTR_UPNP_UDN: "uuid:XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
},
),
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"