Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Send mail action failed #21

Merged
merged 3 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions djangocms_form_builder/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from django import forms
from django.apps import apps
from django.core.exceptions import ImproperlyConfigured
from django.core.mail import mail_admins, send_mail
from django.core.validators import EmailValidator
from django.template import TemplateDoesNotExist
from django.template.loader import render_to_string
Expand Down Expand Up @@ -186,14 +185,16 @@ class Meta:
)

def execute(self, form, request):
recipients = (self.get_parameter(form, "sendemail_recipients") or [])
from django.core.mail import mail_admins, send_mail

recipients = (self.get_parameter(form, "sendemail_recipients") or "").split()
fsbraun marked this conversation as resolved.
Show resolved Hide resolved
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Using whitespace splitting for email addresses is risky. Consider using a more robust delimiter like commas.

Email addresses might contain spaces (e.g., in quoted strings). This could lead to invalid recipient lists.

template_set = self.get_parameter(form, "sendemail_template") or "default"
context = dict(
cleaned_data=form.cleaned_data,
form_name=getattr(form.Meta, "verbose_name", ""),
user=request.user,
user_agent=request.headers["User-Agent"],
referer=request.headers["Referer"],
user_agent=request.headers["User-Agent"] if "User-Agent" in request.headers else "",
referer=request.headers["Referer"] if "Referer" in request.headers else "",
)

html_message = render_to_string(f"djangocms_form_builder/mails/{template_set}/mail_html.html", context)
Expand All @@ -205,19 +206,20 @@ def execute(self, form, request):
subject = render_to_string(f"djangocms_form_builder/mails/{template_set}/subject.txt", context)
except TemplateDoesNotExist:
subject = self.subject % dict(form_name=context["form_name"])

if not recipients:
mail_admins(
return mail_admins(
subject,
message,
fail_silently=True,
html_message=html_message,
)
else:
send_mail(
return send_mail(
subject,
message,
recipients,
self.from_mail,
recipients,
fail_silently=True,
html_message=html_message,
)
Expand Down
69 changes: 69 additions & 0 deletions tests/test_actions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from unittest.mock import patch

from cms.api import add_plugin
from cms.test_utils.testcases import CMSTestCase

from djangocms_form_builder.actions import get_registered_actions

from .fixtures import TestFixture


class ActionTestCase(TestFixture, CMSTestCase):
def setUp(self):
super().setUp()
self.actions = get_registered_actions()
self.save_action = [key for key, value in self.actions if value == "Save form submission"][0]
self.send_mail_action = [key for key, value in self.actions if value == "Send email"][0]

def test_send_mail_action(self):
plugin_instance = add_plugin(
placeholder=self.placeholder,
plugin_type="FormPlugin",
language=self.language,
form_name="test_form",
)
plugin_instance.action_parameters = {"sendemail_recipients": "[email protected] [email protected]", "sendemail_template": "default"}
plugin_instance.form_actions = f"[\"{self.send_mail_action}\"]"
plugin_instance.save()

child_plugin = add_plugin(
placeholder=self.placeholder,
plugin_type="CharFieldPlugin",
language=self.language,
target=plugin_instance,
config={"field_name": "field1"}
)
child_plugin.save()
plugin_instance.child_plugin_instances = [child_plugin]
child_plugin.child_plugin_instances = []

plugin = plugin_instance.get_plugin_class_instance()
plugin.instance = plugin_instance

# Simulate form submission
with patch("django.core.mail.send_mail") as mock_send_mail:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (code-quality): Extract duplicate code into method (extract-duplicate-method)

form = plugin.get_form_class()({}, request=self.get_request("/"))
form.cleaned_data = {"field1": "value1", "field2": "value2"}
form.save()

# Validate send_mail call
mock_send_mail.assert_called_once()
args, kwargs = mock_send_mail.call_args
self.assertEqual(args[0], 'Test form form submission')
self.assertIn('Form submission', args[1])
self.assertEqual(args[3], ['[email protected]', '[email protected]'])

# Test with no recipients
plugin_instance.action_parameters = {"sendemail_recipients": "", "sendemail_template": "default"}
plugin_instance.save()

with patch("django.core.mail.mail_admins") as mock_mail_admins:
form = plugin.get_form_class()({}, request=self.get_request("/"))
form.cleaned_data = {"field1": "value1", "field2": "value2"}
form.save()

# Validate mail_admins call
mock_mail_admins.assert_called_once()
args, kwargs = mock_mail_admins.call_args
self.assertEqual(args[0], 'Test form form submission')
self.assertIn('Form submission', args[1])
Loading