| 1 |
|
| 2 |
import pytest |
| 3 |
from unittest.mock import AsyncMock, MagicMock |
| 4 |
|
| 5 |
import aiohttp |
| 6 |
|
| 7 |
from hole.v5 import HoleV5 |
| 8 |
from hole.v6 import HoleV6 |
| 9 |
from hole import exceptions |
| 10 |
|
| 11 |
|
| 12 |
pytestmark = pytest.mark.asyncio |
| 13 |
|
| 14 |
|
| 15 |
class DummyResponse: |
| 16 |
"""Dummy response class for simulating aiohttp responses.""" |
| 17 |
|
| 18 |
def __init__(self, status=200, json_data=None, text_data=None): |
| 19 |
"""Initialize the dummy response.""" |
| 20 |
self.status = status |
| 21 |
self._json = json_data or {} |
| 22 |
self._text = text_data or "{}" |
| 23 |
|
| 24 |
async def json(self): |
| 25 |
"""Return the JSON data.""" |
| 26 |
return self._json |
| 27 |
|
| 28 |
async def text(self): |
| 29 |
"""Return the text data.""" |
| 30 |
return self._text |
| 31 |
|
| 32 |
|
| 33 |
@pytest.fixture |
| 34 |
def aiohttp_session(): |
| 35 |
"""Fixture for creating a mock aiohttp session.""" |
| 36 |
return MagicMock(spec=aiohttp.ClientSession) |
| 37 |
|
| 38 |
|
| 39 |
@pytest.fixture |
| 40 |
def hole6(aiohttp_session): |
| 41 |
"""Fixture for a HoleV6 client with auth stubbed out.""" |
| 42 |
client = HoleV6("localhost", aiohttp_session, password="pw") |
| 43 |
client.ensure_auth = AsyncMock() |
| 44 |
return client |
| 45 |
|
| 46 |
|
| 47 |
@pytest.mark.asyncio |
| 48 |
async def test_v5_get_data_success(aiohttp_session): |
| 49 |
"""Test successful data retrieval for HoleV5.""" |
| 50 |
aiohttp_session.get = AsyncMock( |
| 51 |
return_value=DummyResponse( |
| 52 |
json_data={ |
| 53 |
"status": "enabled", |
| 54 |
"unique_clients": 2, |
| 55 |
"unique_domains": 3, |
| 56 |
"ads_blocked_today": 4, |
| 57 |
"ads_percentage_today": 5.5, |
| 58 |
"clients_ever_seen": 6, |
| 59 |
"dns_queries_today": 7, |
| 60 |
"domains_being_blocked": 8, |
| 61 |
"queries_cached": 9, |
| 62 |
"queries_forwarded": 10, |
| 63 |
} |
| 64 |
) |
| 65 |
) |
| 66 |
hole5 = HoleV5("localhost", aiohttp_session, api_token="token") |
| 67 |
await hole5.get_data() |
| 68 |
assert hole5.status == "enabled" |
| 69 |
assert hole5.unique_clients == 2 |
| 70 |
assert hole5.unique_domains == 3 |
| 71 |
assert hole5.ads_blocked_today == 4 |
| 72 |
assert hole5.ads_percentage_today == 5.5 |
| 73 |
assert hole5.clients_ever_seen == 6 |
| 74 |
assert hole5.dns_queries_today == 7 |
| 75 |
assert hole5.domains_being_blocked == 8 |
| 76 |
assert hole5.queries_cached == 9 |
| 77 |
assert hole5.queries_forwarded == 10 |
| 78 |
|
| 79 |
|
| 80 |
@pytest.mark.asyncio |
| 81 |
async def test_v5_get_data_connection_error(aiohttp_session): |
| 82 |
"""Test connection error handling for HoleV5.""" |
| 83 |
aiohttp_session.get = AsyncMock(side_effect=aiohttp.ClientError) |
| 84 |
hole5 = HoleV5("localhost", aiohttp_session, api_token="token") |
| 85 |
with pytest.raises(exceptions.HoleConnectionError): |
| 86 |
await hole5.get_data() |
| 87 |
|
| 88 |
|
| 89 |
@pytest.mark.asyncio |
| 90 |
async def test_v6_get_data_success(hole6): |
| 91 |
"""Test successful data retrieval for HoleV6.""" |
| 92 |
hole6._fetch_data = AsyncMock( |
| 93 |
side_effect=[ |
| 94 |
{ |
| 95 |
"queries": { |
| 96 |
"blocked": 1, |
| 97 |
"percent_blocked": 2.5, |
| 98 |
"unique_domains": 3, |
| 99 |
"total": 4, |
| 100 |
"cached": 5, |
| 101 |
"forwarded": 6, |
| 102 |
"replies": {}, |
| 103 |
}, |
| 104 |
"clients": {"active": 2, "total": 3}, |
| 105 |
"gravity": {"domains_being_blocked": 11}, |
| 106 |
}, |
| 107 |
{"domains": ["ad.com"]}, |
| 108 |
{"domains": ["ok.com"]}, |
| 109 |
{"clients": {"active": 2, "total": 3}}, |
| 110 |
{"upstreams": ["8.8.8.8"]}, |
| 111 |
{"blocking": "enabled"}, |
| 112 |
{ |
| 113 |
"version": { |
| 114 |
"core": { |
| 115 |
"local": {"version": "1", "hash": "a"}, |
| 116 |
"remote": {"version": "2", "hash": "b"}, |
| 117 |
}, |
| 118 |
"web": { |
| 119 |
"local": {"version": "1", "hash": "a"}, |
| 120 |
"remote": {"version": "2", "hash": "b"}, |
| 121 |
}, |
| 122 |
"ftl": { |
| 123 |
"local": {"version": "1", "hash": "a"}, |
| 124 |
"remote": {"version": "2", "hash": "b"}, |
| 125 |
}, |
| 126 |
}, |
| 127 |
}, |
| 128 |
] |
| 129 |
) |
| 130 |
await hole6.get_data() |
| 131 |
assert hole6.ads_blocked_today == 1 |
| 132 |
assert hole6.ads_percentage_today == 2.5 |
| 133 |
assert hole6.unique_domains == 3 |
| 134 |
assert hole6.dns_queries_today == 4 |
| 135 |
assert hole6.queries_cached == 5 |
| 136 |
assert hole6.queries_forwarded == 6 |
| 137 |
assert hole6.top_ads == ["ad.com"] |
| 138 |
assert hole6.top_queries == ["ok.com"] |
| 139 |
assert hole6.unique_clients == 2 |
| 140 |
assert hole6.clients_ever_seen == 3 |
| 141 |
assert hole6.status == "enabled" |
| 142 |
|
| 143 |
|
| 144 |
@pytest.mark.asyncio |
| 145 |
async def test_v6_fetch_data_authentication_error(aiohttp_session, hole6): |
| 146 |
"""Test a persistent 401 raises HoleAuthenticationError with status.""" |
| 147 |
hole6.authenticate = AsyncMock() |
| 148 |
aiohttp_session.get = AsyncMock(return_value=DummyResponse(status=401)) |
| 149 |
with pytest.raises(exceptions.HoleAuthenticationError) as err: |
| 150 |
await hole6._fetch_data("/info/version") |
| 151 |
assert err.value.status == 401 |
| 152 |
|
| 153 |
|
| 154 |
@pytest.mark.asyncio |
| 155 |
async def test_v6_fetch_data_error_status(aiohttp_session, hole6): |
| 156 |
"""Test a non-200, non-401 status raises HoleError carrying the status.""" |
| 157 |
aiohttp_session.get = AsyncMock(return_value=DummyResponse(status=500)) |
| 158 |
with pytest.raises(exceptions.HoleError) as err: |
| 159 |
await hole6._fetch_data("/stats/summary") |
| 160 |
assert err.value.status == 500 |
| 161 |
assert not isinstance(err.value, exceptions.HoleAuthenticationError) |
| 162 |
|
| 163 |
|
| 164 |
@pytest.mark.asyncio |
| 165 |
async def test_v6_fetch_data_invalid_response(aiohttp_session, hole6): |
| 166 |
"""Test an unparseable payload raises HoleResponseError.""" |
| 167 |
response = DummyResponse(status=200) |
| 168 |
response.json = AsyncMock(side_effect=ValueError("bad json")) |
| 169 |
aiohttp_session.get = AsyncMock(return_value=response) |
| 170 |
with pytest.raises(exceptions.HoleResponseError): |
| 171 |
await hole6._fetch_data("/info/version") |
| 172 |
|
| 173 |
|
| 174 |
@pytest.mark.asyncio |
| 175 |
async def test_v6_get_versions_success(hole6): |
| 176 |
"""Test get_versions stores the version payload.""" |
| 177 |
versions = {"core": {"local": {"version": "1", "hash": "a"}}} |
| 178 |
hole6._fetch_data = AsyncMock(return_value={"version": versions}) |
| 179 |
await hole6.get_versions() |
| 180 |
assert hole6.versions == versions |
| 181 |
|
| 182 |
|
| 183 |
@pytest.mark.asyncio |
| 184 |
@pytest.mark.parametrize("payload", [[], {"version": "v6"}, {}]) |
| 185 |
async def test_v6_get_versions_unexpected_payload(hole6, payload): |
| 186 |
"""Test an unexpected /info/version shape raises HoleResponseError.""" |
| 187 |
hole6._fetch_data = AsyncMock(return_value=payload) |
| 188 |
with pytest.raises(exceptions.HoleResponseError): |
| 189 |
await hole6.get_versions() |
| 190 |
|
| 191 |
|
| 192 |
@pytest.mark.asyncio |
| 193 |
async def test_v6_authenticate_invalid_password(aiohttp_session): |
| 194 |
"""Test authenticate raises HoleAuthenticationError on 401.""" |
| 195 |
hole6 = HoleV6("localhost", aiohttp_session, password="wrong") |
| 196 |
aiohttp_session.post = AsyncMock(return_value=DummyResponse(status=401)) |
| 197 |
with pytest.raises(exceptions.HoleAuthenticationError) as err: |
| 198 |
await hole6.authenticate() |
| 199 |
assert err.value.status == 401 |