From aa68f359cd34f8f0fe47ad745624a01183e0f713 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Thu, 11 Mar 2021 17:31:11 +0100 Subject: [PATCH] Fix filter refresh handling --- src/adguardhome/filtering.py | 10 +++++++--- tests/test_filtering.py | 29 +++++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/adguardhome/filtering.py b/src/adguardhome/filtering.py index 4b484a3c..df760a13 100644 --- a/src/adguardhome/filtering.py +++ b/src/adguardhome/filtering.py @@ -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( diff --git a/tests/test_filtering.py b/tests/test_filtering.py index 21614624..887a062e 100644 --- a/tests/test_filtering.py +++ b/tests/test_filtering.py @@ -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( @@ -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)