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: prevent sentry errors for on premise subscriptions #2948

Merged
merged 1 commit into from
Nov 9, 2023
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
44 changes: 29 additions & 15 deletions api/organisations/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import json
from datetime import datetime, timedelta
from typing import Type
from unittest import TestCase, mock
from unittest.mock import MagicMock

import pytest
from _pytest.logging import LogCaptureFixture
from django.conf import settings
from django.contrib.auth import get_user_model
from django.core import mail
from django.db.models import Model
from django.urls import reverse
from freezegun import freeze_time
from pytz import UTC
Expand Down Expand Up @@ -718,24 +721,35 @@ def test_when_cancelled_subscription_is_renewed_then_subscription_activated_and_
# and
assert not mail.outbox

def test_when_chargebee_webhook_received_with_unknown_subscription_id_then_404(
self,
):
# Given
data = {
"content": {
"subscription": {"status": "active", "id": "some-random-id"},
"customer": {"email": self.cb_user.email},
}

def test_when_chargebee_webhook_received_with_unknown_subscription_id_then_200(
api_client: APIClient, caplog: LogCaptureFixture, django_user_model: Type[Model]
):
# Given
subscription_id = "some-random-id"
cb_user = django_user_model.objects.create(email="[email protected]", is_staff=True)
api_client.force_authenticate(cb_user)

data = {
"content": {
"subscription": {"status": "active", "id": subscription_id},
"customer": {"email": cb_user.email},
}
}
url = reverse("api-v1:chargebee-webhook")

# When
res = self.client.post(
self.url, data=json.dumps(data), content_type="application/json"
)
# When
res = api_client.post(url, data=json.dumps(data), content_type="application/json")

# Then
assert res.status_code == status.HTTP_200_OK
# Then
assert res.status_code == status.HTTP_200_OK

assert len(caplog.records) == 1
assert caplog.record_tuples[0] == (
"organisations.views",
30,
f"Couldn't get unique subscription for ChargeBee id {subscription_id}",
)


@pytest.mark.django_db
Expand Down
2 changes: 1 addition & 1 deletion api/organisations/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def chargebee_webhook(request):
"Couldn't get unique subscription for ChargeBee id %s"
% subscription_data.get("id")
)
logger.error(error_message)
logger.warning(error_message)
return Response(status=status.HTTP_200_OK)
subscription_status = subscription_data.get("status")
if subscription_status == "active":
Expand Down