Some checks failed
🧪 Integration Testing / 🔧 Test Integration (2025.9.4, 3.13) (push) Failing after 24s
Signed-off-by: Rafal Zielinski <sq4ind@gmail.com>
84 lines
2.3 KiB
Python
84 lines
2.3 KiB
Python
"""Test API functionality."""
|
|
import pytest
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
|
from custom_components.adguard_hub.api import AdGuardHomeAPI
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_session():
|
|
"""Mock aiohttp session."""
|
|
session = MagicMock()
|
|
response = MagicMock()
|
|
response.raise_for_status = MagicMock()
|
|
response.json = AsyncMock(return_value={"status": "ok"})
|
|
response.status = 200
|
|
response.content_length = 100
|
|
|
|
# Create async context manager for session.request
|
|
async def mock_request(*args, **kwargs):
|
|
return response
|
|
|
|
session.request = MagicMock()
|
|
session.request.return_value.__aenter__ = AsyncMock(return_value=response)
|
|
session.request.return_value.__aexit__ = AsyncMock(return_value=None)
|
|
|
|
return session
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_api_connection(mock_session):
|
|
"""Test API connection."""
|
|
api = AdGuardHomeAPI(
|
|
host="test-host",
|
|
port=3000,
|
|
username="admin",
|
|
password="password",
|
|
session=mock_session
|
|
)
|
|
|
|
result = await api.test_connection()
|
|
assert result is True
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_api_get_status(mock_session):
|
|
"""Test getting status."""
|
|
api = AdGuardHomeAPI(
|
|
host="test-host",
|
|
port=3000,
|
|
session=mock_session
|
|
)
|
|
|
|
status = await api.get_status()
|
|
assert status == {"status": "ok"}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_api_context_manager():
|
|
"""Test API as async context manager."""
|
|
async with AdGuardHomeAPI(host="test-host", port=3000) as api:
|
|
assert api is not None
|
|
assert api.host == "test-host"
|
|
assert api.port == 3000
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_api_error_handling():
|
|
"""Test API error handling."""
|
|
from custom_components.adguard_hub.api import AdGuardConnectionError
|
|
|
|
# Test with a session that raises an exception
|
|
session = MagicMock()
|
|
session.request = MagicMock()
|
|
session.request.return_value.__aenter__ = AsyncMock(side_effect=Exception("Connection error"))
|
|
session.request.return_value.__aexit__ = AsyncMock(return_value=None)
|
|
|
|
api = AdGuardHomeAPI(
|
|
host="test-host",
|
|
port=3000,
|
|
session=session
|
|
)
|
|
|
|
with pytest.raises(Exception): # Should raise AdGuardHomeError
|
|
await api.get_status()
|