diff --git a/homeassistant/components/poolsense/config_flow.py b/homeassistant/components/poolsense/config_flow.py index 915fa1c8d06..b40ccaddd7d 100644 --- a/homeassistant/components/poolsense/config_flow.py +++ b/homeassistant/components/poolsense/config_flow.py @@ -20,9 +20,6 @@ class PoolSenseConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - def __init__(self) -> None: - """Initialize PoolSense config flow.""" - async def async_step_user( self, user_input: dict[str, Any] | None = None ) -> ConfigFlowResult: diff --git a/tests/components/poolsense/__init__.py b/tests/components/poolsense/__init__.py index ace3a6997fb..9d7ecb5eb47 100644 --- a/tests/components/poolsense/__init__.py +++ b/tests/components/poolsense/__init__.py @@ -1 +1,12 @@ """Tests for the PoolSense integration.""" + +from homeassistant.core import HomeAssistant + +from tests.common import MockConfigEntry + + +async def setup_integration(hass: HomeAssistant, config_entry: MockConfigEntry) -> None: + """Fixture for setting up the component.""" + config_entry.add_to_hass(hass) + + await hass.config_entries.async_setup(config_entry.entry_id) diff --git a/tests/components/poolsense/conftest.py b/tests/components/poolsense/conftest.py new file mode 100644 index 00000000000..d188eaef1ca --- /dev/null +++ b/tests/components/poolsense/conftest.py @@ -0,0 +1,53 @@ +"""Common fixtures for the Poolsense tests.""" + +from collections.abc import Generator +from unittest.mock import AsyncMock, patch + +import pytest + +from homeassistant.components.poolsense.const import DOMAIN +from homeassistant.const import CONF_EMAIL, CONF_PASSWORD + +from tests.common import MockConfigEntry + + +@pytest.fixture +def mock_setup_entry() -> Generator[AsyncMock, None, None]: + """Override async_setup_entry.""" + with patch( + "homeassistant.components.poolsense.async_setup_entry", + return_value=True, + ) as mock_setup_entry: + yield mock_setup_entry + + +@pytest.fixture +def mock_poolsense_client() -> Generator[AsyncMock, None, None]: + """Mock a PoolSense client.""" + with ( + patch( + "homeassistant.components.poolsense.PoolSense", + autospec=True, + ) as mock_client, + patch( + "homeassistant.components.poolsense.config_flow.PoolSense", + new=mock_client, + ), + ): + client = mock_client.return_value + client.test_poolsense_credentials.return_value = True + yield client + + +@pytest.fixture +def mock_config_entry() -> MockConfigEntry: + """Mock a config entry.""" + return MockConfigEntry( + domain=DOMAIN, + title="test@test.com", + unique_id="test@test.com", + data={ + CONF_EMAIL: "test@test.com", + CONF_PASSWORD: "test", + }, + ) diff --git a/tests/components/poolsense/test_config_flow.py b/tests/components/poolsense/test_config_flow.py index 49f790b5075..5c8b824bfaa 100644 --- a/tests/components/poolsense/test_config_flow.py +++ b/tests/components/poolsense/test_config_flow.py @@ -1,6 +1,6 @@ """Test the PoolSense config flow.""" -from unittest.mock import patch +from unittest.mock import AsyncMock from homeassistant.components.poolsense.const import DOMAIN from homeassistant.config_entries import SOURCE_USER @@ -8,9 +8,13 @@ from homeassistant.const import CONF_EMAIL, CONF_PASSWORD from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType +from tests.common import MockConfigEntry -async def test_show_form(hass: HomeAssistant) -> None: - """Test that the form is served with no input.""" + +async def test_full_form( + hass: HomeAssistant, mock_setup_entry: AsyncMock, mock_poolsense_client: AsyncMock +) -> None: + """Test full flow.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER} ) @@ -18,39 +22,59 @@ async def test_show_form(hass: HomeAssistant) -> None: assert result["type"] is FlowResultType.FORM assert result["step_id"] == "user" + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={CONF_EMAIL: "test@test.com", CONF_PASSWORD: "test"}, + ) -async def test_invalid_credentials(hass: HomeAssistant) -> None: + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["title"] == "test@test.com" + assert result["data"] == { + CONF_EMAIL: "test@test.com", + CONF_PASSWORD: "test", + } + assert result["result"].unique_id == "test@test.com" + + assert len(mock_setup_entry.mock_calls) == 1 + + +async def test_invalid_credentials( + hass: HomeAssistant, mock_setup_entry: AsyncMock, mock_poolsense_client: AsyncMock +) -> None: """Test we handle invalid credentials.""" - with patch( - "poolsense.PoolSense.test_poolsense_credentials", - return_value=False, - ): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_USER}, - data={CONF_EMAIL: "test-email", CONF_PASSWORD: "test-password"}, - ) + mock_poolsense_client.test_poolsense_credentials.return_value = False + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_USER}, + data={CONF_EMAIL: "test@test.com", CONF_PASSWORD: "test"}, + ) assert result["type"] is FlowResultType.FORM assert result["errors"] == {"base": "invalid_auth"} + mock_poolsense_client.test_poolsense_credentials.return_value = True -async def test_valid_credentials(hass: HomeAssistant) -> None: - """Test we handle invalid credentials.""" - with ( - patch("poolsense.PoolSense.test_poolsense_credentials", return_value=True), - patch( - "homeassistant.components.poolsense.async_setup_entry", return_value=True - ) as mock_setup_entry, - ): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_USER}, - data={CONF_EMAIL: "test-email", CONF_PASSWORD: "test-password"}, - ) - await hass.async_block_till_done() + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={CONF_EMAIL: "test@test.com", CONF_PASSWORD: "test"}, + ) assert result["type"] is FlowResultType.CREATE_ENTRY - assert result["title"] == "test-email" - assert len(mock_setup_entry.mock_calls) == 1 + +async def test_duplicate_entry( + hass: HomeAssistant, + mock_poolsense_client: AsyncMock, + mock_config_entry: MockConfigEntry, +) -> None: + """Test we can't add the same entry twice.""" + mock_config_entry.add_to_hass(hass) + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_USER}, + data={CONF_EMAIL: "test@test.com", CONF_PASSWORD: "test"}, + ) + await hass.async_block_till_done() + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "already_configured"