Fix: better catch email workflow placeholder parsing errors (#14129)

This commit is contained in:
shamoon
2026-09-15 16:44:12 -07:00
committed by GitHub
parent f919c981e1
commit e8b7a98d3e
2 changed files with 95 additions and 35 deletions
+60
View File
@@ -3769,6 +3769,66 @@ class TestWorkflows(
expected_str = "Error occurred sending email"
self.assertIn(expected_str, cm.output[0])
@override_settings(
EMAIL_ENABLED=True,
PAPERLESS_URL="http://localhost:8000",
)
@mock.patch("django.core.mail.message.EmailMessage.send")
def test_workflow_email_action_template_error(self, mock_email_send) -> None:
"""
GIVEN:
- Document added workflow with an email action whose body uses an
undefined template variable, followed by an assignment action
WHEN:
- Document consumption finishes
THEN:
- Error is logged, consumption is not aborted
- No email is sent
- Subsequent actions still run
"""
trigger = WorkflowTrigger.objects.create(
type=WorkflowTrigger.WorkflowTriggerType.DOCUMENT_ADDED,
)
email_action = WorkflowActionEmail.objects.create(
subject="Test Notification: {{ doc_title }}",
body="Document Title: {{ title }}",
to="me@example.com",
)
action = WorkflowAction.objects.create(
type=WorkflowAction.WorkflowActionType.EMAIL,
email=email_action,
order=0,
)
assignment_action = WorkflowAction.objects.create(
type=WorkflowAction.WorkflowActionType.ASSIGNMENT,
assign_correspondent=self.c2,
order=1,
)
w = Workflow.objects.create(
name="Workflow 1",
order=0,
)
w.triggers.add(trigger)
w.actions.add(action, assignment_action)
w.save()
doc = Document.objects.create(
title="sample test",
correspondent=self.c,
original_filename="sample.pdf",
)
with self.assertLogs("paperless.workflows", level="ERROR") as cm:
document_consumption_finished.send(
sender=self.__class__,
document=doc,
)
self.assertIn("'title' is undefined", cm.output[0])
mock_email_send.assert_not_called()
doc.refresh_from_db()
self.assertEqual(doc.correspondent, self.c2)
@override_settings(
PAPERLESS_EMAIL_HOST="localhost",
EMAIL_ENABLED=True,
+35 -35
View File
@@ -102,42 +102,42 @@ def execute_email_action(
)
return
subject = (
parse_w_workflow_placeholders(
action.email.subject,
context["correspondent"],
context["document_type"],
context["owner_username"],
context["added"],
context["filename"],
context["current_filename"],
context["created"],
context["title"],
context["doc_url"],
context["id"],
)
if action.email.subject
else ""
)
body = (
parse_w_workflow_placeholders(
action.email.body,
context["correspondent"],
context["document_type"],
context["owner_username"],
context["added"],
context["filename"],
context["current_filename"],
context["created"],
context["title"],
context["doc_url"],
context["id"],
)
if action.email.body
else ""
)
try:
subject = (
parse_w_workflow_placeholders(
action.email.subject,
context["correspondent"],
context["document_type"],
context["owner_username"],
context["added"],
context["filename"],
context["current_filename"],
context["created"],
context["title"],
context["doc_url"],
context["id"],
)
if action.email.subject
else ""
)
body = (
parse_w_workflow_placeholders(
action.email.body,
context["correspondent"],
context["document_type"],
context["owner_username"],
context["added"],
context["filename"],
context["current_filename"],
context["created"],
context["title"],
context["doc_url"],
context["id"],
)
if action.email.body
else ""
)
attachments: list[EmailAttachment] = []
if action.email.include_document:
attachment: EmailAttachment | None = None