Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Candidature : vérifier la cohérence du NIR avec la date de naissance et la civilité #4746

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions itou/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,14 @@ class JobSeekerProfile(models.Model):
ERROR_JOBSEEKER_TITLE = "La civilité du demandeur d'emploi est obligatoire"
ERROR_JOBSEEKER_EDUCATION_LEVEL = "Le niveau de formation du demandeur d'emploi est obligatoire"
ERROR_JOBSEEKER_PE_FIELDS = "L'identifiant et la durée d'inscription à France Travail vont de pair"
ERROR_JOBSEEKER_INCONSISTENT_NIR_TITLE = (
"Une erreur a été détectée."
" La civilité renseignée ne correspond pas au numéro de sécurité sociale%s enregistré."
)
ERROR_JOBSEEKER_INCONSISTENT_NIR_BIRTHDATE = (
"Une erreur a été détectée."
" La date de naissance renseignée ne correspond pas au numéro de sécurité sociale%s enregistré."
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Je verrais bien des erreurs plus spécifique plutôt que de laisser l'utilisateur essayer de deviner.
Peut-être en mettant d'ailleurs l'erreur sur le champs fautif via un self.add_error("birthdate", ...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Des erreurs plus spécifiques d'accord, mais vu que ça concerne des paires de champs, on ne sait pas vraiment quel champ est fautif.

Certes, on a la vérification du NIR avec sa clé normalement, mais d'un point de vue UX je ne sais pas ce qui est le mieux. Je vais demander à Marion

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

J'ai eu le retour de Marion. Elle propose ces messages (pas la peine d'expliquer la structure du NIR, trop complexe).

J'ai utilisé l'opérateur % pour inclure le NIR dans le message d'erreur quand il y a besoin, mais je ne suis pas sûr que ce soit une bonne pratique.


user = models.OneToOneField(
settings.AUTH_USER_MODEL,
Expand Down Expand Up @@ -1065,6 +1073,29 @@ def clean_pole_emploi_fields(cleaned_data):
# the object is shared between the caller and the called routine.
cleaned_data["lack_of_pole_emploi_id_reason"] = ""

@staticmethod
def clean_nir_title_birthdate_fields(cleaned_data, remind_nir_in_error=False):
"""
Validate consistency between NIR, title and birthdate
"""
if cleaned_nir := cleaned_data.get("nir"):
if cleaned_title := cleaned_data.get("title"):
if (cleaned_nir[0] == "1" and cleaned_title != Title.M) or (
cleaned_nir[0] == "2" and cleaned_title != Title.MME
):
raise ValidationError(
JobSeekerProfile.ERROR_JOBSEEKER_INCONSISTENT_NIR_TITLE
% (f" {cleaned_nir}" if remind_nir_in_error else "")
)
if cleaned_birthdate := cleaned_data.get("birthdate"):
nir_year_month = cleaned_nir[1:5]
birthdate_year_month = cleaned_birthdate.strftime("%y%m")
if nir_year_month != birthdate_year_month:
raise ValidationError(
JobSeekerProfile.ERROR_JOBSEEKER_INCONSISTENT_NIR_BIRTHDATE
% (f" {cleaned_nir}" if remind_nir_in_error else "")
)

def _clean_job_seeker_details(self):
# Title is not mandatory for User, but it is for ASP
if not self.user.title:
Expand Down
7 changes: 7 additions & 0 deletions itou/www/apply/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ def __init__(self, *args, **kwargs):
def clean(self):
super().clean()
JobSeekerProfile.clean_pole_emploi_fields(self.cleaned_data)
JobSeekerProfile.clean_nir_title_birthdate_fields(
self.cleaned_data | {"nir": self.instance.jobseeker_profile.nir}, remind_nir_in_error=True
)
EwenKorr marked this conversation as resolved.
Show resolved Hide resolved


class CreateOrUpdateJobSeekerStep1Form(
Expand Down Expand Up @@ -195,6 +198,10 @@ def __init__(self, *args, **kwargs):
}
)

def clean(self):
super().clean()
JobSeekerProfile.clean_nir_title_birthdate_fields(self.cleaned_data)


class CreateOrUpdateJobSeekerStep2Form(JobSeekerAddressForm, forms.ModelForm):
class Meta(JobSeekerAddressForm.Meta):
Expand Down
1 change: 1 addition & 0 deletions itou/www/dashboard/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def __init__(self, *args, **kwargs):
def clean(self):
super().clean()
JobSeekerProfile.clean_pole_emploi_fields(self.cleaned_data)
JobSeekerProfile.clean_nir_title_birthdate_fields(self.cleaned_data)

def save(self, commit=True):
self.instance.last_checked_at = timezone.now()
Expand Down
2 changes: 1 addition & 1 deletion tests/users/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ class Params:

@factory.lazy_attribute
def nir(self):
gender = random.choice([1, 2])
gender = "1" if self.user.title == Title.M else "2"
if self.birthdate:
year = self.birthdate.strftime("%y")
month = self.birthdate.strftime("%m")
Expand Down
217 changes: 214 additions & 3 deletions tests/www/apply/test_submit.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from itou.job_applications.models import JobApplication
from itou.siae_evaluations.models import Sanctions
from itou.users.enums import LackOfNIRReason, LackOfPoleEmploiId
from itou.users.models import User
from itou.users.models import JobSeekerProfile, User
from itou.utils.mocks.address_format import mock_get_first_geocoding_data, mock_get_geocoding_data_by_ban_api_resolved
from itou.utils.models import InclusiveDateRange
from itou.utils.session import SessionNamespace
Expand Down Expand Up @@ -346,7 +346,7 @@ def test_apply_as_jobseeker(self):
response = self.client.get(next_url)
assert response.status_code == 200

nir = "141068078200557"
nir = "178122978200508"
post_data = {"nir": nir, "confirm": 1}

response = self.client.post(next_url, data=post_data)
Expand Down Expand Up @@ -1399,6 +1399,9 @@ def test_apply_as_prescriber(self, _mock):
jobseeker_profile__with_hexa_address=True,
jobseeker_profile__with_education_level=True,
with_ban_geoloc_address=True,
jobseeker_profile__nir="178122978200508",
jobseeker_profile__birthdate=datetime.date(1978, 12, 20),
title="M",
)

