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

GPS : correctif pour les prescripteurs sans organisation #4265

Merged
merged 1 commit into from
Jun 20, 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
43 changes: 24 additions & 19 deletions itou/www/apply/views/submit_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from django.views.generic import TemplateView

from itou.approvals.models import Approval
from itou.companies import enums as companies_enums
from itou.companies.enums import CompanyKind
from itou.companies.models import Company, JobDescription
from itou.eligibility.models import EligibilityDiagnosis
Expand Down Expand Up @@ -83,31 +84,35 @@ def __init__(self):
self.is_gps = False

def setup(self, request, *args, **kwargs):
self.company = get_object_or_404(Company.objects.with_has_active_members(), pk=kwargs["company_pk"])
self.is_gps = "gps" in request.GET and request.GET["gps"] == "true"
self.company = (
get_object_or_404(Company.objects.with_has_active_members(), pk=kwargs["company_pk"])
if not self.is_gps
else Company.unfiltered_objects.get(siret=companies_enums.POLE_EMPLOI_SIRET)
)
self.apply_session = SessionNamespace(request.session, f"job_application-{self.company.pk}")
self.hire_process = kwargs.pop("hire_process", False)

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

self.is_gps = "gps" in request.GET and request.GET["gps"] == "true"

def dispatch(self, request, *args, **kwargs):
if request.user.is_authenticated:
if self.hire_process and request.user.kind != UserKind.EMPLOYER:
raise PermissionDenied("Seuls les employeurs sont autorisés à déclarer des embauches")
elif request.user.kind not in [
UserKind.JOB_SEEKER,
UserKind.PRESCRIBER,
UserKind.EMPLOYER,
]:
raise PermissionDenied("Vous n'êtes pas autorisé à déposer de candidature.")
elif request.user.is_employer and not self.company.has_member(request.user):
raise PermissionDenied("Vous ne pouvez postuler pour un candidat que dans votre structure.")

if not self.company.has_active_members and not self.is_gps:
raise PermissionDenied(
"Cet employeur n'est pas inscrit, vous ne pouvez pas déposer de candidatures en ligne."
)
if not self.is_gps:
if request.user.is_authenticated:
if self.hire_process and request.user.kind != UserKind.EMPLOYER:
raise PermissionDenied("Seuls les employeurs sont autorisés à déclarer des embauches")
elif request.user.kind not in [
UserKind.JOB_SEEKER,
UserKind.PRESCRIBER,
UserKind.EMPLOYER,
]:
raise PermissionDenied("Vous n'êtes pas autorisé à déposer de candidature.")
elif request.user.is_employer and not self.company.has_member(request.user):
raise PermissionDenied("Vous ne pouvez postuler pour un candidat que dans votre structure.")

if not self.company.has_active_members:
raise PermissionDenied(
"Cet employeur n'est pas inscrit, vous ne pouvez pas déposer de candidatures en ligne."
)
return super().dispatch(request, *args, **kwargs)

def get_back_url(self):
Expand Down
7 changes: 5 additions & 2 deletions itou/www/gps/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from django.urls import reverse_lazy
from django.utils.text import format_lazy

from itou.companies import enums as companies_enums
from itou.companies.models import Company
from itou.users.enums import UserKind
from itou.users.models import User
from itou.utils.widgets import RemoteAutocompleteSelect2Widget
Expand Down Expand Up @@ -29,8 +31,9 @@ class GpsUserSearchForm(forms.Form):

is_referent = forms.BooleanField(label="Se rattacher comme référent", required=False)

def __init__(self, company, *args, **kwargs):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
singleton = Company.unfiltered_objects.get(siret=companies_enums.POLE_EMPLOI_SIRET)
self.fields["user"].widget.attrs["data-no-results-url"] = (
reverse_lazy("apply:start", kwargs={"company_pk": company.pk}) + "?gps=true"
reverse_lazy("apply:start", kwargs={"company_pk": singleton.pk}) + "?gps=true"
)
2 changes: 1 addition & 1 deletion itou/www/gps/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def my_groups(request, template_name="gps/my_groups.html"):
redirect_field_name=None,
)
def join_group(request, template_name="gps/join_group.html"):
form = GpsUserSearchForm(request.current_organization, data=request.POST or None)
form = GpsUserSearchForm(data=request.POST or None)

my_groups_url = reverse("gps:my_groups")
back_url = get_safe_url(request, "back_url", my_groups_url)
Expand Down
98 changes: 49 additions & 49 deletions tests/gps/test_create_beneficiary.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from pytest_django.asserts import assertContains, assertRedirects

