Ejemplo n.º 1
0
    def test_merge_approvals(self):

        user = JobSeekerFactory()

        # Approval.
        approval = ApprovalFactory(user=user, start_at=datetime.date(2016, 12, 20), end_at=datetime.date(2018, 12, 20))

        # PoleEmploiApproval 1.
        pe_approval_1 = PoleEmploiApprovalFactory(
            pole_emploi_id=user.pole_emploi_id,
            birthdate=user.birthdate,
            start_at=datetime.date(2018, 12, 20),
            end_at=datetime.date(2020, 12, 20),
        )

        # PoleEmploiApproval 2.
        # Same `start_at` as PoleEmploiApproval 1.
        # But `end_at` earlier than PoleEmploiApproval 1.
        pe_approval_2 = PoleEmploiApprovalFactory(
            pole_emploi_id=user.pole_emploi_id,
            birthdate=user.birthdate,
            start_at=datetime.date(2018, 12, 20),
            end_at=datetime.date(2019, 12, 19),
        )

        # Check timeline.
        approvals_wrapper = ApprovalsWrapper(user)
        self.assertEqual(len(approvals_wrapper.merged_approvals), 3)
        self.assertEqual(approvals_wrapper.merged_approvals[0], pe_approval_1)
        self.assertEqual(approvals_wrapper.merged_approvals[1], pe_approval_2)
        self.assertEqual(approvals_wrapper.merged_approvals[2], approval)
Ejemplo n.º 2
0
    def test_has_valid_eligibility_diagnosis(self):

        # No diagnosis.
        job_seeker = JobSeekerFactory()
        self.assertFalse(job_seeker.has_valid_eligibility_diagnosis)

        # Has Itou diagnosis.
        job_seeker = JobSeekerFactory()
        EligibilityDiagnosisFactory(job_seeker=job_seeker)
        self.assertTrue(job_seeker.has_valid_eligibility_diagnosis)

        # Has valid Pôle emploi diagnosis.
        job_seeker = JobSeekerFactory()
        PoleEmploiApprovalFactory(pole_emploi_id=job_seeker.pole_emploi_id, birthdate=job_seeker.birthdate)
        self.assertTrue(job_seeker.has_valid_eligibility_diagnosis)

        # Has expired Pôle emploi diagnosis.
        job_seeker = JobSeekerFactory()
        end_at = datetime.date.today() - relativedelta(years=2)
        start_at = end_at - relativedelta(years=2)
        PoleEmploiApprovalFactory(
            pole_emploi_id=job_seeker.pole_emploi_id, birthdate=job_seeker.birthdate, start_at=start_at, end_at=end_at
        )
        self.assertFalse(job_seeker.has_valid_eligibility_diagnosis)

        # Has an expired diagnosis
        job_seeker = JobSeekerFactory()
        ExpiredEligibilityDiagnosisFactory(job_seeker=job_seeker)
        self.assertFalse(job_seeker.has_valid_eligibility_diagnosis)
Ejemplo n.º 3
0
    def test_number_with_spaces(self):

        # 12 chars.
        pole_emploi_approval = PoleEmploiApprovalFactory(number="400121910144")
        expected = "40012 19 10144"
        self.assertEqual(pole_emploi_approval.number_with_spaces, expected)

        # 15 chars.
        pole_emploi_approval = PoleEmploiApprovalFactory(number="010331610106A01")
        expected = "01033 16 10106 A01"
        self.assertEqual(pole_emploi_approval.number_with_spaces, expected)
Ejemplo n.º 4
0
    def test_valid_for_pole_emploi_approval_model(self):
        """
        Test for PoleEmploiApproval model.
        """

        start_at = datetime.date.today() - relativedelta(years=1)
        end_at = start_at + relativedelta(years=1)
        PoleEmploiApprovalFactory(start_at=start_at, end_at=end_at)

        start_at = datetime.date.today() - relativedelta(years=5)
        end_at = start_at + relativedelta(years=2)
        PoleEmploiApprovalFactory(start_at=start_at, end_at=end_at)

        self.assertEqual(2, PoleEmploiApproval.objects.count())
        self.assertEqual(1, PoleEmploiApproval.objects.valid().count())
