Skip to content

Commit 301dfd2

Browse files
committed
Allow customisation of expiry email notification schedule
1 parent 221bd62 commit 301dfd2

File tree

5 files changed

+21
-4
lines changed

5 files changed

+21
-4
lines changed

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "RCTab"
3-
version = "1.3.0"
3+
version = "1.3.1"
44
description = "The RCTab API. Manage Azure budgets and usage"
55
authors = []
66

rctab/routers/accounting/send_emails.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,8 @@ def should_send_expiry_email(
364364
one in that period. This is to allow for emails not being sent (e.g. if
365365
the email service goes down). We don't send emails before this
366366
period (shown as ~) or on the day of expiry (denoted by x). We send daily
367-
emails to active subscriptions after expiry (marked with =).
367+
emails to active subscriptions after expiry (marked with =). The exact
368+
date values of | are customisable via the settings module.
368369
369370
Args:
370371
date_of_expiry: Expiry date of a subscription.
@@ -386,7 +387,9 @@ def should_send_expiry_email(
386387
else:
387388
return False
388389

389-
for days_remaining in (1, 7, 30):
390+
# Not technically a frequency, more of a schedule.
391+
expiry_email_schedule: List[int] = get_settings().expiry_email_freq
392+
for days_remaining in expiry_email_schedule:
390393
if date_of_expiry <= date.today() + timedelta(days=days_remaining):
391394
if not date_of_last_email:
392395
return True

rctab/settings.py

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class Settings(BaseSettings):
2626
notifiable_roles: List[str] = ["Contributor"] # Roles to email about a subscription
2727
roles_filter: List[str] = ["Contributor"] # Send emails if one of these changes
2828
admin_email_recipients: List[str] = [] # Recipients of admin emails
29+
expiry_email_freq: List[int] = [1, 7, 30] # Days before expiry to send emails
2930

3031
# Org name, for emails and frontend
3132
organisation: Optional[str] = "My organisation"

tests/test_routes/test_send_emails.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,7 @@ async def test_expiry_looming_doesnt_resend(
920920
mock_send.assert_not_called()
921921

922922

923-
def test_should_send_expiry_email() -> None:
923+
def test_should_send_expiry_email(mocker: MockerFixture) -> None:
924924
# pylint: disable=using-constant-test
925925
# pylint: disable=invalid-name
926926

@@ -1049,6 +1049,17 @@ def test_should_send_expiry_email() -> None:
10491049
)
10501050
is True
10511051
)
1052+
# Try again but having changed the settings
1053+
mock_get_settings = mocker.patch(
1054+
"rctab.routers.accounting.send_emails.get_settings"
1055+
)
1056+
mock_get_settings.return_value.expiry_email_freq = []
1057+
assert (
1058+
send_emails.should_send_expiry_email(
1059+
date_of_expiry, date_of_last_email, SubscriptionState.ENABLED
1060+
)
1061+
is False
1062+
)
10521063

10531064
if False:
10541065
# Visualise!

tests/test_settings.py

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def test_minimal_settings() -> None:
3535
assert settings.notifiable_roles == ["Contributor"]
3636
assert settings.roles_filter == ["Contributor"]
3737
assert settings.admin_email_recipients == []
38+
assert settings.expiry_email_freq == [1, 7, 30]
3839

3940

4041
def test_settings_raises() -> None:
@@ -71,6 +72,7 @@ def test_maximal_settings() -> None:
7172
status_func_public_key="2345",
7273
controller_func_public_key="1234",
7374
admin_email_recipients=["[email protected]"],
75+
expiry_email_freq=[1, 7, 30],
7476
# To stop any local .env files influencing the test
7577
_env_file=None,
7678
)

0 commit comments

Comments
 (0)