Skip to content

Commit

Permalink
[#3470] Slugify form name in pdf submission reports
Browse files Browse the repository at this point in the history
Backport-of: #3480
  • Loading branch information
vaszig authored and sergei-maertens committed Sep 18, 2023
1 parent 361dc2d commit c3d5ac5
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 8 deletions.
2 changes: 1 addition & 1 deletion requirements/ci.txt
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ humanize==3.14.0
# -c requirements/base.txt
# -r requirements/base.txt
# flower
hypothesis==6.58.0
hypothesis==6.84.3
# via -r requirements/test-tools.in
idna==2.10
# via
Expand Down
2 changes: 1 addition & 1 deletion requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ humanize==3.14.0
# -c requirements/ci.txt
# -r requirements/ci.txt
# flower
hypothesis==6.58.0
hypothesis==6.84.3
# via
# -c requirements/ci.txt
# -r requirements/ci.txt
Expand Down
2 changes: 1 addition & 1 deletion src/openforms/submissions/models/submission_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def generate_submission_report_pdf(self) -> str:
)
self.content = ContentFile(
content=pdf_report,
name=f"{form.name}.pdf", # Takes care of replacing spaces with underscores
name=f"{form.slug}.pdf",
)
self.save()
return html_report
Expand Down
22 changes: 21 additions & 1 deletion src/openforms/submissions/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
from unittest.mock import patch

from django.core.exceptions import ValidationError
from django.test import TestCase, override_settings
from django.test import TestCase, override_settings, tag

from hypothesis import given, settings, strategies as st
from hypothesis.extra.django import TestCase as HypothesisTestCase
from privates.test import temp_private_root
from testfixtures import LogCapture

Expand All @@ -19,6 +21,7 @@
from .factories import (
SubmissionFactory,
SubmissionFileAttachmentFactory,
SubmissionReportFactory,
SubmissionStepFactory,
)

Expand Down Expand Up @@ -617,3 +620,20 @@ def test_clear_execution_state_without_execution_state(self):
self.assertFalse(hasattr(submission, "_execution_state"))

submission.clear_execution_state()


@temp_private_root()
class PDFSubmissionReportTests(HypothesisTestCase):
@tag("gh-3470")
@given(
st.text(
min_size=1,
alphabet=st.characters(blacklist_characters="\x00", codec="utf-8"),
)
)
@settings(deadline=500)
def test_names_do_not_break_pdf_saving_to_disk(self, form_name):
report = SubmissionReportFactory.create(submission__form__name=form_name)
report.generate_submission_report_pdf()

self.assertTrue(report.content.storage.exists(report.content.name))
6 changes: 4 additions & 2 deletions src/openforms/submissions/tests/test_submission_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,17 @@ def test_invalid_token_timestamp(self):

@patch("celery.app.task.Task.request")
def test_report_generation(self, mock_request):
submission = SubmissionFactory.create(completed=True, form__name="Test Form")
submission = SubmissionFactory.create(
completed=True, form__name="Test Form", form__slug="test-form"
)
mock_request.id = "some-id"

generate_submission_report(submission.id)

report = submission.report
self.assertEqual("some-id", report.task_id)
# report.content.name contains the path too
self.assertTrue(report.content.name.endswith("Test_Form.pdf"))
self.assertTrue(report.content.name.endswith("test-form.pdf"))

def test_report_is_generated_in_same_language_as_submission(self):
# fixture_data
Expand Down
6 changes: 4 additions & 2 deletions src/openforms/submissions/tests/test_tasks_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
@temp_private_root()
class SubmissionReportGenerationTests(TestCase):
def test_submission_report_metadata(self):
submission = SubmissionFactory.create(completed=True, form__name="Test Form")
submission = SubmissionFactory.create(
completed=True, form__name="Test Form", form__slug="test-form"
)

generate_submission_report.request.id = "some-id"
generate_submission_report.run(submission.id)
Expand All @@ -26,7 +28,7 @@ def test_submission_report_metadata(self):
_("%(title)s: Submission report") % {"title": "Test Form"}, report.title
)
self.assertEqual(submission, report.submission)
self.assertTrue(report.content.name.endswith("Test_Form.pdf"))
self.assertTrue(report.content.name.endswith("test-form.pdf"))
self.assertEqual("some-id", report.task_id)

def test_multiple_value_report_rendering_issue_990(self):
Expand Down

0 comments on commit c3d5ac5

Please sign in to comment.