-
Notifications
You must be signed in to change notification settings - Fork 413
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Handle cancellation date for api usage (#4672)
- Loading branch information
Showing
3 changed files
with
78 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -312,6 +312,33 @@ def test_handle_api_usage_notification_for_organisation_when_billing_starts_at_i | |
] | ||
|
||
|
||
def test_handle_api_usage_notification_for_organisation_when_cancellation_date_is_set( | ||
organisation: Organisation, | ||
inspecting_handler: logging.Handler, | ||
mocker: MockerFixture, | ||
) -> None: | ||
# Given | ||
organisation.subscription.plan = SCALE_UP | ||
organisation.subscription.subscription_id = "fancy_id" | ||
organisation.subscription.cancellation_date = timezone.now() | ||
organisation.subscription.save() | ||
mock_api_usage = mocker.patch("organisations.task_helpers.get_current_api_usage") | ||
mock_api_usage.return_value = 25 | ||
from organisations.task_helpers import logger | ||
|
||
logger.addHandler(inspecting_handler) | ||
|
||
# When | ||
result = handle_api_usage_notification_for_organisation(organisation) | ||
|
||
# Then | ||
assert result is None | ||
assert OrganisationAPIUsageNotification.objects.count() == 0 | ||
|
||
# Check to ensure that error messages haven't been set. | ||
assert inspecting_handler.messages == [] | ||
|
||
|
||
@pytest.mark.freeze_time("2023-01-19T09:09:47.325132+00:00") | ||
def test_handle_api_usage_notifications_when_feature_flag_is_off( | ||
mocker: MockerFixture, | ||
|
@@ -855,6 +882,50 @@ def test_charge_for_api_call_count_overages_scale_up( | |
assert api_billing.billed_at == now | ||
|
||
|
||
@pytest.mark.freeze_time("2023-01-19T09:09:47.325132+00:00") | ||
def test_charge_for_api_call_count_overages_cancellation_date( | ||
organisation: Organisation, | ||
mocker: MockerFixture, | ||
) -> None: | ||
# Given | ||
now = timezone.now() | ||
OrganisationSubscriptionInformationCache.objects.create( | ||
organisation=organisation, | ||
allowed_seats=10, | ||
allowed_projects=3, | ||
allowed_30d_api_calls=100_000, | ||
chargebee_email="[email protected]", | ||
current_billing_term_starts_at=now - timedelta(days=30), | ||
current_billing_term_ends_at=now + timedelta(minutes=30), | ||
) | ||
organisation.subscription.subscription_id = "fancy_sub_id23" | ||
organisation.subscription.plan = "scale-up-v2" | ||
organisation.subscription.cancellation_date = timezone.now() | ||
organisation.subscription.save() | ||
OrganisationAPIUsageNotification.objects.create( | ||
organisation=organisation, | ||
percent_usage=100, | ||
notified_at=now, | ||
) | ||
|
||
get_client_mock = mocker.patch("organisations.tasks.get_client") | ||
client_mock = MagicMock() | ||
get_client_mock.return_value = client_mock | ||
|
||
mock_api_usage = mocker.patch( | ||
"organisations.tasks.get_current_api_usage", | ||
) | ||
assert OrganisationAPIBilling.objects.count() == 0 | ||
|
||
# When | ||
charge_for_api_call_count_overages() | ||
|
||
# Then | ||
assert OrganisationAPIBilling.objects.count() == 0 | ||
mock_api_usage.assert_not_called() | ||
client_mock.get_identity_flags.assert_not_called() | ||
|
||
|
||
@pytest.mark.freeze_time("2023-01-19T09:09:47.325132+00:00") | ||
def test_charge_for_api_call_count_overages_scale_up_when_flagsmith_client_sets_is_enabled_to_false( | ||
organisation: Organisation, | ||
|