69
tests/conftest.py
Normal file
69
tests/conftest.py
Normal file
@@ -0,0 +1,69 @@
|
||||
"""Test configuration for AdGuard Control Hub integration."""
|
||||
import pytest
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
|
||||
from custom_components.adguard_control_hub.const import (
|
||||
CONF_HOST,
|
||||
CONF_PASSWORD,
|
||||
CONF_PORT,
|
||||
CONF_SSL,
|
||||
CONF_USERNAME,
|
||||
CONF_VERIFY_SSL,
|
||||
DOMAIN,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_adguard_api():
|
||||
"""Mock AdGuard API client."""
|
||||
mock_api = MagicMock()
|
||||
mock_api.get_status = AsyncMock(return_value={
|
||||
"version": "0.107.50",
|
||||
"protection_enabled": True,
|
||||
"filtering_enabled": True,
|
||||
"safebrowsing_enabled": True,
|
||||
"parental_enabled": True,
|
||||
"safesearch_enabled": True,
|
||||
"querylog_enabled": True,
|
||||
})
|
||||
mock_api.get_stats = AsyncMock(return_value={
|
||||
"num_dns_queries": 1000,
|
||||
"num_blocked_filtering": 200,
|
||||
"avg_processing_time": 1.5,
|
||||
"num_replaced_safebrowsing": 1500,
|
||||
})
|
||||
mock_api.get_clients = AsyncMock(return_value={
|
||||
"clients": [],
|
||||
"auto_clients": [],
|
||||
})
|
||||
mock_api.set_protection = AsyncMock()
|
||||
mock_api.set_filtering = AsyncMock()
|
||||
mock_api.set_safebrowsing = AsyncMock()
|
||||
mock_api.set_parental_control = AsyncMock()
|
||||
mock_api.set_safe_search = AsyncMock()
|
||||
mock_api.set_query_log = AsyncMock()
|
||||
mock_api.test_connection = AsyncMock(return_value=True)
|
||||
return mock_api
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_config_entry():
|
||||
"""Mock config entry."""
|
||||
return {
|
||||
CONF_HOST: "192.168.1.100",
|
||||
CONF_PORT: 3000,
|
||||
CONF_USERNAME: "admin",
|
||||
CONF_PASSWORD: "password",
|
||||
CONF_SSL: False,
|
||||
CONF_VERIFY_SSL: True,
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_setup_entry():
|
||||
"""Mock setup entry."""
|
||||
with patch("custom_components.adguard_control_hub.AdGuardHomeAPI") as mock_api_class:
|
||||
yield mock_api_class
|
||||
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"}
|
||||
47
tests/test_init.py
Normal file
47
tests/test_init.py
Normal file
@@ -0,0 +1,47 @@
|
||||
"""Test AdGuard Control Hub initialization."""
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.const import CONF_HOST, CONF_PORT
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from custom_components.adguard_control_hub.const import DOMAIN
|
||||
|
||||
|
||||
async def test_setup_entry(hass: HomeAssistant, mock_config_entry, mock_adguard_api) -> None:
|
||||
"""Test successful setup of entry."""
|
||||
with patch(
|
||||
"custom_components.adguard_control_hub.AdGuardHomeAPI"
|
||||
) as mock_api_class:
|
||||
mock_api_class.return_value = mock_adguard_api
|
||||
|
||||
entry = hass.config_entries.async_add(mock_config_entry)
|
||||
|
||||
with patch("custom_components.adguard_control_hub.PLATFORMS", []):
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert entry.state == ConfigEntryState.LOADED
|
||||
assert DOMAIN in hass.data
|
||||
|
||||
|
||||
async def test_unload_entry(hass: HomeAssistant, mock_config_entry, mock_adguard_api) -> None:
|
||||
"""Test successful unload of entry."""
|
||||
with patch(
|
||||
"custom_components.adguard_control_hub.AdGuardHomeAPI"
|
||||
) as mock_api_class:
|
||||
mock_api_class.return_value = mock_adguard_api
|
||||
|
||||
entry = hass.config_entries.async_add(mock_config_entry)
|
||||
|
||||
with patch("custom_components.adguard_control_hub.PLATFORMS", []):
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert entry.state == ConfigEntryState.LOADED
|
||||
|
||||
await hass.config_entries.async_unload(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert entry.state == ConfigEntryState.NOT_LOADED
|
||||
Reference in New Issue
Block a user