-
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: Include free plans for api use notifications (#4204)
- Loading branch information
Showing
8 changed files
with
211 additions
and
48 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
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_usage_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
2 changes: 1 addition & 1 deletion
2
api/organisations/templates/organisations/api_usage_notification_limit.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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import logging | ||
import uuid | ||
from datetime import timedelta | ||
from unittest.mock import MagicMock, call | ||
|
@@ -25,6 +26,7 @@ | |
FREE_PLAN_ID, | ||
MAX_API_CALLS_IN_FREE_PLAN, | ||
MAX_SEATS_IN_FREE_PLAN, | ||
SCALE_UP, | ||
) | ||
from organisations.subscriptions.xero.metadata import XeroSubscriptionMetadata | ||
from organisations.tasks import ( | ||
|
@@ -290,6 +292,8 @@ def test_handle_api_usage_notifications_below_100( | |
) -> None: | ||
# Given | ||
now = timezone.now() | ||
organisation.subscription.plan = SCALE_UP | ||
organisation.subscription.save() | ||
OrganisationSubscriptionInformationCache.objects.create( | ||
organisation=organisation, | ||
allowed_seats=10, | ||
|
@@ -324,7 +328,7 @@ def test_handle_api_usage_notifications_below_100( | |
assert email.body == ( | ||
"Hi there,\n\nThe API usage for Test Org has reached " | ||
"90% within the current subscription period. Please " | ||
"consider upgrading your organisations account limits.\n\n" | ||
"consider upgrading your organisation's account limits.\n\n" | ||
"Thank you!\n\nThe Flagsmith Team\n" | ||
) | ||
|
||
|
@@ -338,7 +342,7 @@ def test_handle_api_usage_notifications_below_100( | |
"<tr>\n\n <td>\n " | ||
"The API usage for Test Org has reached\n " | ||
"90% within the current subscription period.\n " | ||
"Please consider upgrading your organisations account limits.\n" | ||
"Please consider upgrading your organisation's account limits.\n" | ||
" </td>\n\n\n </tr>\n\n " | ||
"<tr>\n\n <td>Thank you!</td>\n\n " | ||
" </tr>\n\n <tr>\n\n " | ||
|
@@ -382,6 +386,8 @@ def test_handle_api_usage_notifications_above_100( | |
) -> None: | ||
# Given | ||
now = timezone.now() | ||
organisation.subscription.plan = SCALE_UP | ||
organisation.subscription.save() | ||
OrganisationSubscriptionInformationCache.objects.create( | ||
organisation=organisation, | ||
allowed_seats=10, | ||
|
@@ -417,7 +423,95 @@ def test_handle_api_usage_notifications_above_100( | |
assert email.body == ( | ||
"Hi there,\n\nThe API usage for Test Org has breached " | ||
"100% within the current subscription period. Please " | ||
"upgrade your organisations account to ensure " | ||
"upgrade your organisation's account to ensure " | ||
"continued service.\n\nThank you!\n\n" | ||
"The Flagsmith Team\n" | ||
) | ||
|
||
assert len(email.alternatives) == 1 | ||
assert len(email.alternatives[0]) == 2 | ||
assert email.alternatives[0][1] == "text/html" | ||
|
||
assert email.alternatives[0][0] == ( | ||
"<table>\n\n <tr>\n\n <td>Hi " | ||
"there,</td>\n\n </tr>\n\n <tr>\n\n " | ||
" <td>\n The API usage for Test Org " | ||
"has breached\n 100% within the " | ||
"current subscription period.\n " | ||
"Please upgrade your organisation's account to ensure " | ||
"continued service.\n </td>\n\n\n " | ||
" </tr>\n\n <tr>\n\n <td>" | ||
"Thank you!</td>\n\n </tr>\n\n <tr>\n\n" | ||
" <td>The Flagsmith Team</td>\n\n " | ||
"</tr>\n\n</table>\n" | ||
) | ||
|
||
assert email.from_email == "[email protected]" | ||
# Extra staff included because threshold is over 100. | ||
assert email.to == ["[email protected]", "[email protected]"] | ||
|
||
assert ( | ||
OrganisationAPIUsageNotification.objects.filter( | ||
organisation=organisation, | ||
).count() | ||
== 1 | ||
) | ||
api_usage_notification = OrganisationAPIUsageNotification.objects.filter( | ||
organisation=organisation, | ||
).first() | ||
|
||
assert api_usage_notification.percent_usage == 100 | ||
|
||
# Now re-run the usage to make sure the notification isn't resent. | ||
handle_api_usage_notifications() | ||
|
||
assert ( | ||
OrganisationAPIUsageNotification.objects.filter( | ||
organisation=organisation, | ||
).count() | ||
== 1 | ||
) | ||
|
||
assert OrganisationAPIUsageNotification.objects.first() == api_usage_notification | ||
|
||
|
||
@pytest.mark.freeze_time("2023-01-19T09:09:47.325132+00:00") | ||
def test_handle_api_usage_notifications_for_free_accounts( | ||
mocker: MockerFixture, | ||
organisation: Organisation, | ||
mailoutbox: list[EmailMultiAlternatives], | ||
) -> None: | ||
# Given | ||
assert organisation.subscription.is_free_plan | ||
assert organisation.subscription.max_api_calls == MAX_API_CALLS_IN_FREE_PLAN | ||
|
||
mock_api_usage = mocker.patch( | ||
"organisations.tasks.get_current_api_usage", | ||
) | ||
mock_api_usage.return_value = MAX_API_CALLS_IN_FREE_PLAN + 5_000 | ||
|
||
get_client_mock = mocker.patch("organisations.tasks.get_client") | ||
client_mock = MagicMock() | ||
get_client_mock.return_value = client_mock | ||
client_mock.get_identity_flags.return_value.is_feature_enabled.return_value = True | ||
|
||
assert not OrganisationAPIUsageNotification.objects.filter( | ||
organisation=organisation, | ||
).exists() | ||
|
||
# When | ||
handle_api_usage_notifications() | ||
|
||
# Then | ||
mock_api_usage.assert_called_once_with(organisation.id, "-30d") | ||
|
||
assert len(mailoutbox) == 1 | ||
email = mailoutbox[0] | ||
assert email.subject == "Flagsmith API use has reached 100%" | ||
assert email.body == ( | ||
"Hi there,\n\nThe API usage for Test Org has breached " | ||
"100% within the current subscription period. Please " | ||
"upgrade your organisation's account to ensure " | ||
"continued service.\n\nThank you!\n\n" | ||
"The Flagsmith Team\n" | ||
) | ||
|
@@ -432,7 +526,7 @@ def test_handle_api_usage_notifications_above_100( | |
" <td>\n The API usage for Test Org " | ||
"has breached\n 100% within the " | ||
"current subscription period.\n " | ||
"Please upgrade your organisations account to ensure " | ||
"Please upgrade your organisation's account to ensure " | ||
"continued service.\n </td>\n\n\n " | ||
" </tr>\n\n <tr>\n\n <td>" | ||
"Thank you!</td>\n\n </tr>\n\n <tr>\n\n" | ||
|
@@ -469,6 +563,51 @@ def test_handle_api_usage_notifications_above_100( | |
assert OrganisationAPIUsageNotification.objects.first() == api_usage_notification | ||
|
||
|
||
def test_handle_api_usage_notifications_missing_info_cache( | ||
mocker: MockerFixture, | ||
organisation: Organisation, | ||
mailoutbox: list[EmailMultiAlternatives], | ||
inspecting_handler: logging.Handler, | ||
) -> None: | ||
# Given | ||
organisation.subscription.plan = SCALE_UP | ||
organisation.subscription.save() | ||
|
||
from organisations.tasks import logger | ||
|
||
logger.addHandler(inspecting_handler) | ||
|
||
assert organisation.has_subscription_information_cache() is False | ||
|
||
mock_api_usage = mocker.patch( | ||
"organisations.tasks.get_current_api_usage", | ||
) | ||
|
||
get_client_mock = mocker.patch("organisations.tasks.get_client") | ||
client_mock = MagicMock() | ||
get_client_mock.return_value = client_mock | ||
client_mock.get_identity_flags.return_value.is_feature_enabled.return_value = True | ||
|
||
assert not OrganisationAPIUsageNotification.objects.filter( | ||
organisation=organisation, | ||
).exists() | ||
|
||
# When | ||
handle_api_usage_notifications() | ||
|
||
# Then | ||
mock_api_usage.assert_not_called() | ||
|
||
assert len(mailoutbox) == 0 | ||
assert not OrganisationAPIUsageNotification.objects.filter( | ||
organisation=organisation, | ||
).exists() | ||
|
||
assert inspecting_handler.messages == [ | ||
f"Paid organisation {organisation.id} is missing subscription information cache" | ||
] | ||
|
||
|
||
@pytest.mark.freeze_time("2023-01-19T09:09:47.325132+00:00") | ||
def test_charge_for_api_call_count_overages_scale_up( | ||
organisation: Organisation, | ||
|
Oops, something went wrong.