Auto-scheduled emails

Co-authored-by: Raphael Michel <michel@rami.io>
This commit is contained in:
Julia Luna
2021-01-21 12:27:11 +01:00
committed by Raphael Michel
parent e4949b6491
commit 64d07a2811
22 changed files with 1479 additions and 81 deletions

View File

@@ -0,0 +1,21 @@
#
# This file is part of pretix (Community Edition).
#
# Copyright (C) 2014-2020 Raphael Michel and contributors
# Copyright (C) 2020-2021 rami.io 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/>.
#

View File

@@ -0,0 +1,90 @@
#
# This file is part of pretix (Community Edition).
#
# Copyright (C) 2014-2020 Raphael Michel and contributors
# Copyright (C) 2020-2021 rami.io 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/>.
#
import datetime
import pytest
from django.utils.timezone import now
from pretix.base.models import Event, Item, Order, OrderPosition, Organizer
@pytest.fixture
def event():
"""Returns an event instance"""
o = Organizer.objects.create(name='Dummy', slug='dummy')
event = Event.objects.create(
organizer=o, name='Dummy', slug='dummy',
date_from=now(),
plugins='pretix.plugins.sendmail,tests.testdummy',
)
return event
@pytest.fixture
def item(event):
"""Returns an item instance"""
return Item.objects.create(name='Test item', event=event, default_price=13)
@pytest.fixture
def item2(event):
return Item.objects.create(name='Test item 2', event=event, default_price=11)
@pytest.fixture
def checkin_list(event):
"""Returns an checkin list instance"""
return event.checkin_lists.create(name="Test Checkinlist", all_products=True)
@pytest.fixture
def order(item):
"""Returns an order instance"""
o = Order.objects.create(event=item.event, status=Order.STATUS_PENDING,
expires=now() + datetime.timedelta(hours=1),
total=13, code='DUMMY', email='dummy@dummy.test',
datetime=now(), locale='en')
return o
@pytest.fixture
def pos(order, item):
return OrderPosition.objects.create(order=order, item=item, price=13)
@pytest.fixture
def event_series(event):
event.has_subevents = True
event.save()
return event
@pytest.fixture
def subevent1(event_series):
se1 = event_series.subevents.create(name='Meow', date_from=now() + datetime.timedelta(days=1))
return se1
@pytest.fixture
def subevent2(event_series):
se2 = event_series.subevents.create(name='Foo', date_from=now() + datetime.timedelta(days=3))
return se2

View File

