Fixhancement: refactor email attachment logic (#11336)

This commit is contained in:
shamoon
2025-11-14 09:28:46 -08:00
committed by GitHub
parent 0cbab1ae80
commit b12f1e757c
4 changed files with 143 additions and 51 deletions

View File

@@ -8,8 +8,10 @@ from typing import TYPE_CHECKING
from unittest import mock
import pytest
from django.conf import settings
from django.contrib.auth.models import Group
from django.contrib.auth.models import User
from django.core import mail
from django.test import override_settings
from django.utils import timezone
from guardian.shortcuts import assign_perm
@@ -21,6 +23,8 @@ from pytest_httpx import HTTPXMock
from rest_framework.test import APIClient
from rest_framework.test import APITestCase
from documents.file_handling import create_source_path_directory
from documents.file_handling import generate_unique_filename
from documents.signals.handlers import run_workflows
from documents.signals.handlers import send_webhook
@@ -2989,6 +2993,70 @@ class TestWorkflows(
mock_email_send.assert_called_once()
@override_settings(
PAPERLESS_EMAIL_HOST="localhost",
EMAIL_ENABLED=True,
PAPERLESS_URL="http://localhost:8000",
EMAIL_BACKEND="django.core.mail.backends.locmem.EmailBackend",
)
def test_workflow_email_attachment_uses_storage_filename(self):
"""
GIVEN:
- Document updated workflow with include document action
- Document stored with formatted storage-path filename
WHEN:
- Workflow sends an email
THEN:
- Attachment filename matches the stored filename
"""
trigger = WorkflowTrigger.objects.create(
type=WorkflowTrigger.WorkflowTriggerType.DOCUMENT_UPDATED,
)
email_action = WorkflowActionEmail.objects.create(
subject="Test Notification: {doc_title}",
body="Test message: {doc_url}",
to="me@example.com",
include_document=True,
)
action = WorkflowAction.objects.create(
type=WorkflowAction.WorkflowActionType.EMAIL,
email=email_action,
)
workflow = Workflow.objects.create(
name="Workflow attachment filename",
order=0,
)
workflow.triggers.add(trigger)
workflow.actions.add(action)
workflow.save()
storage_path = StoragePath.objects.create(
name="Fancy Path",
path="formatted/{{ document.pk }}/{{ title }}",
)
doc = Document.objects.create(
title="workflow doc",
correspondent=self.c,
checksum="workflow-email-attachment",
mime_type="application/pdf",
storage_path=storage_path,
original_filename="workflow-orig.pdf",
)
# eg what happens in update_filename_and_move_files
generated = generate_unique_filename(doc)
destination = (settings.ORIGINALS_DIR / generated).resolve()
create_source_path_directory(destination)
shutil.copy(self.SAMPLE_DIR / "simple.pdf", destination)
Document.objects.filter(pk=doc.pk).update(filename=generated.as_posix())
run_workflows(WorkflowTrigger.WorkflowTriggerType.DOCUMENT_UPDATED, doc)
self.assertEqual(len(mail.outbox), 1)
attachment_names = [att[0] for att in mail.outbox[0].attachments]
self.assertEqual(attachment_names, [Path(generated).name])
@override_settings(
EMAIL_ENABLED=False,
)