diff --git a/tests/components/syncthru/conftest.py b/tests/components/syncthru/conftest.py index 6563e0f7b41..be5896c4956 100644 --- a/tests/components/syncthru/conftest.py +++ b/tests/components/syncthru/conftest.py @@ -12,6 +12,16 @@ from homeassistant.const import CONF_NAME, CONF_URL 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 def mock_syncthru() -> Generator[AsyncMock]: """Mock the SyncThru class.""" diff --git a/tests/components/syncthru/test_config_flow.py b/tests/components/syncthru/test_config_flow.py index c551c94506e..e535ba50470 100644 --- a/tests/components/syncthru/test_config_flow.py +++ b/tests/components/syncthru/test_config_flow.py @@ -1,11 +1,12 @@ """Tests for syncthru config flow.""" -from unittest.mock import AsyncMock, patch +from unittest.mock import AsyncMock from pysyncthru import SyncThruAPINotSupported from homeassistant import config_entries 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.core import HomeAssistant 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: - """Test that the setup form is served.""" +async def test_full_flow( + hass: HomeAssistant, mock_syncthru: AsyncMock, mock_setup_entry: AsyncMock +) -> None: + """Test the full flow.""" 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["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( 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 result = await hass.config_entries.flow.async_init( DOMAIN, - context={"source": config_entries.SOURCE_USER}, - data=FIXTURE_USER_INPUT, + context={"source": SOURCE_USER}, + ) + + 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["step_id"] == "user" 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: - """Test successful flow provides entry creation data.""" - - 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, - ) - + 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"][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.""" url = "http://192.168.1.2/" result = await hass.config_entries.flow.async_init( DOMAIN, - context={"source": config_entries.SOURCE_SSDP}, + context={"source": SOURCE_SSDP}, data=SsdpServiceInfo( ssdp_usn="mock_usn", 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: if k == CONF_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"