Projet

Général

Profil

0001-maps-don-t-fail-is-geojson-url-is-malformed-55468.patch

Lauréline Guérin, 27 juillet 2021 10:21

Télécharger (3,74 ko)

Voir les différences:

Subject: [PATCH] maps: don't fail is geojson url is malformed (#55468)

 combo/apps/maps/models.py | 23 +++++++++++++----------
 tests/test_maps_cells.py  | 12 ++++++++++--
 2 files changed, 23 insertions(+), 12 deletions(-)
combo/apps/maps/models.py
27 27
from django.utils.html import escape
28 28
from django.utils.text import slugify
29 29
from django.utils.translation import ugettext_lazy as _
30
from requests.exceptions import RequestException
30 31
from requests.models import PreparedRequest
31 32

  
32 33
from combo.data.library import register_cell_class
......
217 218
            req.prepare_url(geojson_url, {'circle': '%s,%s,%s' % (center_lng, center_lat, distance)})
218 219
            geojson_url = req.url
219 220

  
220
        response = requests.get(
221
            geojson_url,
222
            remote_service='auto',
223
            cache_duration=self.cache_duration,
224
            user=request.user if (request and self.include_user_identifier) else None,
225
            without_user=not (self.include_user_identifier),
226
            headers={'accept': 'application/json'},
227
        )
228
        if not response.ok:
221
        try:
222
            response = requests.get(
223
                geojson_url,
224
                remote_service='auto',
225
                cache_duration=self.cache_duration,
226
                user=request.user if (request and self.include_user_identifier) else None,
227
                without_user=not (self.include_user_identifier),
228
                headers={'accept': 'application/json'},
229
            )
230
            response.raise_for_status()
231
        except RequestException:
229 232
            return {
230 233
                'type': 'FeatureCollection',
231 234
                'features': [],
232
                '_combo_err_desc': "Bad status code from requested URL",
235
                '_combo_err_desc': "Bad response from requested URL",
233 236
            }
234 237
        try:
235 238
            data = response.json()
tests/test_maps_cells.py
279 279
    cell = Map(page=page, placeholder='content', order=0, public=True)
280 280
    cell.title = 'Map'
281 281
    cell.save()
282
    layer.geojson_url = 'http://example.org/geojson?t1'
282
    layer.geojson_url = 'geojson?t1'
283 283
    layer.save()
284 284
    MapLayerOptions.objects.create(map_cell=cell, map_layer=layer)
285 285

  
286 286
    geojson_url = reverse('mapcell-geojson', kwargs={'cell_id': cell.id, 'layer_slug': layer.slug})
287 287

  
288
    # invalid url - missing schema
289
    resp = app.get(geojson_url)
290
    assert len(resp.json['features']) == 0
291
    assert resp.json['_combo_err_desc'] == 'Bad response from requested URL'
292

  
293
    layer.geojson_url = 'http://example.org/geojson?t1'
294
    layer.save()
295

  
288 296
    # invalid content
289 297
    with mock.patch('combo.utils.requests_wrapper.RequestsSession.get') as requests_get:
290 298
        mock_resp = Response()
......
297 305
        mock_resp.status_code = 500
298 306
        resp = app.get(geojson_url)
299 307
        assert len(resp.json['features']) == 0
300
        assert resp.json['_combo_err_desc'] == 'Bad status code from requested URL'
308
        assert resp.json['_combo_err_desc'] == 'Bad response from requested URL'
301 309

  
302 310
    # check cache duration
303 311
    with mock.patch('combo.utils.requests_wrapper.RequestsSession.request') as requests_get:
304
-