api particulier: more flexible validation for numero_allocataire field (#58080)

This commit is contained in:
Agate 2022-07-18 09:33:30 +02:00
parent 9d9969afdb
commit 0736684511
2 changed files with 19 additions and 2 deletions

View File

@ -345,11 +345,15 @@ class APIParticulier(BaseResource):
},
)
def v2_situation_familiale(self, request, code_postal, numero_allocataire, user=None):
if not code_postal.strip() or not numero_allocataire.strip():
numero_allocataire = numero_allocataire.strip()[:7]
code_postal = code_postal.strip()
if not code_postal or not numero_allocataire:
raise APIError('missing code_postal or numero_allocataire')
if len(numero_allocataire) != 7 or not is_number(numero_allocataire):
raise APIError('numero_allocataire should be a 7 digits number')
return self.get(
data = self.get(
'v2/composition-familiale',
params={
'codePostal': code_postal,
@ -357,6 +361,9 @@ class APIParticulier(BaseResource):
},
user=user,
)
data['data']['numero_allocataire'] = numero_allocataire
data['data']['code_postal'] = code_postal
return data
category = _('Business Process Connectors')

View File

@ -338,6 +338,16 @@ def test_situation_familiale(app, resource, mock_api_particulier):
assert resp.json['err'] == 1
assert '7 digits' in resp.json['err_desc']
# last letter truncated automatically
params['numero_allocataire'] = '1234567a'
resp = endpoint_get(
'/api-particulier/test/situation-familiale', app, resource, 'situation-familiale', params=params
)
assert resp.json['data']['adresse']['codePostalVille'] == '12345 CONDAT'
# cleaned data is also inlcuded in the response
assert resp.json['data']['numero_allocataire'] == '1234567'
assert resp.json['data']['code_postal'] == params['code_postal']
params['code_postal'] = ' '
resp = endpoint_get(
'/api-particulier/test/situation-familiale', app, resource, 'situation-familiale', params=params