Skip to content

Commit

Permalink
Forbid raster layer with QML style combination in Patch
Browse files Browse the repository at this point in the history
  • Loading branch information
index-git committed Jun 17, 2021
1 parent e486b8c commit 782d68c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
1 change: 1 addition & 0 deletions doc/rest.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ Body parameters:
- Taken into account only if `file` is provided.
- *style*, style file
- SLD or QML style file (recognized by the root element of XML: `StyledLayerDescriptor` or `qgis`)
- vector layers can have SLD or QML style, raster layers can not have QML style
- attribute names are [laundered](https://gdal.org/drivers/vector/pg.html#layer-creation-options) to be in line with DB attribute names
- If provided, current layer thumbnail will be temporarily deleted and created again using the new style.
- *access_rights.read*, string
Expand Down
15 changes: 14 additions & 1 deletion src/layman/layer/rest_workspace_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from layman import settings, authn, util as layman_util
from layman.authn import authenticate
from layman.authz import authorize_workspace_publications_decorator
from . import util, LAYER_REST_PATH_NAME
from . import util, LAYER_REST_PATH_NAME, LAYER_TYPE
from .filesystem import input_file, input_style, input_chunk

bp = Blueprint('rest_workspace_layer', __name__)
Expand Down Expand Up @@ -89,6 +89,7 @@ def patch(workspace, layername):
style_file = request.files['sld']

delete_from = None
style_type = None
if style_file:
style_type = input_style.get_style_type_from_file_storage(style_file)
kwargs['style_type'] = style_type
Expand All @@ -98,6 +99,7 @@ def patch(workspace, layername):
delete_from = 'layman.layer.filesystem.input_file'

# FILE NAMES
filenames = None
if delete_from == 'layman.layer.filesystem.input_file':
if use_chunk_upload:
filenames = files
Expand All @@ -106,6 +108,17 @@ def patch(workspace, layername):
input_file.check_filenames(workspace, layername, filenames,
check_crs, ignore_existing_files=True)

if filenames:
file_type = input_file.get_file_type(input_file.get_main_file_name(filenames))
else:
file_type = layman_util.get_publication_info(workspace, LAYER_TYPE, layername, context={'keys': ['file']})['file']['file_type']
if style_type:
style_type_for_check = style_type.code
else:
style_type_for_check = layman_util.get_publication_info(workspace, LAYER_TYPE, layername, context={'keys': ['style']})['style']['type']
if file_type == settings.FILE_TYPE_RASTER and style_type_for_check == 'qml':
raise LaymanError(48, f'Raster layers are not allowed to have QML style.')

props_to_refresh = util.get_same_or_missing_prop_names(workspace, layername)
kwargs['metadata_properties_to_refresh'] = props_to_refresh

Expand Down
38 changes: 36 additions & 2 deletions src/layman/layer/rest_workspace_layer_test.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import json
import sys
from test import process_client
from test import process_client, util as test_util
from test.util import url_for
import requests
import pytest

del sys.modules['layman']

from layman import app
from layman import app, LaymanError


@pytest.mark.usefixtures('ensure_layman')
Expand Down Expand Up @@ -42,3 +42,37 @@ def test_style_value():
assert r_del.status_code >= 400, (r_del.text, style_url)

process_client.delete_workspace_layer(username, layername)


@pytest.mark.parametrize('post_params, patch_params, expected_exc', [
({'file_paths': ['sample/layman.layer/sample_tif_rgb.tif', ],
},
{'style_file': 'sample/style/ne_10m_admin_0_countries.qml',
},
{'http_code': 400,
'code': 48,
'message': 'Wrong combination of parameters',
'detail': 'Raster layers are not allowed to have QML style.',
},
),
({'style_file': 'sample/style/ne_10m_admin_0_countries.qml',
},
{'file_paths': ['sample/layman.layer/sample_tif_rgb.tif', ],
},
{'http_code': 400,
'code': 48,
'message': 'Wrong combination of parameters',
'detail': 'Raster layers are not allowed to have QML style.',
},
),
])
@pytest.mark.usefixtures('ensure_layman')
def test_error(post_params, patch_params, expected_exc):
workspace = 'test_error_workspace'
layer = 'test_error_layer'

process_client.publish_workspace_layer(workspace, layer, **post_params)
with pytest.raises(LaymanError) as exc_info:
process_client.patch_workspace_layer(workspace, layer, **patch_params)
test_util.assert_error(expected_exc, exc_info)
process_client.delete_workspace_layer(workspace, layer)

0 comments on commit 782d68c

Please sign in to comment.