Sendmail rules: Extend filter by order status (#3402)

Add new order status filter settings instead of in form and API, while keeping backwards-compatibility
This commit is contained in:
Phin Wolkwitz
2023-07-21 17:43:19 +02:00
committed by GitHub
parent 26213f2ba9
commit 4b706339ed
10 changed files with 369 additions and 47 deletions

View File

@@ -39,7 +39,7 @@ TEST_RULE_RES = {
'template': {'en': 'foo'},
'all_products': True,
'limit_products': [],
'include_pending': False,
"restrict_to_status": ['p', 'n__valid_if_pending'],
'send_date': '2021-07-08T00:00:00Z',
'send_offset_days': None,
'send_offset_time': None,
@@ -63,11 +63,11 @@ def test_sendmail_rule_list(token_client, organizer, event, rule):
assert res in results
assert len(results) == 1
produces_result = [f'id={rule.id}', 'all_products=true', 'include_pending=false', 'date_is_absolute=true',
produces_result = [f'id={rule.id}', 'all_products=true', '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',
no_produce_result = ['id=12345', 'all_products=false', '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']
@@ -144,7 +144,7 @@ def test_sendmail_rule_create_minimal(token_client, organizer, event):
data={
'subject': {'en': 'meow'},
'template': {'en': 'creative text here'},
'send_date': '2018-03-17T13:31Z',
'send_date': '2018-03-17T13:31Z'
}
)
assert r.send_date == datetime.datetime(2018, 3, 17, 13, 31, tzinfo=datetime.timezone.utc)
@@ -160,7 +160,7 @@ def test_sendmail_rule_create_full(token_client, organizer, event, item):
'template': {'en': 'foobar'},
'all_products': False,
'limit_products': [event.items.first().pk],
'include_pending': True,
"restrict_to_status": ['p', 'n__not_pending_approval_and_not_valid_if_pending', 'n__valid_if_pending'],
'send_offset_days': 3,
'send_offset_time': '09:30',
'date_is_absolute': False,
@@ -173,7 +173,7 @@ def test_sendmail_rule_create_full(token_client, organizer, event, item):
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.restrict_to_status == ['p', 'n__not_pending_approval_and_not_valid_if_pending', 'n__valid_if_pending']
assert r.send_offset_days == 3
assert r.send_offset_time == datetime.time(9, 30)
assert r.date_is_absolute is False
@@ -269,6 +269,85 @@ def test_sendmail_rule_create_invalid(token_client, organizer, event):
create_rule(token_client, organizer, event, data, expected_failure=True, expected_failure_text=failure)
@scopes_disabled()
@pytest.mark.django_db
def test_sendmail_rule_legacy_field(token_client, organizer, event, rule):
r = create_rule(
token_client, organizer, event,
data={
'subject': {'en': 'meow'},
'template': {'en': 'creative text here'},
'send_date': '2018-03-17T13:31Z',
'include_pending': True
}
)
assert r.restrict_to_status == ['p', 'n__not_pending_approval_and_not_valid_if_pending', 'n__valid_if_pending']
r = create_rule(
token_client, organizer, event,
data={
'subject': {'en': 'meow'},
'template': {'en': 'creative text here'},
'send_date': '2018-03-17T13:31Z',
'include_pending': False
}
)
assert r.restrict_to_status == ['p', 'n__valid_if_pending']
@scopes_disabled()
@pytest.mark.django_db
def test_sendmail_rule_restrict_recipients(token_client, organizer, event, rule):
restrictions = ['p', 'e', 'c', 'n__not_pending_approval_and_not_valid_if_pending',
'n__pending_approval', 'n__valid_if_pending', 'n__pending_overdue']
for r in restrictions:
result = create_rule(
token_client, organizer, event,
data={
'subject': {'en': 'meow'},
'template': {'en': 'creative text here'},
'send_date': '2018-03-17T13:31Z',
"restrict_to_status": [r],
},
expected_failure=False
)
assert result.restrict_to_status == [r]
create_rule(
token_client, organizer, event,
data={
'subject': {'en': 'meow'},
'template': {'en': 'creative text here'},
'send_date': '2018-03-17T13:31Z',
"restrict_to_status": ["foo"],
},
expected_failure=True,
expected_failure_text="restrict_to_status may only include valid states"
)
create_rule(
token_client, organizer, event,
data={
'subject': {'en': 'meow'},
'template': {'en': 'creative text here'},
'send_date': '2018-03-17T13:31Z',
"restrict_to_status": [],
},
expected_failure=True,
expected_failure_text="restrict_to_status needs at least one value"
)
create_rule(
token_client, organizer, event,
data={
'subject': {'en': 'meow'},
'template': {'en': 'creative text here'},
'send_date': '2018-03-17T13:31Z',
},
expected_failure=False
)
@scopes_disabled()
@pytest.mark.django_db
def test_sendmail_rule_change(token_client, organizer, event, rule):

View File

@@ -276,12 +276,127 @@ def test_sendmail_rule_send_correct_products(event, order, item, item2):
assert djmail.outbox[0].to[0] == p1.attendee_email
@pytest.mark.django_db
@scopes_disabled()
def run_restriction_test(event, order, restrictions_pass=[], restrictions_fail=[]):
for r in restrictions_pass:
djmail.outbox = []
event.sendmail_rules.create(send_date=dt_now - datetime.timedelta(hours=1), restrict_to_status=[r],
subject='meow', template='meow meow meow')
sendmail_run_rules(None)
assert len(djmail.outbox) == 1, f"email not sent for {r}"
for r in restrictions_fail:
djmail.outbox = []
event.sendmail_rules.create(send_date=dt_now - datetime.timedelta(hours=1), restrict_to_status=[r],
subject='meow', template='meow meow meow')
sendmail_run_rules(None)
assert len(djmail.outbox) == 0, f"email sent for {r} unexpectedly"
@pytest.mark.django_db
@scopes_disabled()
def test_sendmail_rule_restrictions_status_paid(event, order):
order.status = Order.STATUS_PAID
order.save()
order.valid_if_pending = False
restrictions_pass = ['p']
restrictions_fail = ['e', 'c', 'n__not_pending_approval_and_not_valid_if_pending', 'n__pending_approval',
'n__valid_if_pending', 'n__pending_overdue']
run_restriction_test(event, order, restrictions_pass, restrictions_fail)
@pytest.mark.django_db
@scopes_disabled()
def test_sendmail_rule_restrictions_status_pending(event, order):
order.status = Order.STATUS_PENDING
order.require_approval = False
order.valid_if_pending = False
order.save()
restrictions_pass = ['n__not_pending_approval_and_not_valid_if_pending']
restrictions_fail = ['p', 'e', 'c', 'n__pending_approval', "n__valid_if_pending", 'n__pending_overdue']
run_restriction_test(event, order, restrictions_pass, restrictions_fail)
@pytest.mark.django_db
@scopes_disabled()
def test_sendmail_rule_restrictions_status_valid_pending(event, order):
order.status = Order.STATUS_PENDING
order.require_approval = False
order.valid_if_pending = True
order.save()
restrictions_pass = ["n__valid_if_pending"]
restrictions_fail = ['p', 'e', 'c', 'n__not_pending_approval_and_not_valid_if_pending', 'n__pending_approval',
'n__pending_overdue']
run_restriction_test(event, order, restrictions_pass, restrictions_fail)
@pytest.mark.django_db
@scopes_disabled()
def test_sendmail_rule_restrictions_status_approval_pending(event, order):
order.status = Order.STATUS_PENDING
order.require_approval = True
order.valid_if_pending = False
order.save()
restrictions_pass = ['n__pending_approval']
restrictions_fail = ['p', 'e', 'c', 'n__not_pending_approval_and_not_valid_if_pending', "n__valid_if_pending",
'n__pending_overdue']
run_restriction_test(event, order, restrictions_pass, restrictions_fail)
@pytest.mark.django_db
@scopes_disabled()
def test_sendmail_rule_restrictions_status_overdue_pending(event, order):
event.settings.payment_term_expire_automatically = False
order.status = Order.STATUS_PENDING
order.require_approval = False
order.valid_if_pending = False
order.expires = order.expires - datetime.timedelta(days=15)
order.save()
restrictions_pass = ['n__pending_overdue', 'n__not_pending_approval_and_not_valid_if_pending']
restrictions_fail = ['p', 'e', 'c', 'n__pending_approval', "n__valid_if_pending"]
run_restriction_test(event, order, restrictions_pass, restrictions_fail)
@pytest.mark.django_db
@scopes_disabled()
def test_sendmail_rule_restrictions_status_expired(event, order):
order.status = Order.STATUS_EXPIRED
order.save()
restrictions_pass = ['e']
restrictions_fail = ['p', 'c', 'n__not_pending_approval_and_not_valid_if_pending', 'n__pending_approval',
'n__valid_if_pending', 'n__pending_overdue']
run_restriction_test(event, order, restrictions_pass, restrictions_fail)
@pytest.mark.django_db
@scopes_disabled()
def test_sendmail_rule_restrictions_status_canceled(event, order):
order.status = Order.STATUS_CANCELED
order.save()
restrictions_pass = ['c']
restrictions_fail = ['p', 'e', 'n__not_pending_approval_and_not_valid_if_pending', 'n__pending_approval',
'n__valid_if_pending', 'n__pending_overdue']
run_restriction_test(event, order, restrictions_pass, restrictions_fail)
@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,
event.sendmail_rules.create(send_date=dt_now - datetime.timedelta(hours=1),
restrict_to_status=['p', 'n__not_pending_approval_and_not_valid_if_pending',
'n__valid_if_pending'],
subject='meow', template='meow meow meow')
sendmail_run_rules(None)
@@ -294,7 +409,8 @@ def test_sendmail_rule_send_order_pending(event, order):
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,
event.sendmail_rules.create(send_date=dt_now - datetime.timedelta(hours=1),
restrict_to_status=['p', "n__valid_if_pending"],
subject='meow', template='meow meow meow')
sendmail_run_rules(None)
@@ -310,7 +426,8 @@ def test_sendmail_rule_send_order_valid_if_pending(event, order):
order.save()
djmail.outbox = []
event.sendmail_rules.create(send_date=dt_now - datetime.timedelta(hours=1), include_pending=False,
event.sendmail_rules.create(send_date=dt_now - datetime.timedelta(hours=1),
restrict_to_status=['p', "n__valid_if_pending"],
subject='meow', template='meow meow meow')
sendmail_run_rules(None)
@@ -330,7 +447,8 @@ def test_sendmail_rule_send_order_status(status, event, order):
order.status = status
order.save()
event.sendmail_rules.create(send_date=dt_now - datetime.timedelta(hours=1), include_pending=True,
event.sendmail_rules.create(send_date=dt_now - datetime.timedelta(hours=1),
restrict_to_status=['p', "n__valid_if_pending"],
subject='meow', template='meow meow meow')
sendmail_run_rules(None)
@@ -346,7 +464,9 @@ def test_sendmail_rule_send_order_approval(event, order):
order.require_approval = True
order.save()
event.sendmail_rules.create(send_date=dt_now - datetime.timedelta(hours=1), include_pending=True,
event.sendmail_rules.create(send_date=dt_now - datetime.timedelta(hours=1),
restrict_to_status=['p', 'n__not_pending_approval_and_not_valid_if_pending',
'n__valid_if_pending'],
subject='meow', template='meow meow meow')
sendmail_run_rules(None)
@@ -359,7 +479,9 @@ def test_sendmail_rule_send_order_approval(event, order):
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,
event.sendmail_rules.create(send_date=dt_now - datetime.timedelta(hours=1),
restrict_to_status=['p', 'n__not_pending_approval_and_not_valid_if_pending',
'n__valid_if_pending'],
subject='meow', template='meow meow meow')
sendmail_run_rules(None)
@@ -375,7 +497,9 @@ def test_sendmail_rule_only_live(event, order):
event.live = False
event.save()
event.sendmail_rules.create(send_date=dt_now - datetime.timedelta(hours=1), include_pending=True,
event.sendmail_rules.create(send_date=dt_now - datetime.timedelta(hours=1),
restrict_to_status=['p', 'n__not_pending_approval_and_not_valid_if_pending',
'n__valid_if_pending'],
subject='meow', template='meow meow meow')
sendmail_run_rules(None)
@@ -386,7 +510,9 @@ def test_sendmail_rule_only_live(event, order):
@scopes_disabled()
def test_sendmail_rule_disabled(event, order):
djmail.outbox = []
event.sendmail_rules.create(send_date=dt_now - datetime.timedelta(hours=1), include_pending=True,
event.sendmail_rules.create(send_date=dt_now - datetime.timedelta(hours=1),
restrict_to_status=['p', 'n__not_pending_approval_and_not_valid_if_pending',
'n__valid_if_pending'],
subject='meow', template='meow meow meow', enabled=False)
sendmail_run_rules(None)
@@ -405,7 +531,9 @@ def test_sendmail_context_localization(event, order, pos):
)
djmail.outbox = []
event.sendmail_rules.create(send_date=dt_now - datetime.timedelta(hours=1), include_pending=True,
event.sendmail_rules.create(send_date=dt_now - datetime.timedelta(hours=1),
restrict_to_status=['p', 'n__not_pending_approval_and_not_valid_if_pending',
'n__valid_if_pending'],
subject='meow', template='Hallo {name_for_salutation}')
sendmail_run_rules(None)