From 57ce414ca81682a71288018a4d9001604002ec23 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Tue, 1 Mar 2022 11:29:26 +0100 Subject: validate repository responses against JSON schemas * compute_scripts.awk (include_file): don't enforce specific path format on #INCLUDE'd files * .gitmodules, schemas: add Haketilo JSON schemas subrepo * html/install.js (InstallView): import schema validator and run it against downloaded mapping and resource definitions * html/repo_query.js (RepoEntry): import schema validator and run it against obtained query results * test/haketilo_test/unit/test_install.py (test_install_normal_usage, test_install_dialogs): use underscore instead of hyphen in item identifiers * test/haketilo_test/unit/test_install.py (test_install_dialogs): adapt error message test cases to new handling method of invalid JSON instanced * test/haketilo_test/unit/test_repo_query.py (test_repo_query_normal_usage): use underscore instead of hyphen in item identifiers * test/haketilo_test/unit/test_repo_query.py (test_repo_query_messages): use higher sample unsupported schema version to avoid having to modify the test case soon * test/haketilo_test/world_wide_library.py: use underscore instead of hyphen in item identifiers * common/jsonschema.js, common/jsonschema: adapt tdegrunt's jsonschema and include in Haketilo, load schema documents from schemas/ --- test/haketilo_test/unit/test_install.py | 70 +++++++++++++++--------------- test/haketilo_test/unit/test_popup.py | 2 +- test/haketilo_test/unit/test_repo_query.py | 4 +- test/haketilo_test/world_wide_library.py | 8 ++-- 4 files changed, 42 insertions(+), 42 deletions(-) (limited to 'test') diff --git a/test/haketilo_test/unit/test_install.py b/test/haketilo_test/unit/test_install.py index f4bc483..1e2063c 100644 --- a/test/haketilo_test/unit/test_install.py +++ b/test/haketilo_test/unit/test_install.py @@ -70,18 +70,18 @@ def test_install_normal_usage(driver, execute_in_page, complex_variant): 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}' + 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 ( + 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' + root_resource_id = f'resource-a' + root_mapping_id = f'mapping-a' resource_ids = [root_resource_id] files_count = 0 @@ -102,7 +102,7 @@ def test_install_normal_usage(driver, execute_in_page, complex_variant): 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 'resource-a' in containers['resource_preview_container'].text assert_container_displayed('resource_preview_container') execute_in_page('returnval(install_view.resource_back_but);').click() @@ -246,7 +246,7 @@ def test_install_dialogs(driver, execute_in_page, message): browser.tabs.sendMessage = () => new Promise(cb => {}); install_view.show(...arguments); ''', - 'https://hydril.la/', 'mapping', 'mapping_a') + 'https://hydril.la/', 'mapping', 'mapping-a') assert dlg_buts() == [] assert dialog_txt() == 'Fetching data from repository...' @@ -256,7 +256,7 @@ def test_install_dialogs(driver, execute_in_page, message): browser.tabs.sendMessage = () => Promise.resolve({error: "sth"}); install_view.show(...arguments); ''', - 'https://hydril.la/', 'mapping', 'mapping_a') + 'https://hydril.la/', 'mapping', 'mapping-a') assert_dlg(['conf_buts'], 'Failure to communicate with repository :(') elif message == 'HTTP_code_item': @@ -266,7 +266,7 @@ def test_install_dialogs(driver, execute_in_page, message): browser.tabs.sendMessage = () => Promise.resolve(response); install_view.show(...arguments); ''', - 'https://hydril.la/', 'mapping', 'mapping_a') + 'https://hydril.la/', 'mapping', 'mapping-a') assert_dlg(['conf_buts'], 'Repository sent HTTP code 404 :(') elif message == 'invalid_JSON': @@ -276,47 +276,47 @@ def test_install_dialogs(driver, execute_in_page, message): browser.tabs.sendMessage = () => Promise.resolve(response); install_view.show(...arguments); ''', - 'https://hydril.la/', 'mapping', 'mapping_a') + '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: {$schema: "https://hydrilla.koszko.org/schemas/api_mapping_description-2.1.schema.json"} - }; - browser.tabs.sendMessage = () => Promise.resolve(response); + const old_sendMessage = browser.tabs.sendMessage; + browser.tabs.sendMessage = async function(...args) { + const response = await old_sendMessage(...args); + response.json.$schema = "https://hydrilla.koszko.org/schemas/api_mapping_description-255.1.schema.json"; + return response; + } install_view.show(...arguments); ''', - 'https://hydril.la/', 'mapping', 'somemapping', [2, 1]) + 'https://hydril.la/', 'mapping', 'mapping-a', [2022, 5, 10]) assert_dlg(['conf_buts'], - 'Mapping somemapping-2.1 was served using unsupported Hydrilla API version. You might need to update Haketilo.') + 'Mapping mapping-a-2022.5.10 was served using unsupported Hydrilla API version. You might need to update Haketilo.') elif message == 'invalid_response_format': execute_in_page( ''' - const response = { - ok: true, - status: 200, - /* $schema is not a string as it should be. */ - json: {$schema: null} - }; - browser.tabs.sendMessage = () => Promise.resolve(response); + const old_sendMessage = browser.tabs.sendMessage; + browser.tabs.sendMessage = async function(...args) { + const response = await old_sendMessage(...args); + /* identifier is not a string as it should be. */ + response.json.identifier = 1234567; + return response; + } install_view.show(...arguments); ''', - 'https://hydril.la/', 'resource', 'someresource') + 'https://hydril.la/', 'resource', 'resource-a') assert_dlg(['conf_buts'], - 'Resource someresource was served using a nonconforming response format.') + 'Resource resource-a was served using a nonconforming response format.') elif message == 'indexeddb_error_item': execute_in_page( ''' haketilodb.idb_get = () => {throw "some error";}; install_view.show(...arguments); ''', - 'https://hydril.la/', 'mapping', 'mapping_a') + 'https://hydril.la/', 'mapping', 'mapping-a') assert_dlg(['conf_buts'], "Error accessing Haketilo's internal database :(") @@ -326,7 +326,7 @@ def test_install_dialogs(driver, execute_in_page, message): haketilodb.save_items = () => new Promise(() => {}); returnval(install_view.show(...arguments)); ''', - 'https://hydril.la/', 'mapping', 'mapping_b') + 'https://hydril.la/', 'mapping', 'mapping-b') execute_in_page('returnval(install_view.install_but);').click() @@ -343,7 +343,7 @@ def test_install_dialogs(driver, execute_in_page, message): } returnval(install_view.show(...arguments)); ''', - 'https://hydril.la/', 'mapping', 'mapping_b') + 'https://hydril.la/', 'mapping', 'mapping-b') execute_in_page('returnval(install_view.install_but);').click() @@ -355,7 +355,7 @@ def test_install_dialogs(driver, execute_in_page, message): fetch = () => {throw "some error";}; returnval(install_view.show(...arguments)); ''', - 'https://hydril.la/', 'mapping', 'mapping_b') + 'https://hydril.la/', 'mapping', 'mapping-b') execute_in_page('returnval(install_view.install_but);').click() @@ -367,7 +367,7 @@ def test_install_dialogs(driver, execute_in_page, message): fetch = () => Promise.resolve({ok: false, status: 400}); returnval(install_view.show(...arguments)); ''', - 'https://hydril.la/', 'mapping', 'mapping_b') + 'https://hydril.la/', 'mapping', 'mapping-b') execute_in_page('returnval(install_view.install_but);').click() @@ -379,7 +379,7 @@ def test_install_dialogs(driver, execute_in_page, message): fetch = () => Promise.resolve({ok: true, status: 200, text: err}); returnval(install_view.show(...arguments)); ''', - 'https://hydril.la/', 'mapping', 'mapping_b') + 'https://hydril.la/', 'mapping', 'mapping-b') execute_in_page('returnval(install_view.install_but);').click() @@ -396,7 +396,7 @@ def test_install_dialogs(driver, execute_in_page, message): } returnval(install_view.show(...arguments)); ''', - 'https://hydril.la/', 'mapping', 'mapping_b') + 'https://hydril.la/', 'mapping', 'mapping-b') execute_in_page('returnval(install_view.install_but);').click() @@ -413,7 +413,7 @@ def test_install_dialogs(driver, execute_in_page, message): haketilodb.save_items = () => {throw "some error";}; returnval(install_view.show(...arguments)); ''', - 'https://hydril.la/', 'mapping', 'mapping_b') + 'https://hydril.la/', 'mapping', 'mapping-b') execute_in_page('returnval(install_view.install_but);').click() diff --git a/test/haketilo_test/unit/test_popup.py b/test/haketilo_test/unit/test_popup.py index 1fc262c..e62feb7 100644 --- a/test/haketilo_test/unit/test_popup.py +++ b/test/haketilo_test/unit/test_popup.py @@ -224,7 +224,7 @@ def test_popup_repo_query(driver, execute_in_page): # Click the "Show results" button. selector = '.repo_query_buttons > button:first-child' driver.find_element_by_css_selector(selector).click() - shown = lambda d: 'MAPPING_A' in containers['repo_query'].text + shown = lambda d: 'MAPPING-A' in containers['repo_query'].text WebDriverWait(driver, 10).until(shown) # Click the "Cancel" button diff --git a/test/haketilo_test/unit/test_repo_query.py b/test/haketilo_test/unit/test_repo_query.py index c8c4875..177d415 100644 --- a/test/haketilo_test/unit/test_repo_query.py +++ b/test/haketilo_test/unit/test_repo_query.py @@ -99,7 +99,7 @@ def test_repo_query_normal_usage(driver, execute_in_page): assert len(mapping_entries) == 3 - expected_names = ['MAPPING_ABCD', 'MAPPING_ABCD-DEFG-GHIJ', 'MAPPING_A'] + expected_names = ['MAPPING-ABCD', 'MAPPING-ABCD-DEFG-GHIJ', 'MAPPING-A'] for name, entry in zip(expected_names, mapping_entries): assert entry['mapping_name'].text == name @@ -217,7 +217,7 @@ def test_repo_query_messages(driver, execute_in_page, message): const response = { ok: true, status: 200, - json: {$schema: "https://hydrilla.koszko.org/schemas/api_query_result-3.2.1.schema.json"} + json: {$schema: "https://hydrilla.koszko.org/schemas/api_query_result-255.2.1.schema.json"} }; browser.tabs.sendMessage = () => Promise.resolve(response); ''') diff --git a/test/haketilo_test/world_wide_library.py b/test/haketilo_test/world_wide_library.py index a932821..fedfeb6 100644 --- a/test/haketilo_test/world_wide_library.py +++ b/test/haketilo_test/world_wide_library.py @@ -127,14 +127,14 @@ for deps in [(0, 1, 2, 3), (3, 4, 5, 6), (6, 7, 8, 9)]: sample_resource_templates.append({ 'id_suffix': ''.join(letters), 'files_count': deps[0], - 'dependencies': [{'identifier': f'resource_{l}'} for l in letters] + 'dependencies': [{'identifier': f'resource-{l}'} for l in letters] }) suffixes = [srt['id_suffix'] for srt in sample_resource_templates] sample_resource_templates.append({ 'id_suffix': '-'.join(suffixes), 'files_count': 2, - 'dependencies': [{'identifier': f'resource_{suf}'} for suf in suffixes] + 'dependencies': [{'identifier': f'resource-{suf}'} for suf in suffixes] }) for i in range(10): @@ -150,7 +150,7 @@ sample_queries = {} for srt in sample_resource_templates: resource = make_sample_resource() - resource['identifier'] = f'resource_{srt["id_suffix"]}' + resource['identifier'] = f'resource-{srt["id_suffix"]}' resource['long_name'] = resource['identifier'].upper() resource['uuid'] = str(uuid4()) resource['dependencies'] = srt['dependencies'] @@ -164,7 +164,7 @@ for srt in sample_resource_templates: resource_versions[1][-1] += 1 mapping = make_sample_mapping() - mapping['identifier'] = f'mapping_{srt["id_suffix"]}' + mapping['identifier'] = f'mapping-{srt["id_suffix"]}' mapping['long_name'] = mapping['identifier'].upper() mapping['uuid'] = str(uuid4()) mapping['source_copyright'] = resource['source_copyright'] -- cgit v1.2.3