@@ -0,0 +1,352 @@
#
# This file is part of pretix (Community Edition).
#
# Copyright (C) 2014-2020 Raphael Michel and contributors
# Copyright (C) 2020-2021 rami.io 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/>.
#
import datetime
import pytest
import pytz
from django.core import mail as djmail
from django.utils.timezone import now, utc
from django_scopes import scopes_disabled
from pretix.base.models import Order
from pretix.plugins.sendmail.models import Rule, ScheduledMail
from pretix.plugins.sendmail.signals import sendmail_run_rules
@pytest.mark.django_db
def test_sendmail_rule_create_single(event):
dt = now()
r = Rule.objects.create(event=event, subject='dummy mail', template='mail body', send_date=dt)
mails = ScheduledMail.objects.filter(rule=r)
assert mails.count() == 1
mail = mails.get()
assert mail.computed_datetime == dt
dt_now = now()
NZ = pytz.timezone('NZ')
Berlin = pytz.timezone('Europe/Berlin')
@pytest.mark.django_db
@pytest.mark.parametrize(
"event_from,event_to,event_tz,rule,expected",
[
# Tests for all possible configurations of relative times
( # "Absolute"
None,
None,
'UTC',
Rule(date_is_absolute=True, send_date=dt_now),
dt_now
),
( # "Relative, after event start"
None,
None,
'UTC',
Rule(date_is_absolute=False, offset_is_after=True, send_offset_days=1, send_offset_time=datetime.time(hour=9)),
(dt_now + datetime.timedelta(days=1)).replace(hour=9, minute=0, second=0, microsecond=0)
),
( # "Relative, before event start"
datetime.datetime(2021, 5, 17, 12, 14, 0, tzinfo=utc),
None,
'UTC',
Rule(date_is_absolute=False, send_offset_days=2, send_offset_time=datetime.time(hour=0)),
datetime.datetime(2021, 5, 15, 0, tzinfo=utc)
),
( # "Relative, after event end"
datetime.datetime(2021, 5, 17, 18, tzinfo=utc),
datetime.datetime(2021, 5, 18, 5, tzinfo=utc),
'UTC',
Rule(date_is_absolute=False, offset_to_event_end=True, offset_is_after=True, send_offset_days=1, send_offset_time=datetime.time(hour=10)),
datetime.datetime(2021, 5, 19, 10, tzinfo=utc)
),
( # "Relative, before event end"
datetime.datetime(2021, 5, 17, 18, tzinfo=utc),
datetime.datetime(2021, 5, 22, 5, tzinfo=utc),
'UTC',
Rule(date_is_absolute=False, offset_to_event_end=True, offset_is_after=False, send_offset_days=1, send_offset_time=datetime.time(hour=10)),
datetime.datetime(2021, 5, 21, 10, tzinfo=utc)
),
# Tests for timezone quirks
( # Test sending on leap day
datetime.datetime(2020, 2, 27, 9, tzinfo=utc),
None,
'UTC',
Rule(date_is_absolute=False, offset_is_after=True, send_offset_days=2, send_offset_time=datetime.time(hour=9)),
datetime.datetime(2020, 2, 29, 9, tzinfo=utc)
),
( # Test timezone far off from UTC
NZ.localize(datetime.datetime(2021, 5, 17, 22)),
None,
'NZ',
Rule(date_is_absolute=False, offset_is_after=True, send_offset_days=1, send_offset_time=datetime.time(hour=9)),
NZ.localize(datetime.datetime(2021, 5, 18, 9))
),
( # Test across DST change
Berlin.localize(datetime.datetime(2021, 10, 29, 16, 30)),
None,
'Europe/Berlin',
Rule(date_is_absolute=False, offset_is_after=True, send_offset_days=4, send_offset_time=datetime.time(hour=2, minute=30)),
Berlin.localize(datetime.datetime(2021, 11, 2, 2, 30))
),
( # Test ambiguous time at DST change
Berlin.localize(datetime.datetime(2021, 10, 29, 18, 30)),
None,
'Europe/Berlin',
Rule(date_is_absolute=False, offset_is_after=True, send_offset_days=2, send_offset_time=datetime.time(hour=2, minute=30)),
datetime.datetime(2021, 10, 31, 1, 30, tzinfo=utc)
),
( # Test non-existing time at DST change
Berlin.localize(datetime.datetime(2021, 3, 29, 14, 30)),
None,
'Europe/Berlin',
Rule(date_is_absolute=False, offset_is_after=False, send_offset_days=1, send_offset_time=datetime.time(hour=2, minute=30)),
datetime.datetime(2021, 3, 28, 1, 30, tzinfo=utc)
),
])
def test_sendmail_rule_send_time(event_from, event_to, event_tz, rule, expected, event):
if event_from:
event.date_from = event_from
event.save()
if event_to:
event.date_to = event_to
event.save()
event.settings.timezone = event_tz
rule.event = event
rule.save()
m = ScheduledMail.objects.filter(rule=rule).get()
assert m.computed_datetime.astimezone(event.timezone) == expected
@pytest.mark.django_db
@scopes_disabled()
def test_sendmail_rule_recompute(event):
event.has_subevents = True
event.save()
se1 = event.subevents.create(name="meow", date_from=dt_now)
rule = event.sendmail_rules.create(date_is_absolute=False, offset_is_after=False, send_offset_days=1,
send_offset_time=datetime.time(4, 30))
se1.date_from += datetime.timedelta(days=1)
se1.save()
expected = dt_now.replace(hour=4, minute=30, second=0, microsecond=0)
sendmail_run_rules(None)
m = ScheduledMail.objects.filter(rule=rule).first()
assert m.computed_datetime.astimezone(utc) == expected
@pytest.mark.django_db
@pytest.mark.parametrize('send_to,amount_mails,recipients', [
(Rule.CUSTOMERS, 1, ['dummy@dummy.test']),
(Rule.ATTENDEES, 1, ['meow@dummy.test']),
(Rule.BOTH, 2, ['dummy@dummy.test', 'meow@dummy.test']),
])
@scopes_disabled()
def test_sendmail_rule_send_order_vs_pos(send_to, amount_mails, recipients, order, event, item):
djmail.outbox = []
order.status = order.STATUS_PAID
order.save()
order.event.sendmail_rules.create(date_is_absolute=True, send_date=dt_now - datetime.timedelta(hours=1),
send_to=send_to,
subject='meow', template='meow meow meow')
order.all_positions.create(item=item, price=0, attendee_email='meow@dummy.test')
sendmail_run_rules(None)
assert len(djmail.outbox) == amount_mails
_recipients = [mail.to[0] for mail in djmail.outbox]
assert set(recipients) == set(_recipients)
@pytest.mark.django_db
@scopes_disabled()
def test_sendmail_rule_send_attendees_unset_mail(order, event, item):
djmail.outbox = []
order.status = order.STATUS_PAID
order.save()
order.all_positions.create(item=item, price=13)
order.event.sendmail_rules.create(date_is_absolute=True, send_date=dt_now - datetime.timedelta(hours=1),
send_to=Rule.ATTENDEES,
subject='meow', template='meow meow meow')
sendmail_run_rules(None)
assert len(djmail.outbox) == 1
mail = djmail.outbox[0]
assert mail.to[0] == 'dummy@dummy.test'
@pytest.mark.django_db
@scopes_disabled()
def test_sendmail_rule_send_both_same_email(order, event, item):
djmail.outbox = []
order.status = order.STATUS_PAID
order.save()
order.all_positions.create(item=item, price=13, attendee_email='dummy@dummy.test')
order.event.sendmail_rules.create(date_is_absolute=True, send_date=dt_now - datetime.timedelta(hours=1),
send_to=Rule.BOTH,
subject='meow', template='meow meow meow')
sendmail_run_rules(None)
assert len(djmail.outbox) == 1
@pytest.mark.django_db
@scopes_disabled()
def test_sendmail_rule_send_correct_subevent(order, event_series, subevent1, subevent2, item):
djmail.outbox = []
order.status = order.STATUS_PAID
order.save()
event_series.sendmail_rules.create(date_is_absolute=False, offset_is_after=False, send_offset_days=2,
send_offset_time=datetime.time(9, 30), send_to=Rule.ATTENDEES,
subject='meow', template='meow meow meow')
p1 = order.all_positions.create(item=item, price=13, attendee_email='se1@dummy.test', subevent=subevent1)
order.all_positions.create(item=item, price=23, attendee_email='se2@dummy.test', subevent=subevent2)
sendmail_run_rules(None)
assert len(djmail.outbox) == 1
assert djmail.outbox[0].to[0] == p1.attendee_email
@pytest.mark.django_db
@scopes_disabled()
def test_sendmail_rule_send_correct_products(event, order, item, item2):
djmail.outbox = []
order.status = order.STATUS_PAID
order.save()
rule = event.sendmail_rules.create(send_date=dt_now - datetime.timedelta(hours=1), send_to=Rule.ATTENDEES,
subject='meow', template='meow meow meow', all_products=False)
rule.limit_products.set([item])
rule.save()
p1 = order.all_positions.create(item=item, price=13, attendee_email='item1@dummy.test')
order.all_positions.create(item=item2, price=13, attendee_email='item2@dummy.test')
sendmail_run_rules(None)
assert len(djmail.outbox) == 1
assert djmail.outbox[0].to[0] == p1.attendee_email
@pytest.mark.django_db
@scopes_disabled()
def test_sendmail_rule_send_order_pending(event, order):
djmail.outbox = []
event.sendmail_rules.create(send_date=dt_now - datetime.timedelta(hours=1), include_pending=True,
subject='meow', template='meow meow meow')
sendmail_run_rules(None)
assert len(djmail.outbox) == 1
@pytest.mark.django_db
@scopes_disabled()
def test_sendmail_rule_send_order_pending_excluded(event, order):
djmail.outbox = []
event.sendmail_rules.create(send_date=dt_now - datetime.timedelta(hours=1), include_pending=False,
subject='meow', template='meow meow meow')
sendmail_run_rules(None)
assert len(djmail.outbox) == 0
@pytest.mark.django_db
@pytest.mark.parametrize('status', [
Order.STATUS_EXPIRED,
Order.STATUS_CANCELED,
])
@scopes_disabled()
def test_sendmail_rule_send_order_status(status, event, order):
djmail.outbox = []
order.status = status
order.save()
event.sendmail_rules.create(send_date=dt_now - datetime.timedelta(hours=1), include_pending=True,
subject='meow', template='meow meow meow')
sendmail_run_rules(None)
assert len(djmail.outbox) == 0
@pytest.mark.django_db
@scopes_disabled()
def test_sendmail_rule_send_order_approval(event, order):
djmail.outbox = []
order.require_approval = True
order.save()
event.sendmail_rules.create(send_date=dt_now - datetime.timedelta(hours=1), include_pending=True,
subject='meow', template='meow meow meow')
sendmail_run_rules(None)
assert len(djmail.outbox) == 0
@pytest.mark.django_db
@scopes_disabled()
def test_sendmail_rule_only_send_once(event, order):
djmail.outbox = []
event.sendmail_rules.create(send_date=dt_now - datetime.timedelta(hours=1), include_pending=True,
subject='meow', template='meow meow meow')
sendmail_run_rules(None)
assert len(djmail.outbox) == 1
sendmail_run_rules(None)
assert len(djmail.outbox) == 1

