diff --git a/doc/api/resources/index.rst b/doc/api/resources/index.rst index 5a4693083..70aec4d42 100644 --- a/doc/api/resources/index.rst +++ b/doc/api/resources/index.rst @@ -31,5 +31,6 @@ Resources and endpoints webhooks seatingplans exporters + sendmail_rules billing_invoices billing_var diff --git a/doc/api/resources/sendmail_rules.rst b/doc/api/resources/sendmail_rules.rst new file mode 100644 index 000000000..d90d1a28d --- /dev/null +++ b/doc/api/resources/sendmail_rules.rst @@ -0,0 +1,281 @@ +Automated email rules +===================== + +Resource description +-------------------- + +Automated email rules that specify emails that the system will send automatically at a specific point in time, e.g. +the day of the event. + +.. rst-class:: rest-resource-table + +===================================== ========================== ======================================================= +Field Type Description +===================================== ========================== ======================================================= +id integer Internal ID of the rule +enabled boolean If ``false``, the rule is ignored +subject multi-lingual string The subject of the email +template multi-lingual string The body of the email +all_products boolean If ``true``, the email is sent to buyers of all products +limit_products list of integers List of product IDs, if ``all_products`` is not set +include_pending boolean If ``true``, the email is sent to pending orders. If ``false``, + only paid orders are considered. +date_is_absolute boolean If ``true``, the email is set at a specific point in time. +send_date datetime If ``date_is_absolute`` is set: Date and time to send the email. +send_offset_days integer If ``date_is_absolute`` is not set, this is the number of days + before/after the email is sent. +send_offset_time time If ``date_is_absolute`` is not set, this is the time of day the + email is sent on the day specified by ``send_offset_days``. +offset_to_event_end boolean If ``true``, ``send_offset_days`` is relative to the event end + date. Otherwise it is relative to the event start date. +offset_is_after boolean If ``true``, ``send_offset_days`` is the number of days **after** + the event start or end date. Otherwise it is the number of days + **before**. +send_to string Can be ``"orders"`` if the email should be sent to customers + (one email per order), + ``"attendees"`` if the email should be sent to every attendee, + or ``"both"``. + date. Otherwise it is relative to the event start date. +===================================== ========================== ======================================================= + + +Endpoints +--------- + +.. http:get:: /api/v1/organizers/(organizer)/events/(event)/sendmail_rules/ + + Returns a list of all rules configured for an event. + + **Example request**: + + .. sourcecode:: http + + GET /api/v1/organizers/bigevents/events/sampleconf/sendmail_rules/ HTTP/1.1 + Host: pretix.eu + Accept: application/json, text/javascript + + **Example response**: + + .. sourcecode:: http + + HTTP/1.1 200 OK + Vary: Accept + Content-Type: application/json + + { + "count": 1, + "next": null, + "previous": null, + "results": [ + { + "id": 1, + "enabled": true, + "subject": {"en": "See you tomorrow!"}, + "template": {"en": "Don't forget your tickets, download them at {url}"}, + "all_products": true, + "limit_products": [], + "include_pending": false, + "send_date": null, + "send_offset_days": 1, + "send_offset_time": "18:00", + "date_is_absolute": false, + "offset_to_event_end": false, + "offset_is_after": false, + "send_to": "orders" + } + ] + } + + :query page: The page number in case of a multi-page result set, default is 1 + :param organizer: The ``slug`` field of a valid organizer + :param event: The ``slug`` field of the event to fetch + :statuscode 200: no error + :statuscode 401: Authentication failure + :statuscode 403: The requested organizer does not exist **or** you have no permission to view it. + +.. http:get:: /api/v1/organizers/(organizer)/events/(event)/sendmail_rules/(id)/ + + Returns information on one rule, identified by its ID. + + **Example request**: + + .. sourcecode:: http + + GET /api/v1/organizers/bigevents/events/sampleconf/sendmail_rules/1/ HTTP/1.1 + Host: pretix.eu + Accept: application/json, text/javascript + + **Example response**: + + .. sourcecode:: http + + HTTP/1.1 200 OK + Vary: Accept + Content-Type: application/json + + { + "id": 1, + "enabled": true, + "subject": {"en": "See you tomorrow!"}, + "template": {"en": "Don't forget your tickets, download them at {url}"}, + "all_products": true, + "limit_products": [], + "include_pending": false, + "send_date": null, + "send_offset_days": 1, + "send_offset_time": "18:00", + "date_is_absolute": false, + "offset_to_event_end": false, + "offset_is_after": false, + "send_to": "orders" + } + + :param organizer: The ``slug`` field of the organizer to fetch + :param event: The ``slug`` field of the event to fetch + :param id: The ``id`` field of the rule to fetch + :statuscode 200: no error + :statuscode 401: Authentication failure + :statuscode 403: The requested organizer/event/rule does not exist **or** you have no permission to view it. + +.. http:post:: /api/v1/organizers/(organizer)/events/(event)/sendmail_rules/ + + Create a new rule. + + **Example request**: + + .. sourcecode:: http + + POST /api/v1/organizers/bigevents/events/sampleconf/sendmail_rules/ HTTP/1.1 + Host: pretix.eu + Accept: application/json, text/javascript + Content-Type: application/json + Content-Length: 166 + + { + "enabled": true, + "subject": {"en": "See you tomorrow!"}, + "template": {"en": "Don't forget your tickets, download them at {url}"}, + "all_products": true, + "limit_products": [], + "include_pending": false, + "send_date": null, + "send_offset_days": 1, + "send_offset_time": "18:00", + "date_is_absolute": false, + "offset_to_event_end": false, + "offset_is_after": false, + "send_to": "orders" + } + + **Example response**: + + .. sourcecode:: http + + HTTP/1.1 201 Created + Vary: Accept + Content-Type: application/json + + { + "id": 1, + "enabled": true, + "subject": {"en": "See you tomorrow!"}, + "template": {"en": "Don't forget your tickets, download them at {url}"}, + "all_products": true, + "limit_products": [], + "include_pending": false, + "send_date": null, + "send_offset_days": 1, + "send_offset_time": "18:00", + "date_is_absolute": false, + "offset_to_event_end": false, + "offset_is_after": false, + "send_to": "orders" + } + + :param organizer: The ``slug`` field of the organizer to create a rule for + :param event: The ``slug`` field of the event to create a rule for + :statuscode 201: no error + :statuscode 400: The rule could not be created due to invalid submitted data. + :statuscode 401: Authentication failure + :statuscode 403: The requested organizer/event does not exist **or** you have no permission to create rules. + + +.. http:patch:: /api/v1/organizers/(organizer)/events/(event)/sendmail_rules/(id)/ + + Update a rule. You can also use ``PUT`` instead of ``PATCH``. With ``PUT``, you have to provide all fields of + the resource, other fields will be reset to default. With ``PATCH``, you only need to provide the fields that you + want to change. + + **Example request**: + + .. sourcecode:: http + + PATCH /api/v1/organizers/bigevents/events/sampleconf/sendmail_rules/1/ HTTP/1.1 + Host: pretix.eu + Accept: application/json, text/javascript + Content-Type: application/json + Content-Length: 34 + + { + "enabled": false, + } + + **Example response**: + + .. sourcecode:: http + + HTTP/1.1 200 OK + Vary: Accept + Content-Type: text/javascript + + { + "id": 1, + "enabled": false, + "subject": {"en": "See you tomorrow!"}, + "template": {"en": "Don't forget your tickets, download them at {url}"}, + "all_products": true, + "limit_products": [], + "include_pending": false, + "send_date": null, + "send_offset_days": 1, + "send_offset_time": "18:00", + "date_is_absolute": false, + "offset_to_event_end": false, + "offset_is_after": false, + "send_to": "orders" + } + + :param organizer: The ``slug`` field of the organizer to modify + :param event: The ``slug`` field of the event to modify + :param id: The ``id`` field of the rule to modify + :statuscode 200: no error + :statuscode 400: The rule could not be modified due to invalid submitted data. + :statuscode 401: Authentication failure + :statuscode 403: The requested organizer/event/rule does not exist **or** you have no permission to change it. + + +.. http:delete:: /api/v1/organizers/(organizer)/events/(event)/sendmail_rules/(id)/ + + Delete a rule. + + **Example request**: + + .. sourcecode:: http + + DELETE /api/v1/organizers/bigevents/events/sampleconf/sendmail_rules/1/ HTTP/1.1 + Host: pretix.eu + Accept: application/json, text/javascript + + **Example response**: + + .. sourcecode:: http + + HTTP/1.1 204 No Content + Vary: Accept + + :param organizer: The ``slug`` field of the organizer to modify + :param event: The ``slug`` field of the event to modify + :param id: The ``id`` field of the rule to delete + :statuscode 204: no error + :statuscode 401: Authentication failure + :statuscode 403: The requested organizer/event/rule does not exist **or** you have no permission to change it **or** this rule cannot be deleted since it is currently in use. diff --git a/src/pretix/locale/de_Informal/LC_MESSAGES/django.po b/src/pretix/locale/de_Informal/LC_MESSAGES/django.po index 47c2857f1..2e4591367 100644 --- a/src/pretix/locale/de_Informal/LC_MESSAGES/django.po +++ b/src/pretix/locale/de_Informal/LC_MESSAGES/django.po @@ -22735,7 +22735,7 @@ msgstr "" #: pretix/plugins/sendmail/templates/pretixplugins/sendmail/rule_list.html:17 #: pretix/plugins/sendmail/templates/pretixplugins/sendmail/rule_list.html:94 msgid "Create a new rule" -msgstr "Neue Steuer-Regel erstellen" +msgstr "Neue Regel erstellen" #: pretix/plugins/sendmail/templates/pretixplugins/sendmail/rule_list.html:24 msgid "Email subject" diff --git a/src/pretix/plugins/sendmail/api.py b/src/pretix/plugins/sendmail/api.py new file mode 100644 index 000000000..134f353d0 --- /dev/null +++ b/src/pretix/plugins/sendmail/api.py @@ -0,0 +1,111 @@ +# +# 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 . +# +# 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 +# . +# +from django.core.exceptions import ValidationError +from django_filters.rest_framework import DjangoFilterBackend, FilterSet +from django_scopes import scopes_disabled +from rest_framework import viewsets +from rest_framework.filters import OrderingFilter + +from pretix.api.serializers.i18n import I18nAwareModelSerializer +from pretix.plugins.sendmail.models import Rule + + +class RuleSerializer(I18nAwareModelSerializer): + class Meta: + model = Rule + fields = ['id', 'subject', 'template', 'all_products', 'limit_products', 'include_pending', + 'send_date', 'send_offset_days', 'send_offset_time', 'date_is_absolute', + 'offset_to_event_end', 'offset_is_after', 'send_to', 'enabled'] + read_only_fields = ['id'] + + def validate(self, data): + data = super().validate(data) + + full_data = self.to_internal_value(self.to_representation(self.instance)) if self.instance else {} + full_data.update(data) + + if full_data.get('date_is_absolute') is not False: + if any([k in data for k in ['offset_to_event_end', 'offset_is_after']]): + raise ValidationError('date_is_absolute and offset_* are mutually exclusive') + if not full_data.get('send_date'): + raise ValidationError('send_date is required for date_is_absolute=True') + else: + if not all([full_data.get(k) for k in ['send_offset_days', 'send_offset_time']]): + raise ValidationError('send_offset_days and send_offset_time are required for date_is_absolute=False') + + if full_data.get('all_products') is False: + if not full_data.get('limit_products'): + raise ValidationError('limit_products is required when all_products=False') + + return full_data + + def save(self, **kwargs): + return super().save(event=self.context['request'].event) + + +with scopes_disabled(): + class RuleFilter(FilterSet): + class Meta: + model = Rule + fields = ['id', 'all_products', 'include_pending', 'date_is_absolute', + 'offset_to_event_end', 'offset_is_after', 'send_to', 'enabled'] + + +class RuleViewSet(viewsets.ModelViewSet): + queryset = Rule.objects.none() + serializer_class = RuleSerializer + filter_backends = (DjangoFilterBackend, OrderingFilter) + filterset_class = RuleFilter + ordering = ('id',) + ordering_fields = ('id',) + permission = 'can_change_event_settings' + + def get_queryset(self): + return Rule.objects.filter(event=self.request.event) + + def perform_create(self, serializer): + super().perform_create(serializer) + serializer.instance.log_action( + 'pretix.plugins.sendmail.rule.added', + user=self.request.user, + auth=self.request.auth, + data=self.request.data + + ) + + def perform_update(self, serializer): + super().perform_update(serializer) + serializer.instance.log_action( + 'pretix.plugins.sendmail.rule.changed', + user=self.request.user, + auth=self.request.auth, + data=self.request.data + ) + + def perform_destroy(self, instance): + instance.log_action( + 'pretix.plugins.sendmail.rule.deleted', + user=self.request.user, + auth=self.request.auth, + data=self.request.data + ) + super().perform_destroy(instance) diff --git a/src/pretix/plugins/sendmail/models.py b/src/pretix/plugins/sendmail/models.py index 4b397adf2..8e75cf548 100644 --- a/src/pretix/plugins/sendmail/models.py +++ b/src/pretix/plugins/sendmail/models.py @@ -34,6 +34,7 @@ from pretix.base.email import get_email_context from pretix.base.models import ( Event, InvoiceAddress, Item, Order, OrderPosition, SubEvent, ) +from pretix.base.models.base import LoggingMixin from pretix.base.services.mail import SendMailException @@ -164,7 +165,7 @@ class ScheduledMail(models.Model): self.last_successful_order_id = o.pk -class Rule(models.Model): +class Rule(models.Model, LoggingMixin): CUSTOMERS = "orders" ATTENDEES = "attendees" BOTH = "both" diff --git a/src/pretix/plugins/sendmail/signals.py b/src/pretix/plugins/sendmail/signals.py index ad9580676..fe953c88d 100644 --- a/src/pretix/plugins/sendmail/signals.py +++ b/src/pretix/plugins/sendmail/signals.py @@ -119,6 +119,8 @@ def pretixcontrol_logentry_display(sender, logentry, **kwargs): 'pretix.plugins.sendmail.sent': _('Email was sent'), 'pretix.plugins.sendmail.order.email.sent': _('The order received a mass email.'), 'pretix.plugins.sendmail.order.email.sent.attendee': _('A ticket holder of this order received a mass email.'), + 'pretix.plugins.sendmail.rule.added': _('An email rule was created'), + 'pretix.plugins.sendmail.rule.changed': _('An email rule was updated'), 'pretix.plugins.sendmail.rule.order.email.sent': _('A scheduled email was sent to the order'), 'pretix.plugins.sendmail.rule.order.position.email.sent': _('A scheduled email was sent to a ticket holder'), 'pretix.plugins.sendmail.rule.deleted': _('An email rule was deleted'), diff --git a/src/pretix/plugins/sendmail/urls.py b/src/pretix/plugins/sendmail/urls.py index 01630179b..e9266f301 100644 --- a/src/pretix/plugins/sendmail/urls.py +++ b/src/pretix/plugins/sendmail/urls.py @@ -34,7 +34,10 @@ from django.conf.urls import re_path +from pretix.api.urls import event_router + from . import views +from .api import RuleViewSet urlpatterns = [ re_path(r'^control/event/(?P[^/]+)/(?P[^/]+)/sendmail/$', views.SenderView.as_view(), @@ -52,3 +55,4 @@ urlpatterns = [ re_path(r'^control/event/(?P[^/]+)/(?P[^/]+)/sendmail/rules', views.ListRules.as_view(), name='rule.list'), ] +event_router.register(r'sendmail_rules', RuleViewSet) diff --git a/src/pretix/plugins/sendmail/views.py b/src/pretix/plugins/sendmail/views.py index edcdc12d3..ac8065db0 100644 --- a/src/pretix/plugins/sendmail/views.py +++ b/src/pretix/plugins/sendmail/views.py @@ -365,7 +365,10 @@ class CreateRule(EventPermissionRequiredMixin, CreateView): form.instance.event = self.request.event - self.object = form.save() + with transaction.atomic(): + self.object = form.save() + form.instance.log_action('pretix.plugins.sendmail.rule.added', user=self.request.user, + data=dict(form.cleaned_data)) return redirect( 'plugins:sendmail:rule.update', @@ -391,8 +394,11 @@ class UpdateRule(EventPermissionRequiredMixin, UpdateView): 'rule': self.object.pk, }) + @transaction.atomic() def form_valid(self, form): messages.success(self.request, _('Your changes have been saved.')) + form.instance.log_action('pretix.plugins.sendmail.rule.changed', user=self.request.user, + data=dict(form.cleaned_data)) return super().form_valid(form) def form_invalid(self, form): diff --git a/src/tests/api/conftest.py b/src/tests/api/conftest.py index 7468411e0..95d0e79fc 100644 --- a/src/tests/api/conftest.py +++ b/src/tests/api/conftest.py @@ -207,4 +207,10 @@ def taxrule2(event2): return event2.tax_rules.create(name="VAT", rate=25) +@pytest.fixture +@scopes_disabled() +def item(event): + return event.items.create(name='foo', default_price=3) + + utils.setup_databases = scopes_disabled()(utils.setup_databases) diff --git a/src/tests/api/test_permissions.py b/src/tests/api/test_permissions.py index 53f62ebf7..7e0d1b1d1 100644 --- a/src/tests/api/test_permissions.py +++ b/src/tests/api/test_permissions.py @@ -111,6 +111,12 @@ event_permission_sub_urls = [ ('put', 'can_change_event_settings', 'taxrules/1/', 404), ('patch', 'can_change_event_settings', 'taxrules/1/', 404), ('delete', 'can_change_event_settings', 'taxrules/1/', 404), + ('get', 'can_change_event_settings', 'sendmail_rules/', 200), + ('get', 'can_change_event_settings', 'sendmail_rules/1/', 404), + ('post', 'can_change_event_settings', 'sendmail_rules/', 400), + ('put', 'can_change_event_settings', 'sendmail_rules/1/', 404), + ('patch', 'can_change_event_settings', 'sendmail_rules/1/', 404), + ('delete', 'can_change_event_settings', 'sendmail_rules/1/', 404), ('get', 'can_view_vouchers', 'vouchers/', 200), ('get', 'can_view_vouchers', 'vouchers/1/', 404), ('post', 'can_change_vouchers', 'vouchers/', 201), diff --git a/src/tests/api/test_sendmail.py b/src/tests/api/test_sendmail.py new file mode 100644 index 000000000..964347374 --- /dev/null +++ b/src/tests/api/test_sendmail.py @@ -0,0 +1,276 @@ +# +# 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 . +# +# 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 +# . +# +import datetime + +import pytest +from django.utils.timezone import utc +from django_scopes import scopes_disabled + +from pretix.plugins.sendmail.models import Rule + + +@pytest.fixture +def rule(event): + return event.sendmail_rules.create(subject='test', template='foo', + send_date=datetime.datetime(2021, 7, 8, tzinfo=utc)) + + +TEST_RULE_RES = { + 'id': 1, + 'subject': {'en': 'test'}, + 'template': {'en': 'foo'}, + 'all_products': True, + 'limit_products': [], + 'include_pending': False, + 'send_date': '2021-07-08T00:00:00Z', + 'send_offset_days': None, + 'send_offset_time': None, + 'date_is_absolute': True, + 'offset_to_event_end': False, + 'offset_is_after': False, + 'send_to': 'orders', + 'enabled': True, +} + + +@pytest.mark.django_db +def test_sendmail_rule_list(token_client, organizer, event, rule): + res = dict(TEST_RULE_RES) + + res['id'] = rule.pk + + resp = token_client.get(f'/api/v1/organizers/{organizer.slug}/events/{event.slug}/sendmail_rules/') + assert resp.status_code == 200 + results = resp.data['results'] + assert res in results + assert len(results) == 1 + + produces_result = [f'id={rule.id}', 'all_products=true', 'include_pending=false', 'date_is_absolute=true', + 'offset_to_event_end=false', 'offset_is_after=false', 'send_to=orders', 'enabled=true', + f'id={rule.id}&enabled=true'] + + no_produce_result = ['id=12345', 'all_products=false', 'include_pending=true', 'date_is_absolute=false', + 'offset_to_event_end=true', 'offset_is_after=true', 'send_to=both', 'send_to=attendees', + 'enabled=false', f'id={rule.id}&enabled=false'] + + for q in produces_result: + resp = token_client.get(f'/api/v1/organizers/{organizer.slug}/events/{event.slug}/sendmail_rules/?{q}') + assert [res] == resp.data['results'] + + for q in no_produce_result: + resp = token_client.get(f'/api/v1/organizers/{organizer.slug}/events/{event.slug}/sendmail_rules/?{q}') + assert [] == resp.data['results'] + + +@pytest.mark.django_db +def test_sendmail_rule_detail(token_client, organizer, event, rule): + res = dict(TEST_RULE_RES) + res['id'] = rule.pk + + resp = token_client.get(f'/api/v1/organizers/{organizer.slug}/events/{event.slug}/sendmail_rules/{rule.pk}/') + + assert resp.status_code == 200 + assert res == resp.data + + +@scopes_disabled() +def create_rule(token_client, organizer, event, data, expected_failure=False, expected_failure_text=None): + resp = token_client.post( + f'/api/v1/organizers/{organizer.slug}/events/{event.slug}/sendmail_rules/', + data=data, format='json' + ) + if expected_failure: + assert resp.status_code == 400 + if expected_failure_text: + assert expected_failure_text in resp.content.decode(resp.charset) + else: + assert resp.status_code == 201 + with scopes_disabled(): + return Rule.objects.get(pk=resp.data['id']) + + +@scopes_disabled() +@pytest.mark.django_db +def test_sendmail_rule_create_min_fail(token_client, organizer, event): + create_rule( + token_client, organizer, event, + data={ + 'subject': {'en': 'not foobar'} + }, + expected_failure=True + ) + + +@scopes_disabled() +@pytest.mark.django_db +def test_sendmail_rule_create_minimal(token_client, organizer, event): + r = create_rule( + token_client, organizer, event, + data={ + 'subject': {'en': 'meow'}, + 'template': {'en': 'creative text here'}, + 'send_date': '2018-03-17T13:31Z', + } + ) + assert r.send_date == datetime.datetime(2018, 3, 17, 13, 31, tzinfo=utc) + + +@scopes_disabled() +@pytest.mark.django_db +def test_sendmail_rule_create_full(token_client, organizer, event, item): + r = create_rule( + token_client, organizer, event, + data={ + 'subject': {'en': 'mew'}, + 'template': {'en': 'foobar'}, + 'all_products': False, + 'limit_products': [event.items.first().pk], + 'include_pending': True, + 'send_offset_days': 3, + 'send_offset_time': '09:30', + 'date_is_absolute': False, + 'offset_to_event_end': True, + 'offset_is_after': True, + 'send_to': 'both', + 'enabled': False, + } + ) + + assert r.all_products is False + assert [i.pk for i in r.limit_products.all()] == [event.items.first().pk] + assert r.include_pending is True + assert r.send_offset_days == 3 + assert r.send_offset_time == datetime.time(9, 30) + assert r.date_is_absolute is False + assert r.offset_to_event_end is True + assert r.offset_is_after is True + assert r.send_to == 'both' + assert r.enabled is False + + +@scopes_disabled() +@pytest.mark.django_db +def test_sendmail_rule_create_invalid(token_client, organizer, event): + invalid_examples = [ + ( + { + 'subject': {'en': 'foo'}, + 'template': {'en': 'bar'}, + 'send_date': '2018-03-17T13:31Z', + 'offset_to_event_end': True, # needs explicit date_is_absolute=False and specified offset + }, + 'date_is_absolute and offset_* are mutually exclusive' + ), + ( + { + 'subject': {'en': 'foo'}, + 'template': {'en': 'bar'}, + 'send_date': '2018-03-17T13:31Z', + 'offset_is_after': True, + }, + 'date_is_absolute and offset_* are mutually exclusive' + ), + ( + { + 'subject': {'en': 'foo'}, + 'template': {'en': 'bar'}, + 'send_date': '2018-03-17T13:31Z', + 'date_is_absolute': False, + }, + 'send_offset_days and send_offset_time are required' + ), + ( + { + 'subject': {'en': 'foo'}, + 'template': {'en': 'bar'}, + 'send_date': '2018-03-17T13:31Z', + 'date_is_absolute': True, + 'offset_to_event_end': True, + 'send_offset_days': 2, + 'send_offset_time': '09:30', + }, + 'date_is_absolute and offset_* are mutually exclusive' + ), + ( + { + 'subject': {'en': 'foo'}, + 'template': {'en': 'bar'}, + }, + 'send_date is required for date_is_absolute=True' + ), + ( + { + 'subject': {'en': 'foo'}, + 'template': {'en': 'bar'}, + 'date_is_absolute': False, + 'offset_to_event_end': True, + }, + 'send_offset_days and send_offset_time are required' + ), + ( + { + 'subject': {'en': 'foo'}, + 'template': {'en': 'bar'}, + 'send_date': '2018-03-17T13:31Z', + 'date_is_absolute': False, + 'offset_is_after': True, + 'send_offset_days': 2, + }, + 'send_offset_days and send_offset_time are required' + ), + ( + { + 'subject': {'en': 'foo'}, + 'template': {'en': 'bar'}, + 'date_is_absolute': False, + 'offset_is_after': True, + 'send_offset_time': '09:30', + }, + 'send_offset_days and send_offset_time are required' + ) + ] + + for data, failure in invalid_examples: + create_rule(token_client, organizer, event, data, expected_failure=True, expected_failure_text=failure) + + +@scopes_disabled() +@pytest.mark.django_db +def test_sendmail_rule_change(token_client, organizer, event, rule): + token_client.patch( + f'/api/v1/organizers/{organizer.slug}/events/{event.slug}/sendmail_rules/{rule.pk}/', + data={'enabled': False}, format='json' + ) + + rule.refresh_from_db() + + assert rule.enabled is False + + +@scopes_disabled() +@pytest.mark.django_db +def test_sendmail_rule_delete(token_client, organizer, event, rule): + token_client.delete( + f'/api/v1/organizers/{organizer.slug}/events/{event.slug}/sendmail_rules/{rule.pk}/' + ) + + assert Rule.objects.filter(pk=rule.pk).count() == 0