Skip to content

Commit

Permalink
fix: prevent sentry errors for on premise subscriptions (#2948)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewelwell authored Nov 9, 2023
1 parent 7d16197 commit 6f830e2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
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="test@example.com", 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

3 comments on commit 6f830e2

@vercel
Copy link

@vercel vercel bot commented on 6f830e2 Nov 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 6f830e2 Nov 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

docs – ./docs

docs.bullet-train.io
docs-flagsmith.vercel.app
docs.flagsmith.com
docs-git-main-flagsmith.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 6f830e2 Nov 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.