From 07366845115d8ca7c3109e206fb535290cc436bb Mon Sep 17 00:00:00 2001 From: Agate Date: Mon, 18 Jul 2022 09:33:30 +0200 Subject: [PATCH] api particulier: more flexible validation for numero_allocataire field (#58080) --- passerelle/apps/api_particulier/models.py | 11 +++++++++-- tests/test_api_particulier.py | 10 ++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/passerelle/apps/api_particulier/models.py b/passerelle/apps/api_particulier/models.py index a1da492b..2fa9df33 100644 --- a/passerelle/apps/api_particulier/models.py +++ b/passerelle/apps/api_particulier/models.py @@ -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') diff --git a/tests/test_api_particulier.py b/tests/test_api_particulier.py index f8fef890..2187e2bf 100644 --- a/tests/test_api_particulier.py +++ b/tests/test_api_particulier.py @@ -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 -- 2.36.1