strasbourg_eu: handle liferay error 500 (but valid JSON) answers (#53407)
This commit is contained in:
parent
0f21ea0645
commit
60b77ad5da
|
@ -21,6 +21,8 @@ from django.utils.encoding import force_text
|
|||
from django.utils.six.moves.urllib import parse as urlparse
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from requests import RequestException
|
||||
|
||||
from passerelle.base.models import BaseResource
|
||||
from passerelle.compat import json_loads
|
||||
from passerelle.utils.api import endpoint, APIError
|
||||
|
@ -59,17 +61,19 @@ class StrasbourgEu(BaseResource):
|
|||
interests = [] # reset
|
||||
url = urlparse.urljoin(self.liferay_api_url, 'jsonws/interest.interest/set-user-interests')
|
||||
try:
|
||||
response = self.requests.post(
|
||||
url, data={'userId': name_id, 'interestIds': ','.join(interests)}
|
||||
).json()
|
||||
except ValueError:
|
||||
resp = self.requests.post(url, data={'userId': name_id, 'interestIds': ','.join(interests)})
|
||||
resp.raise_for_status()
|
||||
response = resp.json()
|
||||
except (ValueError, RequestException):
|
||||
return {'err': 2, 'err_desc': 'invalid service answer'}
|
||||
if 'error' in response:
|
||||
return {'err': 1, 'err_desc': response.get('error')}
|
||||
url = urlparse.urljoin(self.liferay_api_url, 'jsonws/interest.interest/get-interests')
|
||||
try:
|
||||
response = self.requests.get(url).json()
|
||||
except ValueError:
|
||||
resp = self.requests.get(url)
|
||||
resp.raise_for_status()
|
||||
response = resp.json()
|
||||
except (ValueError, RequestException):
|
||||
return {'err': 2, 'err_desc': 'invalid service answer'}
|
||||
interests = response.get('interests')
|
||||
if interests is None:
|
||||
|
@ -77,8 +81,10 @@ class StrasbourgEu(BaseResource):
|
|||
if name_id is not None:
|
||||
url = urlparse.urljoin(self.liferay_api_url, 'jsonws/interest.interest/get-user-interests')
|
||||
try:
|
||||
user_choices = self.requests.post(url, data={'userId': name_id}).json().get('interests')
|
||||
except ValueError:
|
||||
resp = self.requests.post(url, data={'userId': name_id})
|
||||
resp.raise_for_status()
|
||||
user_choices = resp.json().get('interests')
|
||||
except (ValueError, RequestException):
|
||||
return {'err': 2, 'err_desc': 'invalid service answer'}
|
||||
interests = [x for x in interests if x['id'] in user_choices]
|
||||
for interest in interests:
|
||||
|
@ -98,8 +104,10 @@ class StrasbourgEu(BaseResource):
|
|||
self.liferay_api_url, 'jsonws/notification.notification/get-user-notifications'
|
||||
)
|
||||
try:
|
||||
notifications = self.requests.post(url, data={'userId': name_id}).json()
|
||||
except ValueError:
|
||||
resp = self.requests.post(url, data={'userId': name_id})
|
||||
resp.raise_for_status()
|
||||
notifications = resp.json()
|
||||
except (ValueError, RequestException):
|
||||
return {'err': 2, 'err_desc': 'invalid service answer'}
|
||||
if 'error' in notifications:
|
||||
return {'err': 1, 'err_desc': notifications.get('error')}
|
||||
|
@ -131,8 +139,10 @@ class StrasbourgEu(BaseResource):
|
|||
notification['userId'] = name_id
|
||||
url = urlparse.urljoin(self.liferay_api_url, 'jsonws/notification.notification/add-notification')
|
||||
try:
|
||||
response = self.requests.post(url, data=notification).json()
|
||||
except ValueError:
|
||||
resp = self.requests.post(url, data=notification)
|
||||
resp.raise_for_status()
|
||||
response = resp.json()
|
||||
except (ValueError, RequestException):
|
||||
return {'err': 2, 'err_desc': 'invalid service answer'}
|
||||
if response.get('success'):
|
||||
return {'err': 0, 'err_desc': response.get('success')}
|
||||
|
@ -149,8 +159,10 @@ class StrasbourgEu(BaseResource):
|
|||
if request.method == 'GET':
|
||||
url = urlparse.urljoin(self.liferay_api_url, 'jsonws/favorite.favorite/get-user-favorites')
|
||||
try:
|
||||
response = self.requests.post(url, data={'userId': name_id}).json()
|
||||
except ValueError:
|
||||
resp = self.requests.post(url, data={'userId': name_id})
|
||||
resp.raise_for_status()
|
||||
response = resp.json()
|
||||
except (ValueError, RequestException):
|
||||
return {'err': 2, 'err_desc': 'invalid service answer'}
|
||||
if 'error' in response:
|
||||
return {'err': 1, 'err_desc': response.get('error')}
|
||||
|
@ -165,8 +177,10 @@ class StrasbourgEu(BaseResource):
|
|||
# change type to typeId
|
||||
types_url = urlparse.urljoin(self.liferay_api_url, 'jsonws/favorite.favorite/get-types')
|
||||
try:
|
||||
types = self.requests.get(types_url).json()
|
||||
except ValueError:
|
||||
resp = self.requests.get(types_url)
|
||||
resp.raise_for_status()
|
||||
types = resp.json()
|
||||
except (ValueError, RequestException):
|
||||
return {'err': 2, 'err_desc': 'invalid service answer'}
|
||||
# types: {"types": [{"id": "1", "name": "PLACE"}, ...]}
|
||||
types_dict = {x['name']: x['id'] for x in types['types']}
|
||||
|
@ -175,8 +189,10 @@ class StrasbourgEu(BaseResource):
|
|||
# send favorite
|
||||
url = urlparse.urljoin(self.liferay_api_url, 'jsonws/favorite.favorite/add-favorite')
|
||||
try:
|
||||
response = self.requests.post(url, data=favorite).json()
|
||||
except ValueError:
|
||||
resp = self.requests.post(url, data=favorite)
|
||||
resp.raise_for_status()
|
||||
response = resp.json()
|
||||
except (ValueError, RequestException):
|
||||
return {'err': 2, 'err_desc': 'invalid service answer'}
|
||||
if response.get('success'):
|
||||
return {'err': 0, 'err_desc': response.get('success')}
|
||||
|
@ -198,8 +214,10 @@ class StrasbourgEu(BaseResource):
|
|||
url = urlparse.urljoin(self.liferay_api_url, 'jsonws/favorite.favorite/delete-favorite')
|
||||
params = {'userId': name_id, 'favoriteId': favorite_id}
|
||||
try:
|
||||
response = self.requests.post(url, data=params).json()
|
||||
except ValueError:
|
||||
resp = self.requests.post(url, data=params)
|
||||
resp.raise_for_status()
|
||||
response = resp.json()
|
||||
except (ValueError, RequestException):
|
||||
return {'err': 2, 'err_desc': 'invalid service answer'}
|
||||
if response.get('success'):
|
||||
return {'err': 0, 'err_desc': response.get('success')}
|
||||
|
|
|
@ -151,6 +151,10 @@ def unauthorized_mock(url, request):
|
|||
return {'content': UNAUTHORIZED_EXAMPLE, 'request': request, 'status_code': 200}
|
||||
|
||||
|
||||
def error_500_mock(url, request):
|
||||
return {'content': '{}', 'request': request, 'status_code': 500}
|
||||
|
||||
|
||||
def favorites_mock(url, request):
|
||||
if url.path.endswith('/get-user-favorites'):
|
||||
return {'content': FAVORITES_EXAMPLE, 'request': request, 'status_code': 200}
|
||||
|
@ -206,6 +210,10 @@ def test_interests(app, strasbourg_eu):
|
|||
resp = app.get(endpoint).json
|
||||
assert resp['err_desc'] == 'not authorized'
|
||||
|
||||
with HTTMock(error_500_mock):
|
||||
resp = app.get(endpoint).json
|
||||
assert resp['err_desc'] == 'invalid service answer'
|
||||
|
||||
|
||||
def test_notifications(app, strasbourg_eu, caplog):
|
||||
endpoint = utils.generic_endpoint_url('strasbourg-eu', 'notifications', slug=strasbourg_eu.slug)
|
||||
|
@ -242,6 +250,10 @@ def test_notifications(app, strasbourg_eu, caplog):
|
|||
resp = app.get(endpoint + '?name_id=xxx').json
|
||||
assert resp['err_desc'] == 'not authorized'
|
||||
|
||||
with HTTMock(error_500_mock):
|
||||
resp = app.get(endpoint + '?name_id=xxx').json
|
||||
assert resp['err_desc'] == 'invalid service answer'
|
||||
|
||||
|
||||
def test_favorites(app, strasbourg_eu):
|
||||
endpoint = utils.generic_endpoint_url('strasbourg-eu', 'favorites', slug=strasbourg_eu.slug)
|
||||
|
@ -262,3 +274,7 @@ def test_favorites(app, strasbourg_eu):
|
|||
with HTTMock(unauthorized_mock):
|
||||
resp = app.get(endpoint + '?name_id=xxx').json
|
||||
assert resp['err_desc'] == 'not authorized'
|
||||
|
||||
with HTTMock(error_500_mock):
|
||||
resp = app.get(endpoint + '?name_id=xxx').json
|
||||
assert resp['err_desc'] == 'invalid service answer'
|
||||
|
|
Loading…
Reference in New Issue