218 lines
6.7 KiB
Python
218 lines
6.7 KiB
Python
"""Test configuration and fixtures."""
|
|
import pytest
|
|
from unittest.mock import AsyncMock, MagicMock
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.config_entries import ConfigEntry, SOURCE_USER
|
|
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT, CONF_USERNAME
|
|
|
|
from custom_components.adguard_hub.api import AdGuardHomeAPI
|
|
from custom_components.adguard_hub.const import DOMAIN, CONF_SSL, CONF_VERIFY_SSL
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def auto_enable_custom_integrations(enable_custom_integrations):
|
|
"""Enable custom integrations for all tests."""
|
|
yield
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_config_entry():
|
|
"""Mock config entry for testing."""
|
|
return ConfigEntry(
|
|
version=1,
|
|
minor_version=1,
|
|
domain=DOMAIN,
|
|
title="Test AdGuard Control Hub",
|
|
data={
|
|
CONF_HOST: "192.168.1.100",
|
|
CONF_PORT: 3000,
|
|
CONF_USERNAME: "admin",
|
|
CONF_PASSWORD: "password",
|
|
CONF_SSL: False,
|
|
CONF_VERIFY_SSL: True,
|
|
},
|
|
options={},
|
|
source=SOURCE_USER,
|
|
entry_id="test_entry_id",
|
|
unique_id="192.168.1.100:3000",
|
|
discovery_keys=set(), # Added required parameter
|
|
subentries_data={}, # Added required parameter
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_api():
|
|
"""Mock AdGuard Home API."""
|
|
api = MagicMock(spec=AdGuardHomeAPI)
|
|
api.host = "192.168.1.100"
|
|
api.port = 3000
|
|
api.ssl = False
|
|
api.verify_ssl = True
|
|
|
|
# Mock successful connection
|
|
api.test_connection = AsyncMock(return_value=True)
|
|
|
|
# Mock status response
|
|
api.get_status = AsyncMock(return_value={
|
|
"protection_enabled": True,
|
|
"version": "v0.107.0",
|
|
"dns_port": 53,
|
|
"running": True,
|
|
"dns_addresses": ["192.168.1.100:53"],
|
|
"bootstrap_dns": ["1.1.1.1", "8.8.8.8"],
|
|
"upstream_dns": ["1.1.1.1", "8.8.8.8", "1.0.0.1", "8.8.4.4"],
|
|
"safebrowsing_enabled": True,
|
|
"parental_enabled": False,
|
|
"safesearch_enabled": False,
|
|
"dhcp_available": False,
|
|
})
|
|
|
|
# Mock clients response
|
|
api.get_clients = AsyncMock(return_value={
|
|
"clients": [
|
|
{
|
|
"name": "test_client",
|
|
"ids": ["192.168.1.50"],
|
|
"filtering_enabled": True,
|
|
"safebrowsing_enabled": False,
|
|
"parental_enabled": False,
|
|
"safesearch_enabled": False,
|
|
"use_global_settings": True,
|
|
"use_global_blocked_services": True,
|
|
"blocked_services": {"ids": ["youtube", "gaming"]},
|
|
},
|
|
{
|
|
"name": "test_client_2",
|
|
"ids": ["192.168.1.51"],
|
|
"filtering_enabled": False,
|
|
"safebrowsing_enabled": True,
|
|
"parental_enabled": True,
|
|
"safesearch_enabled": False,
|
|
"use_global_settings": False,
|
|
"blocked_services": {"ids": ["netflix"]},
|
|
}
|
|
]
|
|
})
|
|
|
|
# Mock statistics response
|
|
api.get_statistics = AsyncMock(return_value={
|
|
"num_dns_queries": 10000,
|
|
"num_blocked_filtering": 1500,
|
|
"num_dns_queries_today": 5000,
|
|
"num_blocked_filtering_today": 750,
|
|
"num_replaced_safebrowsing": 50,
|
|
"num_replaced_parental": 25,
|
|
"num_replaced_safesearch": 10,
|
|
"avg_processing_time": 2.5,
|
|
"filtering_rules_count": 75000,
|
|
})
|
|
|
|
# Mock client operations
|
|
api.get_client_by_name = AsyncMock(return_value={
|
|
"name": "test_client",
|
|
"ids": ["192.168.1.50"],
|
|
"filtering_enabled": True,
|
|
"blocked_services": {"ids": ["youtube"]},
|
|
})
|
|
|
|
api.add_client = AsyncMock(return_value={"success": True})
|
|
api.update_client = AsyncMock(return_value={"success": True})
|
|
api.delete_client = AsyncMock(return_value={"success": True})
|
|
api.update_client_blocked_services = AsyncMock(return_value={"success": True})
|
|
api.set_protection = AsyncMock(return_value={"success": True})
|
|
api.close = AsyncMock(return_value=None)
|
|
|
|
return api
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_coordinator(mock_api):
|
|
"""Mock coordinator with test data."""
|
|
from custom_components.adguard_hub import AdGuardControlHubCoordinator
|
|
|
|
coordinator = MagicMock(spec=AdGuardControlHubCoordinator)
|
|
coordinator.last_update_success = True
|
|
coordinator.api = mock_api
|
|
|
|
# Mock clients data
|
|
coordinator.clients = {
|
|
"test_client": {
|
|
"name": "test_client",
|
|
"ids": ["192.168.1.50"],
|
|
"filtering_enabled": True,
|
|
"blocked_services": {"ids": ["youtube"]},
|
|
},
|
|
"test_client_2": {
|
|
"name": "test_client_2",
|
|
"ids": ["192.168.1.51"],
|
|
"filtering_enabled": False,
|
|
"blocked_services": {"ids": ["netflix"]},
|
|
}
|
|
}
|
|
|
|
# Mock statistics data
|
|
coordinator.statistics = {
|
|
"num_dns_queries": 10000,
|
|
"num_blocked_filtering": 1500,
|
|
"avg_processing_time": 2.5,
|
|
"filtering_rules_count": 75000,
|
|
}
|
|
|
|
# Mock protection status
|
|
coordinator.protection_status = {
|
|
"protection_enabled": True,
|
|
"version": "v0.107.0",
|
|
"dns_port": 53,
|
|
"running": True,
|
|
"safebrowsing_enabled": True,
|
|
"parental_enabled": False,
|
|
"safesearch_enabled": False,
|
|
}
|
|
|
|
coordinator.data = {
|
|
"clients": coordinator.clients,
|
|
"statistics": coordinator.statistics,
|
|
"status": coordinator.protection_status,
|
|
}
|
|
|
|
coordinator.async_request_refresh = AsyncMock()
|
|
|
|
return coordinator
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_hass():
|
|
"""Mock Home Assistant instance."""
|
|
hass = MagicMock(spec=HomeAssistant)
|
|
hass.data = {}
|
|
hass.services = MagicMock()
|
|
hass.services.has_service = MagicMock(return_value=False)
|
|
hass.services.register = MagicMock()
|
|
hass.services.remove = MagicMock()
|
|
hass.config_entries = MagicMock()
|
|
hass.config_entries.async_forward_entry_setups = AsyncMock(return_value=True)
|
|
hass.config_entries.async_unload_platforms = AsyncMock(return_value=True)
|
|
return hass
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_aiohttp_session():
|
|
"""Mock aiohttp session."""
|
|
session = MagicMock()
|
|
response = MagicMock()
|
|
response.raise_for_status = MagicMock()
|
|
response.json = AsyncMock(return_value={"status": "ok"})
|
|
response.text = AsyncMock(return_value="OK")
|
|
response.status = 200
|
|
response.content_length = 100
|
|
|
|
# Mock async context manager
|
|
context_manager = MagicMock()
|
|
context_manager.__aenter__ = AsyncMock(return_value=response)
|
|
context_manager.__aexit__ = AsyncMock(return_value=None)
|
|
|
|
session.request = MagicMock(return_value=context_manager)
|
|
session.close = AsyncMock()
|
|
|
|
return session
|