Some checks failed
Code Quality Check / Code Formatting (push) Failing after 23s
Code Quality Check / Security Analysis (push) Failing after 25s
Integration Testing / Integration Tests (2024.12.0, 3.13) (push) Failing after 1m38s
Integration Testing / Integration Tests (2025.9.4, 3.13) (push) Failing after 24s
Signed-off-by: Rafal Zielinski <sq4ind@gmail.com>
82 lines
2.4 KiB
Python
82 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()
|