From c93f7cce74efd7de899cceb783d1ea847a65f848 Mon Sep 17 00:00:00 2001 From: Sergei Maertens Date: Mon, 30 Dec 2024 15:48:15 +0100 Subject: [PATCH] :label: [#4650] Enable type checking on entire registrations email package This should encourage people to pass the proper options and take into account optional/required configuration parameters. --- pyright.pyproject.toml | 3 +- .../registrations/contrib/email/models.py | 2 +- .../contrib/email/tests/test_backend.py | 205 +++++++++--------- .../registrations/contrib/email/views.py | 2 +- 4 files changed, 107 insertions(+), 105 deletions(-) diff --git a/pyright.pyproject.toml b/pyright.pyproject.toml index e69115a161..79555fabcf 100644 --- a/pyright.pyproject.toml +++ b/pyright.pyproject.toml @@ -36,8 +36,7 @@ include = [ "src/openforms/emails/templatetags/cosign_information.py", # Registrations "src/openforms/registrations/tasks.py", - "src/openforms/registrations/contrib/email/config.py", - "src/openforms/registrations/contrib/email/plugin.py", + "src/openforms/registrations/contrib/email/", "src/openforms/registrations/contrib/stuf_zds/options.py", "src/openforms/registrations/contrib/stuf_zds/plugin.py", "src/openforms/registrations/contrib/stuf_zds/typing.py", diff --git a/src/openforms/registrations/contrib/email/models.py b/src/openforms/registrations/contrib/email/models.py index 4097084594..0609972637 100644 --- a/src/openforms/registrations/contrib/email/models.py +++ b/src/openforms/registrations/contrib/email/models.py @@ -95,7 +95,7 @@ class EmailConfig(SingletonModel): ], ) - class Meta: + class Meta: # pyright: ignore[reportIncompatibleVariableOverride] verbose_name = _("Email registration configuration") def __str__(self): diff --git a/src/openforms/registrations/contrib/email/tests/test_backend.py b/src/openforms/registrations/contrib/email/tests/test_backend.py index a49196020c..15c3841adb 100644 --- a/src/openforms/registrations/contrib/email/tests/test_backend.py +++ b/src/openforms/registrations/contrib/email/tests/test_backend.py @@ -29,7 +29,6 @@ from openforms.submissions.attachments import attach_uploads_to_submission_step from openforms.submissions.exports import create_submission_export from openforms.submissions.models import Submission -from openforms.submissions.public_references import set_submission_reference from openforms.submissions.tests.factories import ( SubmissionFactory, SubmissionFileAttachmentFactory, @@ -69,6 +68,15 @@ """ +def _get_sent_email(index: int = 0) -> tuple[mail.EmailMultiAlternatives, str, str]: + message = mail.outbox[index] + assert isinstance(message, mail.EmailMultiAlternatives) + text_body = message.body + html_body = message.alternatives[0][0] + assert isinstance(html_body, str) + return message, str(text_body), html_body + + @override_settings( DEFAULT_FROM_EMAIL="info@open-forms.nl", BASE_URL="https://example.com", @@ -158,9 +166,12 @@ def test_submission_with_email_backend(self): }, language_code="nl", ) + step = ( + submission.submissionstep_set.get() # pyright: ignore[reportAttributeAccessIssue] + ) submission_file_attachment_1 = SubmissionFileAttachmentFactory.create( form_key="file1", - submission_step=submission.submissionstep_set.get(), + submission_step=step, file_name="my-foo.bin", content_type="application/foo", _component_configuration_path="components.2", @@ -168,7 +179,7 @@ def test_submission_with_email_backend(self): ) submission_file_attachment_2 = SubmissionFileAttachmentFactory.create( form_key="file2", - submission_step=submission.submissionstep_set.get(), + submission_step=step, file_name="my-bar.txt", content_type="text/bar", _component_configuration_path="components.3", @@ -193,7 +204,7 @@ def test_submission_with_email_backend(self): # Verify that email was sent self.assertEqual(len(mail.outbox), 1) - message = mail.outbox[0] + message, message_text, message_html = _get_sent_email() self.assertEqual( message.subject, f"Subject: MyName - submission {submission.public_registration_reference}", @@ -202,8 +213,6 @@ def test_submission_with_email_backend(self): self.assertEqual(message.to, ["foo@bar.nl", "bar@foo.nl"]) # Check that the template is used - message_text = message.body - message_html = message.alternatives[0][0] self.assertHTMLValid(message_html) self.assertIn("
  • Backend
  • Frontend
  • ", message_html) @patch("openforms.registrations.contrib.email.plugin.EmailConfig.get_solo") @@ -1006,15 +1002,18 @@ def test_with_global_config_attach_files(self, mock_get_solo): form__internal_name="MyInternalName", form__registration_backend="email", ) + step = ( + submission.submissionstep_set.get() # pyright: ignore[reportAttributeAccessIssue] + ) SubmissionFileAttachmentFactory.create( form_key="file1", - submission_step=submission.submissionstep_set.get(), + submission_step=step, file_name="my-foo.bin", content_type="application/foo", ) SubmissionFileAttachmentFactory.create( form_key="file2", - submission_step=submission.submissionstep_set.get(), + submission_step=step, file_name="my-bar.txt", content_type="text/bar", ) @@ -1071,7 +1070,10 @@ def test_user_defined_variables_included(self): value="test2", ) - email_form_options = dict(to_emails=["foo@bar.nl", "bar@foo.nl"]) + email_form_options: Options = { + "to_emails": ["foo@bar.nl", "bar@foo.nl"], + "attach_files_to_email": None, + } plugin = EmailRegistration("email") plugin.register_submission(submission, email_form_options) @@ -1079,9 +1081,7 @@ def test_user_defined_variables_included(self): # Verify that email was sent self.assertEqual(len(mail.outbox), 1) - message = mail.outbox[0] - message_text = message.body - + _, message_text, _ = _get_sent_email() self.assertIn("User defined var 1: test1", message_text) self.assertIn("User defined var 2: test2", message_text) @@ -1104,11 +1104,11 @@ def test_mime_body_parts_have_content_langauge(self): ): plugin.register_submission(submission, {"to_emails": ["foo@example.com"]}) - message = mail.outbox[0] + message, message_text, message_html = _get_sent_email() self.assertEqual(message.extra_headers["Content-Language"], "en") - self.assertIn("Engels", message.body) + self.assertIn("Engels", message_text) html_message = message.alternatives[0][0] - self.assertIn("Engels", html_message) + self.assertIn("Engels", message_html) @tag("gh-3144") def test_file_attachments_in_registration_email(self): @@ -1204,8 +1204,10 @@ def test_file_attachments_in_registration_email(self): ) attach_uploads_to_submission_step(submission_step) - subject, body_html, body_text = EmailRegistration.render_registration_email( - submission, is_payment_update=False + subject, body_html, body_text = ( + EmailRegistration.render_registration_email( # pyright: ignore[reportAttributeAccessIssue] + submission, is_payment_update=False + ) ) with self.subTest("Normal attachment"): @@ -1222,9 +1224,10 @@ def test_file_attachments_in_registration_email(self): def test_extra_headers(self): submission = SubmissionFactory.create() - email_form_options = dict( - to_emails=["foo@bar.nl", "bar@foo.nl"], - ) + email_form_options: Options = { + "to_emails": ["foo@bar.nl", "bar@foo.nl"], + "attach_files_to_email": None, + } plugin = EmailRegistration("email") with patch( diff --git a/src/openforms/registrations/contrib/email/views.py b/src/openforms/registrations/contrib/email/views.py index 1cc6b119a6..6639dcfd37 100644 --- a/src/openforms/registrations/contrib/email/views.py +++ b/src/openforms/registrations/contrib/email/views.py @@ -16,7 +16,7 @@ def get_email_content(self): subject, html_content, text_content, - ) = EmailRegistration.render_registration_email( + ) = EmailRegistration.render_registration_email( # pyright: ignore[reportAttributeAccessIssue] self.object, is_payment_update=False ) content = html_content if mode == "html" else text_content