Ejemplo n.º 5
0
    def test_apply_as_jobseeker_with_approval_in_waiting_period(self):
        """Apply as jobseeker with an approval in waiting period."""

        siae = SiaeWithMembershipAndJobsFactory(romes=("N1101", "N1105"))
        user = JobSeekerFactory()
        end_at = datetime.date.today() - relativedelta(days=30)
        start_at = end_at - relativedelta(years=2)
        PoleEmploiApprovalFactory(
            pole_emploi_id=user.pole_emploi_id,
            birthdate=user.birthdate,
            start_at=start_at,
            end_at=end_at,
        )
        self.client.login(username=user.email, password=DEFAULT_PASSWORD)

        url = reverse("apply:start", kwargs={"siae_pk": siae.pk})

        # Follow all redirections…
        response = self.client.get(url, follow=True)

        # …until the expected 403.
        self.assertEqual(response.status_code, 403)
        self.assertEqual(
            response.context["exception"],
            ApprovalsWrapper.ERROR_CANNOT_OBTAIN_NEW_FOR_USER,
        )
        last_url = response.redirect_chain[-1][0]
        self.assertEqual(
            last_url,
            reverse("apply:step_check_job_seeker_info",
                    kwargs={"siae_pk": siae.pk}),
        )
Ejemplo n.º 6
0
    def test_merge_approvals(self):

        user = JobSeekerFactory()

        # Create Approval.
        start_at = datetime.date.today() - relativedelta(years=4)
        end_at = start_at + relativedelta(years=2)
        approval = ApprovalFactory(user=user, start_at=start_at, end_at=end_at)

        # Create PoleEmploiApproval.
        start_at = datetime.date.today()
        end_at = start_at + relativedelta(years=2)
        pe_approval = PoleEmploiApprovalFactory(
            pole_emploi_id=user.pole_emploi_id,
            birthdate=user.birthdate,
            start_at=start_at,
            end_at=end_at,
        )

        # Check timeline.
        self.assertTrue(approval.start_at < pe_approval.start_at)

        approvals_wrapper = ApprovalsWrapper(user)
        self.assertEqual(len(approvals_wrapper.merged_approvals), 2)
        self.assertEqual(approvals_wrapper.merged_approvals[0], pe_approval)
        self.assertEqual(approvals_wrapper.merged_approvals[1], approval)
Ejemplo n.º 7
0
 def setUp(self):
     self.job_application = JobApplicationWithApprovalFactory(state=JobApplicationWorkflow.STATE_ACCEPTED)
     self.siae = self.job_application.to_siae
     self.siae_user = self.job_application.to_siae.members.first()
     self.approval = self.job_application.approval
     self.job_seeker = self.job_application.job_seeker
     self.pe_approval = PoleEmploiApprovalFactory()
Ejemplo n.º 8
0
    def test_apply_as_jobseeker_to_siae_with_approval_in_waiting_period(self):
        """
        Apply as jobseeker to a SIAE (not a GEIQ) with an approval in waiting period.
        Waiting period cannot be bypassed.
        """

        # Avoid COVID lockdown specific cases
        now_date = PoleEmploiApproval.LOCKDOWN_START_AT - relativedelta(months=1)
        now = timezone.datetime(year=now_date.year, month=now_date.month, day=now_date.day, tzinfo=timezone.utc)

        with mock.patch("django.utils.timezone.now", side_effect=lambda: now):
            siae = SiaeWithMembershipAndJobsFactory(romes=("N1101", "N1105"))
            user = JobSeekerFactory()
            end_at = now_date - relativedelta(days=30)
            start_at = end_at - relativedelta(years=2)
            PoleEmploiApprovalFactory(
                pole_emploi_id=user.pole_emploi_id, birthdate=user.birthdate, start_at=start_at, end_at=end_at
            )
            self.client.login(username=user.email, password=DEFAULT_PASSWORD)

            url = reverse("apply:start", kwargs={"siae_pk": siae.pk})

            # Follow all redirections…
            response = self.client.get(url, follow=True)

            # …until the expected 403.
            self.assertEqual(response.status_code, 403)
            self.assertEqual(response.context["exception"], ApprovalsWrapper.ERROR_CANNOT_OBTAIN_NEW_FOR_USER)
            last_url = response.redirect_chain[-1][0]
            self.assertEqual(last_url, reverse("apply:step_check_job_seeker_info", kwargs={"siae_pk": siae.pk}))
