-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ [#4650] Using variable value as email recipient
The variable chosen in the email registration (`to_email_from_variable`) will now actually be used for the mailing. `to_emails` is used as fallback, in case that the variable doesn't return a valid email address. The variable will also be used for the payment status update mailing. If `payment_emails` is defined, these will be used as recipients. Otherwise it will use `to_email_from_variable`, and as a last resort the `to_emails`
- Loading branch information
1 parent
897cb65
commit 82c9584
Showing
2 changed files
with
222 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -249,6 +249,124 @@ def test_submission_with_email_backend(self): | |
self.assertIn(f"{expected_download_url_1} (my-foo.bin)", message_text) | ||
self.assertIn(f"{expected_download_url_2} (my-bar.txt)", message_text) | ||
|
||
def test_submission_with_email_backend_using_to_email_from_variable(self): | ||
submission = SubmissionFactory.from_components( | ||
completed=True, | ||
components_list=[ | ||
{"key": "foo", "type": "textfield", "label": "foo"}, | ||
], | ||
submitted_data={"foo": "bar"}, | ||
form__registration_backend="email", | ||
) | ||
SubmissionValueVariableFactory.create( | ||
form_variable__source=FormVariableSources.user_defined, | ||
form_variable__name="User defined var 1", | ||
submission=submission, | ||
key="email_recipient_variable", | ||
value="[email protected]", | ||
) | ||
email_form_options = dict( | ||
to_email_from_variable="email_recipient_variable", | ||
) | ||
email_submission = EmailRegistration("email") | ||
|
||
set_submission_reference(submission) | ||
|
||
with patch("openforms.registrations.contrib.email.utils.EmailConfig.get_solo"): | ||
email_submission.register_submission(submission, email_form_options) | ||
|
||
# Verify that email was sent | ||
self.assertEqual(len(mail.outbox), 1) | ||
|
||
message = mail.outbox[0] | ||
self.assertEqual(message.to, ["[email protected]"]) | ||
|
||
def test_submission_with_email_backend_unknown_to_email_from_variable(self): | ||
submission = SubmissionFactory.from_components( | ||
completed=True, | ||
components_list=[ | ||
{"key": "foo", "type": "textfield", "label": "foo"}, | ||
], | ||
submitted_data={"foo": "bar"}, | ||
form__registration_backend="email", | ||
) | ||
email_form_options = dict( | ||
to_email_from_variable="email_recipient_variable", | ||
) | ||
email_submission = EmailRegistration("email") | ||
|
||
set_submission_reference(submission) | ||
|
||
with patch("openforms.registrations.contrib.email.utils.EmailConfig.get_solo"): | ||
email_submission.register_submission(submission, email_form_options) | ||
|
||
# Verify that email wasn't sent | ||
self.assertEqual(len(mail.outbox), 0) | ||
|
||
def test_submission_with_email_backend_invalid_to_email_from_variable(self): | ||
submission = SubmissionFactory.from_components( | ||
completed=True, | ||
components_list=[ | ||
{"key": "foo", "type": "textfield", "label": "foo"}, | ||
], | ||
submitted_data={"foo": "bar"}, | ||
form__registration_backend="email", | ||
) | ||
SubmissionValueVariableFactory.create( | ||
form_variable__source=FormVariableSources.user_defined, | ||
form_variable__name="User defined var 1", | ||
submission=submission, | ||
key="email_recipient_variable", | ||
value="foo.com", | ||
) | ||
email_form_options = dict( | ||
to_email_from_variable="email_recipient_variable", | ||
) | ||
email_submission = EmailRegistration("email") | ||
|
||
set_submission_reference(submission) | ||
|
||
with patch("openforms.registrations.contrib.email.utils.EmailConfig.get_solo"): | ||
email_submission.register_submission(submission, email_form_options) | ||
|
||
# Verify that email wasn't sent | ||
self.assertEqual(len(mail.outbox), 0) | ||
|
||
def test_submission_with_email_backend_invalid_to_email_from_variable_with_fallback( | ||
self, | ||
): | ||
submission = SubmissionFactory.from_components( | ||
completed=True, | ||
components_list=[ | ||
{"key": "foo", "type": "textfield", "label": "foo"}, | ||
], | ||
submitted_data={"foo": "bar"}, | ||
form__registration_backend="email", | ||
) | ||
SubmissionValueVariableFactory.create( | ||
form_variable__source=FormVariableSources.user_defined, | ||
form_variable__name="User defined var 1", | ||
submission=submission, | ||
key="email_recipient_variable", | ||
value="foo.com", | ||
) | ||
email_form_options = dict( | ||
to_email_from_variable="email_recipient_variable", | ||
to_emails=["[email protected]"], | ||
) | ||
email_submission = EmailRegistration("email") | ||
|
||
set_submission_reference(submission) | ||
|
||
with patch("openforms.registrations.contrib.email.utils.EmailConfig.get_solo"): | ||
email_submission.register_submission(submission, email_form_options) | ||
|
||
# Verify that email was sent | ||
self.assertEqual(len(mail.outbox), 1) | ||
|
||
message = mail.outbox[0] | ||
self.assertEqual(message.to, ["[email protected]"]) | ||
|
||
def test_submission_with_email_backend_strip_out_urls(self): | ||
config = GlobalConfiguration.get_solo() | ||
config.email_template_netloc_allowlist = [] | ||
|
@@ -477,6 +595,71 @@ def test_register_and_update_paid_product_with_payment_email_recipient(self): | |
# check we used the payment_emails | ||
self.assertEqual(message.to, ["[email protected]", "[email protected]"]) | ||
|
||
def test_register_and_update_paid_product_with_payment_email_recipient_and_variable_email_recipient( | ||
self, | ||
): | ||
submission = SubmissionFactory.from_data( | ||
{"voornaam": "Foo"}, | ||
form__product__price=Decimal("11.35"), | ||
form__payment_backend="demo", | ||
registration_success=True, | ||
public_registration_reference="XYZ", | ||
) | ||
SubmissionValueVariableFactory.create( | ||
form_variable__source=FormVariableSources.user_defined, | ||
form_variable__name="User defined var 1", | ||
submission=submission, | ||
key="email_recipient_variable", | ||
value="[email protected]", | ||
) | ||
|
||
email_form_options = dict( | ||
to_emails=["[email protected]", "[email protected]"], | ||
to_email_from_variable="email_recipient_variable", | ||
# payment_emails would override to_emails and to_email_from_variable | ||
payment_emails=["[email protected]", "[email protected]"], | ||
) | ||
email_submission = EmailRegistration("email") | ||
email_submission.update_payment_status(submission, email_form_options) | ||
|
||
self.assertEqual(len(mail.outbox), 1) | ||
|
||
message = mail.outbox[0] | ||
# check we used the payment_emails | ||
self.assertEqual(message.to, ["[email protected]", "[email protected]"]) | ||
|
||
def test_register_and_update_paid_product_with_variable_email_recipient( | ||
self, | ||
): | ||
submission = SubmissionFactory.from_data( | ||
{"voornaam": "Foo"}, | ||
form__product__price=Decimal("11.35"), | ||
form__payment_backend="demo", | ||
registration_success=True, | ||
public_registration_reference="XYZ", | ||
) | ||
SubmissionValueVariableFactory.create( | ||
form_variable__source=FormVariableSources.user_defined, | ||
form_variable__name="User defined var 1", | ||
submission=submission, | ||
key="email_recipient_variable", | ||
value="[email protected]", | ||
) | ||
|
||
email_form_options = dict( | ||
to_emails=["[email protected]", "[email protected]"], | ||
# to_email_from_variable would override to_emails | ||
to_email_from_variable="email_recipient_variable", | ||
) | ||
email_submission = EmailRegistration("email") | ||
email_submission.update_payment_status(submission, email_form_options) | ||
|
||
self.assertEqual(len(mail.outbox), 1) | ||
|
||
message = mail.outbox[0] | ||
# check we used the payment_emails | ||
self.assertEqual(message.to, ["[email protected]"]) | ||
|
||
@override_settings(DEFAULT_FROM_EMAIL="[email protected]") | ||
def test_submission_with_email_backend_export_csv_xlsx(self): | ||
email_form_options = dict( | ||
|