-
Notifications
You must be signed in to change notification settings - Fork 429
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Remove grace period where necessary from blocked notification (#…
…4496) Co-authored-by: Matthew Elwell <[email protected]>
- Loading branch information
1 parent
6ba44f8
commit 9bae21c
Showing
4 changed files
with
50 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
api/organisations/templates/organisations/api_flags_blocked_notification.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -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 == "[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() | ||
|