From 816f3c13f2c4b3d3233f4d6c97396a498914ef57 Mon Sep 17 00:00:00 2001 From: Fabian Braun Date: Wed, 8 Jan 2025 13:15:17 +0100 Subject: [PATCH] Add test for send mail action --- djangocms_form_builder/actions.py | 12 ++++--- tests/test_actions.py | 53 +++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 tests/test_actions.py diff --git a/djangocms_form_builder/actions.py b/djangocms_form_builder/actions.py index 521bbdc..57726be 100644 --- a/djangocms_form_builder/actions.py +++ b/djangocms_form_builder/actions.py @@ -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 @@ -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) @@ -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, diff --git a/tests/test_actions.py b/tests/test_actions.py new file mode 100644 index 0000000..4e71b4a --- /dev/null +++ b/tests/test_actions.py @@ -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": "a@b.c d@e.f", "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], ['a@b.c', 'd@e.f'])