Skip to content

Commit

Permalink
fix: Remove grace period where necessary from blocked notification (#…
Browse files Browse the repository at this point in the history
…4496)

Co-authored-by: Matthew Elwell <[email protected]>
  • Loading branch information
zachaysan and matthewelwell authored Aug 15, 2024
1 parent 6ba44f8 commit 9bae21c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
5 changes: 4 additions & 1 deletion api/organisations/task_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<tr>

<td>
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 <a href="app.flagsmith.com">app.flagsmith.com</a>.
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 <a href="app.flagsmith.com">app.flagsmith.com</a>.
</td>


Expand Down
Original file line number Diff line number Diff line change
@@ -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!

Expand Down
47 changes: 44 additions & 3 deletions api/tests/unit/organisations/test_unit_organisations_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1284,13 +1284,15 @@ 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,
organisation2,
organisation3,
organisation4,
organisation5,
organisation6,
]:
OrganisationSubscriptionInformationCache.objects.create(
organisation=org,
Expand All @@ -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)

Expand Down Expand Up @@ -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()

Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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 == "[email protected]"
assert email2.to == ["[email protected]", "[email protected]"]

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 == "[email protected]"
assert email3.to == ["[email protected]", "[email protected]"]

# Organisations that change their subscription are unblocked.
organisation.subscription.plan = "scale-up-v2"
organisation.subscription.save()
Expand Down

0 comments on commit 9bae21c

Please sign in to comment.