Skip to content
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: Catch full exception instead of runtime error in API usage task #4426

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/organisations/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def handle_api_usage_notifications() -> None:

try:
handle_api_usage_notification_for_organisation(organisation)
except RuntimeError:
except Exception:
logger.error(
f"Error processing api usage for organisation {organisation.id}",
exc_info=True,
Expand Down
55 changes: 55 additions & 0 deletions api/tests/unit/organisations/test_unit_organisations_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading