Skip to content

Commit

Permalink
🎨 [#4650] Clean up email tests
Browse files Browse the repository at this point in the history
Simplified tests by removing indirection and only set up the test data
that is actually required by the test. This also uses some more
ergonomic factory utilities rather than hand-wiring everything.

Now new tests should have better examples to follow.
  • Loading branch information
sergei-maertens committed Dec 30, 2024
1 parent c93f7cc commit b234978
Showing 1 changed file with 46 additions and 59 deletions.
105 changes: 46 additions & 59 deletions src/openforms/registrations/contrib/email/tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@
EmailContentTypeChoices,
EmailEventChoices,
)
from openforms.forms.tests.factories import (
FormDefinitionFactory,
FormFactory,
FormStepFactory,
)
from openforms.payments.constants import PaymentStatus
from openforms.payments.tests.factories import SubmissionPaymentFactory
from openforms.submissions.attachments import attach_uploads_to_submission_step
Expand All @@ -32,7 +27,6 @@
from openforms.submissions.tests.factories import (
SubmissionFactory,
SubmissionFileAttachmentFactory,
SubmissionReportFactory,
SubmissionStepFactory,
SubmissionValueVariableFactory,
TemporaryFileUploadFactory,
Expand Down Expand Up @@ -83,35 +77,6 @@ def _get_sent_email(index: int = 0) -> tuple[mail.EmailMultiAlternatives, str, s
LANGUAGE_CODE="nl",
)
class EmailBackendTests(HTMLAssertMixin, TestCase):
@classmethod
def setUpTestData(cls):
super().setUpTestData()

fd = FormDefinitionFactory.create(
configuration={
"components": [
{
"key": "someField",
"label": "Some Field",
"type": "textfield",
},
{
"key": "someList",
"label": "Some list",
"type": "textfield",
"multiple": True,
},
],
}
)
form = FormFactory.create(
name="MyName",
internal_name="MyInternalName",
registration_backend="email",
)
cls.fs = FormStepFactory.create(form=form, form_definition=fd)
cls.form = form
cls.fd = fd

def setUp(self):
super().setUp()
Expand Down Expand Up @@ -533,7 +498,9 @@ def test_register_and_update_paid_product(self):
self.assertIn("<table", message_html)
self.assertNotIn("<table", message_text)

detail_line = f"Betaling ontvangen voor {self.form.name} (ingezonden op 12:00:00 01-01-2021)"
detail_line = (
f"Betaling ontvangen voor MyName (ingezonden op 12:00:00 01-01-2021)"
)
self.assertIn(detail_line, message_html)
self.assertIn(detail_line, message_text)

Expand Down Expand Up @@ -665,16 +632,29 @@ def test_submission_with_email_backend_export_csv_xlsx(self):
"attachment_formats": [AttachmentFormat.csv, AttachmentFormat.xlsx],
"attach_files_to_email": None,
}

data = {"someField": "value0", "someList": ["value1", "value2"]}

submission = SubmissionFactory.create(form=self.form)
submission_step = SubmissionStepFactory.create(
submission=submission, form_step=self.fs, data=data
submission = SubmissionFactory.from_components(
[
{
"key": "someField",
"label": "Some Field",
"type": "textfield",
},
{
"key": "someList",
"label": "Some list",
"type": "textfield",
"multiple": True,
},
],
submitted_data={"someField": "value0", "someList": ["value1", "value2"]},
completed=True,
completed_on=timezone.make_aware(datetime(2021, 1, 1, 12, 0, 0)),
form__name="MyName",
form__internal_name="MyInternalName",
)
submission_step = (
submission.submissionstep_set.get() # pyright: ignore[reportAttributeAccessIssue]
)
submission.completed_on = timezone.make_aware(datetime(2021, 1, 1, 12, 0, 0))
submission.save()

SubmissionFileAttachmentFactory.create(
submission_step=submission_step,
file_name="my-foo.bin",
Expand Down Expand Up @@ -750,18 +730,26 @@ def test_submission_with_email_backend_export_pdf(self):
"attachment_formats": [AttachmentFormat.pdf],
"attach_files_to_email": None,
}

data = {"foo": "bar", "some_list": ["value1", "value2"]}

submission = SubmissionFactory.create(form=self.form)
submission_step = SubmissionStepFactory.create(
submission=submission, form_step=self.fs, data=data
submission = SubmissionFactory.from_components(
[
{
"key": "someField",
"label": "Some Field",
"type": "textfield",
},
{
"key": "someList",
"label": "Some list",
"type": "textfield",
"multiple": True,
},
],
submitted_data={"someField": "value0", "someList": ["value1", "value2"]},
completed=True,
)
submission_step = (
submission.submissionstep_set.get() # pyright: ignore[reportAttributeAccessIssue]
)
submission.completed_on = timezone.make_aware(datetime(2021, 1, 1, 12, 0, 0))
submission.save()

report = SubmissionReportFactory.create(submission=submission)

SubmissionFileAttachmentFactory.create(
submission_step=submission_step,
file_name="my-foo.bin",
Expand All @@ -785,9 +773,8 @@ def test_submission_with_email_backend_export_pdf(self):
self.assertEqual(len(message.attachments), 1)

pdf_export = message.attachments[0]

self.assertEqual(pdf_export[0], f"{report.title}.pdf")
self.assertEqual(pdf_export[1], report.content.read())
self.assertEqual(pdf_export[0], f"{submission.report.title}.pdf")
self.assertEqual(pdf_export[1], submission.report.content.read())
self.assertEqual(pdf_export[2], "application/pdf")

def test_regression_nested_components_columns(self):
Expand Down

0 comments on commit b234978

Please sign in to comment.