def test_invite_former_member(self): """ Admins can "deactivate" members of the organization (making the membership inactive). A deactivated member must be able to receive new invitations. """ # Invite user (part 1) guest = PrescriberFactory() self.post_data["form-0-first_name"] = guest.first_name self.post_data["form-0-last_name"] = guest.last_name self.post_data["form-0-email"] = guest.email response = self.client.post(INVITATION_URL, data=self.post_data, follow=True) self.assertRedirects(response, INVITATION_URL) self.assert_created_invitation() # Deactivate user self.organization.members.add(guest) guest.save() membership = guest.prescribermembership_set.first() membership.deactivate_membership_by_user(self.organization.members.first()) membership.save() self.assertFalse(guest in self.organization.active_members) # Invite user (the revenge) response = self.client.post(INVITATION_URL, data=self.post_data, follow=True) self.assertRedirects(response, INVITATION_URL) invitations_count = PrescriberWithOrgInvitation.objects.filter(organization=self.organization).count() self.assertEqual(invitations_count, 2)
def test_edit_not_allowed(self): # Ensure that the job seeker is not autonomous (i.e. he did not register by himself). job_application = JobApplicationSentByPrescriberFactory( job_seeker__created_by=PrescriberFactory()) # Lambda prescriber not member of the sender organization prescriber = PrescriberFactory() self.client.login(username=prescriber.email, password=DEFAULT_PASSWORD) url = reverse("dashboard:edit_job_seeker_info", kwargs={"job_application_id": job_application.pk}) response = self.client.get(url) self.assertEqual(response.status_code, 403)
def test_create_job_seeker_by_proxy(self): User = get_user_model() proxy_user = PrescriberFactory() user_data = { "email": "*****@*****.**", "first_name": "John", "last_name": "Doe", "birthdate": "1978-12-20", "phone": "0610101010", } user = User.create_job_seeker_by_proxy(proxy_user, **user_data) self.assertTrue(user.is_job_seeker) self.assertIsNotNone(user.password) self.assertIsNotNone(user.username) self.assertEqual(user.email, user_data["email"]) self.assertEqual(user.first_name, user_data["first_name"]) self.assertEqual(user.last_name, user_data["last_name"]) self.assertEqual(user.birthdate, user_data["birthdate"]) self.assertEqual(user.phone, user_data["phone"]) self.assertEqual(user.created_by, proxy_user) self.assertEqual(user.last_login, None)
def setUp(self): self.organization = PrescriberOrganizationWithMembershipFactory() # Create a second member to make sure emails are also # sent to regular members self.organization.members.add(PrescriberFactory()) self.organization.save() self.sender = self.organization.members.first()
def test_create_job_seeker_by_proxy(self): User = get_user_model() proxy_user = PrescriberFactory() user_data = { "email": "*****@*****.**", "first_name": "John", "last_name": "Doe", "birthdate": "1978-12-20", "phone": "0610101010", "resume_link": "https://urlseemslegit.com/my-cv", } user = User.create_job_seeker_by_proxy(proxy_user, **user_data) self.assertTrue(user.is_job_seeker) self.assertIsNotNone(user.password) self.assertIsNotNone(user.username) self.assertEqual(user.email, user_data["email"]) self.assertEqual(user.first_name, user_data["first_name"]) self.assertEqual(user.last_name, user_data["last_name"]) self.assertEqual(user.birthdate, user_data["birthdate"]) self.assertEqual(user.phone, user_data["phone"]) self.assertEqual(user.created_by, proxy_user) self.assertEqual(user.last_login, None) self.assertEqual(user.resume_link, user_data["resume_link"]) # E-mail already exists, this should raise an error. with self.assertRaises(ValidationError): User.create_job_seeker_by_proxy(proxy_user, **user_data)
def test_apply_as_prescriber_for_approval_in_waiting_period(self): """Apply as prescriber for a job seeker with an approval in waiting period.""" siae = SiaeWithMembershipAndJobsFactory(romes=("N1101", "N1105")) job_seeker = JobSeekerFactory() # Create an approval in waiting period. end_at = datetime.date.today() - relativedelta(days=30) start_at = end_at - relativedelta(years=2) ApprovalFactory(user=job_seeker, start_at=start_at, end_at=end_at) user = PrescriberFactory() 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})) # …choose one, then follow all redirections… post_data = {"nir": job_seeker.nir, "confirm": 1} response = self.client.post(last_url, data=post_data, follow=True) # …until the expected 403. self.assertEqual(response.status_code, 403) self.assertEqual(response.context["exception"], ApprovalsWrapper.ERROR_CANNOT_OBTAIN_NEW_FOR_PROXY) last_url = response.redirect_chain[-1][0] self.assertEqual(last_url, reverse("apply:step_check_job_seeker_info", kwargs={"siae_pk": siae.pk}))
def test_create(self, mock_call_ban_geocoding_api, mock_call_insee_api): """Create a prescriber organization.""" user = PrescriberFactory() self.assertTrue( self.client.login(username=user.email, password=DEFAULT_PASSWORD)) url = reverse("prescribers_views:create_organization") response = self.client.get(url) self.assertEqual(response.status_code, 200) post_data = { "siret": "12000015300011", "phone": "", "email": "", "website": "", "description": "", } response = self.client.post(url, data=post_data) mock_call_insee_api.assert_called_once_with(post_data["siret"]) mock_call_ban_geocoding_api.assert_called_once() self.assertEqual(response.status_code, 302) organization = PrescriberOrganization.objects.get( siret=post_data["siret"]) self.assertIn(user, organization.members.all()) self.assertEqual(1, organization.members.count()) self.assertIn(organization, user.prescriberorganization_set.all()) self.assertEqual(1, user.prescriberorganization_set.count()) membership = user.prescribermembership_set.get( organization=organization) self.assertTrue(membership.is_admin)
def test_invite_existing_user_is_prescriber_without_org(self): guest = PrescriberFactory() self.post_data["form-0-first_name"] = guest.first_name self.post_data["form-0-last_name"] = guest.last_name self.post_data["form-0-email"] = guest.email response = self.client.post(INVITATION_URL, data=self.post_data, follow=True) self.assertRedirects(response, INVITATION_URL) self.assert_created_invitation()
def test_already_a_member(self): # The invited user is already a member self.organization.members.add(PrescriberFactory()) guest = self.organization.members.exclude(email=self.sender.email).first() self.client.login(email=self.sender.email, password=DEFAULT_PASSWORD) self.post_data.update( {"form-0-first_name": guest.first_name, "form-0-last_name": guest.last_name, "form-0-email": guest.email} ) response = self.client.post(INVITATION_URL, data=self.post_data) self.assertEqual(response.status_code, 200) self.assert_invalid_user(response, "Cette personne fait déjà partie de votre organisation.")
def test_valid_for_prescriber(self): user = PrescriberFactory() criterion1 = AdministrativeCriteria.objects.get(pk=13) form_data = { f"{AdministrativeCriteriaForm.LEVEL_2_PREFIX}{criterion1.pk}": "true" } form = AdministrativeCriteriaForm(user, siae=None, data=form_data) form.is_valid() expected_cleaned_data = [criterion1] self.assertEqual(form.cleaned_data, expected_cleaned_data)
def test_accept_existing_user_is_prescriber_without_org(self): user = PrescriberFactory() invitation = PrescriberWithOrgSentInvitationFactory( sender=self.sender, organization=self.organization, first_name=user.first_name, last_name=user.last_name, email=user.email, ) self.client.login(email=user.email, password=DEFAULT_PASSWORD) response = self.client.get(invitation.acceptance_link, follow=True) self.assert_invitation_is_accepted(response, user, invitation)
def test_not_allowed_user(self): # Only employers can currently access the Preferences page. prescriber = PrescriberFactory() self.client.login(username=prescriber.email, password=DEFAULT_PASSWORD) url = reverse("dashboard:edit_user_preferences") response = self.client.get(url) self.assertEqual(response.status_code, 403) job_seeker = JobSeekerFactory() self.client.login(username=job_seeker.email, password=DEFAULT_PASSWORD) url = reverse("dashboard:edit_user_preferences") response = self.client.get(url) self.assertEqual(response.status_code, 403)
def test_get_user_info_for_prescriber_without_organisation(self): user = PrescriberFactory() factory = RequestFactory() request = factory.get("/") request.user = user middleware = SessionMiddleware() middleware.process_request(request) request.session.save() user_info = get_user_info(request) self.assertEqual(user_info.user, user) self.assertEqual(user_info.kind, KIND_PRESCRIBER) self.assertEqual(user_info.prescriber_organization, None) self.assertEqual(user_info.is_authorized_prescriber, False) self.assertEqual(user_info.siae, None)
def test_error_senior_junior(self): """ Test ERROR_SENIOR_JUNIOR. """ user = PrescriberFactory() criterion1 = AdministrativeCriteria.objects.get( name="Senior (+50 ans)") criterion2 = AdministrativeCriteria.objects.get(name="Jeune (-26 ans)") form_data = { f"{AdministrativeCriteriaForm.LEVEL_2_PREFIX}{criterion1.pk}": "true", f"{AdministrativeCriteriaForm.LEVEL_2_PREFIX}{criterion2.pk}": "true", } form = AdministrativeCriteriaForm(user, siae=None, data=form_data) form.is_valid() self.assertIn(form.ERROR_SENIOR_JUNIOR, form.errors["__all__"])
def test_edit_by_prescriber_of_organization(self): job_application = JobApplicationSentByAuthorizedPrescriberOrganizationFactory( ) prescriber = job_application.sender # Ensure that the job seeker is not autonomous (i.e. he did not register by himself). job_application.job_seeker.created_by = prescriber job_application.job_seeker.save() # Log as other member of the same organization other_prescriber = PrescriberFactory() prescribers_factories.PrescriberMembershipFactory( user=other_prescriber, organization=job_application.sender_prescriber_organization) self.client.login(username=other_prescriber.email, password=DEFAULT_PASSWORD) url = reverse("dashboard:edit_job_seeker_info", kwargs={"job_application_id": job_application.pk}) response = self.client.get(url) self.assertEqual(response.status_code, 200)
def test_error_error_long_term_job_seeker(self): """ Test ERROR_LONG_TERM_JOB_SEEKER. """ user = PrescriberFactory() criterion1 = AdministrativeCriteria.objects.get( name="DETLD (+ 24 mois)") criterion2 = AdministrativeCriteria.objects.get( name="DELD (12-24 mois)") form_data = { # Level 1. f"{AdministrativeCriteriaForm.LEVEL_1_PREFIX}{criterion1.pk}": "true", # Level 2. f"{AdministrativeCriteriaForm.LEVEL_2_PREFIX}{criterion2.pk}": "true", } form = AdministrativeCriteriaForm(user, siae=None, data=form_data) form.is_valid() self.assertIn(form.ERROR_LONG_TERM_JOB_SEEKER, form.errors["__all__"])
def test_accept_existing_user_not_logged_in(self): invitation = PrescriberWithOrgSentInvitationFactory(sender=self.sender, organization=self.organization) user = PrescriberFactory() # The user verified its email EmailAddress(user_id=user.pk, email=user.email, verified=True, primary=True).save() invitation = PrescriberWithOrgSentInvitationFactory( sender=self.sender, organization=self.organization, first_name=user.first_name, last_name=user.last_name, email=user.email, ) response = self.client.get(invitation.acceptance_link, follow=True) self.assertIn(reverse("account_login"), response.wsgi_request.get_full_path()) self.assertFalse(invitation.accepted) response = self.client.post( response.wsgi_request.get_full_path(), data={"login": user.email, "password": DEFAULT_PASSWORD}, follow=True, ) self.assertTrue(response.context["user"].is_authenticated) self.assert_invitation_is_accepted(response, user, invitation)
def test_new_prescriber_sees_welcoming_tour_test(self): session = self.client.session session[settings.ITOU_SESSION_PRESCRIBER_SIGNUP_KEY] = { "url_history": [] } session.save() prescriber = PrescriberFactory.build() self.email = prescriber.email url = reverse("signup:prescriber_user") post_data = { "first_name": prescriber.first_name, "last_name": prescriber.last_name, "email": prescriber.email, "password1": PASSWORD, "password2": PASSWORD, } response = self.client.post(url, data=post_data) response = self.verify_email(response.wsgi_request) # User should be redirected to the welcoming tour as he just signed up self.assertEqual(response.wsgi_request.path, reverse("welcoming_tour:index")) self.assertTemplateUsed(response, "welcoming_tour/prescriber.html")
def test_new_signup_warning_email_to_existing_members(self): authorized_organization = AuthorizedPrescriberOrganizationWithMembershipFactory() self.assertEqual(1, authorized_organization.members.count()) user = authorized_organization.members.first() new_user = PrescriberFactory() message = authorized_organization.new_signup_warning_email_to_existing_members(new_user) message.send() self.assertEqual(len(mail.outbox), 1) email = mail.outbox[0] self.assertIn("Un nouvel utilisateur vient de rejoindre votre organisation", email.subject) self.assertIn("Si vous ne connaissez pas cette personne veuillez nous contacter", email.body) self.assertIn(new_user.first_name, email.body) self.assertIn(new_user.last_name, email.body) self.assertIn(new_user.email, email.body) self.assertIn(authorized_organization.display_name, email.body) self.assertIn(authorized_organization.siret, email.body) self.assertIn(authorized_organization.kind, email.body) self.assertEqual(email.from_email, settings.DEFAULT_FROM_EMAIL) self.assertEqual(len(email.to), 1) self.assertEqual(email.to[0], user.email)
def test_expired_invitation_with_existing_user(self): user = PrescriberFactory() invitation = PrescriberWithOrgSentInvitationFactory( first_name=user.first_name, last_name=user.last_name, email=user.email, sender=self.sender, organization=self.organization, ) invitation.sent_at -= timedelta(days=invitation.EXPIRATION_DAYS) invitation.save() self.assertTrue(invitation.has_expired) # GET or POST in this case response = self.client.get(invitation.acceptance_link, follow=True) self.assertContains(response, escape("Cette invitation n'est plus valide.")) self.client.login(email=user.email, password=DEFAULT_PASSWORD) # Try to bypass the first check by directly reaching the join endpoint join_url = reverse("invitations_views:join_prescriber_organization", kwargs={"invitation_id": invitation.id}) response = self.client.get(join_url, follow=True) # The 2 views return the same error message self.assertContains(response, escape("Cette invitation n'est plus valide."))
def test_apply_as_prescriber(self): """Apply as prescriber.""" siae = SiaeWithMembershipAndJobsFactory(romes=("N1101", "N1105")) user = PrescriberFactory() self.client.login(username=user.email, password=DEFAULT_PASSWORD) # Entry point. # ---------------------------------------------------------------------- url = reverse("apply:start", kwargs={"siae_pk": siae.pk}) response = self.client.get(url) self.assertEqual(response.status_code, 302) session = self.client.session session_data = session[settings.ITOU_SESSION_JOB_APPLICATION_KEY] expected_session_data = { "job_seeker_pk": None, "to_siae_pk": siae.pk, "sender_pk": None, "sender_kind": None, "sender_siae_pk": None, "sender_prescriber_organization_pk": None, "job_description_id": None, } self.assertDictEqual(session_data, expected_session_data) next_url = reverse("apply:step_sender", kwargs={"siae_pk": siae.pk}) self.assertEqual(response.url, next_url) # Step determine the sender. # ---------------------------------------------------------------------- response = self.client.get(next_url) self.assertEqual(response.status_code, 302) session = self.client.session session_data = session[settings.ITOU_SESSION_JOB_APPLICATION_KEY] expected_session_data = { "job_seeker_pk": None, "to_siae_pk": siae.pk, "sender_pk": user.pk, "sender_kind": JobApplication.SENDER_KIND_PRESCRIBER, "sender_siae_pk": None, "sender_prescriber_organization_pk": None, "job_description_id": None, } self.assertDictEqual(session_data, expected_session_data) next_url = reverse("apply:step_job_seeker", kwargs={"siae_pk": siae.pk}) self.assertEqual(response.url, next_url) # Step determine the job seeker. # ---------------------------------------------------------------------- response = self.client.get(next_url) self.assertEqual(response.status_code, 200) post_data = {"email": "*****@*****.**"} response = self.client.post(next_url, data=post_data) self.assertEqual(response.status_code, 302) next_url = reverse("apply:step_create_job_seeker", kwargs={"siae_pk": siae.pk}) args = urlencode({"email": post_data["email"]}) self.assertEqual(response.url, f"{next_url}?{args}") # Step create a job seeker. # ---------------------------------------------------------------------- response = self.client.get(next_url) self.assertEqual(response.status_code, 200) post_data = { "email": "*****@*****.**", "first_name": "John", "last_name": "Doe", "birthdate": "20/12/1978", "phone": "0610200305", "pole_emploi_id": "12345678", } response = self.client.post(next_url, data=post_data) self.assertEqual(response.status_code, 302) new_job_seeker = get_user_model().objects.get(email=post_data["email"]) session = self.client.session session_data = session[settings.ITOU_SESSION_JOB_APPLICATION_KEY] expected_session_data = { "job_seeker_pk": new_job_seeker.pk, "to_siae_pk": siae.pk, "sender_pk": user.pk, "sender_kind": JobApplication.SENDER_KIND_PRESCRIBER, "sender_siae_pk": None, "sender_prescriber_organization_pk": None, "job_description_id": None, } self.assertDictEqual(session_data, expected_session_data) next_url = reverse("apply:step_eligibility", kwargs={"siae_pk": siae.pk}) self.assertEqual(response.url, next_url) # Step eligibility. # ---------------------------------------------------------------------- response = self.client.get(next_url) self.assertEqual(response.status_code, 302) next_url = reverse("apply:step_application", kwargs={"siae_pk": siae.pk}) self.assertEqual(response.url, next_url) # Step application. # ---------------------------------------------------------------------- response = self.client.get(next_url) self.assertEqual(response.status_code, 200) 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.", } response = self.client.post(next_url, data=post_data) self.assertEqual(response.status_code, 302) next_url = reverse("apply:list_for_prescriber") self.assertEqual(response.url, next_url) job_application = JobApplication.objects.get(job_seeker=new_job_seeker, sender=user, to_siae=siae) self.assertEqual(job_application.sender_kind, JobApplication.SENDER_KIND_PRESCRIBER) self.assertEqual(job_application.sender_siae, None) self.assertEqual(job_application.sender_prescriber_organization, None) self.assertEqual(job_application.state, job_application.state.workflow.STATE_NEW) self.assertEqual(job_application.message, post_data["message"]) self.assertEqual(job_application.answer, "") self.assertEqual(job_application.selected_jobs.count(), 2) self.assertEqual(job_application.selected_jobs.first().pk, post_data["selected_jobs"][0]) self.assertEqual(job_application.selected_jobs.last().pk, post_data["selected_jobs"][1])
def test_apply_as_prescriber(self): """Apply as prescriber.""" siae = SiaeWithMembershipAndJobsFactory(romes=("N1101", "N1105")) user = PrescriberFactory() self.client.login(username=user.email, password=DEFAULT_PASSWORD) # Entry point. # ---------------------------------------------------------------------- url = reverse("apply:start", kwargs={"siae_pk": siae.pk}) response = self.client.get(url) self.assertEqual(response.status_code, 302) session = self.client.session session_data = session[settings.ITOU_SESSION_JOB_APPLICATION_KEY] expected_session_data = self.default_session_data | { "to_siae_pk": siae.pk, } self.assertDictEqual(session_data, expected_session_data) next_url = reverse("apply:step_sender", kwargs={"siae_pk": siae.pk}) self.assertEqual(response.url, next_url) # Step determine the sender. # ---------------------------------------------------------------------- response = self.client.get(next_url) self.assertEqual(response.status_code, 302) session = self.client.session session_data = session[settings.ITOU_SESSION_JOB_APPLICATION_KEY] expected_session_data = self.default_session_data | { "to_siae_pk": siae.pk, "sender_pk": user.pk, "sender_kind": JobApplication.SENDER_KIND_PRESCRIBER, } self.assertDictEqual(session_data, expected_session_data) next_url = reverse("apply:step_check_job_seeker_nir", kwargs={"siae_pk": siae.pk}) self.assertEqual(response.url, next_url) # Step determine the job seeker with a NIR. # ---------------------------------------------------------------------- response = self.client.get(next_url) self.assertEqual(response.status_code, 200) nir = "141068078200557" post_data = {"nir": nir, "confirm": 1} response = self.client.post(next_url, data=post_data) self.assertEqual(response.status_code, 302) session = self.client.session session_data = session[settings.ITOU_SESSION_JOB_APPLICATION_KEY] expected_session_data = self.default_session_data | { "nir": nir, "to_siae_pk": siae.pk, "sender_pk": user.pk, "sender_kind": JobApplication.SENDER_KIND_PRESCRIBER, } next_url = reverse("apply:step_job_seeker", kwargs={"siae_pk": siae.pk}) self.assertEqual(response.url, next_url) # Step get job seeker e-mail. # ---------------------------------------------------------------------- response = self.client.get(next_url) self.assertEqual(response.status_code, 200) post_data = {"email": "*****@*****.**", "save": "1"} response = self.client.post(next_url, data=post_data) self.assertEqual(response.status_code, 302) next_url = reverse("apply:step_create_job_seeker", kwargs={"siae_pk": siae.pk}) args = urlencode({"email": post_data["email"]}) self.assertEqual(response.url, f"{next_url}?{args}") # Step create a job seeker. # ---------------------------------------------------------------------- response = self.client.get(next_url) self.assertEqual(response.status_code, 200) post_data = { "email": "*****@*****.**", "first_name": "John", "last_name": "Doe", "birthdate": "20/12/1978", "phone": "0610200305", "pole_emploi_id": "12345678", "address_line_1": "55, avenue de la Rose", "address_line_2": "7e étage", "post_code": "67200", "city": "Sommerau (67)", "city_slug": "sommerau-67", } response = self.client.post(next_url, data=post_data) self.assertEqual(response.status_code, 302) new_job_seeker = User.objects.get(email=post_data["email"]) session = self.client.session session_data = session[settings.ITOU_SESSION_JOB_APPLICATION_KEY] expected_session_data = self.default_session_data | { "job_seeker_pk": new_job_seeker.pk, "nir": new_job_seeker.nir, "to_siae_pk": siae.pk, "sender_pk": user.pk, "sender_kind": JobApplication.SENDER_KIND_PRESCRIBER, } self.assertDictEqual(session_data, expected_session_data) next_url = reverse("apply:step_eligibility", kwargs={"siae_pk": siae.pk}) self.assertEqual(response.url, next_url) # Step eligibility. # ---------------------------------------------------------------------- response = self.client.get(next_url) self.assertEqual(response.status_code, 302) next_url = reverse("apply:step_application", kwargs={"siae_pk": siae.pk}) self.assertEqual(response.url, next_url) # Step application. # ---------------------------------------------------------------------- response = self.client.get(next_url) self.assertEqual(response.status_code, 200) 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) self.assertEqual(response.status_code, 302) next_url = reverse("apply:step_application_sent", kwargs={"siae_pk": siae.pk}) self.assertEqual(response.url, next_url) job_application = JobApplication.objects.get(job_seeker=new_job_seeker, sender=user, to_siae=siae) self.assertEqual(job_application.sender_kind, JobApplication.SENDER_KIND_PRESCRIBER) self.assertEqual(job_application.sender_siae, None) self.assertEqual(job_application.sender_prescriber_organization, None) self.assertEqual(job_application.state, job_application.state.workflow.STATE_NEW) self.assertEqual(job_application.message, post_data["message"]) self.assertEqual(job_application.answer, "") self.assertEqual(job_application.selected_jobs.count(), 2) self.assertEqual(job_application.selected_jobs.first().pk, post_data["selected_jobs"][0]) self.assertEqual(job_application.selected_jobs.last().pk, post_data["selected_jobs"][1]) self.assertEqual(job_application.resume_link, post_data["resume_link"])
def setUp(self): self.organization = PrescriberPoleEmploiFactory() self.organization.members.add(PrescriberFactory()) self.sender = self.organization.members.first()
def test_find_or_create_job_seeker__create(self): developer = UserFactory( email=settings.AI_EMPLOYEES_STOCK_DEVELOPER_EMAIL) commune = CommuneFactory( code=getattr(CleanedAiCsvFileMock, CITY_INSEE_COL)) command = self.command # Job seeker not found: create user. df = pandas.DataFrame([CleanedAiCsvFileMock()]) created, job_seeker = command.find_or_create_job_seeker( row=df.iloc[0], created_by=developer) self.assertTrue(created) self.assertTrue(job_seeker) self.assertEqual(User.objects.all().count(), 2) # Clean job_seeker.delete() # # NIR is empty: create user with NIR None, even if another one exists. previous_job_seekers_pk = [ JobSeekerFactory(nir="").pk, JobSeekerFactory(nir=None).pk ] df = pandas.DataFrame( [CleanedAiCsvFileMock(**{ NIR_COL: "", "nir_is_valid": False })]) created, job_seeker = command.find_or_create_job_seeker( row=df.iloc[0], created_by=developer) self.assertTrue(created) self.assertTrue(job_seeker) self.assertEqual(User.objects.all().count(), 4) self.assertEqual(job_seeker.nir, None) # Clean job_seeker.delete() User.objects.filter(pk__in=previous_job_seekers_pk).delete() # # If found job_seeker is not a job seeker: create one with a fake email address. prescriber = PrescriberFactory( email=getattr(CleanedAiCsvFileMock(), EMAIL_COL)) df = pandas.DataFrame([CleanedAiCsvFileMock()]) created, job_seeker = command.find_or_create_job_seeker( row=df.iloc[0], created_by=developer) self.assertTrue(created) self.assertTrue(job_seeker) self.assertEqual(User.objects.all().count(), 3) self.assertEqual(job_seeker.nir, getattr(CleanedAiCsvFileMock(), NIR_COL)) self.assertNotEqual(job_seeker.email, prescriber.email) # Clean job_seeker.delete() prescriber.delete() # Check expected attributes. # Perfect path: NIR, email, ... # Created by developer on XX date. base_data = CleanedAiCsvFileMock() df = pandas.DataFrame([base_data]) created, job_seeker = command.find_or_create_job_seeker( row=df.iloc[0], created_by=developer) self.assertTrue(created) self.assertEqual(job_seeker.created_by.pk, developer.pk) self.assertEqual(job_seeker.date_joined, settings.AI_EMPLOYEES_STOCK_IMPORT_DATE) self.assertEqual(job_seeker.first_name, getattr(base_data, FIRST_NAME_COL).title()) self.assertEqual(job_seeker.last_name, getattr(base_data, LAST_NAME_COL).title()) self.assertEqual(job_seeker.birthdate, getattr(base_data, BIRTHDATE_COL)) self.assertEqual(job_seeker.email, getattr(base_data, EMAIL_COL)) self.assertTrue(job_seeker.address_line_1) self.assertTrue(job_seeker.address_line_2) self.assertEqual(job_seeker.post_code, getattr(base_data, POST_CODE_COL)) self.assertEqual(job_seeker.city, commune.name.title()) self.assertTrue(job_seeker.department) self.assertEqual(job_seeker.phone, getattr(base_data, PHONE_COL)) self.assertEqual(job_seeker.nir, getattr(base_data, NIR_COL)) # Clean job_seeker.delete() # If no email provided: fake email. df = pandas.DataFrame([CleanedAiCsvFileMock(**{EMAIL_COL: ""})]) created, job_seeker = command.find_or_create_job_seeker( row=df.iloc[0], created_by=developer) self.assertTrue(created) self.assertTrue(job_seeker.email.endswith("@email-temp.com")) job_seeker.delete() # A job seeker is found by email address but its NIR is different. # Create a new one. email = getattr(CleanedAiCsvFileMock(), EMAIL_COL) JobSeekerFactory(nir="141062a78200555", email=email) df = pandas.DataFrame([CleanedAiCsvFileMock()]) created, job_seeker = command.find_or_create_job_seeker( row=df.iloc[0], created_by=developer) self.assertTrue(created) self.assertTrue(job_seeker.email.endswith("@email-temp.com")) job_seeker.delete()