-
Notifications
You must be signed in to change notification settings - Fork 429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Include free plans for api use notifications #4204
Merged
zachaysan
merged 9 commits into
main
from
fix/include_free_plans_for_api_use_notifications
Jun 20, 2024
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
30af060
Add free plans to notifications for api use
zachaysan 81bee32
Add test for free plan api use notifications
zachaysan 02b3de2
Merge branch 'main' into fix/include_free_plans_for_api_use_notificat…
zachaysan 86f9220
Fix grammar in tests
zachaysan 736742c
Fix grammar in templates
zachaysan fbf3dc0
Move logger testing fixture to conftest since it's usable multiple pl…
zachaysan c01e4c3
Fix devious bug that would have been missed were it not for codecov
zachaysan dedf8ae
Create test for missing info caches
zachaysan f491c2f
Update line number since the fixture was moved to conftest
zachaysan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,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 +291,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, | ||
|
@@ -382,6 +385,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, | ||
|
@@ -469,6 +474,94 @@ def test_handle_api_usage_notifications_above_100( | |
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 organisations 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 organisations 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_charge_for_api_call_count_overages_scale_up( | ||
organisation: Organisation, | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok no problem. I've fixed the other invocations as well in the other tests.