map: don't fail on invalid geojson data (#53521)
gitea-wip/combo/pipeline/head Build started... Details
gitea/combo/pipeline/head Build started... Details

This commit is contained in:
Lauréline Guérin 2021-04-29 09:45:32 +02:00
parent a71c673437
commit ea21ad5b00
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
2 changed files with 28 additions and 2 deletions

View File

@ -226,8 +226,19 @@ class MapLayer(models.Model):
headers={'accept': 'application/json'},
)
if not response.ok:
return []
data = response.json()
return {
'type': 'FeatureCollection',
'features': [],
'_combo_err_desc': "Bad status code from requested URL",
}
try:
data = response.json()
except json.JSONDecodeError:
return {
'type': 'FeatureCollection',
'features': [],
'_combo_err_desc': "Non JSON response from requested URL",
}
if 'features' in data:
features = data['features']
else:

View File

@ -7,6 +7,7 @@ from django.conf import settings
from django.contrib.auth.models import Group, User
from django.test.client import RequestFactory
from django.urls import reverse
from requests.models import Response
from combo.apps.maps.models import Map, MapLayer, MapLayerOptions
from combo.data.models import Page
@ -284,6 +285,20 @@ def test_get_geojson(app, layer, user):
geojson_url = reverse('mapcell-geojson', kwargs={'cell_id': cell.id, 'layer_slug': layer.slug})
# invalid content
with mock.patch('combo.utils.requests_wrapper.RequestsSession.get') as requests_get:
mock_resp = Response()
mock_resp.status_code = 200
requests_get.return_value = mock_resp
resp = app.get(geojson_url)
assert len(resp.json['features']) == 0
assert resp.json['_combo_err_desc'] == 'Non JSON response from requested URL'
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'
# check cache duration
with mock.patch('combo.utils.requests_wrapper.RequestsSession.request') as requests_get:
requests_get.return_value = mock.Mock(