Ejemplo n.º 9
0
    def test_has_matching_pass_iae_that_belongs_to_another_siae(self):
        """
        Make sure to NOT to redirect to job applications belonging to other SIAEs,
        as this would produce a 404.
        """

        # Initial approvals (PE and PASS)
        self.set_up_pe_approval()

        # Create a job application with a PASS IAE created from a `PoleEmploiApproval`
        # that belongs to another siae.
        job_seeker = JobSeekerFactory()
        pe_approval = PoleEmploiApprovalFactory()
        job_application = JobApplicationWithApprovalFactory(
            state=JobApplicationWorkflow.STATE_ACCEPTED,
            approval__number=pe_approval.number,
            approval__user=job_seeker,
            job_seeker=job_seeker,
        )

        another_siae = job_application.to_siae
        self.assertNotEqual(another_siae, self.siae)

        # This is the current user (NOT a member of `another_siae`).
        self.client.login(username=self.siae_user.email, password=DEFAULT_PASSWORD)

        # The current user should not be able to use the PASS IAE used by another SIAE.
        response = self.client.get(self.url, {"number": job_application.approval.number})
        self.assertNotContains(response, "Continuer")
Ejemplo n.º 10
0
 def test_accept_job_application_sent_by_authorized_prescriber_with_approval_in_waiting_period(
     self
 ):
     user = JobSeekerFactory()
     # Ended 1 year ago.
     end_at = datetime.date.today() - relativedelta(years=1)
     start_at = end_at - relativedelta(years=2)
     approval = PoleEmploiApprovalFactory(
         pole_emploi_id=user.pole_emploi_id,
         birthdate=user.birthdate,
         start_at=start_at,
         end_at=end_at,
     )
     self.assertTrue(approval.is_in_waiting_period)
     job_application = JobApplicationSentByAuthorizedPrescriberOrganizationFactory(
         job_seeker=user, state=JobApplicationWorkflow.STATE_PROCESSING
     )
     # A valid Pôle emploi ID should trigger an automatic approval delivery.
     self.assertNotEqual(job_application.job_seeker.pole_emploi_id, "")
     job_application.accept(user=job_application.to_siae.members.first())
     self.assertIsNotNone(job_application.approval)
     self.assertTrue(job_application.approval_number_sent_by_email)
     self.assertEqual(
         job_application.approval_delivery_mode,
         job_application.APPROVAL_DELIVERY_MODE_AUTOMATIC,
     )
     # Check sent email.
     self.assertEqual(len(mail.outbox), 2)
     self.assertIn("Candidature acceptée", mail.outbox[0].subject)
     self.assertIn("Délivrance d'un PASS IAE pour", mail.outbox[1].subject)