from itou.asp.models import RSAAllocation
from itou.companies import enums as companies_enums
from itou.companies.models import Company
from itou.siae_evaluations.models import Sanctions
from itou.users.enums import LackOfPoleEmploiId, UserKind
from itou.users.models import User
Expand All @@ -18,6 +20,7 @@
from tests.prescribers.factories import PrescriberOrganizationWithMembershipFactory
from tests.siae_evaluations.factories import EvaluatedSiaeFactory
from tests.users.factories import (
EmployerFactory,
JobSeekerFactory,
JobSeekerWithAddressFactory,
PrescriberFactory,
Expand All @@ -34,7 +37,7 @@
)
def test_create_job_seeker(_mock, client):
[city] = create_test_cities(["67"], num_per_department=1)
company = CompanyWithMembershipAndJobsFactory(romes=("N1101", "N1105"))
singleton = Company.unfiltered_objects.get(siret=companies_enums.POLE_EMPLOI_SIRET)

prescriber_organization = PrescriberOrganizationWithMembershipFactory(with_pending_authorization=True)
user = prescriber_organization.members.first()
Expand All @@ -48,10 +51,10 @@ def test_create_job_seeker(_mock, client):

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

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

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

response = client.post(next_url, data={"nir": dummy_job_seeker.jobseeker_profile.nir, "confirm": 1})
Expand All @@ -60,7 +63,7 @@ def test_create_job_seeker(_mock, client):
next_url = (
reverse(
"apply:search_by_email_for_sender",
kwargs={"company_pk": company.pk, "session_uuid": job_seeker_session_name},
kwargs={"company_pk": singleton.pk, "session_uuid": job_seeker_session_name},
)
+ "?gps=true"
)
Expand All @@ -84,7 +87,7 @@ def test_create_job_seeker(_mock, client):
next_url = (
reverse(
"apply:create_job_seeker_step_1_for_sender",
kwargs={"company_pk": company.pk, "session_uuid": job_seeker_session_name},
kwargs={"company_pk": singleton.pk, "session_uuid": job_seeker_session_name},
)
+ "?gps=true"
)
Expand All @@ -103,7 +106,7 @@ def test_create_job_seeker(_mock, client):
response,
reverse(
"apply:search_by_email_for_sender",
kwargs={"company_pk": company.pk, "session_uuid": job_seeker_session_name},
kwargs={"company_pk": singleton.pk, "session_uuid": job_seeker_session_name},
),
)

Expand All @@ -123,7 +126,7 @@ def test_create_job_seeker(_mock, client):
next_url = (
reverse(
"apply:create_job_seeker_step_2_for_sender",
kwargs={"company_pk": company.pk, "session_uuid": job_seeker_session_name},
kwargs={"company_pk": singleton.pk, "session_uuid": job_seeker_session_name},
)
+ "?gps=true"
)
Expand All @@ -147,7 +150,7 @@ def test_create_job_seeker(_mock, client):
next_url = (
reverse(
"apply:create_job_seeker_step_3_for_sender",
kwargs={"company_pk": company.pk, "session_uuid": job_seeker_session_name},
kwargs={"company_pk": singleton.pk, "session_uuid": job_seeker_session_name},
)
+ "?gps=true"
)
Expand Down Expand Up @@ -181,7 +184,7 @@ def test_create_job_seeker(_mock, client):
next_url = (
reverse(
"apply:create_job_seeker_step_end_for_sender",
kwargs={"company_pk": company.pk, "session_uuid": job_seeker_session_name},
kwargs={"company_pk": singleton.pk, "session_uuid": job_seeker_session_name},
)
+ "?gps=true"
)
Expand All @@ -197,34 +200,20 @@ def test_create_job_seeker(_mock, client):
assert list(created_job_seeker.follow_up_group.members.all()) == [user]


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

user = PrescriberFactory()
client.force_login(user)

# Create user for a company that is not the same that the user logged-in
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}) + "?gps=true"
assertRedirects(response, next_url)


def test_gps_bypass(client):
# The sender has a pending authorization, but we should be able to create the user anyway
# Check that we are not redirected to apply:pending_authorization_for_sender"

company = CompanyWithMembershipAndJobsFactory(romes=("N1101", "N1105"))
singleton = Company.unfiltered_objects.get(siret=companies_enums.POLE_EMPLOI_SIRET)

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

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

next_url = reverse("apply:check_nir_for_sender", kwargs={"company_pk": company.pk}) + "?gps=true"
next_url = reverse("apply:check_nir_for_sender", kwargs={"company_pk": singleton.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 @@ -237,32 +226,19 @@ def test_gps_bypass(client):
user = company.members.first()
client.force_login(user)

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}) + "?gps=true"
assertRedirects(response, next_url)

# The company has block job applications but we should be able to create the job_seeker anyway

company = CompanyWithMembershipAndJobsFactory(romes=("N1101", "N1105"), block_job_applications="True")

