solis: handle specific 'del:keys' to remove parts of Solis message (#21842)

This commit is contained in:
Thomas NOËL 2018-02-14 12:52:24 +01:00
parent a4a25eab2d
commit 3279529ddc
2 changed files with 32 additions and 3 deletions

View File

@ -299,15 +299,25 @@ class Solis(BaseResource):
if not isinstance(payload, dict):
raise APIError('payload is not a JSON dict', http_status=400)
# extract files from payload, to send them before the request
# handle specific file: and del: keys
files = []
for key, value in payload.items():
# extract files from payload, to send them before the request
if key.startswith('file:'):
if (isinstance(value, dict) and 'content' in value and 'content_type' in value):
filename = key[5:]
binary_content = base64.b64decode(value['content'])
files.append(('files', (filename, binary_content, value['content_type'])))
del payload[key]
# Solis doesn't accept somes values or dict-of-values if there are empty
# (for example is there is not "conjoint"): remove all these keys if a
# specific "del:key_prefix":true entry exists (for example "del:conjoint")
if key.startswith('del:'):
if value is True:
for k in payload.keys():
if k.startswith(key[4:]):
del payload[k]
del payload[key]
# prepare request data
integration_data = {'demandeApa': unflat(payload)}

View File

@ -468,8 +468,8 @@ def test_solis_apa_integration(app, solis):
with mock.patch('passerelle.utils.Request.post') as requests_post:
def integration_ok(*args, **kwargs):
return utils.FakedResponse(content='', status_code=204)
# requests_post.return_value = utils.FakedResponse(content='', status_code=204)
requests_post.side_effect = [utils.FakedResponse(content='', status_code=204)]
requests_post.return_value = utils.FakedResponse(content='', status_code=204)
# requests_post.side_effect = [utils.FakedResponse(content='', status_code=204)]
url = utils.generic_endpoint_url('solis', 'apa-integration', slug=solis.slug)
demande = {
@ -477,6 +477,8 @@ def test_solis_apa_integration(app, solis):
"beneficiaire_demande_dateDepot": "2018-02-09",
"beneficiaire_etatCivil_civilite": "M",
"beneficiaire_etatCivil_contact_courriel": "benef@yopmail.com",
"conjoint_nom": "Conjnom",
"conjoint_prenom": "Conjprenom",
}
resp = app.post_json(url, params=demande, status=200)
@ -484,10 +486,27 @@ def test_solis_apa_integration(app, solis):
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']['conjoint']['nom'] == 'Conjnom'
assert requests_post.call_args[1]['json']['demandeApa'] == unflat(demande)
assert resp.json['err'] == 0
assert resp.json['data'] is None
# don't send "conjoint" dict to Solis
requests_post.reset_mock()
demande['del:conjoint'] = True
resp = app.post_json(url, params=demande, status=200)
requests_post.assert_called_once()
assert 'conjoint' not in requests_post.call_args[1]['json']['demandeApa']
assert resp.json['err'] == 0
# send "conjoint" dict to Solis
requests_post.reset_mock()
demande['del:conjoint'] = False
resp = app.post_json(url, params=demande, status=200)
requests_post.assert_called_once()
assert requests_post.call_args[1]['json']['demandeApa']['conjoint']['nom'] == 'Conjnom'
assert resp.json['err'] == 0
# add a file
requests_post.reset_mock()
requests_post.side_effect = [