diff options
author | Wojtek Kosior <koszko@koszko.org> | 2022-01-29 00:03:51 +0100 |
---|---|---|
committer | Wojtek Kosior <koszko@koszko.org> | 2022-01-29 00:04:44 +0100 |
commit | 4c6a2323d90e9321ec2b78e226167b3013ea69ab (patch) | |
tree | 682ab3abd53a68d28d04f0470766699dcadd7294 /test | |
parent | ea9df6c7688613783ca114f0f11c6f6baf30036b (diff) | |
download | browser-extension-4c6a2323d90e9321ec2b78e226167b3013ea69ab.tar.gz browser-extension-4c6a2323d90e9321ec2b78e226167b3013ea69ab.zip |
make Haketilo buildable again (for Mozilla)
How cool it is to throw away 5755 lines of code...
Diffstat (limited to 'test')
-rw-r--r-- | test/conftest.py (renamed from test/unit/conftest.py) | 23 | ||||
-rw-r--r-- | test/data/pages/scripts_to_block_1.html | 1 | ||||
-rw-r--r-- | test/test_integration.py | 29 | ||||
-rw-r--r-- | test/unit/test_basic.py | 4 | ||||
-rw-r--r-- | test/unit/test_broadcast.py | 3 | ||||
-rw-r--r-- | test/unit/test_content.py | 8 | ||||
-rw-r--r-- | test/unit/test_indexeddb.py | 2 | ||||
-rw-r--r-- | test/unit/test_patterns_query_manager.py | 19 | ||||
-rw-r--r-- | test/unit/test_repo_query_cacher.py | 11 |
9 files changed, 78 insertions, 22 deletions
diff --git a/test/unit/conftest.py b/test/conftest.py index a3064f1..4eea714 100644 --- a/test/unit/conftest.py +++ b/test/conftest.py @@ -27,22 +27,24 @@ Common fixtures for Haketilo unit tests import pytest from pathlib import Path +from tempfile import TemporaryDirectory from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC -from ..profiles import firefox_safe_mode -from ..server import do_an_internet -from ..extension_crafting import make_extension -from ..world_wide_library import start_serving_script, dump_scripts +from .profiles import firefox_safe_mode +from .server import do_an_internet +from .extension_crafting import make_extension +from .world_wide_library import start_serving_script, dump_scripts +from .misc_constants import here -@pytest.fixture(scope="package") +@pytest.fixture(scope="session") def proxy(): httpd = do_an_internet() yield httpd httpd.shutdown() -@pytest.fixture(scope="package") +@pytest.fixture(scope="session") def _driver(proxy): with firefox_safe_mode() as driver: yield driver @@ -91,6 +93,15 @@ def webextension(driver, request): driver.uninstall_addon(addon_id) ext_path.unlink() +@pytest.fixture() +def haketilo(driver): + addon_id = driver.install_addon(str(here.parent / 'mozilla-build.zip'), + temporary=True) + + yield + + driver.uninstall_addon(addon_id) + script_injector_script = '''\ /* * Selenium by default executes scripts in some weird one-time context. We want diff --git a/test/data/pages/scripts_to_block_1.html b/test/data/pages/scripts_to_block_1.html index 6d868dd..1aa49ee 100644 --- a/test/data/pages/scripts_to_block_1.html +++ b/test/data/pages/scripts_to_block_1.html @@ -19,6 +19,7 @@ --> <html> <head> + <meta name="charset" value="latin1"> <script> window.__run = [...(window.__run || []), 'inline']; </script> diff --git a/test/test_integration.py b/test/test_integration.py new file mode 100644 index 0000000..db5ae43 --- /dev/null +++ b/test/test_integration.py @@ -0,0 +1,29 @@ +# SPDX-License-Identifier: CC0-1.0 + +""" +Haketilo integration tests +""" + +# This file is part of Haketilo +# +# Copyright (C) 2022 Wojtek Kosior <koszko@koszko.org> +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the CC0 1.0 Universal License as published by +# the Creative Commons Corporation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# CC0 1.0 Universal License for more details. + +import pytest +from selenium.webdriver.support.ui import WebDriverWait + +@pytest.mark.usefixtures('haketilo') +def test_integration(driver): + #from time import sleep + #sleep(100000) + + # TODO!!! + pass diff --git a/test/unit/test_basic.py b/test/unit/test_basic.py index 5f42f5d..6ec54cc 100644 --- a/test/unit/test_basic.py +++ b/test/unit/test_basic.py @@ -40,9 +40,9 @@ def test_script_loader(execute_in_page): A trivial test case that verifies Haketilo's .js files can be properly loaded into a test page together with their dependencies. """ - execute_in_page(load_script('common/stored_types.js')) + execute_in_page(load_script('common/indexeddb.js')) - assert execute_in_page('returnval(TYPE_PREFIX.VAR);') == '_' + assert 'mapping' in execute_in_page('returnval(stores.map(s => s[0]));') @pytest.mark.ext_data({}) @pytest.mark.usefixtures('webextension') diff --git a/test/unit/test_broadcast.py b/test/unit/test_broadcast.py index 7de6c80..7c2c051 100644 --- a/test/unit/test_broadcast.py +++ b/test/unit/test_broadcast.py @@ -18,6 +18,7 @@ Haketilo unit tests - message broadcasting # CC0 1.0 Universal License for more details. import pytest +from selenium.webdriver.support.ui import WebDriverWait from ..script_loader import load_script from .utils import broker_js @@ -55,8 +56,8 @@ def test_broadcast(driver, execute_in_page, wait_elem_text): window.open(window.location.href, "_blank"); window.open(window.location.href, "_blank"); ''') + WebDriverWait(driver, 10).until(lambda d: len(d.window_handles) == 3) windows = [*driver.window_handles] - assert len(windows) == 3 # Let's first test if a simple message can be successfully broadcasted driver.switch_to.window(windows[0]) diff --git a/test/unit/test_content.py b/test/unit/test_content.py index 35ab027..8220160 100644 --- a/test/unit/test_content.py +++ b/test/unit/test_content.py @@ -33,7 +33,7 @@ dynamic_script = \ '''; this.haketilo_secret = "abracadabra"; this.haketilo_pattern_tree = {}; - this.haketilo_defualt_allow = false; + this.haketilo_default_allow = false; if (this.haketilo_content_script_main) this.haketilo_content_script_main(); @@ -69,7 +69,7 @@ content_script = \ async function mock_payload_ok([type, res_id]) { if (type === "indexeddb_files") - return [1, 2].map(n => `window.haketilo_injected_${n} = ${n}${n};`); + return {files: [1, 2].map(n => `window.hak_injected_${n} = ${n};`)}; } if (/payload_error/.test(document.URL)) { @@ -162,7 +162,7 @@ def test_content_unprivileged_page(driver, execute_in_page, target1, target2): def vars_made_by_payload(driver): vars_values = driver.execute_script( - 'return [1, 2].map(n => window[`haketilo_injected_${n}`]);' + 'return [1, 2].map(n => window[`hak_injected_${n}`]);' ) if vars_values != [None, None]: return vars_values @@ -174,7 +174,7 @@ def test_content_unprivileged_page(driver, execute_in_page, target1, target2): } elif target2 == 'payload_ok': vars_values = WebDriverWait(driver, 10).until(vars_made_by_payload) - assert vars_values == [11, 22] + assert vars_values == [1, 2] @pytest.mark.ext_data({'content_script': content_script}) @pytest.mark.usefixtures('webextension') diff --git a/test/unit/test_indexeddb.py b/test/unit/test_indexeddb.py index b320cff..550b923 100644 --- a/test/unit/test_indexeddb.py +++ b/test/unit/test_indexeddb.py @@ -318,8 +318,8 @@ def test_haketilodb_track(driver, execute_in_page, wait_elem_text): # will be used to make changes to IndexedDB and window 0 to "track" those # changes. driver.execute_script('window.open(window.location.href, "_blank");') + WebDriverWait(driver, 10).until(lambda d: len(d.window_handles) == 2) windows = [*driver.window_handles] - assert len(windows) == 2 # Create elements that will have tracked data inserted under them. driver.switch_to.window(windows[0]) diff --git a/test/unit/test_patterns_query_manager.py b/test/unit/test_patterns_query_manager.py index c6ebb81..5daf3a0 100644 --- a/test/unit/test_patterns_query_manager.py +++ b/test/unit/test_patterns_query_manager.py @@ -150,14 +150,14 @@ def test_pqm_tree_building(driver, execute_in_page): all([m['identifier'] in last_script for m in sample_mappings])) execute_in_page( - ''' + '''{ const new_setting_val = {name: "default_allow", value: false}; settingchange({key: "default_allow", new_val: new_setting_val}); for (const mapping of arguments[0]) mappingchange({key: mapping.identifier, new_val: mapping}); for (const blocking of arguments[1]) blockingchange({key: blocking.pattern, new_val: blocking}); - ''', + }''', sample_mappings[2:], sample_blocking[2:]) WebDriverWait(driver, 10).until(condition_all_added) @@ -201,6 +201,19 @@ def test_pqm_tree_building(driver, execute_in_page): WebDriverWait(driver, 10).until(condition_all_removed) + def condition_default_allowed_again(driver): + content_script = execute_in_page('returnval(last_script);') + cs_values = get_content_script_values(driver, content_script) + return cs_values['haketilo_default_allow'] == True + + execute_in_page( + '''{ + const new_setting_val = {name: "default_allow", value: true}; + settingchange({key: "default_allow", new_val: new_setting_val}); + }''') + + WebDriverWait(driver, 10).until(condition_default_allowed_again) + content_js = ''' let already_run = false; this.haketilo_content_script_main = function() { @@ -229,8 +242,8 @@ def test_pqm_script_injection(driver, execute_in_page): # Let's open a normal page in a second window. Window 0 will be used to make # changed to IndexedDB and window 1 to test the working of content scripts. driver.execute_script('window.open("about:blank", "_blank");') + WebDriverWait(driver, 10).until(lambda d: len(d.window_handles) == 2) windows = [*driver.window_handles] - assert len(windows) == 2 def run_content_script(): driver.switch_to.window(windows[1]) diff --git a/test/unit/test_repo_query_cacher.py b/test/unit/test_repo_query_cacher.py index b1ce4c8..5fbc5cd 100644 --- a/test/unit/test_repo_query_cacher.py +++ b/test/unit/test_repo_query_cacher.py @@ -65,18 +65,19 @@ def run_content_script_in_new_window(driver, url): Open the provided url in a new tab, find its tab id and return it, with current window changed back to the initial one. """ - initial_handle = driver.current_window_handle - handles = driver.window_handles + handle0 = driver.current_window_handle + initial_handles = [*driver.window_handles] driver.execute_script('window.open(arguments[0], "_blank");', url) - WebDriverWait(driver, 10).until(lambda d: d.window_handles is not handles) - new_handle = [h for h in driver.window_handles if h not in handles][0] + window_added = lambda d: set(d.window_handles) != set(initial_handles) + WebDriverWait(driver, 10).until(window_added) + new_handle = [*set(driver.window_handles).difference(initial_handles)][0] driver.switch_to.window(new_handle) get_tab_id = lambda d: d.execute_script('return window.haketilo_tab;') tab_id = WebDriverWait(driver, 10).until(get_tab_id) - driver.switch_to.window(initial_handle) + driver.switch_to.window(handle0) return tab_id @pytest.mark.ext_data({ |