From 9158998453a2237e9679a204786f91c070a3863e Mon Sep 17 00:00:00 2001 From: Xavier Fernandez Date: Thu, 23 May 2024 13:29:22 +0200 Subject: [PATCH] www.dashboard: validate pole_emploi_id in EditJobSeekerInfoForm --- itou/users/forms.py | 8 ++++++++ tests/www/dashboard/tests.py | 11 +++++++++++ 2 files changed, 19 insertions(+) diff --git a/itou/users/forms.py b/itou/users/forms.py index 10764c29f7..f9f9d4fa44 100644 --- a/itou/users/forms.py +++ b/itou/users/forms.py @@ -52,6 +52,14 @@ def _post_clean(self): jobseeker_profile = JobSeekerProfile() for k, v in self.cleaned_data_from_profile_fields.items(): setattr(jobseeker_profile, k, v) + # super()._post_clean() calls full_clean() on self.instance, which calls self.instance.clean_fields() + # Let's do the same on self.instance.jobseeker_profile (but only for our fields) + try: + jobseeker_profile.clean_fields( + exclude={f.name for f in JobSeekerProfile._meta.fields if f.name not in self.PROFILE_FIELDS} + ) + except ValidationError as e: + self._update_errors(e) try: jobseeker_profile.validate_constraints() except ValidationError as e: diff --git a/tests/www/dashboard/tests.py b/tests/www/dashboard/tests.py index 035eaa26dc..a96c363bed 100644 --- a/tests/www/dashboard/tests.py +++ b/tests/www/dashboard/tests.py @@ -1190,6 +1190,17 @@ def test_edit_with_invalid_pole_emploi_id(self): None, "Renseignez soit un identifiant France Travail (ex pôle emploi), soit la raison de son absence.", ) + post_data["pole_emploi_id"] = "invalide" # No length issue but validate_pole_emploi_id shouldn't be happy + response = self.client.post(url, data=post_data) + assert response.status_code == 200 + self.assertFormError( + response.context["form"], + "pole_emploi_id", + ( + "L'identifiant France Travail (ex pôle emploi) doit être composé de 8 caractères : " + "7 chiffres suivis d'une 1 lettre ou d'un chiffre." + ), + ) def test_edit_as_prescriber(self): user = PrescriberFactory()