diff --git a/api/organisations/task_helpers.py b/api/organisations/task_helpers.py
index 65d382358615..2466e3c4d377 100644
--- a/api/organisations/task_helpers.py
+++ b/api/organisations/task_helpers.py
@@ -26,7 +26,10 @@ def send_api_flags_blocked_notification(organisation: Organisation) -> None:
userorganisation__organisation=organisation,
)
- context = {"organisation": organisation}
+ context = {
+ "organisation": organisation,
+ "grace_period": not hasattr(organisation, "breached_grace_period"),
+ }
message = "organisations/api_flags_blocked_notification.txt"
html_message = "organisations/api_flags_blocked_notification.html"
diff --git a/api/organisations/templates/organisations/api_flags_blocked_notification.html b/api/organisations/templates/organisations/api_flags_blocked_notification.html
index 48dfac9de004..03fdfaa16897 100644
--- a/api/organisations/templates/organisations/api_flags_blocked_notification.html
+++ b/api/organisations/templates/organisations/api_flags_blocked_notification.html
@@ -9,7 +9,7 @@
- This is a system generated notification related to your Flagsmith API Usage. As per previous warnings, we have had to block your company {{ organisation.name }} after the 7 day grace period. Flags are not currently being served for your organization, and will continue to be blocked until your billing period resets or you upgrade your account. You can upgrade your account at app.flagsmith.com.
+ This is a system generated notification related to your Flagsmith API Usage. As per previous warnings, we have had to block your company {{ organisation.name }}{% if grace_period %} after the 7 day grace period{% endif %}. Flags are not currently being served for your organization, and will continue to be blocked until your billing period resets or you upgrade your account. You can upgrade your account at app.flagsmith.com.
|
diff --git a/api/organisations/templates/organisations/api_flags_blocked_notification.txt b/api/organisations/templates/organisations/api_flags_blocked_notification.txt
index 200c009ae1ff..12d4b624efba 100644
--- a/api/organisations/templates/organisations/api_flags_blocked_notification.txt
+++ b/api/organisations/templates/organisations/api_flags_blocked_notification.txt
@@ -1,6 +1,6 @@
Hi there,
-This is a system generated notification related to your Flagsmith API Usage. As per previous warnings, we have had to block your company {{ organisation.name }} after the 7 day grace period. Flags are not currently being served for your organization, and will continue to be blocked until your billing period resets or you upgrade your account. You can upgrade your account at app.flagsmith.com.
+This is a system generated notification related to your Flagsmith API Usage. As per previous warnings, we have had to block your company {{ organisation.name }}{% if grace_period %} after the 7 day grace period{% endif %}. Flags are not currently being served for your organization, and will continue to be blocked until your billing period resets or you upgrade your account. You can upgrade your account at app.flagsmith.com.
Thank you!
diff --git a/api/tests/unit/organisations/test_unit_organisations_tasks.py b/api/tests/unit/organisations/test_unit_organisations_tasks.py
index 4d1a38dd9520..b59e4633144f 100644
--- a/api/tests/unit/organisations/test_unit_organisations_tasks.py
+++ b/api/tests/unit/organisations/test_unit_organisations_tasks.py
@@ -1284,6 +1284,7 @@ def test_restrict_use_due_to_api_limit_grace_period_over(
organisation3 = Organisation.objects.create(name="Org #3")
organisation4 = Organisation.objects.create(name="Org #4")
organisation5 = Organisation.objects.create(name="Org #5")
+ organisation6 = Organisation.objects.create(name="Org #6")
for org in [
organisation,
@@ -1291,6 +1292,7 @@ def test_restrict_use_due_to_api_limit_grace_period_over(
organisation3,
organisation4,
organisation5,
+ organisation6,
]:
OrganisationSubscriptionInformationCache.objects.create(
organisation=org,
@@ -1309,7 +1311,13 @@ def test_restrict_use_due_to_api_limit_grace_period_over(
mock_api_usage.return_value = 12_005
# Add users to test email delivery
- for org in [organisation2, organisation3, organisation4, organisation5]:
+ for org in [
+ organisation2,
+ organisation3,
+ organisation4,
+ organisation5,
+ organisation6,
+ ]:
admin_user.add_organisation(org, role=OrganisationRole.ADMIN)
staff_user.add_organisation(org, role=OrganisationRole.USER)
@@ -1358,6 +1366,15 @@ def test_restrict_use_due_to_api_limit_grace_period_over(
percent_usage=120,
)
+ # Should be immediately blocked because they've previously breached the grace
+ # period
+ OrganisationAPIUsageNotification.objects.create(
+ notified_at=now,
+ organisation=organisation6,
+ percent_usage=120,
+ )
+ OrganisationBreachedGracePeriod.objects.create(organisation=organisation6)
+
# When
restrict_use_due_to_api_limit_grace_period_over()
@@ -1367,6 +1384,7 @@ def test_restrict_use_due_to_api_limit_grace_period_over(
organisation3.refresh_from_db()
organisation4.refresh_from_db()
organisation5.refresh_from_db()
+ organisation6.refresh_from_db()
# Organisation without breaching 100 percent usage is ok.
assert organisation3.stop_serving_flags is False
@@ -1390,6 +1408,9 @@ def test_restrict_use_due_to_api_limit_grace_period_over(
assert organisation2.stop_serving_flags is True
assert organisation2.block_access_to_admin is True
assert organisation2.api_limit_access_block
+ assert organisation6.stop_serving_flags is True
+ assert organisation6.block_access_to_admin is True
+ assert organisation6.api_limit_access_block
client_mock.get_identity_flags.call_args_list == [
call(
@@ -1406,9 +1427,16 @@ def test_restrict_use_due_to_api_limit_grace_period_over(
"subscription.plan": organisation2.subscription.plan,
},
),
+ call(
+ f"org.{organisation6.id}",
+ traits={
+ "organisation_id": organisation6.id,
+ "subscription.plan": organisation6.subscription.plan,
+ },
+ ),
]
- assert len(mailoutbox) == 2
+ assert len(mailoutbox) == 3
email1 = mailoutbox[0]
assert email1.subject == "Flagsmith API use has been blocked due to overuse"
assert email1.body == render_to_string(
@@ -1428,11 +1456,24 @@ def test_restrict_use_due_to_api_limit_grace_period_over(
assert email2.alternatives[0][0] == render_to_string(
"organisations/api_flags_blocked_notification.html",
- context={"organisation": organisation2},
+ context={"organisation": organisation2, "grace_period": False},
)
assert email2.from_email == "noreply@flagsmith.com"
assert email2.to == ["admin@example.com", "staff@example.com"]
+ email3 = mailoutbox[2]
+ assert email3.subject == "Flagsmith API use has been blocked due to overuse"
+ assert len(email3.alternatives) == 1
+ assert len(email3.alternatives[0]) == 2
+ assert email3.alternatives[0][1] == "text/html"
+
+ assert email3.alternatives[0][0] == render_to_string(
+ "organisations/api_flags_blocked_notification.html",
+ context={"organisation": organisation6, "grace_period": False},
+ )
+ assert email3.from_email == "noreply@flagsmith.com"
+ assert email3.to == ["admin@example.com", "staff@example.com"]
+
# Organisations that change their subscription are unblocked.
organisation.subscription.plan = "scale-up-v2"
organisation.subscription.save()