104
tests/test_config_flow.py
Normal file
104
tests/test_config_flow.py
Normal file
@@ -0,0 +1,104 @@
|
||||
"""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
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
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,
|
||||
}
|
||||
|
||||
|
||||
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"}
|
||||
|
||||
|
||||
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"}
|
||||
Reference in New Issue
Block a user