# SPDX-License-Identifier: CC0-1.0 """ Haketilo unit tests - displaying resources and mappings details """ # 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 from selenium.common.exceptions import NoSuchWindowException from ..extension_crafting import ExtraHTML from ..script_loader import load_script from .utils import * @pytest.mark.ext_data({ 'extra_html': ExtraHTML('html/item_preview.html', {}), 'navigate_to': 'html/item_preview.html' }) @pytest.mark.usefixtures('webextension') def test_resource_preview(driver, execute_in_page): """ A test case of the resource preview display function. """ execute_in_page(load_script('html/item_preview.js')) sample_resource = make_sample_resource() uuid = sample_resource['uuid'] preview_div = execute_in_page( ''' let preview_object = resource_preview(arguments[0]); document.body.append(preview_object.main_div); returnval(preview_object.main_div); ''', sample_resource) text = preview_div.text assert '...' not in text assert 'not set' not in text for string in [ *filter(lambda v: type(v) is str, sample_resource.values()), *[rr['identifier'] for rr in sample_resource['dependencies']], *[c['file'] for k in ('source_copyright', 'scripts') for c in sample_resource[k]], item_version_string(sample_resource, True), uuid ]: assert string in text del sample_resource['uuid'] sample_resource['identifier'] = 'hellopear' sample_resource['long_name'] = 'Hello Pear' sample_resource['description'] = 'greets a pear' sample_resource['dependencies'] = [{'identifier': 'hello-msg'}] for key in ('scripts', 'source_copyright'): for file_ref in sample_resource[key]: file_ref['file'] = file_ref['file'].replace('.', '_') preview_div = execute_in_page( ''' returnval(resource_preview(arguments[0], preview_object).main_div); ''', sample_resource) text = preview_div.text for string in [uuid, '...', 'pple', 'hello-message', 'report.spdx', 'LICENSES/CC0-1.0.txt', 'hello.js', 'bye.js']: assert string not in text for string in ['hellopear', 'Hello Pear', 'hello-msg', 'greets a pear', 'report_spdx', 'LICENSES/CC0-1_0_txt', 'hello_js', 'bye_js', 'not set']: assert string in text @pytest.mark.ext_data({ 'extra_html': ExtraHTML('html/item_preview.html', {}), 'navigate_to': 'html/item_preview.html' }) @pytest.mark.usefixtures('webextension') def test_mapping_preview(driver, execute_in_page): """ A test case of the mapping preview display function. """ execute_in_page(load_script('html/item_preview.js')) sample_mapping = make_sample_mapping() uuid = sample_mapping['uuid'] preview_div = execute_in_page( ''' let preview_object = mapping_preview(arguments[0]); document.body.append(preview_object.main_div); returnval(preview_object.main_div); ''', sample_mapping) text = preview_div.text assert '...' not in text assert 'not set' not in text for string in [ *filter(lambda v: type(v) is str, sample_mapping.values()), *[p['identifier'] for p in sample_mapping['payloads'].values()], *[c['file'] for c in sample_mapping['source_copyright']], item_version_string(sample_mapping), uuid ]: assert string in text del sample_mapping['uuid'] sample_mapping['identifier'] = 'example-org-bloated' sample_mapping['long_name'] = 'Example.org Bloated', sample_mapping['payloads'] = dict( [(pat.replace('.org', '.com'), res_id) for pat, res_id in sample_mapping['payloads'].items()] ) for file_ref in sample_mapping['source_copyright']: file_ref['file'] = file_ref['file'].replace('.', '_') preview_div = execute_in_page( ''' returnval(mapping_preview(arguments[0], preview_object).main_div); ''', sample_mapping) text = preview_div.text for string in [uuid, '...', 'inimal', 'example.org', 'report.spdx', 'LICENSES/CC0-1.0.txt']: assert string not in text for string in ['example-org-bloated', 'Example.org Bloated', 'example.com', 'report_spdx', 'LICENSES/CC0-1_0_txt', 'not set']: assert string in text @pytest.mark.ext_data({ 'background_script': broker_js, 'extra_html': [ ExtraHTML('html/item_preview.html', {}), ExtraHTML('html/file_preview.html', {}, wrap_into_htmldoc=False) ], 'navigate_to': 'html/item_preview.html' }) @pytest.mark.usefixtures('webextension') def test_file_preview_link(driver, execute_in_page): """ A test case of links created by preview functions that allow a referenced file to be previewed. """ execute_in_page(load_script('html/item_preview.js')) sample_data = make_complete_sample_data() sample_data['mapping'] = {} execute_in_page('returnval(haketilodb.save_items(arguments[0]));', sample_data) # Cause the "link" to `bye.js` to be invalid. sample_resource = make_sample_resource() sample_resource['scripts'][1]['sha256'] = 'dummy nonexistent hash' execute_in_page( ''' let resource_preview_object = resource_preview(arguments[0], undefined); document.body.append(resource_preview_object.main_div); ''', sample_resource) window0 = driver.window_handles[0] driver.find_element_by_link_text('hello.js').click() def blob_url_navigated(driver): if len(driver.window_handles) < 2: return window1 = [wh for wh in driver.window_handles if wh != window0][0] driver.switch_to.window(window1) try: return driver.current_url.startswith('blob') except NoSuchWindowException: pass WebDriverWait(driver, 10).until(blob_url_navigated) assert sample_files['hello.js']['contents'].strip() \ in driver.find_element_by_tag_name("pre").text driver.close() driver.switch_to.window(window0) driver.find_element_by_link_text('bye.js').click() def get_error_span(driver): if len(driver.window_handles) < 2: return window1 = [wh for wh in driver.window_handles if wh != window0][0] driver.switch_to.window(window1) try: return driver.find_element_by_id('error_msg') except NoSuchWindowException: pass error_span = WebDriverWait(driver, 10).until(get_error_span) assert error_span.is_displayed() assert "Couldn't find file in Haketilo's internal database :(" \ in error_span.text