View File

@@ -0,0 +1,494 @@
#
# This file is part of pretix (Community Edition).
#
# Copyright (C) 2014-2020 Raphael Michel and contributors
# Copyright (C) 2020-2021 rami.io 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/>.
#
# This file is based on an earlier version of pretix which was released under the Apache License 2.0. The full text of
# the Apache License 2.0 can be obtained at <http://www.apache.org/licenses/LICENSE-2.0>.
#
# This file may have since been changed and any changes are released under the terms of AGPLv3 as described above. A
# full history of changes and contributors is available at <https://github.com/pretix/pretix>.
#
# This file contains Apache-licensed contributions copyrighted by: Daniel, Flavia Bastos, Sanket Dasgupta, Tobias Kunze,
# pajowu
#
# Unless required by applicable law or agreed to in writing, software distributed under the Apache License 2.0 is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under the License.
import datetime
import pytest
from django.core import mail as djmail
from django.utils.timezone import now
from django_scopes import scopes_disabled
from pretix.base.models import Checkin, Item, Order, OrderPosition, Team, User
@pytest.fixture
def logged_in_client(client, event):
"""Returns a logged client"""
user = User.objects.create_superuser('dummy@dummy.dummy', 'dummy')
t = Team.objects.create(organizer=event.organizer, can_view_orders=True, can_change_orders=True)
t.members.add(user)
t.limit_events.add(event)
client.force_login(user)
return client
@pytest.fixture
def sendmail_url(event):
"""Returns a url for sendmail"""
url = '/control/event/{orga}/{event}/sendmail/'.format(
event=event.slug, orga=event.organizer.slug,
)
return url
@pytest.fixture
def subevent(event):
event.has_subevents = True
event.save()
se = event.subevents.create(name='se1', date_from=now())
return se
@pytest.mark.django_db
def test_sendmail_view(logged_in_client, sendmail_url, expected=200):
response = logged_in_client.get(sendmail_url)
assert response.status_code == expected
@pytest.mark.django_db
def test_sendmail_simple_case(logged_in_client, sendmail_url, event, order, pos):
djmail.outbox = []
response = logged_in_client.post(sendmail_url,
{'sendto': 'na',
'recipients': 'orders',
'items': pos.item_id,
'subject_0': 'Test subject',
'message_0': 'This is a test file for sending mails.',
},
follow=True)
assert response.status_code == 200
assert 'alert-success' in response.rendered_content
assert len(djmail.outbox) == 1
assert djmail.outbox[0].to == [order.email]
assert djmail.outbox[0].subject == 'Test subject'
assert 'This is a test file for sending mails.' in djmail.outbox[0].body
url = sendmail_url + 'history/'
response = logged_in_client.get(url)
assert response.status_code == 200
assert 'Test subject' in response.rendered_content
@pytest.mark.django_db
def test_sendmail_email_not_sent_if_order_not_match(logged_in_client, sendmail_url, event, order, pos):
djmail.outbox = []
response = logged_in_client.post(sendmail_url,
{'sendto': 'p',
'recipients': 'orders',
'items': pos.item_id,
'subject_0': 'Test subject',
'message_0': 'This is a test file for sending mails.',
},
follow=True)
assert 'alert-danger' in response.rendered_content
assert len(djmail.outbox) == 0
@pytest.mark.django_db
def test_sendmail_preview(logged_in_client, sendmail_url, event, order, pos):
djmail.outbox = []
response = logged_in_client.post(sendmail_url,
{'sendto': 'na',
'recipients': 'orders',
'items': pos.item_id,
'subject_0': 'Test subject',
'message_0': 'This is a test file for sending mails.',
'action': 'preview',
},
follow=True)
assert response.status_code == 200
assert 'E-mail preview' in response.rendered_content
assert len(djmail.outbox) == 0
@pytest.mark.django_db
def test_sendmail_invalid_data(logged_in_client, sendmail_url, event, order, pos):
djmail.outbox = []
response = logged_in_client.post(sendmail_url,
{'sendto': 'na',
'recipients': 'orders',
'items': pos.item_id,
'subject_0': 'Test subject',
},
follow=True)
assert 'has-error' in response.rendered_content
assert len(djmail.outbox) == 0
@pytest.mark.django_db
def test_sendmail_multi_locales(logged_in_client, sendmail_url, event, item):
djmail.outbox = []
event.settings.set('locales', ['en', 'de'])
with scopes_disabled():
o = Order.objects.create(event=item.event, status=Order.STATUS_PAID,
expires=now() + datetime.timedelta(hours=1),
total=13, code='DUMMY', email='dummy@dummy.test',
datetime=now(),
locale='de')
OrderPosition.objects.create(order=o, item=item, price=13)
response = logged_in_client.post(sendmail_url,
{'sendto': 'p',
'recipients': 'orders',
'items': item.pk,
'subject_0': 'Test subject',
'message_0': 'Test message',
'subject_1': 'Benutzer',
'message_1': 'Test nachricht',
},
follow=True)
assert response.status_code == 200
assert 'alert-success' in response.rendered_content
assert len(djmail.outbox) == 1
assert djmail.outbox[0].to == [o.email]
assert djmail.outbox[0].subject == 'Benutzer'
assert 'Test nachricht' in djmail.outbox[0].body
url = sendmail_url + 'history/'
response = logged_in_client.get(url)
assert response.status_code == 200
assert 'Benutzer' in response.rendered_content
assert 'Test nachricht' in response.rendered_content
@pytest.mark.django_db
def test_sendmail_subevents(logged_in_client, sendmail_url, event, order, pos):
event.has_subevents = True
event.save()
with scopes_disabled():
se1 = event.subevents.create(name='Subevent FOO', date_from=now())
se2 = event.subevents.create(name='Bar', date_from=now())
op = order.positions.last()
op.subevent = se1
op.save()
djmail.outbox = []
response = logged_in_client.post(sendmail_url,
{'sendto': 'na',
'recipients': 'orders',
'items': pos.item_id,
'subject_0': 'Test subject',
'message_0': 'This is a test file for sending mails.',
'subevent': se1.pk,
},
follow=True)
assert response.status_code == 200
assert 'alert-success' in response.rendered_content
assert len(djmail.outbox) == 1
djmail.outbox = []
response = logged_in_client.post(sendmail_url,
{'sendto': 'na',
'recipients': 'orders',
'items': pos.item_id,
'subject_0': 'Test subject',
'message_0': 'This is a test file for sending mails.',
'subevent': se2.pk,
},
follow=True)
assert len(djmail.outbox) == 0
url = sendmail_url + 'history/'
response = logged_in_client.get(url)
assert response.status_code == 200
assert 'Subevent FOO' in response.rendered_content
@pytest.mark.django_db
def test_sendmail_placeholder(logged_in_client, sendmail_url, event, order, pos):
djmail.outbox = []
response = logged_in_client.post(sendmail_url,
{'sendto': 'na',
'recipients': 'orders',
'items': pos.item_id,
'subject_0': '{code} Test subject',
'message_0': 'This is a test file for sending mails.',
'action': 'preview',
},
follow=True)
assert response.status_code == 200
assert 'F8VVL' in response.rendered_content
assert len(djmail.outbox) == 0
@pytest.mark.django_db
def test_sendmail_attendee_mails(logged_in_client, sendmail_url, event, order, pos):
p = pos
event.settings.attendee_emails_asked = True
p.attendee_email = 'attendee@dummy.test'
p.save()
djmail.outbox = []
response = logged_in_client.post(sendmail_url,
{'sendto': 'na',
'recipients': 'attendees',
'items': pos.item_id,
'subject_0': 'Test subject',
'message_0': 'This is a test file for sending mails.',
},
follow=True)
assert response.status_code == 200
assert 'alert-success' in response.rendered_content
assert len(djmail.outbox) == 1
assert djmail.outbox[0].to == ['attendee@dummy.test']
assert '/ticket/' in djmail.outbox[0].body
assert '/order/' not in djmail.outbox[0].body
@pytest.mark.django_db
def test_sendmail_both_mails(logged_in_client, sendmail_url, event, order, pos):
p = pos
event.settings.attendee_emails_asked = True
p.attendee_email = 'attendee@dummy.test'
p.save()
djmail.outbox = []
response = logged_in_client.post(sendmail_url,
{'sendto': 'na',
'recipients': 'both',
'items': pos.item_id,
'subject_0': 'Test subject',
'message_0': 'This is a test file for sending mails.',
},
follow=True)
assert response.status_code == 200
assert 'alert-success' in response.rendered_content
assert len(djmail.outbox) == 2
assert djmail.outbox[0].to == ['attendee@dummy.test']
assert '/ticket/' in djmail.outbox[0].body
assert '/order/' not in djmail.outbox[0].body
assert djmail.outbox[1].to == ['dummy@dummy.test']
assert '/ticket/' not in djmail.outbox[1].body
assert '/order/' in djmail.outbox[1].body
@pytest.mark.django_db
def test_sendmail_both_but_same_address(logged_in_client, sendmail_url, event, order, pos):
p = pos
event.settings.attendee_emails_asked = True
p.attendee_email = 'dummy@dummy.test'
p.save()
djmail.outbox = []
response = logged_in_client.post(sendmail_url,
{'sendto': 'na',
'recipients': 'both',
'items': pos.item_id,
'subject_0': 'Test subject',
'message_0': 'This is a test file for sending mails.',
},
follow=True)
assert response.status_code == 200
assert 'alert-success' in response.rendered_content
assert len(djmail.outbox) == 1
assert djmail.outbox[0].to == ['dummy@dummy.test']
assert '/ticket/' not in djmail.outbox[0].body
assert '/order/' in djmail.outbox[0].body
@pytest.mark.django_db
def test_sendmail_attendee_fallback(logged_in_client, sendmail_url, event, order, pos):
p = pos
event.settings.attendee_emails_asked = True
p.attendee_email = None
p.save()
djmail.outbox = []
response = logged_in_client.post(sendmail_url,
{'sendto': 'na',
'recipients': 'attendees',
'items': pos.item_id,
'subject_0': 'Test subject',
'message_0': 'This is a test file for sending mails.',
},
follow=True)
assert response.status_code == 200
assert 'alert-success' in response.rendered_content
assert len(djmail.outbox) == 1
assert djmail.outbox[0].to == ['dummy@dummy.test']
assert '/ticket/' not in djmail.outbox[0].body
assert '/order/' in djmail.outbox[0].body
@pytest.mark.django_db
def test_sendmail_attendee_product_filter(logged_in_client, sendmail_url, event, order, pos):
event.settings.attendee_emails_asked = True
with scopes_disabled():
i2 = Item.objects.create(name='Test item', event=event, default_price=13)
p = pos
p.attendee_email = 'attendee1@dummy.test'
p.save()
order.positions.create(
item=i2, price=0, attendee_email='attendee2@dummy.test'
)
djmail.outbox = []
response = logged_in_client.post(sendmail_url,
{'sendto': 'na',
'recipients': 'attendees',
'items': i2.pk,
'subject_0': 'Test subject',
'message_0': 'This is a test file for sending mails.',
},
follow=True)
assert response.status_code == 200
assert 'alert-success' in response.rendered_content
assert len(djmail.outbox) == 1
assert djmail.outbox[0].to == ['attendee2@dummy.test']
assert '/ticket/' in djmail.outbox[0].body
assert '/order/' not in djmail.outbox[0].body
@pytest.mark.django_db
def test_sendmail_attendee_checkin_filter(logged_in_client, sendmail_url, event, order, checkin_list, item, pos):
event.settings.attendee_emails_asked = True
with scopes_disabled():
chkl2 = event.checkin_lists.create(name="Test Checkinlist 2", all_products=True)
p = pos
p.attendee_email = 'attendee1@dummy.test'
p.save()
pos2 = order.positions.create(item=item, price=0, attendee_email='attendee2@dummy.test')
Checkin.objects.create(position=pos2, list=chkl2)
djmail.outbox = []
response = logged_in_client.post(sendmail_url,
{'sendto': 'na',
'recipients': 'attendees',
'items': pos2.item_id,
'filter_checkins': 'on',
'checkin_lists': [chkl2.id],
'subject_0': 'Test subject',
'message_0': 'This is a test file for sending mails.'
},
follow=True)
assert response.status_code == 200
assert 'alert-success' in response.rendered_content
assert len(djmail.outbox) == 1
assert djmail.outbox[0].to == ['attendee2@dummy.test']
assert '/ticket/' in djmail.outbox[0].body
assert '/order/' not in djmail.outbox[0].body
djmail.outbox = []
response = logged_in_client.post(sendmail_url,
{'sendto': 'na',
'recipients': 'attendees',
'items': pos2.item_id,
'subject_0': 'Test subject',
'message_0': 'This is a test file for sending mails.',
'filter_checkins': 'on',
'not_checked_in': 'on',
},
follow=True)
assert response.status_code == 200
assert 'alert-success' in response.rendered_content
assert len(djmail.outbox) == 1
assert djmail.outbox[0].to == ['attendee1@dummy.test']
assert '/ticket/' in djmail.outbox[0].body
assert '/order/' not in djmail.outbox[0].body
djmail.outbox = []
response = logged_in_client.post(sendmail_url,
{'sendto': 'na',
'recipients': 'attendees',
'items': pos2.item_id,
'subject_0': 'Test subject',
'message_0': 'This is a test file for sending mails.',
'filter_checkins': 'on',
'checkin_lists': [chkl2.id],
'not_checked_in': 'on',
},
follow=True)
assert response.status_code == 200
assert 'alert-success' in response.rendered_content
assert len(djmail.outbox) == 2
assert djmail.outbox[0].to == ['attendee1@dummy.test']
assert djmail.outbox[1].to == ['attendee2@dummy.test']
# Test that filtering is ignored if filter_checkins is not set
djmail.outbox = []
response = logged_in_client.post(sendmail_url,
{'sendto': 'na',
'recipients': 'attendees',
'items': pos2.item_id,
'subject_0': 'Test subject',
'message_0': 'This is a test file for sending mails.',
'not_checked_in': 'on',
},
follow=True)
assert response.status_code == 200
assert 'alert-success' in response.rendered_content
assert len(djmail.outbox) == 2
assert '/ticket/' in djmail.outbox[0].body
assert '/order/' not in djmail.outbox[0].body
assert '/ticket/' in djmail.outbox[1].body
assert '/order/' not in djmail.outbox[1].body
to_emails = set(*zip(*[mail.to for mail in djmail.outbox]))
assert to_emails == {'attendee1@dummy.test', 'attendee2@dummy.test'}
# Test that filtering is ignored if filter_checkins is not set
djmail.outbox = []
response = logged_in_client.post(sendmail_url,
{'sendto': 'na',
'recipients': 'attendees',
'items': pos2.item_id,
'subject_0': 'Test subject',
'message_0': 'This is a test file for sending mails.',
'checkin_lists': [chkl2.id],
},
follow=True)
assert response.status_code == 200
assert 'alert-success' in response.rendered_content
assert len(djmail.outbox) == 2
assert '/ticket/' in djmail.outbox[0].body
assert '/order/' not in djmail.outbox[0].body
assert '/ticket/' in djmail.outbox[1].body
assert '/order/' not in djmail.outbox[1].body
to_emails = set(*zip(*[mail.to for mail in djmail.outbox]))
assert to_emails == {'attendee1@dummy.test', 'attendee2@dummy.test'}