core/tests/components/simplefin/test_config_flow.py

165 lines
5.2 KiB
Python
Raw Normal View History

Add SimpleFIN integration (#108336) * reset to latest dev branch * Update homeassistant/components/simplefin/sensor.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * formatting tweak * Removed info errors * fix bad billing error message * addressing PR * addressing PR * reauth abort and already_confiugred added to strings.json * adding the reauth message * ruff * update reqs * reset to latest dev branch * Update homeassistant/components/simplefin/sensor.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * formatting tweak * Removed info errors * fix bad billing error message * addressing PR * addressing PR * reauth abort and already_confiugred added to strings.json * adding the reauth message * ruff * update reqs * Update homeassistant/components/simplefin/__init__.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Addressing a few PR comments - removing nix - and adding runtime data * updated comments * rename config flow * pulling reauth :( - inline stuff * inline more * fixed a tab issue * reverting changes * various PR updates and code removal * generator async add * Update homeassistant/components/simplefin/__init__.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/simplefin/config_flow.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/simplefin/config_flow.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/simplefin/sensor.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/simplefin/sensor.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * always callable * Update homeassistant/components/simplefin/coordinator.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * no-verify * type * fixing missing domain * it looks like this file is gone now * typing * sorta pass * fix license * Update homeassistant/components/simplefin/config_flow.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/simplefin/entity.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * addressing PR * Update homeassistant/components/simplefin/strings.json Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * move property to entity.py * moved stuff out to else block * Initial Snappshot Testing ... still have unadressed changes to make * Addressing PR Comments * pushing back to joost * removing non-needed file * added more asserts * reducing mocks - need to fix patch paths still * Changed patch to be more localized to config_flow * Removed unneeded fixture * Moved coordinator around * Cleaning up the code * Removed a NOQA" * Upping the number of asserts * cleanup * fixed abort call * incremental update - for Josot... changed a function signature and removed an annotatoin * no-verify * Added an abort test * ruff * increased coverage but it might not pass muster for JOOST * increased coverage but it might not pass muster for JOOST * Much nicer test now * tried to simplify * Fix nits --------- Co-authored-by: Erik Montnemery <erik@montnemery.com> Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
2024-07-10 10:44:04 +00:00
"""Test config flow."""
from unittest.mock import AsyncMock
import pytest
from simplefin4py.exceptions import (
SimpleFinAuthError,
SimpleFinClaimError,
SimpleFinInvalidAccountURLError,
SimpleFinInvalidClaimTokenError,
SimpleFinPaymentRequiredError,
)
from homeassistant.components.simplefin import CONF_ACCESS_URL
from homeassistant.components.simplefin.const import DOMAIN
from homeassistant.config_entries import SOURCE_USER
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from homeassistant.helpers import entity_registry as er
from .conftest import MOCK_ACCESS_URL
from tests.common import MockConfigEntry
async def test_successful_claim(
hass: HomeAssistant,
mock_setup_entry: AsyncMock,
mock_simplefin_client: AsyncMock,
) -> None:
"""Test successful token claim in config flow."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
assert not result["errors"]
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_ACCESS_URL: "donJulio"},
)
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == "SimpleFIN"
assert result["data"] == {CONF_ACCESS_URL: MOCK_ACCESS_URL}
async def test_already_setup(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry,
mock_simplefin_client: AsyncMock,
) -> None:
"""Test all entities."""
mock_config_entry.add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_ACCESS_URL: MOCK_ACCESS_URL},
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"
async def test_access_url(
hass: HomeAssistant,
mock_simplefin_client: AsyncMock,
mock_setup_entry: AsyncMock,
) -> None:
"""Test standard config flow."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_ACCESS_URL: "http://user:password@string"},
)
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["data"][CONF_ACCESS_URL] == "http://user:password@string"
assert result["title"] == "SimpleFIN"
@pytest.mark.parametrize(
("side_effect", "error_key"),
[
(SimpleFinInvalidAccountURLError, "url_error"),
(SimpleFinPaymentRequiredError, "payment_required"),
(SimpleFinAuthError, "invalid_auth"),
],
)
async def test_access_url_errors(
hass: HomeAssistant,
mock_simplefin_client: AsyncMock,
side_effect: Exception,
error_key: str,
) -> None:
"""Test the various errors we can get in access_url mode."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
mock_simplefin_client.claim_setup_token.side_effect = side_effect
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_ACCESS_URL: "donJulio"},
)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": error_key}
mock_simplefin_client.claim_setup_token.side_effect = None
# Pass the entry creation
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_ACCESS_URL: "http://user:password@string"},
)
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["data"] == {CONF_ACCESS_URL: "http://user:password@string"}
assert result["title"] == "SimpleFIN"
@pytest.mark.parametrize(
("side_effect", "error_key"),
[
(SimpleFinInvalidClaimTokenError, "invalid_claim_token"),
(SimpleFinClaimError, "claim_error"),
],
)
async def test_claim_token_errors(
hass: HomeAssistant,
mock_simplefin_client: AsyncMock,
side_effect: Exception,
error_key: str,
) -> None:
"""Test config flow with various token claim errors."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
mock_simplefin_client.claim_setup_token.side_effect = side_effect
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_ACCESS_URL: "donJulio"},
)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": error_key}
mock_simplefin_client.claim_setup_token.side_effect = None
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_ACCESS_URL: "donJulio"},
)
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["data"] == {CONF_ACCESS_URL: "https://i:am@yomama.house.com"}
assert result["title"] == "SimpleFIN"