-
Notifications
You must be signed in to change notification settings - Fork 415
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Create API usage notification butter bar (#3698)
- Loading branch information
Showing
7 changed files
with
219 additions
and
4 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
from datetime import datetime | ||
from datetime import datetime, timedelta | ||
from unittest import mock | ||
|
||
import pytest | ||
from django.conf import settings | ||
from django.utils import timezone | ||
from pytest_mock import MockerFixture | ||
from rest_framework.test import override_settings | ||
|
||
from environments.models import Environment | ||
from organisations.chargebee.metadata import ChargebeeObjMetadata | ||
from organisations.models import ( | ||
OranisationAPIUsageNotification, | ||
Organisation, | ||
OrganisationSubscriptionInformationCache, | ||
Subscription, | ||
|
@@ -533,3 +535,40 @@ def test_organisation_subscription_get_api_call_overage( | |
|
||
# Then | ||
assert overage == expected_overage | ||
|
||
|
||
def test_reset_of_api_notifications(organisation: Organisation) -> None: | ||
# Given | ||
now = timezone.now() | ||
osic = OrganisationSubscriptionInformationCache.objects.create( | ||
organisation=organisation, | ||
allowed_seats=10, | ||
allowed_projects=3, | ||
allowed_30d_api_calls=100, | ||
chargebee_email="[email protected]", | ||
current_billing_term_starts_at=now - timedelta(days=45), | ||
current_billing_term_ends_at=now + timedelta(days=320), | ||
) | ||
|
||
# Create a notification which should be deleted shortly. | ||
OranisationAPIUsageNotification.objects.create( | ||
organisation=organisation, | ||
percent_usage=90, | ||
notified_at=now, | ||
) | ||
|
||
# Keep a notification which should not be deleted. | ||
organisation2 = Organisation.objects.create(name="Test org2") | ||
oapiun = OranisationAPIUsageNotification.objects.create( | ||
organisation=organisation2, | ||
percent_usage=90, | ||
notified_at=now, | ||
) | ||
|
||
# When | ||
osic.allowed_30d_api_calls *= 2 | ||
osic.save() | ||
|
||
# Then | ||
assert OranisationAPIUsageNotification.objects.count() == 1 | ||
assert OranisationAPIUsageNotification.objects.first() == oapiun |
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 @@ | |
from organisations.chargebee.metadata import ChargebeeObjMetadata | ||
from organisations.invites.models import Invite | ||
from organisations.models import ( | ||
OranisationAPIUsageNotification, | ||
Organisation, | ||
OrganisationRole, | ||
OrganisationSubscriptionInformationCache, | ||
|
@@ -1679,3 +1680,120 @@ def test_user_from_another_organisation_cannot_list_group_summaries( | |
|
||
# Then | ||
assert response.status_code == status.HTTP_403_FORBIDDEN | ||
|
||
|
||
def test_defaults_to_empty_api_notifications_when_no_subscription_information_cache( | ||
staff_client: APIClient, | ||
organisation: Organisation, | ||
) -> None: | ||
# Given | ||
url = reverse( | ||
"api-v1:organisations:organisation-api-usage-notification", | ||
args=[organisation.id], | ||
) | ||
|
||
now = timezone.now() | ||
OranisationAPIUsageNotification.objects.create( | ||
organisation=organisation, | ||
percent_usage=90, | ||
notified_at=now, | ||
) | ||
|
||
assert hasattr(organisation, "subscription_information_cache") is False | ||
|
||
# When | ||
response = staff_client.get(url) | ||
|
||
# Then | ||
# There are no results even if there is a notification because | ||
# the information cache can't provide an estimate as to API usage. | ||
assert response.status_code == status.HTTP_200_OK | ||
assert response.data["results"] == [] | ||
|
||
|
||
@pytest.mark.freeze_time("2023-01-19T09:09:47.325132+00:00") | ||
def test_retrieves_api_usage_notifications( | ||
staff_client: APIClient, | ||
organisation: Organisation, | ||
) -> None: | ||
# Given | ||
url = reverse( | ||
"api-v1:organisations:organisation-api-usage-notification", | ||
args=[organisation.id], | ||
) | ||
|
||
now = timezone.now() | ||
OrganisationSubscriptionInformationCache.objects.create( | ||
organisation=organisation, | ||
allowed_seats=10, | ||
allowed_projects=3, | ||
allowed_30d_api_calls=100, | ||
chargebee_email="[email protected]", | ||
current_billing_term_starts_at=now - timedelta(days=45), | ||
current_billing_term_ends_at=now + timedelta(days=320), | ||
) | ||
|
||
# Add three notifications, but we only get the 100% one. | ||
OranisationAPIUsageNotification.objects.create( | ||
organisation=organisation, | ||
percent_usage=90, | ||
notified_at=now, | ||
) | ||
OranisationAPIUsageNotification.objects.create( | ||
organisation=organisation, | ||
percent_usage=75, | ||
notified_at=now, | ||
) | ||
OranisationAPIUsageNotification.objects.create( | ||
organisation=organisation, | ||
percent_usage=100, | ||
notified_at=now, | ||
) | ||
|
||
# When | ||
response = staff_client.get(url) | ||
|
||
# Then | ||
assert response.status_code == status.HTTP_200_OK | ||
|
||
assert len(response.data["results"]) == 1 | ||
assert response.data["results"][0]["notified_at"] == "2023-01-19T09:09:47.325132Z" | ||
assert response.data["results"][0]["organisation_id"] == organisation.id | ||
assert response.data["results"][0]["percent_usage"] == 100 | ||
|
||
|
||
@pytest.mark.freeze_time("2023-01-19T09:09:47.325132+00:00") | ||
def test_doesnt_retrieve_stale_api_usage_notifications( | ||
staff_client: APIClient, | ||
organisation: Organisation, | ||
) -> None: | ||
# Given | ||
url = reverse( | ||
"api-v1:organisations:organisation-api-usage-notification", | ||
args=[organisation.id], | ||
) | ||
|
||
now = timezone.now() | ||
OrganisationSubscriptionInformationCache.objects.create( | ||
organisation=organisation, | ||
allowed_seats=10, | ||
allowed_projects=3, | ||
allowed_30d_api_calls=100, | ||
chargebee_email="[email protected]", | ||
current_billing_term_starts_at=now - timedelta(days=45), | ||
current_billing_term_ends_at=now + timedelta(days=320), | ||
) | ||
|
||
# Create a notification in the past which should not be shown. | ||
OranisationAPIUsageNotification.objects.create( | ||
organisation=organisation, | ||
percent_usage=90, | ||
notified_at=now - timedelta(20), | ||
) | ||
|
||
# When | ||
response = staff_client.get(url) | ||
|
||
# Then | ||
assert response.status_code == status.HTTP_200_OK | ||
assert len(response.data["results"]) == 0 |