From f44dcdb2d5810c4dfbcc35ddf665007706cc3f85 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 | 6 +++++- tests/test_api_particulier.py | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/passerelle/apps/api_particulier/models.py b/passerelle/apps/api_particulier/models.py index f2594f74..31275a49 100644 --- a/passerelle/apps/api_particulier/models.py +++ b/passerelle/apps/api_particulier/models.py @@ -344,8 +344,12 @@ 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( diff --git a/tests/test_api_particulier.py b/tests/test_api_particulier.py index f8fef890..367c52b7 100644 --- a/tests/test_api_particulier.py +++ b/tests/test_api_particulier.py @@ -338,6 +338,13 @@ 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' + params['code_postal'] = ' ' resp = endpoint_get( '/api-particulier/test/situation-familiale', app, resource, 'situation-familiale', params=params -- 2.36.1