core/tests/components/adax/test_config_flow.py

79 lines
2.4 KiB
Python

"""Test the Adax config flow."""
from unittest.mock import patch
from homeassistant import config_entries
from homeassistant.components.adax.const import ACCOUNT_ID, DOMAIN
from homeassistant.const import CONF_PASSWORD
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry
TEST_DATA = {
ACCOUNT_ID: 12345,
CONF_PASSWORD: "pswd",
}
async def test_form(hass: HomeAssistant) -> None:
"""Test we get the form."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == "form"
assert result["errors"] is None
with patch("adax.get_adax_token", return_value="test_token",), patch(
"homeassistant.components.adax.async_setup_entry",
return_value=True,
) as mock_setup_entry:
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
TEST_DATA,
)
await hass.async_block_till_done()
assert result2["type"] == "create_entry"
assert result2["title"] == TEST_DATA["account_id"]
assert result2["data"] == {
"account_id": TEST_DATA["account_id"],
"password": TEST_DATA["password"],
}
assert len(mock_setup_entry.mock_calls) == 1
async def test_form_cannot_connect(hass: HomeAssistant) -> None:
"""Test we handle cannot connect error."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
"adax.get_adax_token",
return_value=None,
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
TEST_DATA,
)
assert result2["type"] == "form"
assert result2["errors"] == {"base": "cannot_connect"}
async def test_flow_entry_already_exists(hass: HomeAssistant) -> None:
"""Test user input for config_entry that already exists."""
first_entry = MockConfigEntry(
domain="adax",
data=TEST_DATA,
unique_id=TEST_DATA[ACCOUNT_ID],
)
first_entry.add_to_hass(hass)
with patch("adax.get_adax_token", return_value="token"):
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}, data=TEST_DATA
)
assert result["type"] == "abort"
assert result["reason"] == "already_configured"