# Entry point.
Expand Down Expand Up @@ -1473,6 +1476,36 @@ def test_apply_as_prescriber(self, _mock):
geispolsheim = create_city_geispolsheim()
birthdate = dummy_job_seeker.jobseeker_profile.birthdate

# Let's check for consistency between the NIR, the birthdate and the title.
# ----------------------------------------------------------------------

post_data = {
"title": "MME", # inconsistent title
"first_name": dummy_job_seeker.first_name,
"last_name": dummy_job_seeker.last_name,
"birthdate": birthdate,
"lack_of_nir": False,
"lack_of_nir_reason": "",
}
response = self.client.post(next_url, data=post_data)
assert response.status_code == 200
self.assertContains(response, JobSeekerProfile.ERROR_JOBSEEKER_INCONSISTENT_NIR_TITLE % "")

post_data = {
"title": "M",
"first_name": dummy_job_seeker.first_name,
"last_name": dummy_job_seeker.last_name,
"birthdate": datetime.date(1978, 11, 20), # inconsistent birthdate
"lack_of_nir": False,
"lack_of_nir_reason": "",
}
response = self.client.post(next_url, data=post_data)
assert response.status_code == 200
self.assertContains(response, JobSeekerProfile.ERROR_JOBSEEKER_INCONSISTENT_NIR_BIRTHDATE % "")

# Resume to valid data and proceed with "normal" flow.
# ----------------------------------------------------------------------

post_data = {
"title": dummy_job_seeker.title,
"first_name": dummy_job_seeker.first_name,
Expand Down Expand Up @@ -1674,6 +1707,41 @@ def test_apply_as_prescriber_on_job_seeker_tunnel(self):
response, reverse("apply:start", kwargs={"company_pk": company.pk}), fetch_redirect_response=False
)

def test_check_info_as_prescriber_for_job_seeker_with_incomplete_info(self):
company = CompanyFactory(with_membership=True, with_jobs=True, romes=("N1101", "N1105"))
user = PrescriberFactory()
self.client.force_login(user)

dummy_job_seeker = JobSeekerFactory(
jobseeker_profile__with_hexa_address=True,
jobseeker_profile__with_education_level=True,
with_ban_geoloc_address=True,
jobseeker_profile__nir="178122978200508",
jobseeker_profile__birthdate=None,
title="M",
)

next_url = reverse(
"apply:step_check_job_seeker_info",
kwargs={"company_pk": company.pk, "job_seeker_public_id": dummy_job_seeker.public_id},
)

