"""Test API functionality.""" import pytest from unittest.mock import AsyncMock, MagicMock from custom_components.adguard_hub.api import AdGuardHomeAPI class TestAdGuardHomeAPI: """Test the AdGuard Home API wrapper.""" def test_api_initialization(self): """Test API initialization.""" api = AdGuardHomeAPI( host="192.168.1.100", port=3000, username="admin", password="password", ssl=True, ) assert api.host == "192.168.1.100" assert api.port == 3000 assert api.username == "admin" assert api.password == "password" assert api.ssl is True assert api.base_url == "https://192.168.1.100:3000" @pytest.mark.asyncio async def test_api_context_manager(self): """Test API as async context manager.""" async with AdGuardHomeAPI(host="192.168.1.100", port=3000) as api: assert api is not None assert api.host == "192.168.1.100" assert api.port == 3000 @pytest.mark.asyncio async def test_test_connection_success(self): """Test successful connection test.""" session = MagicMock() response = MagicMock() response.status = 200 response.json = AsyncMock(return_value={"protection_enabled": True}) response.raise_for_status = MagicMock() response.content_length = 100 context_manager = MagicMock() context_manager.__aenter__ = AsyncMock(return_value=response) context_manager.__aexit__ = AsyncMock(return_value=None) session.request = MagicMock(return_value=context_manager) api = AdGuardHomeAPI(host="192.168.1.100", session=session) result = await api.test_connection() assert result is True