2626
2727
2828class MockSubscription (object ):
29- def __init__ (self , subscription_params ):
29+ def __init__ (self , subscription_params : dict ):
3030 self .items = {"data" : [{"id" : "abc" }]}
3131 self .cancel_at_period_end = False
3232 self .current_period_end = 1633512445
33- self .latest_invoice = subscription_params ["latest_invoice" ]
33+ self .latest_invoice = subscription_params .get (
34+ "latest_invoice" ,
35+ {
36+ "id" : "in_123" ,
37+ "status" : "complete" ,
38+ },
39+ )
40+
41+ default_payment_method = {
42+ "id" : "pm_123" ,
43+ "card" : {
44+ "brand" : "visa" ,
45+ "exp_month" : 12 ,
46+ "exp_year" : 2024 ,
47+ "last4" : "abcd" ,
48+ },
49+ }
3450 self .customer = {
3551 "invoice_settings" : {
36- "default_payment_method" : subscription_params ["default_payment_method" ]
52+ "default_payment_method" : subscription_params .get (
53+ "default_payment_method" , default_payment_method
54+ )
3755 },
3856 "id" : "cus_LK&*Hli8YLIO" ,
3957 "discount" : None ,
4058 "email" : None ,
4159 }
42- self .schedule = subscription_params ["schedule_id" ]
43- self .collection_method = subscription_params ["collection_method" ]
60+ self .schedule = subscription_params .get ("schedule_id" )
61+ self .status = subscription_params .get ("status" , "active" )
62+ self .collection_method = subscription_params .get (
63+ "collection_method" , "charge_automatically"
64+ )
4465 self .trial_end = subscription_params .get ("trial_end" )
4566
4667 customer_coupon = subscription_params .get ("customer_coupon" )
@@ -1104,6 +1125,7 @@ def test_update_payment_method_without_body(self):
11041125 response = self .client .patch (url , format = "json" )
11051126 assert response .status_code == status .HTTP_400_BAD_REQUEST
11061127
1128+ @patch ("services.billing.StripeService._is_unverified_payment_method" )
11071129 @patch ("services.billing.stripe.Subscription.retrieve" )
11081130 @patch ("services.billing.stripe.PaymentMethod.attach" )
11091131 @patch ("services.billing.stripe.Customer.modify" )
@@ -1114,12 +1136,15 @@ def test_update_payment_method(
11141136 modify_customer_mock ,
11151137 attach_payment_mock ,
11161138 retrieve_subscription_mock ,
1139+ is_unverified_payment_method_mock ,
11171140 ):
11181141 self .current_owner .stripe_customer_id = "flsoe"
11191142 self .current_owner .stripe_subscription_id = "djfos"
11201143 self .current_owner .save ()
11211144 f = open ("./services/tests/samples/stripe_invoice.json" )
11221145
1146+ is_unverified_payment_method_mock .return_value = False
1147+
11231148 default_payment_method = {
11241149 "card" : {
11251150 "brand" : "visa" ,
@@ -1435,14 +1460,15 @@ def test_update_can_change_name_and_email(self):
14351460 assert self .current_owner .name == expected_name
14361461 assert self .current_owner .email == expected_email
14371462
1463+ @patch ("services.billing.stripe.Subscription.retrieve" )
14381464 @patch ("services.billing.StripeService.modify_subscription" )
1439- def test_update_handles_stripe_error (self , modify_sub_mock ):
1465+ def test_update_handles_stripe_error (self , retrieve_sub_mock , modify_sub_mock ):
14401466 code , message = 402 , "Not right, wrong in fact"
14411467 desired_plan = {"value" : PlanName .CODECOV_PRO_MONTHLY .value , "quantity" : 12 }
14421468 self .current_owner .stripe_customer_id = "flsoe"
14431469 self .current_owner .stripe_subscription_id = "djfos"
14441470 self .current_owner .save ()
1445-
1471+ retrieve_sub_mock . return_value = MockSubscription ({})
14461472 modify_sub_mock .side_effect = StripeError (message = message , http_status = code )
14471473
14481474 response = self ._update (
@@ -1456,9 +1482,12 @@ def test_update_handles_stripe_error(self, modify_sub_mock):
14561482 assert response .status_code == code
14571483 assert response .data ["detail" ] == message
14581484
1485+ @patch ("services.billing.stripe.Subscription.retrieve" )
14591486 @patch ("api.internal.owner.serializers.send_sentry_webhook" )
14601487 @patch ("services.billing.StripeService.modify_subscription" )
1461- def test_update_sentry_plan_monthly (self , modify_sub_mock , send_sentry_webhook ):
1488+ def test_update_sentry_plan_monthly (
1489+ self , modify_sub_mock , send_sentry_webhook , retrieve_sub_mock
1490+ ):
14621491 desired_plan = {"value" : PlanName .SENTRY_MONTHLY .value , "quantity" : 12 }
14631492 self .current_owner .stripe_customer_id = "flsoe"
14641493 self .current_owner .stripe_subscription_id = "djfos"
@@ -1499,9 +1528,15 @@ def test_update_sentry_plan_monthly_with_users_org(
14991528 )
15001529 send_sentry_webhook .assert_called_once_with (self .current_owner , org )
15011530
1531+ @patch ("services.billing.stripe.Subscription.retrieve" )
15021532 @patch ("api.internal.owner.serializers.send_sentry_webhook" )
15031533 @patch ("services.billing.StripeService.modify_subscription" )
1504- def test_update_sentry_plan_annual (self , modify_sub_mock , send_sentry_webhook ):
1534+ def test_update_sentry_plan_annual (
1535+ self ,
1536+ modify_sub_mock ,
1537+ send_sentry_webhook ,
1538+ retrieve_sub_mock ,
1539+ ):
15051540 desired_plan = {"value" : PlanName .SENTRY_YEARLY .value , "quantity" : 12 }
15061541 self .current_owner .stripe_customer_id = "flsoe"
15071542 self .current_owner .stripe_subscription_id = "djfos"
0 commit comments