solis: enhance check_status, factorize with ping endpoint (#21738)

This commit is contained in:
Thomas NOËL 2018-02-08 20:28:16 +01:00
parent d0e89d2301
commit 729160755e
2 changed files with 20 additions and 17 deletions

View File

@ -112,21 +112,21 @@ class Solis(BaseResource):
raise APIError('invalid JSON content:%r' % response.content[:1024])
def check_status(self):
# just raise an exception if something goes wrong
'''
Raise an exception if something goes wrong.
If OK, returns something usable by ping() endpoint.
'''
pong = self.request('main/isAlive')
if not pong.get('response').startswith('Solis API'):
raise Exception('bad main/isAlive response body')
try:
if not pong.get('response').startswith('Solis API est op'):
raise APIError('response is %r' % pong.get('response'), data=pong)
except (AttributeError, KeyError):
raise APIError('invalid response: %r' % pong, data=pong)
return {'data': 'pong', 'response': pong.get('response')}
@endpoint(name='ping', description=_('Check Solis API availability'))
def ping(self, request):
pong = self.request('main/isAlive')
try:
response = pong['response'] or 'empty'
except (TypeError, KeyError):
raise APIError('invalid response: %r' % pong)
if not response.startswith('Solis API'):
raise APIError('response is %s' % response, data=pong)
return {'data': 'pong', 'response': response}
return self.check_status()
def apa_token(self, user_id, code):
response = self.request('asg/apa/generationJeton', data={

View File

@ -12,7 +12,6 @@ from passerelle.apps.solis.models import Solis, SolisAPALink
from passerelle.base.models import ApiUser, AccessRight
NAMEID = 'bebe'
ISALIVE = '''{"response":"Solis API est opérationnel."}'''
APATOKEN = '''{"token":"1c2562e6-b0a9-4bcf-b669-e33a42397147","endDate":"2017-10-11T10:22:40.342"}'''
APATOKEN_403 = '''[{"logref":"db15cb8a-4d05-4e4f-b4e1-44ec39dc11e3","message":"Erreur d'authentification m\xc3\xa9tier ASG APA: Code confidentiel non valide pour l'individu 2823255","links":[]}]'''
APAINFOS = {
@ -74,12 +73,16 @@ def test_solis_ping(app, solis, ping_response):
endpoint = utils.generic_endpoint_url('solis', 'ping', slug=solis.slug)
with mock.patch('passerelle.utils.RequestSession.request') as requests_get:
ping_response.content = 'error'
requests_get.return_value = ping_response
resp = app.get(endpoint, status=200)
assert resp.json['err'] == 1
for bad_content in ('error',
'{"foo": "bar"}',
'["not", "a", "dict"]',
'{"response": "Solis API est en panne"}'):
ping_response.content = bad_content
requests_get.return_value = ping_response
resp = app.get(endpoint, status=200)
assert resp.json['err'] == 1
ping_response.content = ISALIVE
ping_response.content = '{"response":"Solis API est opérationnel."}'
requests_get.return_value = ping_response
resp = app.get(endpoint, status=200)
assert resp.json['err'] == 0