Files
core/tests/components/whois/test_config_flow.py
T

159 lines
4.7 KiB
Python

"""Tests for the Whois config flow."""
from unittest.mock import AsyncMock, MagicMock, call
import pytest
from syrupy.assertion import SnapshotAssertion
from whoisdomain.exceptions import (
FailedParsingWhoisOutputError,
UnknownDateFormatError,
UnknownTldError,
WhoisCommandFailedError,
WhoisPrivateRegistryError,
WhoisQuotaExceededError,
)
from homeassistant.components.whois.const import DOMAIN
from homeassistant.config_entries import SOURCE_USER
from homeassistant.const import CONF_DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from tests.common import MockConfigEntry
@pytest.mark.usefixtures("mock_whois")
async def test_full_user_flow(
hass: HomeAssistant,
mock_setup_entry: AsyncMock,
mock_whois: MagicMock,
snapshot: SnapshotAssertion,
) -> None:
"""Test the full user configuration flow."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result.get("type") is FlowResultType.FORM
assert result.get("step_id") == "user"
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={CONF_DOMAIN: "Example.com"},
)
assert result2.get("type") is FlowResultType.CREATE_ENTRY
assert result2 == snapshot
mock_whois.assert_called_once_with("example.com", whoisOnly=True)
assert len(mock_setup_entry.mock_calls) == 1
@pytest.mark.parametrize(
("throw", "reason"),
[
pytest.param(UnknownTldError, "unknown_tld", id="UnknownTld-unknown_tld"),
pytest.param(
FailedParsingWhoisOutputError,
"unexpected_response",
id="FailedParsingWhoisOutput-unexpected_response",
),
pytest.param(
UnknownDateFormatError,
"unknown_date_format",
id="UnknownDateFormat-unknown_date_format",
),
pytest.param(
WhoisCommandFailedError,
"whois_command_failed",
id="WhoisCommandFailed-whois_command_failed",
),
pytest.param(
WhoisPrivateRegistryError,
"private_registry",
id="WhoisPrivateRegistry-private_registry",
),
pytest.param(
WhoisQuotaExceededError,
"quota_exceeded",
id="WhoisQuotaExceeded-quota_exceeded",
),
],
)
async def test_full_flow_with_error(
hass: HomeAssistant,
mock_setup_entry: AsyncMock,
mock_whois: MagicMock,
snapshot: SnapshotAssertion,
throw: Exception,
reason: str,
) -> None:
"""Test the full user configuration flow with an error.
This tests tests a full config flow, with an error happening; allowing
the user to fix the error and try again.
"""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result.get("type") is FlowResultType.FORM
assert result.get("step_id") == "user"
mock_whois.side_effect = throw
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={CONF_DOMAIN: "Example.com"},
)
assert result2.get("type") is FlowResultType.FORM
assert result2.get("step_id") == "user"
assert result2.get("errors") == {"base": reason}
assert len(mock_setup_entry.mock_calls) == 0
assert len(mock_whois.mock_calls) == 1
mock_whois.assert_called_once_with("example.com", whoisOnly=True)
mock_whois.side_effect = None
result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"],
user_input={CONF_DOMAIN: "Example.com"},
)
assert result3.get("type") is FlowResultType.CREATE_ENTRY
assert result3 == snapshot
assert len(mock_setup_entry.mock_calls) == 1
assert len(mock_whois.mock_calls) == 2
assert mock_whois.mock_calls == [
call("example.com", whoisOnly=True),
call("example.com", whoisOnly=True),
]
@pytest.mark.usefixtures("mock_whois")
async def test_already_configured(
hass: HomeAssistant,
mock_setup_entry: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test we abort if already configured."""
mock_config_entry.add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result.get("type") is FlowResultType.FORM
assert result.get("step_id") == "user"
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={CONF_DOMAIN: "HOME-Assistant.io"},
)
assert result2.get("type") is FlowResultType.ABORT
assert result2.get("reason") == "already_configured"
assert len(mock_setup_entry.mock_calls) == 0