-
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: Catch full exception instead of runtime error in API usage task (#…
- Loading branch information
Showing
2 changed files
with
56 additions
and
1 deletion.
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 |
---|---|---|
|
@@ -513,6 +513,61 @@ 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_with_error( | ||
mocker: MockerFixture, | ||
organisation: Organisation, | ||
inspecting_handler: logging.Handler, | ||
) -> None: | ||
# Given | ||
from organisations.tasks import logger | ||
|
||
logger.addHandler(inspecting_handler) | ||
|
||
now = timezone.now() | ||
organisation.subscription.plan = SCALE_UP | ||
organisation.subscription.subscription_id = "fancy_id" | ||
organisation.subscription.save() | ||
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), | ||
) | ||
|
||
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 | ||
|
||
api_usage_patch = mocker.patch( | ||
"organisations.tasks.handle_api_usage_notification_for_organisation", | ||
side_effect=ValueError("An error occurred"), | ||
) | ||
|
||
# When | ||
handle_api_usage_notifications() | ||
|
||
# Then | ||
api_usage_patch.assert_called_once_with(organisation) | ||
assert ( | ||
OrganisationAPIUsageNotification.objects.filter( | ||
organisation=organisation, | ||
).count() | ||
== 0 | ||
) | ||
assert len(inspecting_handler.messages) == 1 | ||
error_message = inspecting_handler.messages[0].split("\n")[0] | ||
|
||
assert ( | ||
error_message | ||
== f"Error processing api usage for organisation {organisation.id}" | ||
) | ||
|
||
|
||
@pytest.mark.freeze_time("2023-01-19T09:09:47.325132+00:00") | ||
def test_handle_api_usage_notifications_for_free_accounts( | ||
mocker: MockerFixture, | ||
|