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 ++++++++++++++++----------------- 1 file changed, 35 insertions(+), 35 deletions(-) (limited to 'test/haketilo_test/unit/test_install.py') 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() -- cgit v1.2.3