Files
adguard-control-hub/tests/test_api.py
Rafal Zielinski ed94d40e96
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
fix: Complete fixes: tests, workflows, coverage
Signed-off-by: Rafal Zielinski <sq4ind@gmail.com>
2025-09-28 17:58:31 +01:00

81 lines
2.4 KiB
Python

"""Test AdGuard Home API client."""
import pytest
from unittest.mock import AsyncMock, patch
import aiohttp
from custom_components.adguard_hub.api import (
AdGuardHomeAPI,
AdGuardConnectionError,
AdGuardAuthError,
AdGuardTimeoutError,
)
class TestAdGuardHomeAPI:
"""Test AdGuard Home API client."""
@pytest.fixture
def api(self, mock_aiohttp_session):
"""Create API instance."""
return AdGuardHomeAPI(
host="192.168.1.100",
port=3000,
username="admin",
password="password",
session=mock_aiohttp_session,
)
def test_api_initialization(self):
"""Test API initialization."""
api = AdGuardHomeAPI(
host="192.168.1.100",
port=3000,
username="admin",
password="password",
)
assert api.host == "192.168.1.100"
assert api.port == 3000
assert api.username == "admin"
assert api.password == "password"
assert api.base_url == "http://192.168.1.100:3000"
@pytest.mark.asyncio
async def test_connection_success(self, api):
"""Test successful connection."""
result = await api.test_connection()
assert result is True
@pytest.mark.asyncio
async def test_get_status(self, api, mock_aiohttp_session):
"""Test getting status."""
expected_response = {
"protection_enabled": True,
"version": "v0.108.0",
"running": True,
}
mock_aiohttp_session.request.return_value.__aenter__.return_value.json = AsyncMock(
return_value=expected_response
)
result = await api.get_status()
assert result == expected_response
@pytest.mark.asyncio
async def test_auth_error(self, api, mock_aiohttp_session):
"""Test authentication error."""
mock_aiohttp_session.request.return_value.__aenter__.return_value.status = 401
with pytest.raises(AdGuardAuthError):
await api.get_status()
@pytest.mark.asyncio
async def test_connection_error(self, api, mock_aiohttp_session):
"""Test connection error."""
mock_aiohttp_session.request.side_effect = aiohttp.ClientConnectorError(
None, OSError("Connection failed")
)
with pytest.raises(AdGuardConnectionError):
await api.get_status()