Projet

Général

Profil

0001-dataviz-avoid-crash-getting-statistics-from-bad-prov.patch

Valentin Deniaud, 23 décembre 2020 12:18

Télécharger (2,27 ko)

Voir les différences:

Subject: [PATCH] dataviz: avoid crash getting statistics from bad provider
 (#49692)

 combo/apps/dataviz/__init__.py |  7 +++++--
 tests/test_dataviz.py          | 14 +++++++++++---
 2 files changed, 16 insertions(+), 5 deletions(-)
combo/apps/dataviz/__init__.py
52 52

  
53 53
            for site_key, site_dict in sites.items():
54 54
                site_title = site_dict.pop('title', '')
55
                result = requests.get(
55
                response = requests.get(
56 56
                    url, remote_service=site_dict, without_user=True, headers={'accept': 'application/json'}
57
                ).json()
57
                )
58
                if response.status_code != 200:
59
                    continue
58 60

  
61
                result = response.json()
59 62
                if isinstance(result, dict):
60 63
                    result = result['data']  # detect new api
61 64

  
tests/test_dataviz.py
1205 1205
    catalog = {'data': [{'url': 'https://stat.com/stats/1/', 'name': 'Test', 'id': 'test'}]}
1206 1206

  
1207 1207
    @urlmatch(scheme='https', netloc=r'stat.com', path='/stats/')
1208
    def url_mock(url, request):
1209
        return {'content': json.dumps(catalog), 'status_code': 200}
1208
    def server_error(url, request):
1209
        return {'content': 'error', 'status_code': 500}
1210 1210

  
1211 1211
    appconfig = apps.get_app_config('dataviz')
1212
    with HTTMock(url_mock):
1212
    with HTTMock(server_error):
1213
        appconfig.hourly()
1214
    assert Statistic.objects.count() == statistics_count
1215

  
1216
    @urlmatch(scheme='https', netloc=r'stat.com', path='/stats/')
1217
    def success(url, request):
1218
        return {'content': json.dumps(catalog), 'status_code': 200}
1219

  
1220
    with HTTMock(success):
1213 1221
        appconfig.hourly()
1214 1222

  
1215 1223
    assert Statistic.objects.count() == statistics_count + 1
1216
-