post_data = {
"phone": "",
"birthdate": datetime.date(1978, 11, 20), # inconsistent birthdate
"pole_emploi_id": "3454471C",
}
response = self.client.post(next_url, data=post_data)
assert response.status_code == 200
self.assertContains(
response,
(
"Une erreur a été détectée. "
"La date de naissance renseignée ne correspond pas au numéro de sécurité "
"sociale 178122978200508 enregistré."
Comment on lines +1739 to +1741
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

J'ai écrit le message d'erreur en dur pour être sûr le NIR soit bien inclus.

),
)


@pytest.mark.ignore_unknown_variable_template_error("job_seeker")
class ApplyAsPrescriberNirExceptionsTest(TestCase):
Expand Down Expand Up @@ -1943,6 +2011,36 @@ def _test_apply_as_company(self, user, company, dummy_job_seeker, _mock):
geispolsheim = create_city_geispolsheim()
birthdate = dummy_job_seeker.jobseeker_profile.birthdate

# Let's check for consistency between the NIR, the birthdate and the title.
# ----------------------------------------------------------------------

post_data = {
"title": "M", # inconsistent title
"first_name": dummy_job_seeker.first_name,
"last_name": dummy_job_seeker.last_name,
"birthdate": birthdate,
"lack_of_nir": False,
"lack_of_nir_reason": "",
}
response = self.client.post(next_url, data=post_data)
assert response.status_code == 200
self.assertContains(response, JobSeekerProfile.ERROR_JOBSEEKER_INCONSISTENT_NIR_TITLE % "")

post_data = {
"title": "MME",
"first_name": dummy_job_seeker.first_name,
"last_name": dummy_job_seeker.last_name,
"birthdate": datetime.date(1978, 11, 20), # inconsistent birthdate
"lack_of_nir": False,
"lack_of_nir_reason": "",
}
response = self.client.post(next_url, data=post_data)
assert response.status_code == 200
self.assertContains(response, JobSeekerProfile.ERROR_JOBSEEKER_INCONSISTENT_NIR_BIRTHDATE % "")

# Resume to valid data and proceed with "normal" flow.
# ----------------------------------------------------------------------

post_data = {
"title": dummy_job_seeker.title,
"first_name": dummy_job_seeker.first_name,
Expand Down Expand Up @@ -2127,6 +2225,9 @@ def test_apply_as_employer(self):
jobseeker_profile__with_hexa_address=True,
jobseeker_profile__with_education_level=True,
with_ban_geoloc_address=True,
jobseeker_profile__nir="278122978200555",
jobseeker_profile__birthdate=datetime.date(1978, 12, 20),
title="MME",
)
self._test_apply_as_company(employer, company, dummy_job_seeker)

Expand All @@ -2142,9 +2243,47 @@ def test_apply_as_another_employer(self):
jobseeker_profile__with_hexa_address=True,
jobseeker_profile__with_education_level=True,
with_ban_geoloc_address=True,
jobseeker_profile__nir="278122978200555",
jobseeker_profile__birthdate=datetime.date(1978, 12, 20),
title="MME",
)
self._test_apply_as_company(employer, company, dummy_job_seeker)

def test_check_info_as_employer_for_job_seeker_with_incomplete_info(self):
company = CompanyFactory(with_membership=True, with_jobs=True, romes=("N1101", "N1105"))
employer = EmployerFactory(with_company=True)
self.client.force_login(employer)

dummy_job_seeker = JobSeekerFactory(
jobseeker_profile__with_hexa_address=True,
jobseeker_profile__with_education_level=True,
with_ban_geoloc_address=True,
jobseeker_profile__nir="278122978200555",
jobseeker_profile__birthdate=None,
title="MME",
)

next_url = reverse(
"apply:step_check_job_seeker_info",
kwargs={"company_pk": company.pk, "job_seeker_public_id": dummy_job_seeker.public_id},
)

post_data = {
"phone": "",
"birthdate": datetime.date(1978, 11, 20), # inconsistent birthdate
"pole_emploi_id": "3454471C",
}
response = self.client.post(next_url, data=post_data)
assert response.status_code == 200
self.assertContains(
response,
(
"Une erreur a été détectée. "
"La date de naissance renseignée ne correspond pas au numéro de sécurité "
"sociale 278122978200555 enregistré."
),
)

@pytest.mark.ignore_unknown_variable_template_error("job_seeker")
def test_cannot_create_job_seeker_with_pole_emploi_email(self):
# It's unlikely to happen
Expand Down Expand Up @@ -2235,6 +2374,9 @@ def test_hire_as_company(self, _mock):
jobseeker_profile__with_hexa_address=True,
jobseeker_profile__with_education_level=True,
with_ban_geoloc_address=True,
jobseeker_profile__nir="178122978200508",
jobseeker_profile__birthdate=datetime.date(1978, 12, 20),
title="M",
)

