Skip to content

Commit

Permalink
fix: don't put GPS status in session
Browse files Browse the repository at this point in the history
Putting it in session can have many side effects on job application
(like not being able to restart a job application from multiple starting
point).
Passing the gps flag as query param when we need it is better suited for
what we want to do now and avoid messing with the already complicated
session management for a job application.
  • Loading branch information
vjousse committed May 29, 2024
1 parent 1eeddd9 commit 55739c8
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 94 deletions.
43 changes: 29 additions & 14 deletions itou/templates/apply/submit_step_job_seeker.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,42 @@ <h3 class="modal-title" id="email-confirmation-label">Email existant</h3>
</p>
<p>
L'identité du candidat est une information clé pour la structure.
Si cette candidature n'est pas pour <b>{{ job_seeker.get_full_name|mask_unless:can_view_personal_information }}</b>, cliquez sur
« Ce n'est pas mon candidat » afin d'enregistrer ses informations
personnelles.
{% if is_gps %}
Si vous ne souhaitez pas suivre <b>{{ job_seeker.get_full_name|mask_unless:can_view_personal_information }}</b>, cliquez sur « Suivre un autre bénéficiaire » afin d'enregistrer ses informations personnelles.
{% else %}
Si cette candidature n'est pas pour <b>{{ job_seeker.get_full_name|mask_unless:can_view_personal_information }}</b>, cliquez sur
« Ce n'est pas mon candidat » afin d'enregistrer ses informations
personnelles.
{% endif %}
</p>
{% if can_add_nir %}
<p>
En cliquant sur « Continuer », <b>vous acceptez que le numéro de sécurité sociale {{ nir|format_nir }} soit associé à ce candidat.</b>
</p>
{% endif %}
</div>
<div class="modal-footer">
{# Reload this page with a new form. #}
En cliquant sur « Continuer », <b>vous acceptez que le numéro de sécurité sociale {{ nir|format_nir }} soit associé à ce
{% if is_gps %}
bénéficiaire
{% else %}
candidat
{% endif %}
.</b>
</p>
{% endif %}
</div>
<div class="modal-footer">

{# Reload this page with a new form. #}
{% if is_gps %}
{% bootstrap_button "Suivre un autre bénéficiaire" button_type="submit" button_class="btn btn-sm btn-outline-primary" name="cancel" value="1" %}
{% else %}
{% bootstrap_button "Ce n'est pas mon candidat" button_type="submit" button_class="btn btn-sm btn-outline-primary" name="cancel" value="1" %}
{# Go to the next step. #}
{% bootstrap_button "Continuer" button_type="submit" button_class="btn btn-sm btn-primary" name="confirm" value="1" %}
</div>
{% endif %}
{# Go to the next step. #}
{% bootstrap_button "Continuer" button_type="submit" button_class="btn btn-sm btn-primary" name="confirm" value="1" %}
</div>
</div>
</div>
{% endif %}
</form>
</div>
{% endif %}
</form>
{% endblock %}

{% block script %}
Expand Down
2 changes: 1 addition & 1 deletion itou/www/apply/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def __init__(self, *args, job_seeker=None, is_gps=False, **kwargs):
if self.job_seeker:
self.fields["nir"].label = "Votre numéro de sécurité sociale"
else:
self.fields["nir"].label = "Numéro de sécurité sociale du " + "bénéficiaire" if is_gps else "candidat"
self.fields["nir"].label = "Numéro de sécurité sociale du " + ("bénéficiaire" if is_gps else "candidat")

def clean_nir(self):
nir = self.cleaned_data["nir"].upper()
Expand Down
32 changes: 16 additions & 16 deletions itou/www/apply/views/submit_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,7 @@ def setup(self, request, *args, **kwargs):

super().setup(request, *args, **kwargs)

gps_activated_with_request_paramater = "gps" in request.GET and request.GET["gps"] == "true"

if not self.apply_session.exists() and not gps_activated_with_request_paramater:
self.is_gps = False
else:
self.is_gps = (
self.apply_session.exists() and self.apply_session.get("gps", False)
) or gps_activated_with_request_paramater
self.is_gps = "gps" in request.GET and request.GET["gps"] == "true"

def dispatch(self, request, *args, **kwargs):
if request.user.is_authenticated:
Expand Down Expand Up @@ -217,12 +210,8 @@ def get(self, request, *args, **kwargs):
if self.company.block_job_applications and not self.company.has_member(request.user):
raise Http404("Cette organisation n'accepte plus de candidatures pour le moment.")

# Create a sub-session for this job application process
gps_flag_in_request = True if ("gps" in request.GET and request.GET["gps"] == "true") or () else False

self.apply_session.init(
{"selected_jobs": [request.GET["job_description_id"]] if "job_description_id" in request.GET else []}
| ({"gps": True} if gps_flag_in_request else {})
)

# Warn message if prescriber's authorization is pending
Expand All @@ -236,7 +225,10 @@ def get(self, request, *args, **kwargs):
reverse("apply:pending_authorization_for_sender", kwargs={"company_pk": self.company.pk})
)

return HttpResponseRedirect(reverse(f"apply:check_nir_for_{tunnel}", kwargs={"company_pk": self.company.pk}))
return HttpResponseRedirect(
reverse(f"apply:check_nir_for_{tunnel}", kwargs={"company_pk": self.company.pk})
+ ("?gps=true" if self.is_gps else "")
)


class PendingAuthorizationForSender(ApplyStepForSenderBaseView):
Expand Down Expand Up @@ -317,6 +309,7 @@ def redirect_to_check_email(self, session_uuid):
view_name = "apply:search_by_email_for_hire" if self.hire_process else "apply:search_by_email_for_sender"
return HttpResponseRedirect(
reverse(view_name, kwargs={"company_pk": self.company.pk, "session_uuid": session_uuid})
+ ("?gps=true" if self.is_gps else "")
)

def post(self, request, *args, **kwargs):
Expand Down Expand Up @@ -403,6 +396,7 @@ def post(self, request, *args, **kwargs):
reverse(
view_name, kwargs={"company_pk": self.company.pk, "session_uuid": self.job_seeker_session.name}
)
+ ("?gps=true" if self.is_gps else "")
)

# Ask the sender to confirm the email we found is associated to the correct user
Expand All @@ -429,7 +423,13 @@ def post(self, request, *args, **kwargs):
messages.warning(request, msg)
logger.exception("step_job_seeker: error when saving job_seeker=%s nir=%s", job_seeker, nir)
else:
return self.redirect_to_check_infos(job_seeker.pk)
if self.is_gps:
FollowUpGroup.objects.follow_beneficiary(
beneficiary=job_seeker, user=request.user, is_referent=True
)
return HttpResponseRedirect(reverse("gps:my_groups"))
else:
return self.redirect_to_check_infos(job_seeker.pk)

return self.render_to_response(
self.get_context_data(**kwargs)
Expand Down Expand Up @@ -470,14 +470,14 @@ def get_back_url(self):
return reverse(
view_name,
kwargs={"company_pk": self.company.pk, "session_uuid": self.job_seeker_session.name},
)
) + ("?gps=true" if self.is_gps else "")

def get_next_url(self):
view_name = self.next_hire_url if self.hire_process else self.next_apply_url
return reverse(
view_name,
kwargs={"company_pk": self.company.pk, "session_uuid": self.job_seeker_session.name},
)
) + ("?gps=true" if self.is_gps else "")


class CreateJobSeekerStep1ForSenderView(CreateJobSeekerForSenderBaseView):
Expand Down
100 changes: 37 additions & 63 deletions tests/gps/test_create_beneficiary.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ def test_create_job_seeker(_mock, client):
with_ban_geoloc_address=True,
)

