From 6b64cf7a1716fad574ec46953c3cc45078eb2204 Mon Sep 17 00:00:00 2001 From: index-git Date: Wed, 13 Sep 2023 14:02:27 +0200 Subject: [PATCH] Encapsulate definition of expected rest response --- tests/asserts/final/publication/rest.py | 68 +++++++++++++++---- .../x_forwarded_prefix/rest_endpoints_test.py | 52 +++----------- 2 files changed, 63 insertions(+), 57 deletions(-) diff --git a/tests/asserts/final/publication/rest.py b/tests/asserts/final/publication/rest.py index 11db69e02..7ae2e3351 100644 --- a/tests/asserts/final/publication/rest.py +++ b/tests/asserts/final/publication/rest.py @@ -2,11 +2,46 @@ from celery import states from layman import settings -from test_tools import process_client, util as test_util +from test_tools import process_client, util as test_util, assert_util PROXY_PREFIX = '/layman-proxy' +def get_expected_urls_in_rest_response(workspace, publ_type, name, *, rest_method, proxy_prefix, geodata_type=None): + assert rest_method in {'post', 'patch', 'get', 'delete', 'multi_delete'} + publ_type_directory = f'{publ_type.split(".")[1]}s' + result = { + 'url': f'http://{settings.LAYMAN_PROXY_SERVER_NAME}{proxy_prefix}/rest/workspaces/{workspace}/{publ_type_directory}/{name}' + } + if rest_method in ['patch', 'get']: + result['thumbnail'] = { + 'url': f'http://{settings.LAYMAN_PROXY_SERVER_NAME}{proxy_prefix}/rest/workspaces/{workspace}/{publ_type_directory}/{name}/thumbnail' + } + result['metadata'] = { + 'comparison_url': f'http://{settings.LAYMAN_PROXY_SERVER_NAME}{proxy_prefix}/rest/workspaces/{workspace}/{publ_type_directory}/{name}/metadata-comparison', + } + if publ_type == process_client.LAYER_TYPE: + result['wms'] = { + 'url': f'http://{settings.LAYMAN_PROXY_SERVER_NAME}{proxy_prefix}/geoserver/{workspace}_wms/ows', + } + result['sld'] = { + 'url': f'http://{settings.LAYMAN_PROXY_SERVER_NAME}{proxy_prefix}/rest/workspaces/{workspace}/{publ_type_directory}/{name}/style', + } + result['style'] = { + 'url': f'http://{settings.LAYMAN_PROXY_SERVER_NAME}{proxy_prefix}/rest/workspaces/{workspace}/{publ_type_directory}/{name}/style', + } + if geodata_type == settings.GEODATA_TYPE_VECTOR: + result['wfs'] = { + 'url': f'http://{settings.LAYMAN_PROXY_SERVER_NAME}{proxy_prefix}/geoserver/{workspace}/wfs' + } + else: + result['file'] = { + 'url': f'http://{settings.LAYMAN_PROXY_SERVER_NAME}{proxy_prefix}/rest/workspaces/{workspace}/{publ_type_directory}/{name}/file', + } + + return result + + def is_complete_in_rest(rest_publication_detail): assert 'layman_metadata' in rest_publication_detail, f'rest_publication_detail={rest_publication_detail}' assert rest_publication_detail['layman_metadata']['publication_status'] == 'COMPLETE', f'rest_publication_detail={rest_publication_detail}' @@ -101,15 +136,17 @@ def get_layer_with_x_forwarded_prefix(workspace, name, headers, ): 'X-Forwarded-Prefix': proxy_prefix, } rest_layer_info = process_client.get_workspace_layer(workspace, name, headers=headers) - assert rest_layer_info['url'] == f'http://{settings.LAYMAN_PROXY_SERVER_NAME}{proxy_prefix}/rest/workspaces/{workspace}/layers/{name}' - assert rest_layer_info['thumbnail']['url'] == f'http://{settings.LAYMAN_PROXY_SERVER_NAME}{proxy_prefix}/rest/workspaces/{workspace}/layers/{name}/thumbnail' - assert rest_layer_info['metadata']['comparison_url'] == f'http://{settings.LAYMAN_PROXY_SERVER_NAME}{proxy_prefix}/rest/workspaces/{workspace}/layers/{name}/metadata-comparison' - assert rest_layer_info['wms']['url'] == f'http://{settings.LAYMAN_PROXY_SERVER_NAME}{proxy_prefix}/geoserver/{workspace}_wms/ows' geodata_type = rest_layer_info['geodata_type'] - if geodata_type == settings.GEODATA_TYPE_VECTOR: - assert rest_layer_info['wfs']['url'] == f'http://{settings.LAYMAN_PROXY_SERVER_NAME}{proxy_prefix}/geoserver/{workspace}/wfs' - assert rest_layer_info['sld']['url'] == f'http://{settings.LAYMAN_PROXY_SERVER_NAME}{proxy_prefix}/rest/workspaces/{workspace}/layers/{name}/style' - assert rest_layer_info['style']['url'] == f'http://{settings.LAYMAN_PROXY_SERVER_NAME}{proxy_prefix}/rest/workspaces/{workspace}/layers/{name}/style' + + exp_resp = get_expected_urls_in_rest_response(workspace, process_client.LAYER_TYPE, name, + rest_method='get', + proxy_prefix=proxy_prefix, + geodata_type=geodata_type, + ) + assert_util.assert_same_values_for_keys( + expected=exp_resp, + tested=rest_layer_info, + ) def get_map_with_x_forwarded_prefix(workspace, name, headers, ): @@ -119,7 +156,12 @@ def get_map_with_x_forwarded_prefix(workspace, name, headers, ): 'X-Forwarded-Prefix': proxy_prefix, } rest_map_info = process_client.get_workspace_map(workspace, name, headers=headers) - assert rest_map_info['url'] == f'http://{settings.LAYMAN_PROXY_SERVER_NAME}{proxy_prefix}/rest/workspaces/{workspace}/maps/{name}' - assert rest_map_info['file']['url'] == f'http://{settings.LAYMAN_PROXY_SERVER_NAME}{proxy_prefix}/rest/workspaces/{workspace}/maps/{name}/file' - assert rest_map_info['thumbnail']['url'] == f'http://{settings.LAYMAN_PROXY_SERVER_NAME}{proxy_prefix}/rest/workspaces/{workspace}/maps/{name}/thumbnail' - assert rest_map_info['metadata']['comparison_url'] == f'http://{settings.LAYMAN_PROXY_SERVER_NAME}{proxy_prefix}/rest/workspaces/{workspace}/maps/{name}/metadata-comparison' + exp_resp = get_expected_urls_in_rest_response(workspace, process_client.MAP_TYPE, name, + rest_method='get', + proxy_prefix=proxy_prefix, + geodata_type=None, + ) + assert_util.assert_same_values_for_keys( + expected=exp_resp, + tested=rest_map_info, + ) diff --git a/tests/dynamic_data/publications/x_forwarded_prefix/rest_endpoints_test.py b/tests/dynamic_data/publications/x_forwarded_prefix/rest_endpoints_test.py index 706412cbc..7e0693741 100644 --- a/tests/dynamic_data/publications/x_forwarded_prefix/rest_endpoints_test.py +++ b/tests/dynamic_data/publications/x_forwarded_prefix/rest_endpoints_test.py @@ -1,8 +1,8 @@ -from layman import settings from tests import EnumTestTypes, Publication +from tests.asserts.final.publication import rest as assert_rest from tests.dynamic_data import base_test, base_test_classes from tests.dynamic_data.publications import common_publications -from test_tools import assert_util, process_client +from test_tools import assert_util pytest_generate_tests = base_test.pytest_generate_tests @@ -32,48 +32,12 @@ def test_publication(self, publication, rest_method): proxy_prefix = '/layman-proxy' response = rest_method.fn(publication, args={'headers': {'X-Forwarded-Prefix': proxy_prefix}}) publication_response = response[0] if isinstance(response, list) and len(response) == 1 else response - if rest_method == self.patch_publication: # pylint: disable=W0143 - if publication.type == process_client.LAYER_TYPE: - exp_resp = { - 'url': f'http://{settings.LAYMAN_PROXY_SERVER_NAME}{proxy_prefix}/rest/workspaces/{publication.workspace}/{publication.type.split(".")[1]}s/{publication.name}', - 'thumbnail': { - 'url': f'http://{settings.LAYMAN_PROXY_SERVER_NAME}{proxy_prefix}/rest/workspaces/{publication.workspace}/layers/{publication.name}/thumbnail' - }, - 'metadata': { - 'comparison_url': f'http://{settings.LAYMAN_PROXY_SERVER_NAME}{proxy_prefix}/rest/workspaces/{publication.workspace}/layers/{publication.name}/metadata-comparison', - }, - 'wms': { - 'url': f'http://{settings.LAYMAN_PROXY_SERVER_NAME}{proxy_prefix}/geoserver/{publication.workspace}_wms/ows', - }, - 'sld': { - 'url': f'http://{settings.LAYMAN_PROXY_SERVER_NAME}{proxy_prefix}/rest/workspaces/{publication.workspace}/layers/{publication.name}/style', - }, - 'style': { - 'url': f'http://{settings.LAYMAN_PROXY_SERVER_NAME}{proxy_prefix}/rest/workspaces/{publication.workspace}/layers/{publication.name}/style', - }, - } - - geodata_type = response['geodata_type'] - if geodata_type == settings.GEODATA_TYPE_VECTOR: - exp_resp['wfs'] = { - 'url': f'http://{settings.LAYMAN_PROXY_SERVER_NAME}{proxy_prefix}/geoserver/{publication.workspace}/wfs' - } - else: - exp_resp = { - 'url': f'http://{settings.LAYMAN_PROXY_SERVER_NAME}{proxy_prefix}/rest/workspaces/{publication.workspace}/{publication.type.split(".")[1]}s/{publication.name}', - 'thumbnail': { - 'url': f'http://{settings.LAYMAN_PROXY_SERVER_NAME}{proxy_prefix}/rest/workspaces/{publication.workspace}/{publication.type.split(".")[1]}s/{publication.name}/thumbnail' - }, - 'file': { - 'url': f'http://{settings.LAYMAN_PROXY_SERVER_NAME}{proxy_prefix}/rest/workspaces/{publication.workspace}/{publication.type.split(".")[1]}s/{publication.name}/file' - }, - 'metadata': { - 'comparison_url': f'http://{settings.LAYMAN_PROXY_SERVER_NAME}{proxy_prefix}/rest/workspaces/{publication.workspace}/{publication.type.split(".")[1]}s/{publication.name}/metadata-comparison', - }, - } - - else: - exp_resp = {'url': f'http://{settings.LAYMAN_PROXY_SERVER_NAME}{proxy_prefix}/rest/workspaces/{publication.workspace}/{publication.type.split(".")[1]}s/{publication.name}'} + geodata_type = publication_response.get('geodata_type') + exp_resp = assert_rest.get_expected_urls_in_rest_response(publication.workspace, publication.type, publication.name, + rest_method=rest_method.enum_item.publ_name_part, + proxy_prefix=proxy_prefix, + geodata_type=geodata_type, + ) assert_util.assert_same_values_for_keys( expected=exp_resp,