Ejemplo n.º 11
0
    def test_get_or_create_from_valid(self):

        # With an existing valid `PoleEmploiApproval`.

        user = JobSeekerFactory()
        valid_pe_approval = PoleEmploiApprovalFactory(
            pole_emploi_id=user.pole_emploi_id, birthdate=user.birthdate, number="625741810182A01"
        )
        approvals_wrapper = ApprovalsWrapper(user)

        approval = Approval.get_or_create_from_valid(approvals_wrapper)

        self.assertTrue(isinstance(approval, Approval))
        self.assertEqual(approval.start_at, valid_pe_approval.start_at)
        self.assertEqual(approval.end_at, valid_pe_approval.end_at)
        self.assertEqual(approval.number, valid_pe_approval.number[:12])
        self.assertEqual(approval.user, user)
        self.assertEqual(approval.created_by, None)

        # With an existing valid `Approval`.

        user = JobSeekerFactory()
        valid_approval = ApprovalFactory(user=user, start_at=datetime.date.today() - relativedelta(days=1))
        approvals_wrapper = ApprovalsWrapper(user)

        approval = Approval.get_or_create_from_valid(approvals_wrapper)
        self.assertTrue(isinstance(approval, Approval))
        self.assertEqual(approval, valid_approval)
Ejemplo n.º 12
0
    def test_one_account_no_nir(self):
        """
        No account with this NIR is found.
        A search by email is proposed.
        An account is found for this email.
        This NIR account is empty.
        An update is expected.
        """
        job_seeker = JobSeekerFactory(nir="")
        # Create an approval to bypass the eligibility diagnosis step.
        PoleEmploiApprovalFactory(birthdate=job_seeker.birthdate, pole_emploi_id=job_seeker.pole_emploi_id)
        siae, user = self.create_test_data()
        self.client.login(username=user.email, password=DEFAULT_PASSWORD)

        url = reverse("apply:start", kwargs={"siae_pk": siae.pk})

        # Follow all redirections…
        response = self.client.get(url, follow=True)

        # …until a job seeker has to be determined.
        self.assertEqual(response.status_code, 200)
        last_url = response.redirect_chain[-1][0]
        self.assertEqual(last_url, reverse("apply:step_check_job_seeker_nir", kwargs={"siae_pk": siae.pk}))

        # Enter an a non-existing NIR.
        # ----------------------------------------------------------------------
        nir = "141068078200557"
        post_data = {"nir": nir, "confirm": 1}
        response = self.client.post(last_url, data=post_data)
        next_url = reverse("apply:step_job_seeker", kwargs={"siae_pk": siae.pk})
        self.assertRedirects(response, next_url)

        # Enter an existing email.
        # ----------------------------------------------------------------------
        post_data = {"email": job_seeker.email, "save": "1"}
        response = self.client.post(next_url, data=post_data)
        next_url = reverse("apply:step_check_job_seeker_info", kwargs={"siae_pk": siae.pk})
        self.assertRedirects(response, next_url, target_status_code=302)

        # Follow all redirections until the end.
        # ----------------------------------------------------------------------
        response = self.client.get(next_url, follow=True)
        self.assertTrue(response.status_code, 200)

        next_url = reverse("apply:step_application", kwargs={"siae_pk": siae.pk})
        post_data = {
            "selected_jobs": [siae.job_description_through.first().pk, siae.job_description_through.last().pk],
            "message": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
            "resume_link": "https://server.com/rockie-balboa.pdf",
        }
        response = self.client.post(next_url, data=post_data, follow=True)
        expected_url = reverse("apply:step_application_sent", kwargs={"siae_pk": siae.pk})
        last_url = response.redirect_chain[-1][0]
        self.assertEqual(expected_url, last_url)

        # Make sure the job seeker NIR is now filled in.
        # ----------------------------------------------------------------------
        job_seeker.refresh_from_db()
        self.assertEqual(job_seeker.nir, nir)
Ejemplo n.º 13
0
 def test_status_with_valid_pole_emploi_approval(self):
     user = JobSeekerFactory()
     approval = PoleEmploiApprovalFactory(pole_emploi_id=user.pole_emploi_id, birthdate=user.birthdate)
     approvals_wrapper = ApprovalsWrapper(user)
     self.assertEqual(approvals_wrapper.status, ApprovalsWrapper.VALID)
     self.assertFalse(approvals_wrapper.has_in_waiting_period)
     self.assertTrue(approvals_wrapper.has_valid)
     self.assertEqual(approvals_wrapper.latest_approval, approval)
