From 4c6a2323d90e9321ec2b78e226167b3013ea69ab Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Sat, 29 Jan 2022 00:03:51 +0100 Subject: make Haketilo buildable again (for Mozilla) How cool it is to throw away 5755 lines of code... --- test/conftest.py | 174 +++++++++++++++++++++++++++++++ test/data/pages/scripts_to_block_1.html | 1 + test/test_integration.py | 29 ++++++ test/unit/conftest.py | 163 ----------------------------- test/unit/test_basic.py | 4 +- test/unit/test_broadcast.py | 3 +- test/unit/test_content.py | 8 +- test/unit/test_indexeddb.py | 2 +- test/unit/test_patterns_query_manager.py | 19 +++- test/unit/test_repo_query_cacher.py | 11 +- 10 files changed, 235 insertions(+), 179 deletions(-) create mode 100644 test/conftest.py create mode 100644 test/test_integration.py delete mode 100644 test/unit/conftest.py (limited to 'test') diff --git a/test/conftest.py b/test/conftest.py new file mode 100644 index 0000000..4eea714 --- /dev/null +++ b/test/conftest.py @@ -0,0 +1,174 @@ +# SPDX-License-Identifier: GPL-3.0-or-later + +""" +Common fixtures for Haketilo unit tests +""" + +# This file is part of Haketilo. +# +# Copyright (C) 2021 Wojtek Kosior +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# 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 +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# I, Wojtek Kosior, thereby promise not to sue for violation of this file's +# license. Although I request that you do not make use of this code in a +# proprietary program, I am not going to enforce this in court. + +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 .misc_constants import here + +@pytest.fixture(scope="session") +def proxy(): + httpd = do_an_internet() + yield httpd + httpd.shutdown() + +@pytest.fixture(scope="session") +def _driver(proxy): + with firefox_safe_mode() as driver: + yield driver + driver.quit() + +def close_all_but_one_window(driver): + while len(driver.window_handles) > 1: + driver.switch_to.window(driver.window_handles[-1]) + driver.close() + driver.switch_to.window(driver.window_handles[0]) + +@pytest.fixture() +def driver(_driver, request): + nav_target = request.node.get_closest_marker('get_page') + close_all_but_one_window(_driver) + _driver.get(nav_target.args[0] if nav_target else 'about:blank') + _driver.implicitly_wait(0) + yield _driver + +@pytest.fixture() +def webextension(driver, request): + ext_data = request.node.get_closest_marker('ext_data') + if ext_data is None: + raise Exception('"webextension" fixture requires "ext_data" marker to be set') + ext_data = ext_data.args[0].copy() + + navigate_to = ext_data.get('navigate_to') + if navigate_to is not None: + del ext_data['navigate_to'] + + driver.get('https://gotmyowndoma.in/') + ext_path = make_extension(Path(driver.firefox_profile.path), **ext_data) + addon_id = driver.install_addon(str(ext_path), temporary=True) + WebDriverWait(driver, 10).until( + EC.url_matches('^moz-extension://.*') + ) + + if navigate_to is not None: + testpage_url = driver.execute_script('return window.location.href;') + driver.get(testpage_url.replace('testpage.html', navigate_to)) + + yield + + close_all_but_one_window(driver) + driver.get('https://gotmyowndoma.in/') + 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 + * separately-loaded scripts to be able to access global variables defined + * before, including those declared with `const` or `let`. To achieve that, we + * run our scripts by injecting them into the page with a 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 +# +# 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/conftest.py b/test/unit/conftest.py deleted file mode 100644 index a3064f1..0000000 --- a/test/unit/conftest.py +++ /dev/null @@ -1,163 +0,0 @@ -# SPDX-License-Identifier: GPL-3.0-or-later - -""" -Common fixtures for Haketilo unit tests -""" - -# This file is part of Haketilo. -# -# Copyright (C) 2021 Wojtek Kosior -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# 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 -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -# -# I, Wojtek Kosior, thereby promise not to sue for violation of this file's -# license. Although I request that you do not make use of this code in a -# proprietary program, I am not going to enforce this in court. - -import pytest -from pathlib import Path -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 - -@pytest.fixture(scope="package") -def proxy(): - httpd = do_an_internet() - yield httpd - httpd.shutdown() - -@pytest.fixture(scope="package") -def _driver(proxy): - with firefox_safe_mode() as driver: - yield driver - driver.quit() - -def close_all_but_one_window(driver): - while len(driver.window_handles) > 1: - driver.switch_to.window(driver.window_handles[-1]) - driver.close() - driver.switch_to.window(driver.window_handles[0]) - -@pytest.fixture() -def driver(_driver, request): - nav_target = request.node.get_closest_marker('get_page') - close_all_but_one_window(_driver) - _driver.get(nav_target.args[0] if nav_target else 'about:blank') - _driver.implicitly_wait(0) - yield _driver - -@pytest.fixture() -def webextension(driver, request): - ext_data = request.node.get_closest_marker('ext_data') - if ext_data is None: - raise Exception('"webextension" fixture requires "ext_data" marker to be set') - ext_data = ext_data.args[0].copy() - - navigate_to = ext_data.get('navigate_to') - if navigate_to is not None: - del ext_data['navigate_to'] - - driver.get('https://gotmyowndoma.in/') - ext_path = make_extension(Path(driver.firefox_profile.path), **ext_data) - addon_id = driver.install_addon(str(ext_path), temporary=True) - WebDriverWait(driver, 10).until( - EC.url_matches('^moz-extension://.*') - ) - - if navigate_to is not None: - testpage_url = driver.execute_script('return window.location.href;') - driver.get(testpage_url.replace('testpage.html', navigate_to)) - - yield - - close_all_but_one_window(driver) - driver.get('https://gotmyowndoma.in/') - driver.uninstall_addon(addon_id) - ext_path.unlink() - -script_injector_script = '''\ -/* - * Selenium by default executes scripts in some weird one-time context. We want - * separately-loaded scripts to be able to access global variables defined - * before, including those declared with `const` or `let`. To achieve that, we - * run our scripts by injecting them into the page with a