dataviz: avoid crash getting statistics from bad provider (#49692)

This commit is contained in:
Valentin Deniaud 2020-12-23 11:57:47 +01:00
parent a4546a919b
commit 8abb71b11c
2 changed files with 16 additions and 5 deletions

View File

@ -51,10 +51,13 @@ class AppConfig(django.apps.AppConfig):
for site_key, site_dict in sites.items():
site_title = site_dict.pop('title', '')
result = requests.get(
response = requests.get(
url, remote_service=site_dict, without_user=True, headers={'accept': 'application/json'}
).json()
)
if response.status_code != 200:
continue
result = response.json()
if isinstance(result, dict):
result = result['data'] # detect new api

View File

@ -1204,11 +1204,19 @@ def test_dataviz_api_list_statistics(new_api_statistics, settings):
catalog = {'data': [{'url': 'https://stat.com/stats/1/', 'name': 'Test', 'id': 'test'}]}
@urlmatch(scheme='https', netloc=r'stat.com', path='/stats/')
def url_mock(url, request):
return {'content': json.dumps(catalog), 'status_code': 200}
def server_error(url, request):
return {'content': 'error', 'status_code': 500}
appconfig = apps.get_app_config('dataviz')
with HTTMock(url_mock):
with HTTMock(server_error):
appconfig.hourly()
assert Statistic.objects.count() == statistics_count
@urlmatch(scheme='https', netloc=r'stat.com', path='/stats/')
def success(url, request):
return {'content': json.dumps(catalog), 'status_code': 200}
with HTTMock(success):
appconfig.hourly()
assert Statistic.objects.count() == statistics_count + 1