Skip to content

Commit

Permalink
Swap error messages for validate_birth_location
Browse files Browse the repository at this point in the history
The error messages were swapped wrt their conditions.
Refactor the validator to be clearer by cascading ifs.

Add missing tests to all classes using the mixin.
  • Loading branch information
francoisfreitag committed Dec 6, 2024
1 parent bd94ca0 commit 5328db7
Show file tree
Hide file tree
Showing 5 changed files with 384 additions and 88 deletions.
15 changes: 8 additions & 7 deletions itou/utils/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,14 @@ def validate_birthdate(birthdate):


def validate_birth_location(birth_country, birth_place):
# If birth country is France, then birth place must be provided
if birth_country and birth_country.code == Country.INSEE_CODE_FRANCE and not birth_place:
raise ValidationError("Il n'est pas possible de saisir une commune de naissance hors de France.")

# If birth country is not France, do not fill a birth place (no ref file)
if birth_country and birth_country.code != Country.INSEE_CODE_FRANCE and birth_place:
raise ValidationError("Si le pays de naissance est la France, la commune de naissance est obligatoire.")
if birth_country:
if birth_country.code == Country.INSEE_CODE_FRANCE:
if not birth_place:
raise ValidationError(
"Si le pays de naissance est la France, la commune de naissance est obligatoire."
)
elif birth_place:
raise ValidationError("Il n'est pas possible de saisir une commune de naissance hors de France.")


AF_NUMBER_PREFIX_REGEXPS = [
Expand Down
55 changes: 53 additions & 2 deletions tests/www/apply/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
)

from itou.approvals.models import Approval, Suspension
from itou.asp.models import Commune
from itou.asp.models import Commune, Country
from itou.cities.models import City
from itou.companies.enums import CompanyKind, ContractType, JobDescriptionSource
from itou.eligibility.enums import CERTIFIABLE_ADMINISTRATIVE_CRITERIA_KINDS, AuthorKind
Expand All @@ -49,7 +49,7 @@
ApprovalFactory,
SuspensionFactory,
)
from tests.asp.factories import CommuneFactory, CountryFranceFactory
from tests.asp.factories import CommuneFactory, CountryEuropeFactory, CountryFranceFactory
from tests.cities.factories import create_test_cities
from tests.companies.factories import CompanyFactory, CompanyMembershipFactory, JobDescriptionFactory
from tests.eligibility.factories import GEIQEligibilityDiagnosisFactory, IAEEligibilityDiagnosisFactory
Expand Down Expand Up @@ -2738,6 +2738,57 @@ def test_accept_updated_birthdate_invalidating_birth_place(self, client, mocker)
post_data["birthdate"] = birth_place.start_date + datetime.timedelta(days=1)
response, _ = self.accept_job_application(client, job_application, post_data=post_data, assert_successful=True)

@freeze_time("2024-09-11")
def test_accept_born_in_france_no_birth_place(self, client, mocker):
birthdate = datetime.date(1995, 12, 27)
job_application = self.create_job_application(
with_certifiable_criteria=True,
job_seeker__jobseeker_profile__birthdate=birthdate,
)
client.force_login(job_application.to_company.members.get())
post_data = self._accept_view_post_data(job_application=job_application)
post_data["birth_country"] = Country.objects.get(code=Country.INSEE_CODE_FRANCE).pk
del post_data["birth_place"]
response = client.post(
reverse("apply:accept", kwargs={"job_application_id": job_application.pk}),
headers={"hx-request": "true"},
data=post_data,
)
assertContains(
response,
"""
<div class="alert alert-danger" role="alert">
Si le pays de naissance est la France, la commune de naissance est obligatoire.
</div>""",
html=True,
count=1,
)

@freeze_time("2024-09-11")
def test_accept_born_outside_of_france_specifies_birth_place(self, client, mocker):
birthdate = datetime.date(1995, 12, 27)
job_application = self.create_job_application(
with_certifiable_criteria=True,
job_seeker__jobseeker_profile__birthdate=birthdate,
)
client.force_login(job_application.to_company.members.get())
post_data = self._accept_view_post_data(job_application=job_application)
post_data["birth_country"] = CountryEuropeFactory().pk
response = client.post(
reverse("apply:accept", kwargs={"job_application_id": job_application.pk}),
headers={"hx-request": "true"},
data=post_data,
)
assertContains(
response,
"""
<div class="alert alert-danger" role="alert">
Il n'est pas possible de saisir une commune de naissance hors de France.
</div>""",
html=True,
count=1,
)


class TestProcessTemplates:
"""
Expand Down
32 changes: 32 additions & 0 deletions tests/www/employee_record_views/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,38 @@ def test_birthplace_outside_of_france(self, client):

assertRedirects(response, target_url)

def test_born_in_france_no_birthplace(self, client):
client.force_login(self.user)
client.get(self.url)
data = _get_user_form_data(self.job_seeker)
del data["birth_place"]
response = client.post(self.url, data=data)
assertContains(
response,
"""
<div class="alert alert-danger" role="alert">
Si le pays de naissance est la France, la commune de naissance est obligatoire.
</div>""",
html=True,
count=1,
)

def test_born_outside_of_france_specifies_birthplace(self, client):
client.force_login(self.user)
client.get(self.url)
data = _get_user_form_data(self.job_seeker)
data["birth_country"] = CountryOutsideEuropeFactory().pk
response = client.post(self.url, data=data)
assertContains(
response,
"""
<div class="alert alert-danger" role="alert">
Il n'est pas possible de saisir une commune de naissance hors de France.
</div>""",
html=True,
count=1,
)

def test_pass_step_1_without_geolocated_address(self, client):
# Do not mess with job seeker profile and geolocation at step 1
# just check user model info
Expand Down
79 changes: 0 additions & 79 deletions tests/www/job_seekers_views/test_create.py

This file was deleted.

Loading

0 comments on commit 5328db7

Please sign in to comment.