Skip to content

Commit

Permalink
fix: Some uncaught exceptions related to billing (#604)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajay-sentry authored Jun 5, 2024
1 parent 160c874 commit 86fd9b1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 19 deletions.
30 changes: 27 additions & 3 deletions billing/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import time
from datetime import datetime, timedelta
from unittest.mock import patch

import pytest
import stripe
from django.conf import settings
from freezegun import freeze_time
Expand All @@ -10,6 +10,7 @@
from rest_framework.reverse import reverse
from rest_framework.test import APIRequestFactory, APITestCase

from codecov_auth.models import Owner
from codecov_auth.tests.factories import OwnerFactory
from core.tests.factories import RepositoryFactory
from plan.constants import PlanName, TrialDaysAmount
Expand Down Expand Up @@ -167,8 +168,31 @@ def test_customer_subscription_deleted_deactivates_all_repos(self):
}
)

assert (
self.owner.repository_set.filter(activated=True, active=True).count() == 0
@patch("logging.Logger.info")
def test_customer_subscription_deleted_no_customer(self, log_info_mock):
self.owner.plan = "users-inappy"
self.owner.plan_user_count = 20
self.owner.save()

self._send_event(
payload={
"type": "customer.subscription.deleted",
"data": {
"object": {
"id": "HUH",
"customer": "nah",
"plan": {"name": self.owner.plan},
}
},
}
)

log_info_mock.assert_called_with(
"Customer Subscription Deleted - Couldn't find owner, subscription likely already deleted",
extra={
"stripe_subscription_id": "HUH",
"stripe_customer_id": "nah",
},
)

def test_customer_created_logs_and_doesnt_crash(self):
Expand Down
41 changes: 25 additions & 16 deletions billing/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,31 @@ def invoice_payment_failed(self, invoice: stripe.Invoice) -> None:
self._log_updated(updated)

def customer_subscription_deleted(self, subscription: stripe.Subscription) -> None:
log.info(
"Customer Subscription Deleted - Setting free plan and deactivating repos for stripe customer",
extra=dict(
stripe_subscription_id=subscription.id,
try:
log.info(
"Customer Subscription Deleted - Setting free plan and deactivating repos for stripe customer",
extra=dict(
stripe_subscription_id=subscription.id,
stripe_customer_id=subscription.customer,
),
)
owner: Owner = Owner.objects.get(
stripe_customer_id=subscription.customer,
),
)
owner: Owner = Owner.objects.get(
stripe_customer_id=subscription.customer,
stripe_subscription_id=subscription.id,
)
plan_service = PlanService(current_org=owner)
plan_service.set_default_plan_data()
owner.repository_set.update(active=False, activated=False)
stripe_subscription_id=subscription.id,
)
plan_service = PlanService(current_org=owner)
plan_service.set_default_plan_data()
owner.repository_set.update(active=False, activated=False)

self._log_updated(1)
self._log_updated(1)
except Owner.DoesNotExist:
log.info(
"Customer Subscription Deleted - Couldn't find owner, subscription likely already deleted",
extra=dict(
stripe_subscription_id=subscription.id,
stripe_customer_id=subscription.customer,
),
)

def subscription_schedule_created(
self, schedule: stripe.SubscriptionSchedule
Expand All @@ -90,7 +99,7 @@ def subscription_schedule_created(
extra=dict(
stripe_customer_id=subscription.customer,
stripe_subscription_id=subscription.id,
ownerid=subscription.metadata["obo_organization"],
ownerid=subscription.metadata.get("obo_organization"),
plan=plan_name,
quantity=subscription.quantity,
),
Expand All @@ -114,7 +123,7 @@ def subscription_schedule_updated(
extra=dict(
stripe_customer_id=subscription.customer,
stripe_subscription_id=subscription.id,
ownerid=subscription.metadata["obo_organization"],
ownerid=subscription.metadata.get("obo_organization"),
plan=plan_name,
quantity=quantity,
),
Expand Down

0 comments on commit 86fd9b1

Please sign in to comment.