mirror of
https://github.com/home-assistant/core.git
synced 2026-02-21 02:18:47 +00:00
Co-authored-by: NjDaGreat <1754227@students.wits.ac.za> Co-authored-by: NjeruFluss <161302608+NjeruFluss@users.noreply.github.com> Co-authored-by: Josef Zweck <josef@zweck.dev> Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
109 lines
3.2 KiB
Python
109 lines
3.2 KiB
Python
"""Tests for the Fluss+ config flow."""
|
|
|
|
from unittest.mock import AsyncMock
|
|
|
|
from fluss_api import (
|
|
FlussApiClientAuthenticationError,
|
|
FlussApiClientCommunicationError,
|
|
)
|
|
import pytest
|
|
|
|
from homeassistant.components.fluss.const import DOMAIN
|
|
from homeassistant.config_entries import SOURCE_USER
|
|
from homeassistant.const import CONF_API_KEY
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.data_entry_flow import FlowResultType
|
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
|
|
async def test_full_flow(
|
|
hass: HomeAssistant, mock_api_client: AsyncMock, mock_setup_entry: AsyncMock
|
|
) -> None:
|
|
"""Test full config flow."""
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": SOURCE_USER}
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.FORM
|
|
assert result["step_id"] == "user"
|
|
assert result["errors"] == {}
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"], {CONF_API_KEY: "valid_api_key"}
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
|
assert result["title"] == "My Fluss+ Devices"
|
|
assert result["data"] == {CONF_API_KEY: "valid_api_key"}
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("exception", "expected_error"),
|
|
[
|
|
(FlussApiClientAuthenticationError, "invalid_auth"),
|
|
(FlussApiClientCommunicationError, "cannot_connect"),
|
|
(Exception, "unknown"),
|
|
],
|
|
)
|
|
async def test_step_user_errors(
|
|
hass: HomeAssistant,
|
|
mock_api_client: AsyncMock,
|
|
mock_setup_entry: AsyncMock,
|
|
exception: Exception,
|
|
expected_error: str,
|
|
) -> None:
|
|
"""Test error cases for user step with recovery."""
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": SOURCE_USER}
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.FORM
|
|
assert result["step_id"] == "user"
|
|
assert result["errors"] == {}
|
|
|
|
user_input = {CONF_API_KEY: "some_api_key"}
|
|
|
|
mock_api_client.async_get_devices.side_effect = exception
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"], {CONF_API_KEY: "valid_api_key"}
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.FORM
|
|
assert result["step_id"] == "user"
|
|
assert result["errors"] == {"base": expected_error}
|
|
|
|
mock_api_client.async_get_devices.side_effect = None
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
user_input,
|
|
)
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
|
|
|
|
|
async def test_duplicate_entry(
|
|
hass: HomeAssistant,
|
|
mock_api_client: AsyncMock,
|
|
mock_setup_entry: AsyncMock,
|
|
mock_config_entry: MockConfigEntry,
|
|
) -> None:
|
|
"""Test error cases for user step with recovery."""
|
|
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
|
|
assert result["step_id"] == "user"
|
|
assert result["errors"] == {}
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
{CONF_API_KEY: "test_api_key"},
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.ABORT
|
|
assert result["reason"] == "already_configured"
|