mirror of
https://github.com/home-assistant/core.git
synced 2026-10-23 14:55:07 -04:00
Add reconfiguration flow to Notifications for Android TV / Fire TV (#169111)
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
"""Common fixtures for the Notifications for Android TV / Fire TV tests."""
|
||||
|
||||
from collections.abc import Generator
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.nfandroidtv.const import DOMAIN
|
||||
from homeassistant.const import CONF_HOST
|
||||
|
||||
from . import HOST, NAME
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_notifications_android_tv() -> Generator[MagicMock]:
|
||||
"""Mock notifications_android_tv."""
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nfandroidtv.config_flow.Notifications", autospec=True
|
||||
) as mock_client:
|
||||
client = mock_client.return_value
|
||||
client.cls = mock_client
|
||||
|
||||
yield client
|
||||
|
||||
|
||||
@pytest.fixture(name="config_entry")
|
||||
def mock_config_entry() -> MockConfigEntry:
|
||||
"""Mock Notifications for Android TV / Fire TV configuration entry."""
|
||||
return MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
title=NAME,
|
||||
data={CONF_HOST: HOST},
|
||||
entry_id="123456789",
|
||||
)
|
||||
@@ -1,11 +1,13 @@
|
||||
"""Test NFAndroidTV config flow."""
|
||||
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from notifications_android_tv.notifications import ConnectError
|
||||
import pytest
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components.nfandroidtv.const import DOMAIN
|
||||
from homeassistant.const import CONF_HOST
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
|
||||
@@ -97,3 +99,102 @@ async def test_flow_user_unknown_error(hass: HomeAssistant) -> None:
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
assert result["errors"] == {"base": "unknown"}
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_notifications_android_tv")
|
||||
async def test_flow_reconfigure(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test reconfigure flow."""
|
||||
|
||||
config_entry.add_to_hass(hass)
|
||||
result = await config_entry.start_reconfigure_flow(hass)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "reconfigure"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_HOST: "4.3.2.1"},
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "reconfigure_successful"
|
||||
assert config_entry.data[CONF_HOST] == "4.3.2.1"
|
||||
|
||||
assert len(hass.config_entries.async_entries()) == 1
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("exception", "error"), [(ConnectError, "cannot_connect"), (ValueError, "unknown")]
|
||||
)
|
||||
async def test_flow_reconfigure_errors(
|
||||
hass: HomeAssistant,
|
||||
mock_notifications_android_tv: MagicMock,
|
||||
config_entry: MockConfigEntry,
|
||||
exception: Exception,
|
||||
error: str,
|
||||
) -> None:
|
||||
"""Test reconfigure flow errors."""
|
||||
|
||||
config_entry.add_to_hass(hass)
|
||||
result = await config_entry.start_reconfigure_flow(hass)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "reconfigure"
|
||||
|
||||
mock_notifications_android_tv.cls.side_effect = exception
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_HOST: "4.3.2.1"},
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {"base": error}
|
||||
|
||||
mock_notifications_android_tv.cls.side_effect = None
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_HOST: "4.3.2.1"},
|
||||
)
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "reconfigure_successful"
|
||||
assert config_entry.data[CONF_HOST] == "4.3.2.1"
|
||||
|
||||
assert len(hass.config_entries.async_entries()) == 1
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_notifications_android_tv")
|
||||
async def test_flow_reconfigure_already_configured(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test reconfigure flow aborts if already configured."""
|
||||
MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
title="Android TV / Fire TV (4.3.2.1)",
|
||||
data={CONF_HOST: "4.3.2.1"},
|
||||
entry_id="987654321",
|
||||
).add_to_hass(hass)
|
||||
|
||||
config_entry.add_to_hass(hass)
|
||||
result = await config_entry.start_reconfigure_flow(hass)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "reconfigure"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_HOST: "4.3.2.1"},
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
assert len(hass.config_entries.async_entries()) == 2
|
||||
|
||||
Reference in New Issue
Block a user