Skip to content

Commit

Permalink
✅ [#4930] Add test for export view
Browse files Browse the repository at this point in the history
  • Loading branch information
sergei-maertens committed Dec 20, 2024
1 parent 33b6e2e commit 10f5300
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<h1>{% trans 'Export submission statistics' %}</h1>

<div id="content-main">
<form action="." method="post">
<form action="." method="post" id="export-statistics">
{% csrf_token %}

<fieldset class="module aligned">
Expand Down
40 changes: 40 additions & 0 deletions src/openforms/forms/tests/admin/test_form_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@
from django.utils.translation import gettext as _

from django_webtest import WebTest
from freezegun import freeze_time
from maykin_2fa.test import disable_admin_mfa

from openforms.accounts.tests.factories import UserFactory
from openforms.logging import logevent
from openforms.registrations.contrib.demo.plugin import DemoRegistration
from openforms.submissions.tests.factories import SubmissionFactory

from ...forms import ExportStatisticsForm


@disable_admin_mfa()
Expand Down Expand Up @@ -57,3 +63,37 @@ def test_navigate_from_changelist(self):
export_page = changelist.click(_("Export submission statistics"))

self.assertEqual(export_page.request.path, self.admin_url)

def test_successful_export_downloads_file(self):
user = UserFactory.create(
is_staff=True,
user_permissions=["forms.view_formstatistics"],
)
# create some log records for submissions
with freeze_time("2024-12-20T16:44:00+01:00"):
sub1, sub2, sub3 = SubmissionFactory.create_batch(
3, registration_success=True
)
plugin = DemoRegistration("demo")
logevent.registration_success(sub1, plugin=plugin)
logevent.registration_success(sub2, plugin=plugin)
logevent.registration_success(sub3, plugin=plugin)

export_page = self.app.get(self.admin_url, user=user)
form = export_page.forms["export-statistics"]

# fill out the form and submit it
form["start_date"] = "2024-12-01"
form["end_date"] = "2025-01-01"
response = form.submit()

self.assertEqual(response.status_code, 200)
self.assertEqual(
response.content_type,
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
)
self.assertGreater(int(response["Content-Length"]), 0)
self.assertEqual(
response["Content-Disposition"],
'attachment; filename="submissions_2024-12-01_2025-01-01.xlsx"',
)

0 comments on commit 10f5300

Please sign in to comment.