Skip to content

Commit

Permalink
Fix filter refresh handling
Browse files Browse the repository at this point in the history
  • Loading branch information
frenck committed Mar 11, 2021
1 parent 40a0f05 commit aa68f35
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
10 changes: 7 additions & 3 deletions src/adguardhome/filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,20 +176,24 @@ async def disable_url(self, url: str) -> None:
"Failed disabling URL on AdGuard Home filter"
) from exception

async def refresh(self, force=False) -> None:
async def refresh(self, *, allowlist: bool = False, force: bool = False) -> None:
"""Reload filtering subscriptions from URLs specified in AdGuard Home.
Args:
force: Force the reload of all filter subscriptions.
allowlist: True to update the allowlists, False for the blocklists.
Raises:
AdGuardHomeError: Failed to refresh filter subscriptions.
"""
force = "true" if force else "false"
force_value = "true" if force else "false"

try:
await self._adguard.request(
"filtering/refresh", method="POST", params={"force": force}
"filtering/refresh",
method="POST",
json_data={"whitelist": allowlist},
params={"force": force_value},
)
except AdGuardHomeError as exception:
raise AdGuardHomeError(
Expand Down
29 changes: 25 additions & 4 deletions tests/test_filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,31 @@ async def response_handler(request):
@pytest.mark.asyncio
async def test_refresh(aresponses):
"""Test enabling filter subscription in AdGuard Home filtering."""

async def response_handler_whitelist(request):
"""Response handler for this test."""
data = await request.json()
assert data == {"whitelist": True}
return aresponses.Response(status=200)

async def response_handler_blocklist(request):
"""Response handler for this test."""
data = await request.json()
assert data == {"whitelist": False}
return aresponses.Response(status=200)

aresponses.add(
"example.com:3000",
"/control/filtering/refresh?force=false",
"POST",
aresponses.Response(status=200, text="OK"),
response_handler_blocklist,
match_querystring=True,
)
aresponses.add(
"example.com:3000",
"/control/filtering/refresh?force=false",
"POST",
response_handler_whitelist,
match_querystring=True,
)
aresponses.add(
Expand All @@ -358,7 +378,8 @@ async def test_refresh(aresponses):

async with aiohttp.ClientSession() as session:
adguard = AdGuardHome("example.com", session=session)
await adguard.filtering.refresh(False)
await adguard.filtering.refresh(True)
await adguard.filtering.refresh(force=False)
await adguard.filtering.refresh(force=False, allowlist=True)
await adguard.filtering.refresh(force=True)
with pytest.raises(AdGuardHomeError):
await adguard.filtering.refresh(False)
await adguard.filtering.refresh(force=False)

0 comments on commit aa68f35

Please sign in to comment.