maps: don't fail is geojson url is malformed (#55468)
gitea-wip/combo/pipeline/head There was a failure building this commit Details
gitea/combo/pipeline/head Build started... Details

This commit is contained in:
Lauréline Guérin 2021-07-27 10:20:19 +02:00
parent 14e3aa9f02
commit fe4a5c59e3
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
2 changed files with 29 additions and 12 deletions

View File

@ -27,6 +27,7 @@ from django.utils.encoding import python_2_unicode_compatible
from django.utils.html import escape
from django.utils.text import slugify
from django.utils.translation import ugettext_lazy as _
from requests.exceptions import RequestException
from requests.models import PreparedRequest
from combo.data.library import register_cell_class
@ -217,19 +218,21 @@ class MapLayer(models.Model):
req.prepare_url(geojson_url, {'circle': '%s,%s,%s' % (center_lng, center_lat, distance)})
geojson_url = req.url
response = requests.get(
geojson_url,
remote_service='auto',
cache_duration=self.cache_duration,
user=request.user if (request and self.include_user_identifier) else None,
without_user=not (self.include_user_identifier),
headers={'accept': 'application/json'},
)
if not response.ok:
try:
response = requests.get(
geojson_url,
remote_service='auto',
cache_duration=self.cache_duration,
user=request.user if (request and self.include_user_identifier) else None,
without_user=not (self.include_user_identifier),
headers={'accept': 'application/json'},
)
response.raise_for_status()
except RequestException as e:
return {
'type': 'FeatureCollection',
'features': [],
'_combo_err_desc': "Bad status code from requested URL",
'_combo_err_desc': "Bad response from requested URL (%s)" % e,
}
try:
data = response.json()

View File

@ -279,12 +279,23 @@ def test_get_geojson(app, layer, user):
cell = Map(page=page, placeholder='content', order=0, public=True)
cell.title = 'Map'
cell.save()
layer.geojson_url = 'http://example.org/geojson?t1'
layer.geojson_url = 'geojson?t1'
layer.save()
MapLayerOptions.objects.create(map_cell=cell, map_layer=layer)
geojson_url = reverse('mapcell-geojson', kwargs={'cell_id': cell.id, 'layer_slug': layer.slug})
# invalid url - missing schema
resp = app.get(geojson_url)
assert len(resp.json['features']) == 0
assert resp.json['_combo_err_desc'] == (
"Bad response from requested URL (Invalid URL 'geojson?t1': No schema supplied. "
"Perhaps you meant http://geojson?t1?)"
)
layer.geojson_url = 'http://example.org/geojson?t1'
layer.save()
# invalid content
with mock.patch('combo.utils.requests_wrapper.RequestsSession.get') as requests_get:
mock_resp = Response()
@ -297,7 +308,10 @@ def test_get_geojson(app, layer, user):
mock_resp.status_code = 500
resp = app.get(geojson_url)
assert len(resp.json['features']) == 0
assert resp.json['_combo_err_desc'] == 'Bad status code from requested URL'
assert (
resp.json['_combo_err_desc']
== 'Bad response from requested URL (500 Server Error: None for url: None)'
)
# check cache duration
with mock.patch('combo.utils.requests_wrapper.RequestsSession.request') as requests_get: