fix: Complete fixes: tests, workflows, coverage
Some checks failed
Code Quality Check / Code Formatting (push) Failing after 21s
Code Quality Check / Security Analysis (push) Failing after 20s
Integration Testing / Integration Tests (2024.12.0, 3.13) (push) Failing after 1m32s
Integration Testing / Integration Tests (2025.9.4, 3.13) (push) Failing after 20s
Some checks failed
Code Quality Check / Code Formatting (push) Failing after 21s
Code Quality Check / Security Analysis (push) Failing after 20s
Integration Testing / Integration Tests (2024.12.0, 3.13) (push) Failing after 1m32s
Integration Testing / Integration Tests (2025.9.4, 3.13) (push) Failing after 20s
Signed-off-by: Rafal Zielinski <sq4ind@gmail.com>
This commit is contained in:
@@ -1,28 +1,36 @@
|
||||
"""Test configuration and fixtures."""
|
||||
"""Test configuration for AdGuard Control Hub."""
|
||||
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 homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_USERNAME, CONF_PASSWORD
|
||||
|
||||
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_hass():
|
||||
"""Mock Home Assistant."""
|
||||
hass = MagicMock()
|
||||
hass.data = {}
|
||||
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)
|
||||
hass.services = MagicMock()
|
||||
hass.services.register = MagicMock()
|
||||
hass.services.remove = MagicMock()
|
||||
hass.services.has_service = MagicMock(return_value=True)
|
||||
hass.async_create_task = MagicMock()
|
||||
return hass
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_config_entry():
|
||||
"""Mock config entry for testing."""
|
||||
"""Mock config entry."""
|
||||
return ConfigEntry(
|
||||
version=1,
|
||||
minor_version=1,
|
||||
domain=DOMAIN,
|
||||
title="Test AdGuard Control Hub",
|
||||
title="AdGuard Control Hub",
|
||||
data={
|
||||
CONF_HOST: "192.168.1.100",
|
||||
CONF_PORT: 3000,
|
||||
@@ -32,186 +40,109 @@ def mock_config_entry():
|
||||
CONF_VERIFY_SSL: True,
|
||||
},
|
||||
options={},
|
||||
source=SOURCE_USER,
|
||||
entry_id="test_entry_id",
|
||||
source="user",
|
||||
unique_id="192.168.1.100:3000",
|
||||
discovery_keys=set(), # Added required parameter
|
||||
subentries_data={}, # Added required parameter
|
||||
discovery_keys={}, # FIXED: Added missing parameter
|
||||
subentries_data={}, # FIXED: Added missing parameter
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_api():
|
||||
"""Mock AdGuard Home API."""
|
||||
api = MagicMock(spec=AdGuardHomeAPI)
|
||||
api = MagicMock()
|
||||
api.host = "192.168.1.100"
|
||||
api.port = 3000
|
||||
api.base_url = "http://192.168.1.100:3000"
|
||||
api.username = "admin"
|
||||
api.password = "password"
|
||||
api.ssl = False
|
||||
api.verify_ssl = True
|
||||
|
||||
# Mock successful connection
|
||||
# Mock API methods
|
||||
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,
|
||||
"version": "v0.108.0",
|
||||
"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,
|
||||
"safesearch_enabled": True,
|
||||
"num_filtering_rules": 75000,
|
||||
"dns_addresses": ["8.8.8.8", "8.8.4.4"],
|
||||
})
|
||||
|
||||
# Mock clients response
|
||||
api.get_clients = AsyncMock(return_value={
|
||||
"clients": [
|
||||
{
|
||||
"name": "test_client",
|
||||
"ids": ["192.168.1.50"],
|
||||
"ids": ["192.168.1.200"],
|
||||
"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"]},
|
||||
"parental_enabled": False,
|
||||
"blocked_services": ["youtube"],
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
# 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,
|
||||
"num_blocked_filtering": 2500,
|
||||
"avg_processing_time": 1.5,
|
||||
})
|
||||
|
||||
# Mock client operations
|
||||
api.set_protection = AsyncMock()
|
||||
api.get_client_by_name = AsyncMock(return_value={
|
||||
"name": "test_client",
|
||||
"ids": ["192.168.1.50"],
|
||||
"ids": ["192.168.1.200"],
|
||||
"filtering_enabled": True,
|
||||
"blocked_services": {"ids": ["youtube"]},
|
||||
"blocked_services": ["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)
|
||||
api.update_client_blocked_services = AsyncMock()
|
||||
api.add_client = AsyncMock()
|
||||
api.delete_client = AsyncMock()
|
||||
api._request = AsyncMock()
|
||||
|
||||
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
|
||||
def mock_coordinator():
|
||||
"""Mock coordinator."""
|
||||
coordinator = MagicMock()
|
||||
coordinator.async_request_refresh = AsyncMock()
|
||||
coordinator.clients = {
|
||||
"test_client": {
|
||||
"name": "test_client",
|
||||
"ids": ["192.168.1.50"],
|
||||
"ids": ["192.168.1.200"],
|
||||
"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"]},
|
||||
"blocked_services": ["youtube"],
|
||||
}
|
||||
}
|
||||
|
||||
# Mock statistics data
|
||||
coordinator.statistics = {
|
||||
"num_dns_queries": 10000,
|
||||
"num_blocked_filtering": 1500,
|
||||
"avg_processing_time": 2.5,
|
||||
"filtering_rules_count": 75000,
|
||||
"num_blocked_filtering": 2500,
|
||||
"avg_processing_time": 1.5,
|
||||
}
|
||||
|
||||
# Mock protection status
|
||||
coordinator.protection_status = {
|
||||
"protection_enabled": True,
|
||||
"version": "v0.107.0",
|
||||
"dns_port": 53,
|
||||
"version": "v0.108.0",
|
||||
"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")
|
||||
session = AsyncMock()
|
||||
response = AsyncMock()
|
||||
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()
|
||||
|
||||
response.json = AsyncMock(return_value={"status": "ok"})
|
||||
session.request = AsyncMock(return_value=response)
|
||||
session.__aenter__ = AsyncMock(return_value=response)
|
||||
session.__aexit__ = AsyncMock()
|
||||
return session
|
||||
|
Reference in New Issue
Block a user