mirror of
https://github.com/home-assistant/core.git
synced 2025-07-28 07:37:34 +00:00
Rework Snapcast config flow tests (#148434)
This commit is contained in:
parent
511ffdc03c
commit
6f31057d30
@ -10,7 +10,6 @@ from snapcast.control.server import CONTROL_PORT
|
||||
from snapcast.control.stream import Snapstream
|
||||
|
||||
from homeassistant.components.snapcast.const import DOMAIN
|
||||
from homeassistant.components.snapcast.coordinator import Snapserver
|
||||
from homeassistant.const import CONF_HOST, CONF_PORT
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
@ -25,6 +24,16 @@ def mock_setup_entry() -> Generator[AsyncMock]:
|
||||
yield mock_setup_entry
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_server(mock_create_server: AsyncMock) -> Generator[AsyncMock]:
|
||||
"""Override async_setup_entry."""
|
||||
with patch(
|
||||
"homeassistant.components.snapcast.config_flow.snapcast.control.create_server",
|
||||
return_value=mock_create_server,
|
||||
) as mock_server:
|
||||
yield mock_server
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_create_server(
|
||||
mock_group: AsyncMock,
|
||||
@ -64,15 +73,6 @@ async def mock_config_entry() -> MockConfigEntry:
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_server_connection() -> Generator[Snapserver]:
|
||||
"""Create a mock server connection."""
|
||||
|
||||
# Patch the start method of the Snapserver class to avoid network connections
|
||||
with patch.object(Snapserver, "start", new_callable=AsyncMock) as mock_start:
|
||||
yield mock_start
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_group(stream: str, streams: dict[str, AsyncMock]) -> AsyncMock:
|
||||
"""Create a mock Snapgroup."""
|
||||
|
@ -1,91 +1,103 @@
|
||||
"""Test the Snapcast module."""
|
||||
|
||||
import socket
|
||||
from unittest.mock import AsyncMock, patch
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant import config_entries, setup
|
||||
from homeassistant.components.snapcast.const import DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_USER
|
||||
from homeassistant.const import CONF_HOST, CONF_PORT
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
TEST_CONNECTION = {CONF_HOST: "snapserver.test", CONF_PORT: 1705}
|
||||
|
||||
pytestmark = pytest.mark.usefixtures("mock_setup_entry")
|
||||
TEST_CONNECTION = {CONF_HOST: "127.0.0.1", CONF_PORT: 1705}
|
||||
|
||||
|
||||
async def test_form(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None:
|
||||
"""Test we get the form and handle errors and successful connection."""
|
||||
await setup.async_setup_component(hass, "persistent_notification", {})
|
||||
async def test_full_flow(
|
||||
hass: HomeAssistant, mock_setup_entry: AsyncMock, mock_server: AsyncMock
|
||||
) -> None:
|
||||
"""Test the full flow."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
assert not result["errors"]
|
||||
|
||||
# test invalid host error
|
||||
with patch("snapcast.control.create_server", side_effect=socket.gaierror):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
TEST_CONNECTION,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
assert result["errors"] == {"base": "invalid_host"}
|
||||
|
||||
# test connection error
|
||||
with patch("snapcast.control.create_server", side_effect=ConnectionRefusedError):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
TEST_CONNECTION,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
assert result["errors"] == {"base": "cannot_connect"}
|
||||
|
||||
# test success
|
||||
with patch("snapcast.control.create_server"):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
TEST_CONNECTION,
|
||||
)
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
TEST_CONNECTION,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "Snapcast"
|
||||
assert result["data"] == {CONF_HOST: "snapserver.test", CONF_PORT: 1705}
|
||||
assert result["data"] == {CONF_HOST: "127.0.0.1", CONF_PORT: 1705}
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_abort(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None:
|
||||
"""Test config flow abort if device is already configured."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data=TEST_CONNECTION,
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
@pytest.mark.parametrize(
|
||||
("exception", "error"),
|
||||
[
|
||||
(socket.gaierror, "invalid_host"),
|
||||
(ConnectionRefusedError, "cannot_connect"),
|
||||
],
|
||||
)
|
||||
async def test_exceptions(
|
||||
hass: HomeAssistant,
|
||||
mock_setup_entry: AsyncMock,
|
||||
mock_server: AsyncMock,
|
||||
exception: Exception,
|
||||
error: str,
|
||||
) -> None:
|
||||
"""Test we get the form and handle errors and successful connection."""
|
||||
|
||||
mock_server.side_effect = exception
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
assert not result["errors"]
|
||||
|
||||
with patch("snapcast.control.create_server", side_effect=socket.gaierror):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
TEST_CONNECTION,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
TEST_CONNECTION,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
assert result["errors"] == {"base": error}
|
||||
|
||||
mock_server.side_effect = None
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
TEST_CONNECTION,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
|
||||
|
||||
async def test_already_setup(
|
||||
hass: HomeAssistant, mock_config_entry: MockConfigEntry
|
||||
) -> None:
|
||||
"""Test config flow abort if device is already configured."""
|
||||
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 not result["errors"]
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
TEST_CONNECTION,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
Loading…
x
Reference in New Issue
Block a user