From 6d1764e7321ee6e517d37494e271abfc18c177c0 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Sat, 7 May 2016 23:31:35 +0200 Subject: [PATCH] Clear blocked hosts on start correctly --- qutebrowser/browser/adblock.py | 1 + tests/unit/browser/test_adblock.py | 38 +++++++++++++++++------------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/qutebrowser/browser/adblock.py b/qutebrowser/browser/adblock.py index 55218bf27..20c096391 100644 --- a/qutebrowser/browser/adblock.py +++ b/qutebrowser/browser/adblock.py @@ -116,6 +116,7 @@ class HostBlocker: self._local_hosts_file = None else: self._local_hosts_file = os.path.join(data_dir, 'blocked-hosts') + self.on_config_changed() config_dir = standarddir.config() if config_dir is None: diff --git a/tests/unit/browser/test_adblock.py b/tests/unit/browser/test_adblock.py index a5423b238..49605b4a6 100644 --- a/tests/unit/browser/test_adblock.py +++ b/tests/unit/browser/test_adblock.py @@ -367,22 +367,28 @@ def test_blocking_with_whitelist(config_stub, basedir, download_stub, assert_urls(host_blocker) -# XXX Intended behavior ? -# User runs qutebrowser with host-blocking enabled -# A blocklist is present in host-block-lists and blocked_hosts is populated -# -# User quits qutebrowser, empties host-block-lists from his config -# User restarts qutebrowser, does adblock-update (which will return None) -# read_hosts still returns hosts from unchanged blocked_hosts file -# -# Is this intended behavior or shouldn't on_config_changed be also called -# during HostBlocker instance init to avoid this ? -# -# As a comparison, -# host-block-lists is emptied with qutebrowser running -# on_config_changed slot is activated -# blocked_hosts is emptied -# read_hosts doesn't return any host, as expected +def test_config_change_initial(config_stub, basedir, download_stub, + data_tmpdir, tmpdir): + """Test the following scenario: + + - A blocklist is present in host-block-lists and blocked_hosts is populated + - User quits qutebrowser, empties host-block-lists from his config + - User restarts qutebrowser, does adblock-update + """ + create_blocklist(tmpdir, blocked_hosts=BLOCKLIST_HOSTS, + name='blocked-hosts', line_format='one_per_line') + config_stub.data = { + 'content': { + 'host-block-lists': None, + 'host-blocking-enabled': True, + 'host-blocking-whitelist': None, + } + } + host_blocker = adblock.HostBlocker() + host_blocker.read_hosts() + for str_url in URLS_TO_CHECK: + assert not host_blocker.is_blocked(QUrl(str_url)) + def test_config_change(config_stub, basedir, download_stub, data_tmpdir, tmpdir):