Files
pretix_original/src/tests/base/test_export.py
T
Kian CrossandRaphael Michel 54eadaffcc Improve admin-facing email templates (#6216)
* Improve subject lines for admin-facing emails

A few of the current subjects are ambiguous about the expected
action, and some omit context that would help in an inbox preview
(which event, which address). The rewrites bring them closer to
common conventions in modern transactional email (verb-led,
recipient-addressed, with recipient-meaningful variables). Two
themes:

- Action-required emails lead with the action verb. "Reset your
  password", "Confirm event cancellation and bulk refund", and
  "Confirm <address> as a sender address" tell the recipient up
  front what's expected, where "Password recovery", "Bulk-refund
  confirmation" and "Sender address verification" did not.

- Surface the relevant variable when the email is about something
  specific. "Data shredding completed for <event>" is more useful
  than the generic version when an admin manages several events.
  "You've been invited to join <organizer>" names the inviting
  organizer. "Confirm <address> as a sender address" names the
  address.

The remaining rewrites are lighter rewordings. "New sign-in to
your account" replaces "Login from new source detected" because
"source" is jargon a non-technical recipient wouldn't recognise.
"Changes to your account" replaces "Account information changed"
because the possessive frames the email as being about the
recipient's own account.

Also fixes a hardcoded "pretix" in the confirmation-code subject.

* Standardise admin email sign-offs as "Thanks, The <instance> Team"

The current sign-offs ("Best regards, Your <instance> team") have
a formal tone. A review of the last ~20 transactional emails in
my inbox showed most senders use something friendlier:

- Thanks: Deliveroo, Starling Bank, GitHub, Cloudflare
- Thank you: AWS
- Sincerely: Google Workspace

A small minority (e.g., Sentry) had no sign-off at all. "Thanks"
was the most common, and among that group "The <instance> Team"
was the consistent phrasing rather than "Your <instance> team".

Two templates (cancel_confirm, export_failed) didn't have a
sign-off; they now get one for consistency. Notification emails
are deliberately excluded: they're system alerts rather than
direct correspondence.

* Add anti-phishing notice to admin emails containing confirmation codes

Three admin emails send the recipient a confirmation code to
enter back into a form: confirmation_code, email_setup, and
cancel_confirm. Only confirmation_code had an anti-phishing
warning, and its wording was awkward ("Please do never give this
code to another person. Our support team will never ask for this
code.").

This commit standardises the warning across all three:

> Don't share this code with anyone. The <instance> team will
> never ask you for it.

* Add structured details to login-notice email

The single-sentence body ("The login was performed using <agent>
on <os> from <country>.") is replaced with a labelled bullet list:
Time, Browser, Operating system, Device, Country.

Time and Device are new fields. Device is omitted when ua-parser
can't identify the device, Country when GeoIP isn't available,
so the user only sees fields with real values.

* Restructure notification.txt for clearer layout

- Attributes: bullet list instead of paragraph-per-attribute.

- Actions: label gets a colon, URL on its own paragraph (was
  4-space-indented code block).

- Footer: separated by --- and bulleted (manage / disable
  links). "Click here X" phrasing dropped (incidentally moots
  a missing-"to" typo).

- Minor whitespace fix: detail-block endif now matches the
  placement of the rest of the template.

notification.html's footer text is also updated, only to match
the new .txt wording (link labels and intro line). No
structural changes to the HTML template.

* Improve confirmation-code email reason strings

- Drop the redundant "to confirm" opener.

- Replace hardcoded "your pretix account" in email_verify
  with "{instance}".

* Polish admin email body copy

A small wording and formatting pass on the admin email bodies,
in three loosely-grouped themes:

1. Sentence case for body text (previously lowercase after
   "Hello,"), matching standard English convention.

2. Light restructuring where helpful: bullet lists for sets
   of labelled facts; 4-space-indented code blocks for codes
   the recipient is meant to type back.

3. Phrasing polish. Some sentences tightened or shortened.
   Largely matters of taste, but generally read smoother.

---------

Co-authored-by: Raphael Michel <michel@pretix.eu>
2026-07-06 17:25:52 +02:00

365 lines
14 KiB
Python

#
# This file is part of pretix (Community Edition).
#
# Copyright (C) 2014-2020 Raphael Michel and contributors
# Copyright (C) 2020-today pretix GmbH and contributors
#
# This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General
# Public License as published by the Free Software Foundation in version 3 of the License.
#
# ADDITIONAL TERMS APPLY: Pursuant to Section 7 of the GNU Affero General Public License, additional terms are
# applicable granting you additional permissions and placing additional restrictions on your usage of this software.
# Please refer to the pretix LICENSE file to obtain the full terms applicable to this work. If you did not receive
# this file, see <https://pretix.eu/about/en/license>.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along with this program. If not, see
# <https://www.gnu.org/licenses/>.
#
from datetime import datetime, time, timedelta, timezone
import pytest
from django.core import mail as djmail
from django.utils.timezone import now
from django_scopes import scope
from freezegun import freeze_time
from pretix.base.models import (
Event, Organizer, ScheduledEventExport, ScheduledOrganizerExport, User,
)
from pretix.base.services.export import run_scheduled_exports
@pytest.fixture(scope='function')
def event():
o = Organizer.objects.create(name='Dummy', slug='dummy')
event = Event.objects.create(
organizer=o, name='Dummy', slug='dummy',
date_from=datetime(2023, 1, 19, 2, 30, 0, tzinfo=timezone.utc),
plugins='pretix.plugins.banktransfer'
)
o.settings.timezone = "Europe/Berlin"
with scope(organizer=o):
yield event
@pytest.fixture
def team(event):
return event.organizer.teams.create(all_events=True, all_event_permissions=True)
@pytest.fixture
def user(team):
user = User.objects.create_user('dummy@dummy.dummy', 'dummy')
team.members.add(user)
return user
@pytest.mark.django_db
@freeze_time("2023-01-18 03:00:00+01:00")
def test_event_run_sets_new_time(event, user):
s = ScheduledEventExport(event=event, owner=user)
s.schedule_rrule = "DTSTART:20230118T000000\nRRULE:FREQ=DAILY;INTERVAL=1;WKST=MO"
s.schedule_rrule_time = time(2, 30, 0)
s.schedule_next_run = now() - timedelta(minutes=5)
s.save()
run_scheduled_exports(None)
s.refresh_from_db()
assert s.schedule_next_run == datetime(2023, 1, 19, 2, 30, 0, tzinfo=event.timezone)
@pytest.mark.django_db
@freeze_time("2023-01-18 03:00:00+01:00")
def test_event_not_run_when_failed_5_times(event, user):
s = ScheduledEventExport(event=event, owner=user)
s.schedule_rrule = "DTSTART:20230118T000000\nRRULE:FREQ=DAILY;INTERVAL=1;WKST=MO"
s.schedule_rrule_time = time(2, 30, 0)
s.schedule_next_run = datetime(2023, 1, 18, 2, 30, 0, tzinfo=event.timezone)
s.error_counter = 5
s.save()
run_scheduled_exports(None)
s.refresh_from_db()
assert s.schedule_next_run == datetime(2023, 1, 18, 2, 30, 0, tzinfo=event.timezone)
@pytest.mark.django_db
@freeze_time("2023-01-18 03:00:00+01:00")
def test_event_fail_invalid_config(event, user):
djmail.outbox = []
s = ScheduledEventExport(event=event, owner=user)
s.export_identifier = " invalid "
s.schedule_rrule = "DTSTART:20230118T000000\nRRULE:FREQ=DAILY;INTERVAL=1;WKST=MO"
s.schedule_rrule_time = time(2, 30, 0)
s.schedule_next_run = now() - timedelta(minutes=5)
s.error_counter = 0
s.save()
run_scheduled_exports(None)
s.refresh_from_db()
assert s.schedule_next_run > now()
assert s.error_counter == 1
assert len(djmail.outbox) == 1
assert djmail.outbox[0].subject == "Scheduled export failed"
assert "Reason: Export type not found" in djmail.outbox[0].body
assert djmail.outbox[0].to == [user.email]
@pytest.mark.django_db
@freeze_time("2023-01-18 03:00:00+01:00")
def test_event_fail_user_inactive(event, user):
djmail.outbox = []
s = ScheduledEventExport(event=event, owner=user)
s.export_identifier = "orderlist"
s.schedule_rrule = "DTSTART:20230118T000000\nRRULE:FREQ=DAILY;INTERVAL=1;WKST=MO"
s.schedule_rrule_time = time(2, 30, 0)
s.schedule_next_run = now() - timedelta(minutes=5)
s.error_counter = 0
s.save()
user.is_active = False
user.save()
run_scheduled_exports(None)
s.refresh_from_db()
assert s.schedule_next_run > now()
assert s.error_counter == 1
assert len(djmail.outbox) == 0 # no mails sent to inactive user
@pytest.mark.django_db
@freeze_time("2023-01-18 03:00:00+01:00")
def test_event_fail_user_no_permission(event, user, team):
djmail.outbox = []
s = ScheduledEventExport(event=event, owner=user)
s.export_identifier = "orderlist"
s.schedule_rrule = "DTSTART:20230118T000000\nRRULE:FREQ=DAILY;INTERVAL=1;WKST=MO"
s.schedule_rrule_time = time(2, 30, 0)
s.schedule_next_run = now() - timedelta(minutes=5)
s.error_counter = 0
s.save()
team.all_event_permissions = False
team.limit_event_permissions = {"event.vouchers:read": True}
team.save()
run_scheduled_exports(None)
s.refresh_from_db()
assert s.schedule_next_run > now()
assert s.error_counter == 1
assert len(djmail.outbox) == 1
assert djmail.outbox[0].subject == "Scheduled export failed"
assert "Reason: Export type not found or permission denied." in djmail.outbox[0].body
assert djmail.outbox[0].to == [user.email]
@pytest.mark.django_db(transaction=True)
@freeze_time("2023-01-18 03:00:00+01:00")
def test_event_ok(event, user, team):
djmail.outbox = []
s = ScheduledEventExport(event=event, owner=user)
s.export_identifier = "orderlist"
s.export_form_data = {"_format": "xlsx", "paid_only": False}
s.mail_additional_recipients = "boss@example.org,boss@example.net"
s.mail_additional_recipients_cc = "assistant@example.net"
s.mail_additional_recipients_bcc = "archive@example.net"
s.mail_subject = "Report 1"
s.mail_template = "Here is the report."
s.schedule_rrule = "DTSTART:20230118T000000\nRRULE:FREQ=DAILY;INTERVAL=1;WKST=MO"
s.schedule_rrule_time = time(2, 30, 0)
s.schedule_next_run = now() - timedelta(minutes=5)
s.error_counter = 1
s.save()
run_scheduled_exports(None)
s.refresh_from_db()
assert s.schedule_next_run > now()
assert s.error_counter == 0
assert len(djmail.outbox) == 1
assert djmail.outbox[0].subject == "Report 1"
assert "Here is the report." in djmail.outbox[0].body
assert djmail.outbox[0].to == ["boss@example.org", "boss@example.net"]
assert djmail.outbox[0].cc == ["assistant@example.net", user.email]
assert djmail.outbox[0].bcc == ["archive@example.net"]
assert len(djmail.outbox[0].attachments) == 1
assert djmail.outbox[0].attachments[0][0] == "dummy_orders.xlsx"
@pytest.mark.django_db
@freeze_time("2023-01-18 03:00:00+01:00")
def test_organizer_run_sets_new_time(event, user):
s = ScheduledOrganizerExport(organizer=event.organizer, owner=user, timezone="Europe/Berlin")
s.schedule_rrule = "DTSTART:20230118T000000\nRRULE:FREQ=DAILY;INTERVAL=1;WKST=MO"
s.schedule_rrule_time = time(2, 30, 0)
s.schedule_next_run = now() - timedelta(minutes=5)
s.save()
run_scheduled_exports(None)
s.refresh_from_db()
assert s.schedule_next_run == datetime(2023, 1, 19, 2, 30, 0, tzinfo=event.timezone)
@pytest.mark.django_db
@freeze_time("2023-01-18 03:00:00+01:00")
def test_organizer_not_run_when_failed_5_times(event, user):
s = ScheduledOrganizerExport(organizer=event.organizer, owner=user)
s.schedule_rrule = "DTSTART:20230118T000000\nRRULE:FREQ=DAILY;INTERVAL=1;WKST=MO"
s.schedule_rrule_time = time(2, 30, 0)
s.schedule_next_run = datetime(2023, 1, 18, 2, 30, 0, tzinfo=event.timezone)
s.error_counter = 5
s.save()
run_scheduled_exports(None)
s.refresh_from_db()
assert s.schedule_next_run == datetime(2023, 1, 18, 2, 30, 0, tzinfo=event.timezone)
@pytest.mark.django_db
@freeze_time("2023-01-18 03:00:00+01:00")
def test_organizer_fail_invalid_config(event, user):
djmail.outbox = []
s = ScheduledOrganizerExport(organizer=event.organizer, owner=user)
s.export_identifier = " invalid "
s.schedule_rrule = "DTSTART:20230118T000000\nRRULE:FREQ=DAILY;INTERVAL=1;WKST=MO"
s.schedule_rrule_time = time(2, 30, 0)
s.schedule_next_run = now() - timedelta(minutes=5)
s.error_counter = 0
s.save()
run_scheduled_exports(None)
s.refresh_from_db()
assert s.schedule_next_run > now()
assert s.error_counter == 1
assert len(djmail.outbox) == 1
assert djmail.outbox[0].subject == "Scheduled export failed"
assert "Reason: Export type not found" in djmail.outbox[0].body
assert djmail.outbox[0].to == [user.email]
@pytest.mark.django_db
@freeze_time("2023-01-18 03:00:00+01:00")
def test_organizer_fail_user_inactive(event, user):
djmail.outbox = []
s = ScheduledOrganizerExport(organizer=event.organizer, owner=user)
s.export_identifier = "orderlist"
s.schedule_rrule = "DTSTART:20230118T000000\nRRULE:FREQ=DAILY;INTERVAL=1;WKST=MO"
s.schedule_rrule_time = time(2, 30, 0)
s.schedule_next_run = now() - timedelta(minutes=5)
s.error_counter = 0
s.save()
user.is_active = False
user.save()
run_scheduled_exports(None)
s.refresh_from_db()
assert s.schedule_next_run > now()
assert s.error_counter == 1
assert len(djmail.outbox) == 0 # no mails sent to inactive user
@pytest.mark.django_db
@freeze_time("2023-01-18 03:00:00+01:00")
def test_organizer_fail_user_does_not_have_specific_permission(event, user, team):
djmail.outbox = []
s = ScheduledOrganizerExport(organizer=event.organizer, owner=user)
s.export_identifier = "customerlist"
s.schedule_rrule = "DTSTART:20230118T000000\nRRULE:FREQ=DAILY;INTERVAL=1;WKST=MO"
s.schedule_rrule_time = time(2, 30, 0)
s.schedule_next_run = now() - timedelta(minutes=5)
s.error_counter = 0
s.save()
team.all_event_permissions = False
team.limit_event_permissions = {"organizer.giftcards:write": True}
team.save()
run_scheduled_exports(None)
s.refresh_from_db()
assert s.schedule_next_run > now()
assert s.error_counter == 1
assert len(djmail.outbox) == 1
assert djmail.outbox[0].subject == "Scheduled export failed"
assert "Reason: Export type not found or permission denied." in djmail.outbox[0].body
assert djmail.outbox[0].to == [user.email]
@pytest.mark.django_db(transaction=True)
@freeze_time("2023-01-18 03:00:00+01:00")
def test_organizer_limited_to_events(event, user, team):
djmail.outbox = []
s = ScheduledOrganizerExport(organizer=event.organizer, owner=user)
s.export_identifier = "eventdata"
s.export_form_data = {"_format": "default", "all_events": True}
s.mail_subject = "Report 1"
s.mail_template = "Here is the report."
s.schedule_rrule = "DTSTART:20230118T000000\nRRULE:FREQ=DAILY;INTERVAL=1;WKST=MO"
s.schedule_rrule_time = time(2, 30, 0)
s.schedule_next_run = now() - timedelta(minutes=5)
s.error_counter = 0
s.save()
event2 = Event.objects.create(
organizer=event.organizer, name='Dummy', slug='dummy2',
date_from=datetime(2023, 1, 19, 2, 30, 0, tzinfo=timezone.utc),
plugins='pretix.plugins.banktransfer'
)
team.all_events = False
team.save()
team.limit_events.add(event2)
run_scheduled_exports(None)
s.refresh_from_db()
assert s.schedule_next_run > now()
assert s.error_counter == 0
assert len(djmail.outbox) == 1
assert djmail.outbox[0].subject == "Report 1"
assert "Here is the report." in djmail.outbox[0].body
assert djmail.outbox[0].to == [user.email]
assert len(djmail.outbox[0].attachments) == 1
assert djmail.outbox[0].attachments[0][0] == "dummy_events.csv"
assert len(djmail.outbox[0].attachments[0][1].splitlines()) == 2
@pytest.mark.django_db(transaction=True)
@freeze_time("2023-01-18 03:00:00+01:00")
def test_organizer_ok(event, user, team):
djmail.outbox = []
s = ScheduledOrganizerExport(organizer=event.organizer, owner=user)
s.export_identifier = "eventdata"
s.export_form_data = {"_format": "default", "all_events": True}
s.mail_additional_recipients = "boss@example.org,boss@example.net"
s.mail_additional_recipients_cc = "assistant@example.net"
s.mail_additional_recipients_bcc = "archive@example.net"
s.mail_subject = "Report 1"
s.mail_template = "Here is the report."
s.schedule_rrule = "DTSTART:20230118T000000\nRRULE:FREQ=DAILY;INTERVAL=1;WKST=MO"
s.schedule_rrule_time = time(2, 30, 0)
s.schedule_next_run = now() - timedelta(minutes=5)
s.error_counter = 1
s.save()
Event.objects.create(
organizer=event.organizer, name='Dummy', slug='dummy2',
date_from=datetime(2023, 1, 19, 2, 30, 0, tzinfo=timezone.utc),
plugins='pretix.plugins.banktransfer'
)
run_scheduled_exports(None)
s.refresh_from_db()
assert s.schedule_next_run > now()
assert s.error_counter == 0
assert len(djmail.outbox) == 1
assert djmail.outbox[0].subject == "Report 1"
assert "Here is the report." in djmail.outbox[0].body
assert djmail.outbox[0].to == ["boss@example.org", "boss@example.net"]
assert djmail.outbox[0].cc == ["assistant@example.net", user.email]
assert djmail.outbox[0].bcc == ["archive@example.net"]
assert len(djmail.outbox[0].attachments) == 1
assert djmail.outbox[0].attachments[0][0] == "dummy_events.csv"
assert len(djmail.outbox[0].attachments[0][1].splitlines()) == 3