Skip to content
This repository has been archived by the owner on Mar 15, 2018. It is now read-only.

Avoid 500 error when IARC returns invalid JSON, regardless of the reason #3523

Merged
merged 2 commits into from
Mar 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 16 additions & 4 deletions lib/iarc_v2/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ def _iarc_request(endpoint_name, data):
url = urljoin(settings.IARC_V2_SERVICE_ENDPOINT, endpoint_name)
headers = _iarc_headers()
response = requests.post(url, headers=headers, json=data)
return response.json()
try:
value = response.json()
except ValueError:
value = {}
return value


def _iarc_app_data(app):
Expand Down Expand Up @@ -76,6 +80,8 @@ def get_rating_changes(date=None):

start_date = date or datetime.datetime.utcnow()
end_date = start_date - datetime.timedelta(days=1)
log.info('Calling IARC GetRatingChanges from %s to %s',
start_date.strftime('%Y-%m-%d'), end_date.strftime('%Y-%m-%d'))
data = _iarc_request('GetRatingChanges', {
'StartDate': start_date.strftime('%Y-%m-%d'),
'EndDate': end_date.strftime('%Y-%m-%d'),
Expand Down Expand Up @@ -114,6 +120,8 @@ def search_and_attach_cert(app, cert_id):

def search_cert(app, cert_id):
"""Ask IARC for information about a cert."""
log.info('Calling IARC SearchCert for app %s, cert %s',
app.pk, unicode(UUID(cert_id)))
data = _iarc_request('SearchCerts', {'CertID': unicode(UUID(cert_id))})
# We don't care about MatchFound, serializer won't find the right fields
# if no match is found.
Expand All @@ -125,6 +133,8 @@ def _attach_to_cert(app, cert_id):
"""Tell IARC to attach a cert to an app."""
data = _iarc_app_data(app)
data['CertID'] = unicode(UUID(cert_id))
log.info('Calling IARC AttachToCert for app %s, cert %s',
app.pk, data['CertID'])
return _iarc_request('AttachToCert', data)


Expand All @@ -139,7 +149,7 @@ def publish(app_id):
return
try:
cert_id = app.iarc_cert.cert_id
data = _update_certs(cert_id, 'Publish')
data = _update_certs(app_id, cert_id, 'Publish')
except IARCCert.DoesNotExist:
data = None
return data
Expand All @@ -156,7 +166,7 @@ def unpublish(app_id):
return
try:
cert_id = app.iarc_cert.cert_id
data = _update_certs(cert_id, 'RemoveProduct')
data = _update_certs(app_id, cert_id, 'RemoveProduct')
except IARCCert.DoesNotExist:
data = None
return data
Expand All @@ -172,7 +182,7 @@ def refresh(app):
# changes.


def _update_certs(cert_id, action):
def _update_certs(app_id, cert_id, action):
"""
UpdateCerts to tell IARC when we publish or unpublish a product.
Endpoint can handle batch updates, but we only need one at a time.
Expand All @@ -194,4 +204,6 @@ def _update_certs(cert_id, action):
'Action': action,
}]
}
log.info('Calling IARC UpdateCert %s for app %s, cert %s',
action, app_id, data['UpdateList'][0]['CertID'])
return _iarc_request('UpdateCerts', data)
6 changes: 6 additions & 0 deletions lib/iarc_v2/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ def test_iarc_app_data_not_public(self):
'Publish': False,
'ProductName': unicode(self.app.name)})

@responses.activate
def test_search_cert_error(self):
setup_mock_response('SearchCerts', data='<!DOCTYPE html>error')
serializer = search_cert(mock.Mock(), unicode(uuid4()))
eq_(serializer.is_valid(), False)

@responses.activate
def test_search_cert_uuid_hex_string_w_separators(self):
setup_mock_response('SearchCerts')
Expand Down