From 19304cd1ae4e4ba4f6dcf4f1db14de1e4e70c250 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Mon, 10 Jan 2022 23:38:56 +0100 Subject: improve item list styling; add payload creation form; exend dialog mechanism --- test/unit/test_payload_create.py | 121 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 test/unit/test_payload_create.py (limited to 'test/unit/test_payload_create.py') diff --git a/test/unit/test_payload_create.py b/test/unit/test_payload_create.py new file mode 100644 index 0000000..cd08d43 --- /dev/null +++ b/test/unit/test_payload_create.py @@ -0,0 +1,121 @@ +# SPDX-License-Identifier: CC0-1.0 + +""" +Haketilo unit tests - using a form to create simple site payload +""" + +# 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 +import re +from selenium.webdriver.support.ui import WebDriverWait + +from ..extension_crafting import ExtraHTML +from ..script_loader import load_script +from .utils import clear_indexeddb, get_db_contents, sample_files + +broker_js = lambda: load_script('background/broadcast_broker.js') + ';start();' + +uuidv4_re = re.compile( + r'^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$', + re.IGNORECASE +) + +sample_patterns = ''' +http://example.com/*** + +https://*.example.org/**''' + +sample_form_data = { + 'identifier': 'someid', + 'long_name': 'Some Name', + 'description': 'blah blah blah', + 'patterns': sample_patterns, + 'script': sample_files['hello.js']['contents'] +} + +def fill_form_with_sample_data(execute_in_page, sample_data_override={}, + form_ctx='form_ctx'): + form_data = sample_form_data.copy() + form_data.update(sample_data_override) + execute_in_page( + f''' + for (const [key, value] of Object.entries(arguments[0])) + {form_ctx}[key].value = value; + ''', + form_data) + +@pytest.mark.ext_data({ + 'background_script': broker_js, + 'extra_html': ExtraHTML('html/payload_create.html', {}), + 'navigate_to': 'html/payload_create.html' +}) +@pytest.mark.usefixtures('webextension') +def test_payload_create(driver, execute_in_page): + """ + A test case of creating a simple payload using a form. + """ + clear_indexeddb(execute_in_page) + execute_in_page(load_script('html/payload_create.js')) + + create_but, main_div = execute_in_page( + ''' + const form_ctx = payload_create_form(); + document.body.append(form_ctx.main_div); + returnval([form_ctx.create_but, form_ctx.main_div]); + ''') + + fill_form_with_sample_data(execute_in_page) + + create_but.click() + + def success_reported(driver): + return 'Successfully saved payload' in main_div.text + + WebDriverWait(driver, 10).until(success_reported) + + db_contents = get_db_contents(execute_in_page) + + assert uuidv4_re.match(db_contents['resources'][0]['uuid']) + assert db_contents['resources'] == [{ + 'source_name': 'local-someid', + 'source_copyright': [], + 'type': 'resource', + 'identifier': 'local-someid', + 'long_name': 'Some Name', + 'uuid': db_contents['resources'][0]['uuid'], + 'version': [1], + 'description': 'blah blah blah', + 'dependencies': [], + 'scripts': [{ + 'file': 'payload.js', + 'hash_key': sample_files['hello.js']['hash_key'] + }] + }] + + assert uuidv4_re.match(db_contents['mappings'][0]['uuid']) + assert db_contents['mappings'] == [{ + 'source_name': 'local-someid', + 'source_copyright': [], + 'type': 'mapping', + 'identifier': 'local-someid', + 'long_name': 'Some Name', + 'uuid': db_contents['mappings'][0]['uuid'], + 'version': [1], + 'description': 'blah blah blah', + 'payloads': { + 'http://example.com/***': {'identifier': 'local-someid'}, + 'https://*.example.org/**': {'identifier': 'local-someid'} + } + }] -- cgit v1.2.3