Files
adguard-control-hub/tests/test_config_flow.py
Rafal Zielinski e23d60d895
Some checks failed
Tests / test (3.13) (push) Failing after 26s
Tests / lint (push) Failing after 16s
Tests / hacs (push) Failing after 30s
fix: some minor fixes
Signed-off-by: Rafal Zielinski <sq4ind@gmail.com>
2025-10-02 16:09:10 +01:00

109 lines
3.5 KiB
Python

"""Test AdGuard Control Hub config flow."""
import pytest
from unittest.mock import AsyncMock, patch
from homeassistant import config_entries
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT, CONF_USERNAME
from homeassistant.core import HomeAssistant
from custom_components.adguard_control_hub.config_flow import (
CannotConnect,
InvalidAuth,
)
from custom_components.adguard_control_hub.const import CONF_SSL, CONF_VERIFY_SSL, DOMAIN
@pytest.mark.asyncio
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
@pytest.mark.asyncio
async def test_form_valid_connection(hass: HomeAssistant, mock_adguard_api) -> None:
"""Test we get the form with valid connection."""
with patch(
"custom_components.adguard_control_hub.config_flow.AdGuardHomeAPI"
) as mock_api_class:
mock_api_class.return_value = mock_adguard_api
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_HOST: "192.168.1.100",
CONF_PORT: 3000,
CONF_USERNAME: "admin",
CONF_PASSWORD: "password",
CONF_SSL: False,
CONF_VERIFY_SSL: True,
},
)
await hass.async_block_till_done()
assert result2["type"] == "create_entry"
assert result2["title"] == "AdGuard Home (192.168.1.100)"
assert result2["data"] == {
CONF_HOST: "192.168.1.100",
CONF_PORT: 3000,
CONF_USERNAME: "admin",
CONF_PASSWORD: "password",
CONF_SSL: False,
CONF_VERIFY_SSL: True,
}
@pytest.mark.asyncio
async def test_form_cannot_connect(hass: HomeAssistant) -> None:
"""Test we handle cannot connect error."""
with patch(
"custom_components.adguard_control_hub.config_flow.validate_input",
side_effect=CannotConnect,
):
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_HOST: "192.168.1.100",
CONF_PORT: 3000,
},
)
assert result2["type"] == "form"
assert result2["errors"] == {"base": "cannot_connect"}
@pytest.mark.asyncio
async def test_form_invalid_auth(hass: HomeAssistant) -> None:
"""Test we handle invalid auth error."""
with patch(
"custom_components.adguard_control_hub.config_flow.validate_input",
side_effect=InvalidAuth,
):
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_HOST: "192.168.1.100",
CONF_PORT: 3000,
CONF_USERNAME: "admin",
CONF_PASSWORD: "wrong",
},
)
assert result2["type"] == "form"
assert result2["errors"] == {"base": "invalid_auth"}