1
0
mirror of https://github.com/home-assistant/core.git synced 2026-07-02 12:17:30 +01:00
Files
core/tests/components/nzbget/test_config_flow.py
T

136 lines
3.9 KiB
Python

"""Test the NZBGet config flow."""
from unittest.mock import patch
from pynzbgetapi import NZBGetAPIException
from homeassistant.components.nzbget.const import CONF_MORE_OPTIONS, DOMAIN
from homeassistant.config_entries import SOURCE_USER
from homeassistant.const import CONF_VERIFY_SSL
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from . import (
ENTRY_CONFIG,
USER_INPUT,
_patch_async_setup_entry,
_patch_history,
_patch_status,
_patch_version,
)
from tests.common import MockConfigEntry
async def test_user_form(hass: HomeAssistant) -> None:
"""Test we get the user initiated form."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {}
with (
_patch_version(),
_patch_status(),
_patch_history(),
_patch_async_setup_entry() as mock_setup_entry,
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
USER_INPUT,
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == "10.10.10.30"
assert result["data"][CONF_VERIFY_SSL] is False
assert len(mock_setup_entry.mock_calls) == 1
async def test_user_form_with_verify_ssl(hass: HomeAssistant) -> None:
"""Test the user flow with verify SSL option."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {}
user_input = {
**USER_INPUT,
CONF_MORE_OPTIONS: {CONF_VERIFY_SSL: True},
}
with (
_patch_version(),
_patch_status(),
_patch_history(),
_patch_async_setup_entry() as mock_setup_entry,
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input,
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == "10.10.10.30"
assert result["data"][CONF_VERIFY_SSL] is True
assert len(mock_setup_entry.mock_calls) == 1
async def test_user_form_cannot_connect(hass: HomeAssistant) -> None:
"""Test we handle cannot connect error."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
with patch(
"homeassistant.components.nzbget.coordinator.NZBGetAPI.version",
side_effect=NZBGetAPIException(),
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
USER_INPUT,
)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": "cannot_connect"}
async def test_user_form_unexpected_exception(hass: HomeAssistant) -> None:
"""Test we handle unexpected exception."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
with patch(
"homeassistant.components.nzbget.coordinator.NZBGetAPI.version",
side_effect=Exception(),
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
USER_INPUT,
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "unknown"
async def test_user_form_single_instance_allowed(hass: HomeAssistant) -> None:
"""Test that configuring more than one instance is rejected."""
entry = MockConfigEntry(domain=DOMAIN, data=ENTRY_CONFIG)
entry.add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_USER},
data=USER_INPUT,
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "single_instance_allowed"