Ejemplo n.º 14
0
    def test_find_for(self):

        user = JobSeekerFactory()
        pe_approval = PoleEmploiApprovalFactory(pole_emploi_id=user.pole_emploi_id, birthdate=user.birthdate)
        search_results = PoleEmploiApproval.objects.find_for(user)
        self.assertEqual(search_results.count(), 1)
        self.assertEqual(search_results.first(), pe_approval)
        PoleEmploiApproval.objects.all().delete()
Ejemplo n.º 15
0
    def test_is_valid(self):

        # End today.
        end_at = datetime.date.today()
        start_at = end_at - relativedelta(years=2)
        approval = PoleEmploiApprovalFactory(start_at=start_at, end_at=end_at)
        self.assertTrue(approval.is_valid)

        # Ended yesterday.
        end_at = datetime.date.today() - relativedelta(days=1)
        start_at = end_at - relativedelta(years=2)
        approval = PoleEmploiApprovalFactory(start_at=start_at, end_at=end_at)
        self.assertFalse(approval.is_valid)

        # Start tomorrow.
        start_at = datetime.date.today() + relativedelta(days=1)
        end_at = start_at + relativedelta(years=2)
        approval = PoleEmploiApprovalFactory(start_at=start_at, end_at=end_at)
        self.assertTrue(approval.is_valid)
Ejemplo n.º 16
0
    def test_approval_in_the_future(self):
        """
        The search for PE approval screen should display that there is no results
        if a PE approval number was searched for but it is in the future
        """
        today = timezone.now().date()

        pe_approval = PoleEmploiApprovalFactory(start_at=today + relativedelta(days=10))

        job_application = JobApplicationWithApprovalFactory(state=JobApplicationWorkflow.STATE_ACCEPTED)
        siae_user = job_application.to_siae.members.first()
        self.client.login(username=siae_user.email, password=DEFAULT_PASSWORD)

        response = self.client.get(self.url, {"number": pe_approval.number})
        self.assertNotContains(response, "Continuer")
Ejemplo n.º 17
0
 def test_accept_job_application_sent_by_prescriber_with_approval_in_waiting_period(
         self):
     user = JobSeekerFactory()
     # Ended 1 year ago.
     end_at = datetime.date.today() - relativedelta(years=1)
     start_at = end_at - relativedelta(years=2)
     approval = PoleEmploiApprovalFactory(
         pole_emploi_id=user.pole_emploi_id,
         birthdate=user.birthdate,
         start_at=start_at,
         end_at=end_at)
     self.assertTrue(approval.is_in_waiting_period)
     job_application = JobApplicationSentByPrescriberOrganizationFactory(
         job_seeker=user, state=JobApplicationWorkflow.STATE_PROCESSING)
     with self.assertRaises(xwf_models.AbortTransition):
         job_application.accept(
             user=job_application.to_siae.members.first())
Ejemplo n.º 18
0
    def set_up_pe_approval(self, with_job_application=True):
        # pylint: disable=attribute-defined-outside-init
        self.pe_approval = PoleEmploiApprovalFactory()

        self.siae = SiaeWithMembershipFactory()
        self.siae_user = self.siae.members.first()
        if with_job_application:
            self.job_application = JobApplicationWithApprovalFactory(
                to_siae=self.siae,
                state=JobApplicationWorkflow.STATE_ACCEPTED,
                approval__number=self.pe_approval.number,
            )
            self.approval = self.job_application.approval
            self.job_seeker = self.job_application.job_seeker
        else:
            self.approval = None
            self.job_seeker = None