geispolsheim = create_city_geispolsheim()
Expand Down Expand Up @@ -2298,6 +2440,36 @@ def test_hire_as_company(self, _mock):

birthdate = dummy_job_seeker.jobseeker_profile.birthdate

# Let's check for consistency between the NIR, the birthdate and the title.
# ----------------------------------------------------------------------

post_data = {
"title": "MME", # inconsistent title
"first_name": dummy_job_seeker.first_name,
"last_name": dummy_job_seeker.last_name,
"birthdate": birthdate,
"lack_of_nir": False,
"lack_of_nir_reason": "",
}
response = self.client.post(next_url, data=post_data)
assert response.status_code == 200
self.assertContains(response, JobSeekerProfile.ERROR_JOBSEEKER_INCONSISTENT_NIR_TITLE % "")

post_data = {
"title": "M",
"first_name": dummy_job_seeker.first_name,
"last_name": dummy_job_seeker.last_name,
"birthdate": datetime.date(1978, 11, 20), # inconsistent birthdate
"lack_of_nir": False,
"lack_of_nir_reason": "",
}
response = self.client.post(next_url, data=post_data)
assert response.status_code == 200
self.assertContains(response, JobSeekerProfile.ERROR_JOBSEEKER_INCONSISTENT_NIR_BIRTHDATE % "")

# Resume to valid data and proceed with "normal" flow.
# ----------------------------------------------------------------------

post_data = {
"title": dummy_job_seeker.title,
"first_name": dummy_job_seeker.first_name,
Expand Down Expand Up @@ -2990,7 +3162,12 @@ class UpdateJobSeekerBaseTestCase(TestCase):
@classmethod
def setUpTestData(cls):
cls.company = CompanyFactory(subject_to_eligibility=True, with_membership=True)
cls.job_seeker = JobSeekerFactory(with_ban_geoloc_address=True)
cls.job_seeker = JobSeekerFactory(
with_ban_geoloc_address=True,
jobseeker_profile__nir="178122978200508",
jobseeker_profile__birthdate=datetime.date(1978, 12, 20),
title="M",
)
cls.step_1_url = reverse(
cls.STEP_1_VIEW_NAME,
kwargs={"company_pk": cls.company.pk, "job_seeker_public_id": cls.job_seeker.public_id},
Expand Down Expand Up @@ -3037,6 +3214,38 @@ def _check_everything_allowed(self, user, extra_post_data_1=None):
self.assertContains(response, self.job_seeker.first_name)
self.assertNotContains(response, self.INFO_MODIFIABLE_PAR_CANDIDAT_UNIQUEMENT)

# Let's check for consistency between the NIR, the birthdate and the title.
# (but do not check when there is no NIR)
# ----------------------------------------------------------------------

if self.job_seeker.jobseeker_profile.nir != "":
post_data = {
"title": "MME", # Inconsistent title
"first_name": self.job_seeker.first_name,
"last_name": self.job_seeker.last_name,
"birthdate": self.job_seeker.jobseeker_profile.birthdate,
"lack_of_nir": False,
"lack_of_nir_reason": "",
}
response = self.client.post(self.step_1_url, data=post_data)
assert response.status_code == 200
self.assertContains(response, JobSeekerProfile.ERROR_JOBSEEKER_INCONSISTENT_NIR_TITLE % "")

post_data = {
"title": "M",
"first_name": self.job_seeker.first_name,
"last_name": self.job_seeker.last_name,
"birthdate": datetime.date(1978, 11, 20), # Inconsistent birthdate
"lack_of_nir": False,
"lack_of_nir_reason": "",
}
response = self.client.post(self.step_1_url, data=post_data)
assert response.status_code == 200
self.assertContains(response, JobSeekerProfile.ERROR_JOBSEEKER_INCONSISTENT_NIR_BIRTHDATE % "")

# Resume to valid data and proceed with "normal" flow.
# ----------------------------------------------------------------------

NEW_FIRST_NAME = "New first name"
PROCESS_TITLE = "Modification du compte candidat"

Expand Down Expand Up @@ -3627,6 +3836,8 @@ def test_detect_existing_job_seeker(client):

job_seeker = JobSeekerFactory(
jobseeker_profile__nir="",
jobseeker_profile__birthdate=datetime.date(1997, 1, 1),
title="M",
first_name="Jérémy",
email="[email protected]",
)
Expand Down
Loading