def test_form_loads_manager_data(self): issuer: Issuer = IssuerFactory() manager: Manager = ManagerFactory(is_aidant=True) form = ManagerForm() organisation: OrganisationRequest = DraftOrganisationRequestFactory( issuer=issuer, manager=manager ) self.__open_form_url(issuer, organisation) field_names = list(form.fields.keys()) field_names.remove("is_aidant") element: WebElement = self.selenium.find_element( By.CSS_SELECTOR, f"#id_{PersonnelForm.MANAGER_FORM_PREFIX}-is_aidant", ) self.assertIsNotNone( element.get_attribute("checked"), "Manager is also an aidant, checkbox should have been checked", ) for field_name in field_names: element: WebElement = self.selenium.find_element( By.CSS_SELECTOR, f"#id_{PersonnelForm.MANAGER_FORM_PREFIX}-{field_name}", ) self.assertEqual( element.get_attribute("value"), getattr(manager, field_name) )
def setUpTestData(cls): cls.client = Client() cls.pattern_name = "habilitation_modify_organisation" cls.template_name = "organisation_form.html" cls.issuer: Issuer = IssuerFactory() cls.organisation: OrganisationRequest = DraftOrganisationRequestFactory( issuer=cls.issuer )
def setUpTestData(cls): cls.client = Client() cls.pattern_name = "habilitation_organisation_view" cls.template_name = "view_organisation_request.html" cls.issuer = IssuerFactory() cls.organisation: OrganisationRequest = OrganisationRequestFactory( issuer=cls.issuer )
def test_render_warning_when_issuer_already_exists(self): issuer: Issuer = IssuerFactory() data = utils.get_form(IssuerForm).clean() data["email"] = issuer.email response = self.client.post(reverse(self.pattern_name), data) self.assertTemplateUsed(response, "issuer_already_exists_warning.html")
def test_for_issuer(self, _): issuer: Issuer = IssuerFactory(email_verified=False) email_confirmation = IssuerEmailConfirmation.for_issuer(issuer) self.assertEqual( email_confirmation.key, "ph1odxqrd3kd5tveroyxjnctlhveevtkusqvd96lar5fb1zhvdzbgwuenkwdtmqs", ) self.assertEqual(email_confirmation.issuer, issuer) self.assertEqual(email_confirmation.created, self.NOW)
def test_send(self, send_mock: Mock): email_confirmation = IssuerEmailConfirmation.for_issuer( IssuerFactory(email_verified=False)) request = HttpRequest() self.assertIs(email_confirmation.sent, None) email_confirmation.send(request) self.assertEqual(email_confirmation.sent, self.NOW) send_mock.assert_called_with(IssuerEmailConfirmation, request=request, confirmation=email_confirmation)
def test_confirm_on_expired_confirmation_returns_none(self): issuer: Issuer = IssuerFactory(email_verified=False) email_confirmation = IssuerEmailConfirmation.objects.create( issuer=issuer, sent=now() - timedelta(days=self.EXPIRE_DAYS + 3)) with patch("aidants_connect_habilitation.models.Issuer.save" ) as mock_save: self.assertFalse(issuer.email_verified) self.assertIs(email_confirmation.confirm(), None) self.assertFalse(issuer.email_verified) mock_save.assert_not_called()
def test_confirm_on_already_confirmed_user_just_return_email(self): issuer: Issuer = IssuerFactory(email_verified=True) email_confirmation = IssuerEmailConfirmation.objects.create( issuer=issuer, sent=now() - timedelta(days=self.EXPIRE_DAYS + 3)) with patch("aidants_connect_habilitation.models.Issuer.save" ) as mock_save: self.assertTrue(issuer.email_verified) self.assertEqual(email_confirmation.confirm(), issuer.email) self.assertTrue(issuer.email_verified) mock_save.assert_not_called()
def setUpTestData(cls): cls.client = Client() cls.pattern_name = "habilitation_issuer_email_confirmation_confirm" cls.template_name = "email_confirmation_confirm.html" cls.issuer: Issuer = IssuerFactory(email_verified=False) cls.email_confirmation = IssuerEmailConfirmation.objects.create( issuer=cls.issuer, sent=now() ) cls.expired_email_confirmation = IssuerEmailConfirmation.objects.create( issuer=cls.issuer, sent=now() - timedelta(days=settings.EMAIL_CONFIRMATION_EXPIRE_DAYS + 2), )
def test_404_on_unrelated_issuer_confirmation(self): unrelated_issuer: Issuer = IssuerFactory(email_verified=False) response = self.client.get( reverse( self.pattern_name, kwargs={ "issuer_id": unrelated_issuer.issuer_id, "key": self.email_confirmation.key, }, ) ) self.assertEqual(response.status_code, 404)
def test_confirm_saves_issuer_model(self): issuer: Issuer = IssuerFactory(email_verified=False) email_confirmation = IssuerEmailConfirmation.objects.create( issuer=issuer, sent=now() + timedelta(days=365)) with patch("aidants_connect_habilitation.models.Issuer.save" ) as mock_save: self.assertFalse(issuer.email_verified) self.assertEqual(email_confirmation.confirm(), issuer.email) self.assertTrue(issuer.email_verified) # Ensure mocking correctly works for the next test mock_save.assert_called()
def test_form_submit_modify_multiple_aidants(self): issuer: Issuer = IssuerFactory() manager: Manager = ManagerFactory() organisation: OrganisationRequest = DraftOrganisationRequestFactory( issuer=issuer, manager=manager, ) for _ in range(4): AidantRequestFactory(organisation=organisation) self.__open_form_url(issuer, organisation) modified_aidant_idx = 2 modified_aidant_email = self.selenium.find_element( By.CSS_SELECTOR, f"#id_{PersonnelForm.AIDANTS_FORMSET_PREFIX}-{modified_aidant_idx}-email", ).get_attribute("value") new_aidant_form: AidantRequestForm = get_form(AidantRequestForm) aidant_data = new_aidant_form.cleaned_data for field_name in new_aidant_form.fields: element: WebElement = self.selenium.find_element( By.CSS_SELECTOR, f"#id_{PersonnelForm.AIDANTS_FORMSET_PREFIX}-" f"{modified_aidant_idx}-{field_name}", ) element.clear() element.send_keys(aidant_data[field_name]) self.selenium.find_element(By.CSS_SELECTOR, "#add-aidant-btn").click() self.selenium.find_element(By.CSS_SELECTOR, '[type="submit"]').click() path = reverse( "habilitation_validation", kwargs={ "issuer_id": str(organisation.issuer.issuer_id), "uuid": str(organisation.uuid), }, ) WebDriverWait(self.selenium, 10).until(url_matches(f"^.+{path}$")) organisation.refresh_from_db() self.assertEqual(organisation.aidant_requests.count(), 4) with self.assertRaises(AidantRequest.DoesNotExist): AidantRequest.objects.get(email=modified_aidant_email) saved_aidant = organisation.aidant_requests.get(email=aidant_data["email"]) for field_name in new_aidant_form.fields: self.assertEqual(getattr(saved_aidant, field_name), aidant_data[field_name])
def test_redirect_on_unverified_issuer_email(self): unverified_issuer: Issuer = IssuerFactory(email_verified=False) organisation = OrganisationRequestFactory(issuer=unverified_issuer) response = self.client.get( self.get_url(unverified_issuer.issuer_id, organisation.uuid) ) self.assertRedirects( response, reverse( "habilitation_issuer_email_confirmation_waiting", kwargs={"issuer_id": unverified_issuer.issuer_id}, ), )
def test_key_expired(self): issuer: Issuer = IssuerFactory(email_verified=False) send_limit = self.NOW - timedelta(days=self.EXPIRE_DAYS) email_confirmation = IssuerEmailConfirmation.objects.create( issuer=issuer, sent=send_limit + timedelta(seconds=1)) self.assertFalse(email_confirmation.key_expired) email_confirmation = IssuerEmailConfirmation.objects.create( issuer=issuer, sent=send_limit - timedelta(seconds=1)) self.assertTrue(email_confirmation.key_expired)
def test_404_on_bad_uuid(self): issuer: Issuer = IssuerFactory() response = self.client.get(self.get_url(issuer.issuer_id, uuid4())) self.assertEqual(response.status_code, 404) cleaned_data = utils.get_form(OrganisationRequestForm).clean() cleaned_data["type"] = cleaned_data["type"].id response = self.client.post( self.get_url(issuer.issuer_id, uuid4()), cleaned_data, ) self.assertEqual(response.status_code, 404)
def test_redirect_on_unverified_issuer_email(self): unverified_issuer: Issuer = IssuerFactory(email_verified=False) response = self.client.get( reverse( self.pattern_name, kwargs={"issuer_id": unverified_issuer.issuer_id} ) ) self.assertRedirects( response, reverse( "habilitation_issuer_email_confirmation_waiting", kwargs={"issuer_id": unverified_issuer.issuer_id}, ), )
def test_404_on_unrelated_issuer_id(self): unrelated_issuer = IssuerFactory() response: HttpResponse = self.client.get( self.get_url(unrelated_issuer.issuer_id, self.organisation.uuid) ) self.assertEqual(response.status_code, 404) cleaned_data = utils.get_form(OrganisationRequestForm).clean() response = self.client.post( self.get_url(unrelated_issuer.issuer_id, self.organisation.uuid), cleaned_data, ) self.assertEqual(response.status_code, 404)
def test_signal_sends_mail(self, send_mail_mock: Mock): email_confirmation = IssuerEmailConfirmation.for_issuer( IssuerFactory(email_verified=False)) request = HttpRequest() request.META["SERVER_NAME"] = "localhost" request.META["SERVER_PORT"] = "3000" email_confirmation.send(request) send_mail_mock.assert_called_with( from_email=self.EMAIL_FROM, recipient_list=[email_confirmation.issuer.email], subject=self.EMAIL_SUBJECT, message=ANY, html_message=ANY, )
def test_js_managment_form_aidant_count_is_modified(self): issuer: Issuer = IssuerFactory() organisation: OrganisationRequest = DraftOrganisationRequestFactory( issuer=issuer ) self.__open_form_url(issuer, organisation) input_el = self.selenium.find_element( By.CSS_SELECTOR, "input[name$='TOTAL_FORMS']" ) for i in range(1, 5): self.assertEqual(str(i), input_el.get_attribute("value")) self.selenium.find_element(By.CSS_SELECTOR, "#add-aidant-btn").click() self.assertEqual(str(i + 1), input_el.get_attribute("value"))
def test_send_email_when_issuer_already_exists(self, send_mail_mock: Mock): issuer: Issuer = IssuerFactory() data = utils.get_form(IssuerForm).clean() data["email"] = issuer.email self.client.post(reverse(self.pattern_name), data) send_mail_mock.assert_called_with( from_email=settings.EMAIL_HABILITATION_ISSUER_EMAIL_ALREADY_EXISTS_FROM, recipient_list=[issuer.email], subject=settings.EMAIL_HABILITATION_ISSUER_EMAIL_ALREADY_EXISTS_SUBJECT, message=ANY, html_message=ANY, )
def test_submit_form_without_phone_passes(self): email = Faker().email() issuer = IssuerFactory.build(email=email) self.__open_form_url() self.__fill_form_and_submit( issuer, ["first_name", "last_name", "email", "profession"]) path = reverse( "habilitation_issuer_email_confirmation_waiting", kwargs={"issuer_id": Issuer.objects.get(email=email).issuer_id}, ) WebDriverWait(self.selenium, 10).until(url_matches(f"^.+{path}$"))
def test_js_aidant_form_is_added_to_formset(self): issuer: Issuer = IssuerFactory() organisation: OrganisationRequest = DraftOrganisationRequestFactory( issuer=issuer ) self.__open_form_url(issuer, organisation) for i in range(1, 5): input_el = self.selenium.find_elements( By.CSS_SELECTOR, ".aidant-form-container" ) self.assertEqual(i, len(input_el)) self.selenium.find_element(By.CSS_SELECTOR, "#add-aidant-btn").click() input_el = self.selenium.find_elements( By.CSS_SELECTOR, ".aidant-form-container" ) self.assertEqual(i + 1, len(input_el))
def test_form_submit_multiple_aidants(self): issuer: Issuer = IssuerFactory() manager: Manager = ManagerFactory() organisation: OrganisationRequest = DraftOrganisationRequestFactory( issuer=issuer, manager=manager, ) # Setup 2 initial requests AidantRequestFactory(organisation=organisation) AidantRequestFactory(organisation=organisation) self.__open_form_url(issuer, organisation) for i in range(2, 6): aidant_form: AidantRequestForm = get_form(AidantRequestForm) aidant_data = aidant_form.cleaned_data for field_name in aidant_form.fields: element: WebElement = self.selenium.find_element( By.CSS_SELECTOR, f"#id_{PersonnelForm.AIDANTS_FORMSET_PREFIX}-{i}-{field_name}", ) element.clear() element.send_keys(aidant_data[field_name]) self.selenium.find_element(By.CSS_SELECTOR, "#add-aidant-btn").click() self.selenium.find_element(By.CSS_SELECTOR, '[type="submit"]').click() path = reverse( "habilitation_validation", kwargs={ "issuer_id": str(organisation.issuer.issuer_id), "uuid": str(organisation.uuid), }, ) WebDriverWait(self.selenium, 10).until(url_matches(f"^.+{path}$")) organisation.refresh_from_db() self.assertEqual(organisation.aidant_requests.count(), 6)
def test_get_redirect_on_previously_confirmed(self): confirmed_issuer: Issuer = IssuerFactory(email_verified=True) email_confirmation = IssuerEmailConfirmation.objects.create( issuer=confirmed_issuer, sent=now() ) response = self.client.get( reverse( self.pattern_name, kwargs={ "issuer_id": confirmed_issuer.issuer_id, "key": email_confirmation.key, }, ) ) self.assertRedirects( response, reverse( "habilitation_new_organisation", kwargs={"issuer_id": confirmed_issuer.issuer_id}, ), )
def test_js_added_aidant_form_has_correct_id(self): """ This test verifies that the JS code generates forms exactly like AidantRequestFormSet would. """ issuer: Issuer = IssuerFactory() organisation: OrganisationRequest = DraftOrganisationRequestFactory( issuer=issuer ) self.__open_form_url(issuer, organisation) self.selenium.find_element(By.CSS_SELECTOR, "#add-aidant-btn").click() self.selenium.find_element(By.CSS_SELECTOR, "#add-aidant-btn").click() self.selenium.find_element(By.CSS_SELECTOR, "#add-aidant-btn").click() form_elts = self.selenium.find_elements( By.CSS_SELECTOR, ".aidant-form-container" ) self.assertEqual(len(form_elts), 4) form = AidantRequestForm() for i, _ in enumerate(form_elts): for field_name, field in form.fields.items(): html_id = f"id_{PersonnelForm.AIDANTS_FORMSET_PREFIX}-{i}-{field_name}" try: field_label = self.selenium.find_element( By.CSS_SELECTOR, f'[for="{html_id}"]' ) except NoSuchElementException: self.fail( f"Label for form element {html_id} not found. " "Was form 'prefix' or 'auto_id' modified?" ) self.assertEqual(field_label.text, field.label)
def test_form_submit_no_aidants(self): issuer: Issuer = IssuerFactory() manager: Manager = ManagerFactory() organisation: OrganisationRequest = DraftOrganisationRequestFactory( issuer=issuer, manager=manager, ) self.__open_form_url(issuer, organisation) self.selenium.find_element(By.CSS_SELECTOR, '[type="submit"]').click() path = reverse( "habilitation_validation", kwargs={ "issuer_id": str(organisation.issuer.issuer_id), "uuid": str(organisation.uuid), }, ) WebDriverWait(self.selenium, 10).until(url_matches(f"^.+{path}$")) organisation.refresh_from_db() self.assertEqual(organisation.aidant_requests.count(), 0)
def test_its_me_button_fills_manager_form(self): issuer: Issuer = IssuerFactory() organisation: OrganisationRequest = DraftOrganisationRequestFactory( issuer=issuer, ) form = IssuerForm() self.__open_form_url(issuer, organisation) for field_name in form.fields: element: WebElement = self.selenium.find_element( By.CSS_SELECTOR, f"#manager-subform [name$='{field_name}']" ) self.assertEqual(element.get_attribute("value"), "") self.selenium.find_element(By.CSS_SELECTOR, "#its-me-manager").click() for field_name in form.fields: element: WebElement = self.selenium.find_element( By.CSS_SELECTOR, f"#manager-subform [name$='{field_name}']" ) field_value = getattr(issuer, field_name) self.assertEqual(element.get_attribute("value"), field_value)
def test_cannot_submit_form_without_aidants_displays_errors(self): issuer: Issuer = IssuerFactory() manager: Manager = ManagerFactory(is_aidant=False) organisation: OrganisationRequest = DraftOrganisationRequestFactory( issuer=issuer, manager=manager ) self.__open_form_url(issuer, organisation) self.selenium.find_element(By.CSS_SELECTOR, '[type="submit"]').click() error_element = self.selenium.find_element(By.CSS_SELECTOR, "form p.errorlist") self.assertEqual( error_element.text, "Vous devez déclarer au moins 1 aidant si le ou la responsable de " "l'organisation n'est pas elle-même déclarée comme aidante", ) error_element = self.selenium.find_element( By.CSS_SELECTOR, "#manager-subform p.errorlist" ) self.assertEqual( error_element.text, "Veuillez cocher cette case ou déclarer au moins un aidant ci-dessous", ) error_element = self.selenium.find_element( By.CSS_SELECTOR, ".aidant-forms p.errorlist" ) self.assertEqual( error_element.text, "Vous devez déclarer au moins 1 aidant si le ou la responsable de " "l'organisation n'est pas elle-même déclarée comme aidante", )
def test_js_hide_add_aidant_form_button_on_max(self): issuer: Issuer = IssuerFactory() organisation: OrganisationRequest = DraftOrganisationRequestFactory( issuer=issuer ) self.__open_form_url(issuer, organisation) add_aidant_button = self.selenium.find_element( By.CSS_SELECTOR, "#add-aidant-btn" ) max_value = self.selenium.find_element( By.CSS_SELECTOR, "input[name$='MAX_NUM_FORMS']" ).get_attribute("value") # Manually set the number of forms to the max - 1 for the Stimulus controller self.selenium.execute_script( """document.querySelector("[data-controller='personnel-form']")""" f""".dataset.personnelFormFormCountValue = {int(max_value) - 1}""" ) self.assertTrue(add_aidant_button.is_displayed()) add_aidant_button.click() self.assertFalse(add_aidant_button.is_displayed())
def test_email_confirmation_process(self, send_mail_mock: Mock): email = Faker().email() issuer = IssuerFactory.build(email=email) self.__open_form_url() self.__fill_form_and_submit( issuer, ["first_name", "last_name", "email", "profession", "phone"]) path = reverse( "habilitation_issuer_email_confirmation_waiting", kwargs={"issuer_id": Issuer.objects.get(email=email).issuer_id}, ) WebDriverWait(self.selenium, 10).until(url_matches(f"^.+{path}$")) send_mail_mock.assert_called_with( from_email=ANY, recipient_list=[email], subject=ANY, message=ANY, html_message=ANY, ) # Test resend mail send_mail_mock.reset_mock() send_mail_mock.assert_not_called() self.selenium.find_element( By.XPATH, """//button[normalize-space() = "Renvoyer l'email de confirmation"]""", ).click() send_mail_mock.assert_called_with( from_email=ANY, recipient_list=[email], subject=ANY, message=ANY, html_message=ANY, ) # Confirm email issuer = Issuer.objects.get(email=email) email_confirmation = issuer.email_confirmations.first() self.open_live_url( reverse( "habilitation_issuer_email_confirmation_confirm", kwargs={ "issuer_id": issuer.issuer_id, "key": email_confirmation.key }, )) self.selenium.find_element(By.CSS_SELECTOR, '[type="submit"]').click() path = reverse( "habilitation_new_organisation", kwargs={"issuer_id": issuer.issuer_id}, ) WebDriverWait(self.selenium, 10).until(url_matches(f"^.+{path}$"))