Ejemplo n.º 19
0
 def test_accept_job_application_sent_by_job_seeker_with_valid_approval(
         self):
     job_seeker = JobSeekerFactory()
     pe_approval = PoleEmploiApprovalFactory(
         pole_emploi_id=job_seeker.pole_emploi_id,
         birthdate=job_seeker.birthdate)
     job_application = JobApplicationSentByJobSeekerFactory(
         job_seeker=job_seeker,
         state=JobApplicationWorkflow.STATE_PROCESSING)
     job_application.accept(user=job_application.to_siae.members.first())
     self.assertIsNotNone(job_application.approval)
     self.assertEqual(job_application.approval.number, pe_approval.number)
     self.assertTrue(job_application.approval_number_sent_by_email)
     self.assertEqual(job_application.approval_delivery_mode,
                      job_application.APPROVAL_DELIVERY_MODE_AUTOMATIC)
     # Check sent email.
     self.assertEqual(len(mail.outbox), 2)
     self.assertIn("Candidature acceptée", mail.outbox[0].subject)
     self.assertIn("Délivrance d'un PASS IAE pour", mail.outbox[1].subject)
Ejemplo n.º 20
0
 def test_waiting_period_end(self):
     end_at = datetime.date(2000, 1, 1)
     start_at = datetime.date(1998, 1, 1)
     approval = PoleEmploiApprovalFactory(start_at=start_at, end_at=end_at)
     expected = datetime.date(2002, 1, 1)
     self.assertEqual(approval.waiting_period_end, expected)
Ejemplo n.º 21
0
 def test_originates_from_itou(self):
     approval = ApprovalFactory(number="999990000001")
     self.assertTrue(approval.originates_from_itou)
     approval = PoleEmploiApprovalFactory(number="625741810182")
     self.assertFalse(approval.originates_from_itou)