default_session_data = {"selected_jobs": [], "gps": True}

response = client.get(reverse("dashboard:index"))

apply_start_url = reverse("apply:start", kwargs={"company_pk": company.pk}) + "?gps=true"
Expand All @@ -54,23 +52,22 @@ def test_create_job_seeker(_mock, client):

assert response.status_code == 302

# The GPS flag should have been set in session
assert client.session[f"job_application-{company.pk}"] == default_session_data

next_url = reverse("apply:check_nir_for_sender", kwargs={"company_pk": company.pk})
next_url = reverse("apply:check_nir_for_sender", kwargs={"company_pk": company.pk}) + "?gps=true"
assertRedirects(response, next_url)

response = client.get(next_url)
assert response.status_code == 200

response = client.post(next_url, data={"nir": dummy_job_seeker.jobseeker_profile.nir, "confirm": 1})
assert client.session[f"job_application-{company.pk}"] == default_session_data
assert response.status_code == 302

job_seeker_session_name = str(resolve(response.url).kwargs["session_uuid"])
next_url = reverse(
"apply:search_by_email_for_sender",
kwargs={"company_pk": company.pk, "session_uuid": job_seeker_session_name},
job_seeker_session_name = str(resolve(response.url.replace("?gps=true", "")).kwargs["session_uuid"])
next_url = (
reverse(
"apply:search_by_email_for_sender",
kwargs={"company_pk": company.pk, "session_uuid": job_seeker_session_name},
)
+ "?gps=true"
)

assert response.url == next_url
Expand All @@ -84,7 +81,7 @@ def test_create_job_seeker(_mock, client):

response = client.post(next_url, data={"email": dummy_job_seeker.email, "confirm": "1"})
assert response.status_code == 302
job_seeker_session_name = str(resolve(response.url).kwargs["session_uuid"])
job_seeker_session_name = str(resolve(response.url.replace("?gps=true", "")).kwargs["session_uuid"])

expected_job_seeker_session = {
"user": {
Expand All @@ -96,9 +93,12 @@ def test_create_job_seeker(_mock, client):
}
assert client.session[job_seeker_session_name] == expected_job_seeker_session

next_url = reverse(
"apply:create_job_seeker_step_1_for_sender",
kwargs={"company_pk": company.pk, "session_uuid": job_seeker_session_name},
next_url = (
reverse(
"apply:create_job_seeker_step_1_for_sender",
kwargs={"company_pk": company.pk, "session_uuid": job_seeker_session_name},
)
+ "?gps=true"
)
assert response.url == next_url

Expand Down Expand Up @@ -131,9 +131,12 @@ def test_create_job_seeker(_mock, client):
expected_job_seeker_session["user"] |= post_data
assert client.session[job_seeker_session_name] == expected_job_seeker_session

next_url = reverse(
"apply:create_job_seeker_step_2_for_sender",
kwargs={"company_pk": company.pk, "session_uuid": job_seeker_session_name},
next_url = (
reverse(
"apply:create_job_seeker_step_2_for_sender",
kwargs={"company_pk": company.pk, "session_uuid": job_seeker_session_name},
)
+ "?gps=true"
)
assert response.url == next_url

Expand All @@ -156,9 +159,12 @@ def test_create_job_seeker(_mock, client):
expected_job_seeker_session["user"] |= post_data | {"address_line_2": "", "address_for_autocomplete": None}
assert client.session[job_seeker_session_name] == expected_job_seeker_session

next_url = reverse(
"apply:create_job_seeker_step_3_for_sender",
kwargs={"company_pk": company.pk, "session_uuid": job_seeker_session_name},
next_url = (
reverse(
"apply:create_job_seeker_step_3_for_sender",
kwargs={"company_pk": company.pk, "session_uuid": job_seeker_session_name},
)
+ "?gps=true"
)
assert response.url == next_url

Expand Down Expand Up @@ -191,9 +197,12 @@ def test_create_job_seeker(_mock, client):
}
assert client.session[job_seeker_session_name] == expected_job_seeker_session

next_url = reverse(
"apply:create_job_seeker_step_end_for_sender",
kwargs={"company_pk": company.pk, "session_uuid": job_seeker_session_name},
next_url = (
reverse(
"apply:create_job_seeker_step_end_for_sender",
kwargs={"company_pk": company.pk, "session_uuid": job_seeker_session_name},
)
+ "?gps=true"
)
assert response.url == next_url

Expand All @@ -211,7 +220,7 @@ def test_create_job_seeker_for_any_company(client):
apply_start_url = reverse("apply:start", kwargs={"company_pk": company.pk}) + "?gps=true"
response = client.get(apply_start_url)

next_url = reverse("apply:check_nir_for_sender", kwargs={"company_pk": company.pk})
next_url = reverse("apply:check_nir_for_sender", kwargs={"company_pk": company.pk}) + "?gps=true"
assertRedirects(response, next_url)


Expand All @@ -228,7 +237,7 @@ def test_gps_bypass(client):
apply_start_url = reverse("apply:start", kwargs={"company_pk": company.pk}) + "?gps=true"
response = client.get(apply_start_url)

next_url = reverse("apply:check_nir_for_sender", kwargs={"company_pk": company.pk})
next_url = reverse("apply:check_nir_for_sender", kwargs={"company_pk": company.pk}) + "?gps=true"
assertRedirects(response, next_url)

# SIAE has an active suspension, but we should be able to create the job_seeker for GPS
Expand All @@ -244,7 +253,7 @@ def test_gps_bypass(client):
apply_start_url = reverse("apply:start", kwargs={"company_pk": company.pk}) + "?gps=true"
response = client.get(apply_start_url)

next_url = reverse("apply:check_nir_for_sender", kwargs={"company_pk": company.pk})
next_url = reverse("apply:check_nir_for_sender", kwargs={"company_pk": company.pk}) + "?gps=true"
assertRedirects(response, next_url)

# The company has block job applications but we should be able to create the job_seeker anyway
Expand All @@ -257,40 +266,5 @@ def test_gps_bypass(client):
apply_start_url = reverse("apply:start", kwargs={"company_pk": company.pk}) + "?gps=true"
response = client.get(apply_start_url)

next_url = reverse("apply:check_nir_for_sender", kwargs={"company_pk": company.pk})
next_url = reverse("apply:check_nir_for_sender", kwargs={"company_pk": company.pk}) + "?gps=true"
assertRedirects(response, next_url)


def test_gps_flag_reset(client):
company = CompanyWithMembershipAndJobsFactory(romes=("N1101", "N1105"))

prescriber_organization = PrescriberOrganizationWithMembershipFactory()
user = prescriber_organization.members.first()
client.force_login(user)

default_session_data = {"selected_jobs": [], "gps": True}

response = client.get(reverse("dashboard:index"))

apply_start_url = reverse("apply:start", kwargs={"company_pk": company.pk}) + "?gps=true"

response = client.get(apply_start_url)

# The GPS flag should have been set in session
assert client.session[f"job_application-{company.pk}"] == default_session_data

next_url = reverse("apply:check_nir_for_sender", kwargs={"company_pk": company.pk})
assertRedirects(response, next_url)

client.get(next_url)

# The GPS flag should always be present in session
assert client.session[f"job_application-{company.pk}"] == default_session_data

# If we start a new apply session without the GPS flag, it should be removed from the session
apply_start_url = reverse("apply:start", kwargs={"company_pk": company.pk})

response = client.get(apply_start_url)

# The GPS flag should have been removed from the session
assert client.session[f"job_application-{company.pk}"] == {"selected_jobs": []}

0 comments on commit 55739c8

Please sign in to comment.