user = PrescriberFactory()
client.force_login(user)

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

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


@pytest.mark.ignore_unknown_variable_template_error("job_seeker")
def test_existing_user_with_email(client):
"""
An user with the same email already exists, create the group with this user
A user with the same email already exists, create the group with this user
"""
company = CompanyWithMembershipAndJobsFactory(romes=("N1101", "N1105"))
singleton = Company.unfiltered_objects.get(siret=companies_enums.POLE_EMPLOI_SIRET)
# Only authorized prescribers can add a NIR.
# See User.can_add_nir
prescriber_organization = PrescriberOrganizationWithMembershipFactory(authorized=True)
Expand All @@ -272,12 +248,12 @@ def test_existing_user_with_email(client):
client.force_login(user)

# Follow all redirections…
response = client.get(reverse("apply:start", kwargs={"company_pk": company.pk}) + "?gps=true", follow=True)
response = client.get(reverse("apply:start", kwargs={"company_pk": singleton.pk}) + "?gps=true", follow=True)

# …until a job seeker has to be determined.
assert response.status_code == 200
last_url = response.redirect_chain[-1][0]
assert last_url == reverse("apply:check_nir_for_sender", kwargs={"company_pk": company.pk}) + "?gps=true"
assert last_url == reverse("apply:check_nir_for_sender", kwargs={"company_pk": singleton.pk}) + "?gps=true"

# Enter a non-existing NIR.
# ----------------------------------------------------------------------
Expand All @@ -288,7 +264,7 @@ def test_existing_user_with_email(client):
next_url = (
reverse(
"apply:search_by_email_for_sender",
kwargs={"company_pk": company.pk, "session_uuid": job_seeker_session_name},
kwargs={"company_pk": singleton.pk, "session_uuid": job_seeker_session_name},
)
+ "?gps=true"
)
Expand Down Expand Up @@ -323,7 +299,7 @@ def test_existing_user_with_nir(client):
"""
An user with the same NIR already exists, create the group with this user
"""
company = CompanyWithMembershipAndJobsFactory(romes=("N1101", "N1105"))
singleton = Company.unfiltered_objects.get(siret=companies_enums.POLE_EMPLOI_SIRET)

nir = "141068078200557"
prescriber_organization = PrescriberOrganizationWithMembershipFactory(authorized=True)
Expand All @@ -333,12 +309,12 @@ def test_existing_user_with_nir(client):
client.force_login(user)

# Follow all redirections…
response = client.get(reverse("apply:start", kwargs={"company_pk": company.pk}) + "?gps=true", follow=True)
response = client.get(reverse("apply:start", kwargs={"company_pk": singleton.pk}) + "?gps=true", follow=True)

# …until a job seeker has to be determined.
assert response.status_code == 200
last_url = response.redirect_chain[-1][0]
assert last_url == reverse("apply:check_nir_for_sender", kwargs={"company_pk": company.pk}) + "?gps=true"
assert last_url == reverse("apply:check_nir_for_sender", kwargs={"company_pk": singleton.pk}) + "?gps=true"

# Enter an existing NIR.
# ----------------------------------------------------------------------
Expand All @@ -358,3 +334,27 @@ def test_existing_user_with_nir(client):
job_seeker.refresh_from_db()
assert job_seeker.follow_up_group is not None
assert user in job_seeker.follow_up_group.members.all()


@pytest.mark.parametrize(
"UserFactory, factory_args",
[
(PrescriberFactory, {"membership": True}),
(PrescriberFactory, {"membership": False}),
(EmployerFactory, {"with_company": True}),
],
)
def test_creation_by_user_kind(client, UserFactory, factory_args):
singleton = Company.unfiltered_objects.get(siret=companies_enums.POLE_EMPLOI_SIRET)
user = UserFactory(**factory_args)
client.force_login(user)
# Assert contains link.
create_beneficiary_url = reverse("apply:start", kwargs={"company_pk": singleton.pk}) + "?gps=true"
response = client.get(reverse("gps:join_group"))
assertContains(response, create_beneficiary_url)
response = client.get(create_beneficiary_url)
assert response.status_code == 302
assert (
response["Location"]
== reverse("apply:check_nir_for_sender", kwargs={"company_pk": singleton.pk}) + "?gps=true"
)
15 changes: 0 additions & 15 deletions tests/gps/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,6 @@ def test_join_group_of_a_job_seeker(is_referent, client, snapshot):

response = client.get(url)

company = response.context["request"].current_organization

assert (
str(
parse_response_to_soup(
response,
"#join_group",
replace_in_attr=[
("data-no-results-url", f"/apply/{company.pk}/start", "/apply/[PK of Company]/start"),
],
)
)
== snapshot
)

post_data = {
"user": job_seeker.id,
"is_referent": is_referent,
Expand Down