Ejemplo n.º 22
0
    def test_valid_and_expired_methods(self):
        """
        Test both `has_considered_valid()`, `last_considered_valid()` and `last_expired()` methods.
        """

        # No diagnosis.
        job_seeker = JobSeekerFactory()
        has_considered_valid = EligibilityDiagnosis.objects.has_considered_valid(job_seeker=job_seeker)
        last_considered_valid = EligibilityDiagnosis.objects.last_considered_valid(job_seeker=job_seeker)
        last_expired = EligibilityDiagnosis.objects.last_expired(job_seeker=job_seeker)
        self.assertIsNone(last_considered_valid)
        self.assertIsNone(last_expired)
        self.assertFalse(has_considered_valid)

        # Has Itou diagnosis.
        diagnosis = EligibilityDiagnosisFactory()
        has_considered_valid = EligibilityDiagnosis.objects.has_considered_valid(job_seeker=diagnosis.job_seeker)
        last_considered_valid = EligibilityDiagnosis.objects.last_considered_valid(job_seeker=diagnosis.job_seeker)
        last_expired = EligibilityDiagnosis.objects.last_expired(job_seeker=diagnosis.job_seeker)
        self.assertEqual(last_considered_valid, diagnosis)
        self.assertIsNone(last_expired)
        self.assertTrue(has_considered_valid)

        # Has a valid PASS IAE but NO diagnosis.
        approval = ApprovalFactory()
        job_seeker = approval.user
        self.assertEqual(0, job_seeker.eligibility_diagnoses.count())
        has_considered_valid = EligibilityDiagnosis.objects.has_considered_valid(job_seeker=job_seeker)
        last_considered_valid = EligibilityDiagnosis.objects.last_considered_valid(job_seeker=job_seeker)
        last_expired = EligibilityDiagnosis.objects.last_expired(job_seeker=job_seeker)
        self.assertTrue(has_considered_valid)
        self.assertIsNone(last_considered_valid)
        self.assertIsNone(last_expired)

        # Has valid Pôle emploi diagnosis.
        job_seeker = JobSeekerFactory()
        PoleEmploiApprovalFactory(pole_emploi_id=job_seeker.pole_emploi_id, birthdate=job_seeker.birthdate)
        has_considered_valid = EligibilityDiagnosis.objects.has_considered_valid(job_seeker=job_seeker)
        last_considered_valid = EligibilityDiagnosis.objects.last_considered_valid(job_seeker=job_seeker)
        last_expired = EligibilityDiagnosis.objects.last_expired(job_seeker=job_seeker)
        self.assertTrue(has_considered_valid)
        self.assertIsNone(last_considered_valid)
        self.assertIsNone(last_expired)

        # Has expired Pôle emploi diagnosis.
        job_seeker = JobSeekerFactory()
        end_at = datetime.date.today() - relativedelta(years=2)
        start_at = end_at - relativedelta(years=2)
        PoleEmploiApprovalFactory(
            pole_emploi_id=job_seeker.pole_emploi_id, birthdate=job_seeker.birthdate, start_at=start_at, end_at=end_at
        )
        has_considered_valid = EligibilityDiagnosis.objects.has_considered_valid(job_seeker=job_seeker)
        last_considered_valid = EligibilityDiagnosis.objects.last_considered_valid(job_seeker=job_seeker)
        last_expired = EligibilityDiagnosis.objects.last_expired(job_seeker=job_seeker)
        self.assertFalse(has_considered_valid)
        self.assertIsNone(last_considered_valid)
        self.assertIsNone(last_expired)

        # Has expired Itou diagnosis.
        expired_diagnosis = ExpiredEligibilityDiagnosisFactory()
        has_considered_valid = EligibilityDiagnosis.objects.has_considered_valid(
            job_seeker=expired_diagnosis.job_seeker
        )
        last_considered_valid = EligibilityDiagnosis.objects.last_considered_valid(
            job_seeker=expired_diagnosis.job_seeker
        )
        last_expired = EligibilityDiagnosis.objects.last_expired(job_seeker=expired_diagnosis.job_seeker)
        self.assertFalse(has_considered_valid)
        self.assertIsNone(last_considered_valid)
        self.assertIsNotNone(last_expired)

        # Has expired Itou diagnosis but has an ongoing PASS IAE.
        expired_diagnosis = ExpiredEligibilityDiagnosisFactory()
        ApprovalFactory(user=expired_diagnosis.job_seeker)
        has_considered_valid = EligibilityDiagnosis.objects.has_considered_valid(
            job_seeker=expired_diagnosis.job_seeker
        )
        last_considered_valid = EligibilityDiagnosis.objects.last_considered_valid(
            job_seeker=expired_diagnosis.job_seeker
        )
        last_expired = EligibilityDiagnosis.objects.last_expired(job_seeker=expired_diagnosis.job_seeker)

        self.assertTrue(has_considered_valid)
        self.assertEqual(last_considered_valid, expired_diagnosis)
        self.assertIsNone(last_expired)

        # Has Itou diagnosis made by an SIAE.
        siae1 = SiaeWithMembershipFactory()
        siae2 = SiaeWithMembershipFactory()
        diagnosis = EligibilityDiagnosisMadeBySiaeFactory(author_siae=siae1)
        # From `siae1` perspective.
        has_considered_valid = EligibilityDiagnosis.objects.has_considered_valid(
            job_seeker=diagnosis.job_seeker, for_siae=siae1
        )
        last_considered_valid = EligibilityDiagnosis.objects.last_considered_valid(
            job_seeker=diagnosis.job_seeker, for_siae=siae1
        )
        last_expired = EligibilityDiagnosis.objects.last_expired(job_seeker=diagnosis.job_seeker, for_siae=siae1)
        self.assertTrue(has_considered_valid)
        self.assertEqual(last_considered_valid, diagnosis)
        self.assertIsNone(last_expired)
        # From `siae2` perspective.
        has_considered_valid = EligibilityDiagnosis.objects.has_considered_valid(
            job_seeker=diagnosis.job_seeker, for_siae=siae2
        )
        last_considered_valid = EligibilityDiagnosis.objects.last_considered_valid(
            job_seeker=diagnosis.job_seeker, for_siae=siae2
        )
        last_expired = EligibilityDiagnosis.objects.last_expired(job_seeker=diagnosis.job_seeker, for_siae=siae2)
        self.assertFalse(has_considered_valid)
        self.assertIsNone(last_considered_valid)
        self.assertIsNone(last_expired)

        # Has Itou diagnosis made by a prescriber.
        siae = SiaeWithMembershipFactory()
        prescriber_diagnosis = EligibilityDiagnosisFactory()
        # From siae perspective.
        has_considered_valid = EligibilityDiagnosis.objects.has_considered_valid(
            job_seeker=prescriber_diagnosis.job_seeker, for_siae=siae
        )
        last_considered_valid = EligibilityDiagnosis.objects.last_considered_valid(
            job_seeker=prescriber_diagnosis.job_seeker, for_siae=siae
        )
        last_expired = EligibilityDiagnosis.objects.last_expired(
            job_seeker=prescriber_diagnosis.job_seeker, for_siae=siae
        )
        self.assertTrue(has_considered_valid)
        self.assertEqual(last_considered_valid, prescriber_diagnosis)
        self.assertIsNone(last_expired)

        # Has 2 Itou diagnoses: 1 made by an SIAE prior to another one by a prescriber.
        job_seeker = JobSeekerFactory()
        siae = SiaeWithMembershipFactory()
        prescriber_diagnosis = EligibilityDiagnosisFactory(job_seeker=job_seeker)
        # From `siae` perspective.
        has_considered_valid = EligibilityDiagnosis.objects.has_considered_valid(job_seeker=job_seeker, for_siae=siae)
        last_considered_valid = EligibilityDiagnosis.objects.last_considered_valid(
            job_seeker=job_seeker, for_siae=siae
        )
        last_expired = EligibilityDiagnosis.objects.last_expired(job_seeker=job_seeker, for_siae=siae)
        self.assertTrue(has_considered_valid)
        # A diagnosis made by a prescriber takes precedence.
        self.assertEqual(last_considered_valid, prescriber_diagnosis)
        self.assertIsNone(last_expired)

        # Has an expired Itou diagnoses made by another SIAE.
        job_seeker = JobSeekerFactory()
        siae1 = SiaeWithMembershipFactory()
        siae2 = SiaeWithMembershipFactory()
        expired_diagnosis = ExpiredEligibilityDiagnosisMadeBySiaeFactory(job_seeker=job_seeker, author_siae=siae1)

        # From `siae` perspective.
        last_expired = EligibilityDiagnosis.objects.last_expired(job_seeker=job_seeker, for_siae=siae1)
        self.assertEqual(last_expired, expired_diagnosis)
        last_expired = EligibilityDiagnosis.objects.last_expired(job_seeker=job_seeker, for_siae=siae2)
        self.assertIsNone(last_expired)

        # Has 2 Itou diagnoses: 1 is considered expired and the second is considered valid.
        job_seeker = JobSeekerFactory()
        ExpiredEligibilityDiagnosisFactory(job_seeker=job_seeker)
        EligibilityDiagnosisFactory(job_seeker=job_seeker)

        last_expired = EligibilityDiagnosis.objects.last_expired(job_seeker=job_seeker)
        last_considered_valid = EligibilityDiagnosis.objects.last_considered_valid(job_seeker=job_seeker)
        # When has a valid diagnosis, `last_expired` return None
        self.assertIsNotNone(last_considered_valid)
        self.assertIsNone(last_expired)

        # Has 2 Itou diagnoses expired: the last one expired must be returned.
        job_seeker = JobSeekerFactory()

        date_6m = (
            timezone.now() - relativedelta(months=EligibilityDiagnosis.EXPIRATION_DELAY_MONTHS) - relativedelta(day=1)
        )
        date_12m = date_6m - relativedelta(months=6)

        expired_diagnosis_old = EligibilityDiagnosisFactory(job_seeker=job_seeker, created_at=date_12m)
        last_expired = EligibilityDiagnosis.objects.last_expired(job_seeker=job_seeker)
        self.assertEqual(last_expired, expired_diagnosis_old)

        expired_diagnosis_last = EligibilityDiagnosisFactory(job_seeker=job_seeker, created_at=date_6m)
        last_expired = EligibilityDiagnosis.objects.last_expired(job_seeker=job_seeker)
        self.assertEqual(last_expired, expired_diagnosis_last)