Skip to content

Commit

Permalink
BGDIINF_SB-2238: Improved error message for invalid sr in profile
Browse files Browse the repository at this point in the history
endpoint.
  • Loading branch information
ltshb committed May 5, 2022
1 parent 0e3b762 commit c391c00
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 18 deletions.
13 changes: 13 additions & 0 deletions app/helpers/validation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

from shapely.geometry import Polygon

from flask import abort

from app.helpers.helpers import float_raise_nan
from app.settings import VALID_SRID

bboxes = {
2056:
Expand Down Expand Up @@ -38,3 +41,13 @@ def srs_guesser(geom):
sr = epsg
break
return sr


def validate_sr(sr):
if sr not in VALID_SRID:
abort(
400,
"Please provide a valid number for the spatial reference system model: "
f"{', '.join(map(str, VALID_SRID))}"
)
return sr
11 changes: 0 additions & 11 deletions app/helpers/validation/height.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from flask import abort

from app.helpers.helpers import float_raise_nan
from app.settings import VALID_SRID


def validate_lon_lat(lon, lat):
Expand All @@ -22,13 +21,3 @@ def validate_lon_lat(lon, lat):
abort(400, "Please provide numerical values for the parameter 'northing'/'lat'")

return lon, lat


def validate_sr(sr):
if sr not in VALID_SRID:
abort(
400,
"Please provide a valid number for the spatial reference system model: "
f"{','.join(map(str, VALID_SRID))}"
)
return sr
7 changes: 2 additions & 5 deletions app/helpers/validation/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from app.helpers.profile_helpers import PROFILE_DEFAULT_AMOUNT_POINTS
from app.helpers.profile_helpers import PROFILE_MAX_AMOUNT_POINTS
from app.helpers.validation import srs_guesser
from app.helpers.validation import validate_sr

logger = logging.getLogger(__name__)
max_content_length = 32 * 1024 * 1024 # 32MB
Expand Down Expand Up @@ -107,11 +108,7 @@ def read_spatial_reference(linestring):
abort(400, "No 'sr' given and cannot be guessed from 'geom'")
spatial_reference = sr

if spatial_reference not in (21781, 2056):
abort(
400,
"Please provide a valid number for the spatial reference system model 21781 or 2056"
)
validate_sr(spatial_reference)
return spatial_reference


Expand Down
2 changes: 1 addition & 1 deletion app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
from app.helpers.route import prefix_route
from app.helpers.validation import bboxes
from app.helpers.validation import srs_guesser
from app.helpers.validation import validate_sr
from app.helpers.validation.height import validate_lon_lat
from app.helpers.validation.height import validate_sr
# add route prefix
from app.statistics.statistics import load_json
from app.statistics.statistics import prepare_data
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def test_profile_invalid_sr_json_valid(self, mock_georaster_utils):
self.assert_response_contains(
resp,
"Please provide a valid number for the spatial reference "
"system model 21781 or 2056"
"system model: 21781, 2056"
)

@patch('app.routes.georaster_utils')
Expand Down

0 comments on commit c391c00

Please sign in to comment.