solis: add apa-integration endpoint (#21739)
This commit is contained in:
parent
729160755e
commit
c3b7b085c6
|
@ -43,6 +43,24 @@ def simplify(s):
|
|||
return s.strip().lower()
|
||||
|
||||
|
||||
def unflat(flatten_dict, separator='_'):
|
||||
'''
|
||||
Expand a "flatten" dict:
|
||||
>>> unflat({'foo': 'bar', 'two_foo': 'one', 'two_bar': 'two'})
|
||||
{'foo': 'bar', 'two': {'foo': 'one', 'bar': 'two'}}
|
||||
'''
|
||||
dict_ = {}
|
||||
for key, value in flatten_dict.items():
|
||||
root = dict_
|
||||
keys = [x.strip() for x in key.split(separator)]
|
||||
for key in keys[:-1]: # build branch
|
||||
if key not in root:
|
||||
root[key] = {}
|
||||
root = root[key]
|
||||
root[keys[-1]] = value # add value in leaf
|
||||
return dict_
|
||||
|
||||
|
||||
def keystore_upload_to(instance, filename):
|
||||
return '%s/%s/keystore/%s' % (instance.get_connector_slug(), instance.id, filename)
|
||||
|
||||
|
@ -271,6 +289,23 @@ class Solis(BaseResource):
|
|||
'information': information})
|
||||
return {'data': users}
|
||||
|
||||
@endpoint(name='apa-integration', perm='can_access', methods=['post'],
|
||||
description=_('Send data to "integrationDemandeApa"'))
|
||||
def apa_integration(self, request):
|
||||
try:
|
||||
payload = json.loads(request.body)
|
||||
except ValueError:
|
||||
raise APIError('payload is not a JSON object', http_status=400)
|
||||
if not isinstance(payload, dict):
|
||||
raise APIError('payload is not a JSON dict', http_status=400)
|
||||
|
||||
integration_data = {'demandeApa': unflat(payload)}
|
||||
|
||||
endpoint = 'asg/apa/integrationDemandeApa'
|
||||
response = self.request(endpoint, data=integration_data, response_is_json=False)
|
||||
return {'data': 'OK', 'response': response}
|
||||
|
||||
|
||||
@endpoint(name='referential', perm='can_access',
|
||||
pattern=r'^(?P<module>[\w-]+)/(?P<name>[\w-]+)/$',
|
||||
example_pattern='{module}/{name}/',
|
||||
|
|
|
@ -8,7 +8,7 @@ from StringIO import StringIO
|
|||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.core.files import File
|
||||
|
||||
from passerelle.apps.solis.models import Solis, SolisAPALink
|
||||
from passerelle.apps.solis.models import Solis, SolisAPALink, unflat
|
||||
from passerelle.base.models import ApiUser, AccessRight
|
||||
|
||||
NAMEID = 'bebe'
|
||||
|
@ -453,3 +453,46 @@ def test_solis_referentiels(app, solis):
|
|||
assert resp.json['err'] == 1
|
||||
assert resp.json['err_desc'] == "error status:500 'boum', content:'crash'"
|
||||
assert resp.json['data'] == {'json_content': None, 'status_code': 500}
|
||||
|
||||
|
||||
def test_unflat_dict():
|
||||
assert unflat({'foo': 'bar', 'two_foo': 'one', 'two_bar': 'two'}) == {'foo': 'bar', 'two': {'foo': 'one', 'bar': 'two'}}
|
||||
|
||||
|
||||
def test_solis_apa_integration(app, solis):
|
||||
api = ApiUser.objects.create(username='all', keytype='', key='')
|
||||
obj_type = ContentType.objects.get_for_model(solis)
|
||||
AccessRight.objects.create(codename='can_access', apiuser=api, resource_type=obj_type,
|
||||
resource_pk=solis.pk)
|
||||
|
||||
with mock.patch('passerelle.utils.Request.post') as requests_post:
|
||||
requests_post.return_value = utils.FakedResponse(content='', status_code=204)
|
||||
url = utils.generic_endpoint_url('solis', 'apa-integration', slug=solis.slug)
|
||||
|
||||
demande = {
|
||||
"beneficiaire_demande_aide": "APAD",
|
||||
"beneficiaire_demande_dateDepot": "2018-02-09",
|
||||
"beneficiaire_etatCivil_civilite": "M",
|
||||
"beneficiaire_etatCivil_contact_courriel": "benef@yopmail.com",
|
||||
}
|
||||
|
||||
resp = app.post_json(url, params=demande, status=200)
|
||||
|
||||
requests_post.assert_called_once()
|
||||
assert requests_post.call_args[0][0].endswith('/solisapi/asg/apa/integrationDemandeApa')
|
||||
assert requests_post.call_args[1]['json']['demandeApa']['beneficiaire']['demande']['aide'] == 'APAD'
|
||||
assert requests_post.call_args[1]['json']['demandeApa'] == unflat(demande)
|
||||
assert resp.json['err'] == 0
|
||||
assert resp.json['data'] == 'OK'
|
||||
assert resp.json['response'] == ''
|
||||
|
||||
# invalid inputs
|
||||
requests_post.reset_mock()
|
||||
resp = app.post_json(url, params=['not', 'a', 'dict'], status=400)
|
||||
requests_post.assert_not_called()
|
||||
assert resp.json['err'] == 1
|
||||
assert resp.json['err_desc'] == 'payload is not a JSON dict'
|
||||
resp = app.post(url, params='coin', status=400)
|
||||
requests_post.assert_not_called()
|
||||
assert resp.json['err'] == 1
|
||||
assert resp.json['err_desc'] == 'payload is not a JSON object'
|
||||
|
|
Loading…
Reference in New Issue