summaryrefslogtreecommitdiff
path: root/test/unit/test_install.py
diff options
context:
space:
mode:
Diffstat (limited to 'test/unit/test_install.py')
-rw-r--r--test/unit/test_install.py429
1 files changed, 429 insertions, 0 deletions
diff --git a/test/unit/test_install.py b/test/unit/test_install.py
new file mode 100644
index 0000000..cb8fe36
--- /dev/null
+++ b/test/unit/test_install.py
@@ -0,0 +1,429 @@
+# SPDX-License-Identifier: CC0-1.0
+
+"""
+Haketilo unit tests - item installation dialog
+"""
+
+# 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
+import json
+from selenium.webdriver.support.ui import WebDriverWait
+
+from ..extension_crafting import ExtraHTML
+from ..script_loader import load_script
+from .utils import *
+
+def content_script():
+ script = load_script('content/repo_query_cacher.js')
+ return f'{script}; {tab_id_asker}; start();'
+
+def background_script():
+ script = load_script('background/broadcast_broker.js',
+ '#IMPORT background/CORS_bypass_server.js')
+ return f'{script}; {tab_id_responder}; start(); CORS_bypass_server.start();'
+
+def setup_view(driver, execute_in_page):
+ tab_id = run_content_script_in_new_window(driver, 'https://gotmyowndoma.in')
+
+ execute_in_page(load_script('html/install.js'))
+ container_ids, containers_objects = execute_in_page(
+ '''
+ const cb_calls = [];
+ const install_view = new InstallView(arguments[0],
+ () => cb_calls.push("show"),
+ () => cb_calls.push("hide"));
+ document.body.append(install_view.main_div);
+ const ets = () => install_view.item_entries;
+ const shw = slice => [cb_calls.slice(slice || 0), install_view.shown];
+ returnval([container_ids, container_ids.map(cid => install_view[cid])]);
+ ''',
+ tab_id)
+
+ containers = dict(zip(container_ids, containers_objects))
+
+ def assert_container_displayed(container_id):
+ for cid, cobj in zip(container_ids, containers_objects):
+ assert (cid == container_id) == cobj.is_displayed()
+
+ return containers, assert_container_displayed
+
+install_ext_data = {
+ 'content_script': content_script,
+ 'background_script': background_script,
+ 'extra_html': ExtraHTML('html/install.html', {}),
+ 'navigate_to': 'html/install.html'
+}
+
+@pytest.mark.ext_data(install_ext_data)
+@pytest.mark.usefixtures('webextension')
+@pytest.mark.parametrize('complex_variant', [False, True])
+def test_install_normal_usage(driver, execute_in_page, complex_variant):
+ """
+ Test of the normal package installation procedure with one mapping and,
+ depending on parameter, one or many resources.
+ """
+ containers, assert_container_displayed = setup_view(driver, execute_in_page)
+
+ assert execute_in_page('returnval(shw());') == [[], False]
+
+ if complex_variant:
+ # The resource/mapping others depend on.
+ root_id = 'abcd-defg-ghij'
+ root_resource_id = f'resource_{root_id}'
+ root_mapping_id = f'mapping_{root_id}'
+ # Those ids are used to check the alphabetical ordering.
+ resource_ids = [f'resource_{letters}' for letters in (
+ 'a', 'abcd', root_id, 'b', 'c',
+ 'd', 'defg', 'e', 'f',
+ 'g', 'ghij', 'h', 'i', 'j'
+ )]
+ files_count = 9
+ else:
+ root_resource_id = f'resource_a'
+ root_mapping_id = f'mapping_a'
+ resource_ids = [root_resource_id]
+ files_count = 0
+
+ # Preview the installation of a resource, show resource's details, close
+ # the details and cancel installation.
+ execute_in_page('returnval(install_view.show(...arguments));',
+ 'https://hydril.la/', 'resource', root_resource_id)
+
+ assert execute_in_page('returnval(shw());') == [['show'], True]
+ assert f'{root_resource_id}-2021.11.11-1'\
+ in containers['install_preview'].text
+ assert_container_displayed('install_preview')
+
+ entries = execute_in_page('returnval(ets().map(e => e.main_li.innerText));')
+ assert len(entries) == len(resource_ids)
+ # Verify alphabetical ordering.
+ assert all([id in text for id, text in zip(resource_ids, entries)])
+
+ assert not execute_in_page('returnval(ets()[0].old_ver);').is_displayed()
+ execute_in_page('returnval(ets()[0].details_but);').click()
+ assert 'resource_a' in containers['resource_preview_container'].text
+ assert_container_displayed('resource_preview_container')
+
+ execute_in_page('returnval(install_view.resource_back_but);').click()
+ assert_container_displayed('install_preview')
+
+ assert execute_in_page('returnval(shw());') == [['show'], True]
+ execute_in_page('returnval(install_view.cancel_but);').click()
+ assert execute_in_page('returnval(shw());') == [['show', 'hide'], False]
+
+ # Preview the installation of a mapping and a resource, show mapping's
+ # details, close the details and commit the installation.
+ execute_in_page('returnval(install_view.show(...arguments));',
+ 'https://hydril.la/', 'mapping',
+ root_mapping_id, [2022, 5, 10])
+
+ assert execute_in_page('returnval(shw(2));') == [['show'], True]
+ assert_container_displayed('install_preview')
+
+ entries = execute_in_page('returnval(ets().map(e => e.main_li.innerText));')
+ assert len(entries) == len(resource_ids) + 1
+ assert f'{root_mapping_id}-2022.5.10' in entries[0]
+ # Verify alphabetical ordering.
+ assert all([id in text for id, text in zip(resource_ids, entries[1:])])
+
+ assert not execute_in_page('returnval(ets()[0].old_ver);').is_displayed()
+ execute_in_page('returnval(ets()[0].details_but);').click()
+ assert root_mapping_id in containers['mapping_preview_container'].text
+ assert_container_displayed('mapping_preview_container')
+
+ execute_in_page('returnval(install_view.mapping_back_but);').click()
+ assert_container_displayed('install_preview')
+
+ execute_in_page('returnval(install_view.install_but);').click()
+ installed = lambda d: 'ly installed!' in containers['dialog_container'].text
+ WebDriverWait(driver, 10000).until(installed)
+
+ assert execute_in_page('returnval(shw(2));') == [['show'], True]
+ execute_in_page('returnval(install_view.dialog_ctx.ok_but);').click()
+ assert execute_in_page('returnval(shw(2));') == [['show', 'hide'], False]
+
+ # Verify the install
+ db_contents = get_db_contents(execute_in_page)
+ for item_type, ids in \
+ [('mapping', {root_mapping_id}), ('resource', set(resource_ids))]:
+ assert set([it['identifier'] for it in db_contents[item_type]]) == ids
+
+ assert all([len(db_contents[store]) == files_count
+ for store in ('files', 'file_uses')])
+
+ # Update the installed mapping to a newer version.
+ execute_in_page('returnval(install_view.show(...arguments));',
+ 'https://hydril.la/', 'mapping', root_mapping_id)
+ assert execute_in_page('returnval(shw(4));') == [['show'], True]
+ # resources are already in the newest versions, hence they should not appear
+ # in the install preview list.
+ assert execute_in_page('returnval(ets().length);') == 1
+ # Mapping's version update information should be displayed.
+ assert execute_in_page('returnval(ets()[0].old_ver);').is_displayed()
+ execute_in_page('returnval(install_view.install_but);').click()
+
+ WebDriverWait(driver, 10).until(installed)
+
+ assert execute_in_page('returnval(shw(4));') == [['show'], True]
+ execute_in_page('returnval(install_view.dialog_ctx.ok_but);').click()
+ assert execute_in_page('returnval(shw(4));') == [['show', 'hide'], False]
+
+ # Verify the newer version install.
+ old_db_contents, db_contents = db_contents, get_db_contents(execute_in_page)
+ old_db_contents['mapping'][0]['version'][-1] += 1
+ assert db_contents['mapping'] == old_db_contents['mapping']
+
+ # All items are up to date - verify dialog is instead shown in this case.
+ execute_in_page('install_view.show(...arguments);',
+ 'https://hydril.la/', 'mapping', root_mapping_id)
+ assert execute_in_page('returnval(shw(6));') == [['show'], True]
+ assert_container_displayed('dialog_container')
+ assert 'Nothing to do - packages already installed.' \
+ in containers['dialog_container'].text
+ execute_in_page('returnval(install_view.dialog_ctx.ok_but);').click()
+ assert execute_in_page('returnval(shw(6));') == [['show', 'hide'], False]
+
+@pytest.mark.ext_data(install_ext_data)
+@pytest.mark.usefixtures('webextension')
+@pytest.mark.parametrize('message', [
+ 'fetching_data',
+ 'failure_to_communicate_sendmessage',
+ 'HTTP_code_item',
+ 'invalid_JSON',
+ 'newer_API_version',
+ 'invalid_API_version',
+ 'indexeddb_error_item',
+ 'installing',
+ 'indexeddb_error_file_uses',
+ 'failure_to_communicate_fetch',
+ 'HTTP_code_file',
+ 'not_valid_text',
+ 'sha256_mismatch',
+ 'indexeddb_error_write'
+])
+def test_install_dialogs(driver, execute_in_page, message):
+ """
+ Test of various error and loading messages used in install view.
+ """
+ containers, assert_container_displayed = setup_view(driver, execute_in_page)
+
+ def dlg_buts():
+ return execute_in_page(
+ '''{
+ const dlg = install_view.dialog_ctx;
+ const ids = ['ask_buts', 'conf_buts'];
+ returnval(ids.filter(id => !dlg[id].classList.contains("hide")));
+ }''')
+
+ def dialog_txt():
+ return execute_in_page(
+ 'returnval(install_view.dialog_ctx.msg.textContent);'
+ )
+
+ def assert_dlg(awaited_buttons, expected_msg, hides_install_view=True,
+ button_to_click='ok_but'):
+ WebDriverWait(driver, 10).until(lambda d: dlg_buts() == awaited_buttons)
+
+ assert expected_msg == dialog_txt()
+
+ execute_in_page(
+ f'returnval(install_view.dialog_ctx.{button_to_click});'
+ ).click()
+
+ if hides_install_view:
+ assert execute_in_page('returnval(shw());') == \
+ [['show', 'hide'], False]
+
+ if message == 'fetching_data':
+ execute_in_page(
+ '''
+ browser.tabs.sendMessage = () => new Promise(cb => {});
+ install_view.show(...arguments);
+ ''',
+ 'https://hydril.la/', 'mapping', 'mapping_a')
+
+ assert dlg_buts() == []
+ assert dialog_txt() == 'Fetching data from repository...'
+ elif message == 'failure_to_communicate_sendmessage':
+ execute_in_page(
+ '''
+ browser.tabs.sendMessage = () => Promise.resolve({error: "sth"});
+ install_view.show(...arguments);
+ ''',
+ 'https://hydril.la/', 'mapping', 'mapping_a')
+
+ assert_dlg(['conf_buts'], 'Failure to communicate with repository :(')
+ elif message == 'HTTP_code_item':
+ execute_in_page(
+ '''
+ const response = {ok: false, status: 404};
+ browser.tabs.sendMessage = () => Promise.resolve(response);
+ install_view.show(...arguments);
+ ''',
+ 'https://hydril.la/', 'mapping', 'mapping_a')
+
+ assert_dlg(['conf_buts'], 'Repository sent HTTP code 404 :(')
+ elif message == 'invalid_JSON':
+ execute_in_page(
+ '''
+ const response = {ok: true, status: 200, error_json: "sth"};
+ browser.tabs.sendMessage = () => Promise.resolve(response);
+ install_view.show(...arguments);
+ ''',
+ 'https://hydril.la/', 'mapping', 'mapping_a')
+
+ assert_dlg(['conf_buts'], "Repository's response is not valid JSON :(")
+ elif message == 'newer_API_version':
+ execute_in_page(
+ '''
+ const response = {
+ ok: true,
+ status: 200,
+ json: {api_schema_version: [99, 99]}
+ };
+ browser.tabs.sendMessage = () => Promise.resolve(response);
+ install_view.show(...arguments);
+ ''',
+ 'https://hydril.la/', 'mapping', 'somemapping', [2, 1])
+
+ assert_dlg(['conf_buts'],
+ 'Mapping somemapping-2.1 was served using unsupported Hydrilla API version (99.99). You might need to update Haketilo.')
+ elif message == 'invalid_API_version':
+ execute_in_page(
+ '''
+ const response = {
+ ok: true,
+ status: 200,
+ /* API version here is not an array as it should be. */
+ json: {api_schema_version: 123}
+ };
+ browser.tabs.sendMessage = () => Promise.resolve(response);
+ install_view.show(...arguments);
+ ''',
+ 'https://hydril.la/', 'resource', 'someresource')
+
+ assert_dlg(['conf_buts'],
+ 'Resource someresource was served using unsupported Hydrilla API version. You might need to update Haketilo.')
+ elif message == 'indexeddb_error_item':
+ execute_in_page(
+ '''
+ haketilodb.idb_get = () => {throw "some error";};
+ install_view.show(...arguments);
+ ''',
+ 'https://hydril.la/', 'mapping', 'mapping_a')
+
+ assert_dlg(['conf_buts'],
+ "Error accessing Haketilo's internal database :(")
+ elif message == 'installing':
+ execute_in_page(
+ '''
+ haketilodb.save_items = () => new Promise(() => {});
+ returnval(install_view.show(...arguments));
+ ''',
+ 'https://hydril.la/', 'mapping', 'mapping_b')
+
+ execute_in_page('returnval(install_view.install_but);').click()
+
+ assert dlg_buts() == []
+ assert dialog_txt() == 'Installing...'
+ elif message == 'indexeddb_error_file_uses':
+ execute_in_page(
+ '''
+ const old_idb_get = haketilodb.idb_get;
+ haketilodb.idb_get = function(transaction, store_name, identifier) {
+ if (store_name === "file_uses")
+ throw "some error";
+ return old_idb_get(...arguments);
+ }
+ returnval(install_view.show(...arguments));
+ ''',
+ 'https://hydril.la/', 'mapping', 'mapping_b')
+
+ execute_in_page('returnval(install_view.install_but);').click()
+
+ assert_dlg(['conf_buts'],
+ "Error accessing Haketilo's internal database :(")
+ elif message == 'failure_to_communicate_fetch':
+ execute_in_page(
+ '''
+ fetch = () => {throw "some error";};
+ returnval(install_view.show(...arguments));
+ ''',
+ 'https://hydril.la/', 'mapping', 'mapping_b')
+
+ execute_in_page('returnval(install_view.install_but);').click()
+
+ assert_dlg(['conf_buts'],
+ 'Failure to communicate with repository :(')
+ elif message == 'HTTP_code_file':
+ execute_in_page(
+ '''
+ fetch = () => Promise.resolve({ok: false, status: 400});
+ returnval(install_view.show(...arguments));
+ ''',
+ 'https://hydril.la/', 'mapping', 'mapping_b')
+
+ execute_in_page('returnval(install_view.install_but);').click()
+
+ assert_dlg(['conf_buts'], 'Repository sent HTTP code 400 :(')
+ elif message == 'not_valid_text':
+ execute_in_page(
+ '''
+ const err = () => {throw "some error";};
+ fetch = () => Promise.resolve({ok: true, status: 200, text: err});
+ returnval(install_view.show(...arguments));
+ ''',
+ 'https://hydril.la/', 'mapping', 'mapping_b')
+
+ execute_in_page('returnval(install_view.install_but);').click()
+
+ assert_dlg(['conf_buts'], "Repository's response is not valid text :(")
+ elif message == 'sha256_mismatch':
+ execute_in_page(
+ '''
+ let old_fetch = fetch, url_used;
+ fetch = async function(url) {
+ url_used = url;
+ const response = await old_fetch(...arguments);
+ const text = () => response.text().then(t => t + ":d");
+ return {ok: response.ok, status: response.status, text};
+ }
+ returnval(install_view.show(...arguments));
+ ''',
+ 'https://hydril.la/', 'mapping', 'mapping_b')
+
+ execute_in_page('returnval(install_view.install_but);').click()
+
+ get_url_used = lambda d: execute_in_page('returnval(url_used);')
+ url_used = WebDriverWait(driver, 10).until(get_url_used)
+ print ((url_used,))
+
+ assert dlg_buts() == ['conf_buts']
+ assert dialog_txt() == \
+ f'{url_used} served a file with different SHA256 cryptographic sum :('
+ elif message == 'indexeddb_error_write':
+ execute_in_page(
+ '''
+ haketilodb.save_items = () => {throw "some error";};
+ returnval(install_view.show(...arguments));
+ ''',
+ 'https://hydril.la/', 'mapping', 'mapping_b')
+
+ execute_in_page('returnval(install_view.install_but);').click()
+
+ assert_dlg(['conf_buts'],
+ "Error writing to Haketilo's internal database :(")
+ else:
+ raise Exception('made a typo in test function params?')