Skip to content

Commit

Permalink
Add test for send mail action
Browse files Browse the repository at this point in the history
  • Loading branch information
fsbraun committed Jan 8, 2025
1 parent df72563 commit 816f3c1
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
12 changes: 7 additions & 5 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):
from django.core.mail import mail_admins, send_mail

recipients = (self.get_parameter(form, "sendemail_recipients") or "").split()
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,15 +206,16 @@ 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,
self.from_mail,
Expand Down
53 changes: 53 additions & 0 deletions tests/test_actions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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 tests.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:
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]'])

0 comments on commit 816f3c1

Please sign in to comment.