|
2 | 2 | from unittest.mock import MagicMock, call, patch |
3 | 3 |
|
4 | 4 | import requests |
| 5 | +import stripe |
5 | 6 | from django.conf import settings |
6 | 7 | from django.test import TestCase |
7 | 8 | from freezegun import freeze_time |
|
106 | 107 | "next_payment_attempt": None, |
107 | 108 | "number": "EF0A41E-0001", |
108 | 109 | "paid": True, |
109 | | - "payment_intent": None, |
| 110 | + "payment_intent": {"id": "pi_3P4567890123456789012345", "status": "completed"}, |
110 | 111 | "period_end": 1489789420, |
111 | 112 | "period_start": 1487370220, |
112 | 113 | "post_payment_credit_notes_amount": 0, |
@@ -1622,15 +1623,21 @@ def test_update_payment_method_when_no_subscription(self): |
1622 | 1623 | @patch("services.billing.stripe.PaymentMethod.attach") |
1623 | 1624 | @patch("services.billing.stripe.Customer.modify") |
1624 | 1625 | @patch("services.billing.stripe.Subscription.modify") |
| 1626 | + @patch("services.billing.StripeService._is_unverified_payment_method") |
1625 | 1627 | def test_update_payment_method( |
1626 | | - self, modify_sub_mock, modify_customer_mock, attach_payment_mock |
| 1628 | + self, |
| 1629 | + is_unverified_mock, |
| 1630 | + modify_sub_mock, |
| 1631 | + modify_customer_mock, |
| 1632 | + attach_payment_mock, |
1627 | 1633 | ): |
1628 | 1634 | payment_method_id = "pm_1234567" |
1629 | 1635 | subscription_id = "sub_abc" |
1630 | 1636 | customer_id = "cus_abc" |
1631 | 1637 | owner = OwnerFactory( |
1632 | 1638 | stripe_subscription_id=subscription_id, stripe_customer_id=customer_id |
1633 | 1639 | ) |
| 1640 | + is_unverified_mock.return_value = False |
1634 | 1641 | self.stripe.update_payment_method(owner, payment_method_id) |
1635 | 1642 | attach_payment_mock.assert_called_once_with( |
1636 | 1643 | payment_method_id, customer=customer_id |
@@ -2166,17 +2173,31 @@ def test_update_plan_to_users_basic_sets_plan_if_no_subscription_id( |
2166 | 2173 | @patch("services.tests.test_billing.MockPaymentService.create_checkout_session") |
2167 | 2174 | @patch("services.tests.test_billing.MockPaymentService.modify_subscription") |
2168 | 2175 | @patch("services.tests.test_billing.MockPaymentService.delete_subscription") |
| 2176 | + @patch("services.tests.test_billing.MockPaymentService.get_subscription") |
2169 | 2177 | def test_update_plan_modifies_subscription_if_user_plan_and_subscription_exists( |
2170 | 2178 | self, |
| 2179 | + get_subscription_mock, |
2171 | 2180 | delete_subscription_mock, |
2172 | 2181 | modify_subscription_mock, |
2173 | 2182 | create_checkout_session_mock, |
2174 | 2183 | set_default_plan_data, |
2175 | 2184 | ): |
2176 | 2185 | owner = OwnerFactory(stripe_subscription_id=10) |
2177 | 2186 | desired_plan = {"value": PlanName.CODECOV_PRO_YEARLY.value, "quantity": 10} |
2178 | | - self.billing_service.update_plan(owner, desired_plan) |
2179 | 2187 |
|
| 2188 | + get_subscription_mock.return_value = stripe.util.convert_to_stripe_object( |
| 2189 | + { |
| 2190 | + "schedule": None, |
| 2191 | + "current_period_start": 1489799420, |
| 2192 | + "current_period_end": 1492477820, |
| 2193 | + "quantity": 10, |
| 2194 | + "name": PlanName.CODECOV_PRO_YEARLY.value, |
| 2195 | + "id": 215, |
| 2196 | + "status": "active", |
| 2197 | + } |
| 2198 | + ) |
| 2199 | + |
| 2200 | + self.billing_service.update_plan(owner, desired_plan) |
2180 | 2201 | modify_subscription_mock.assert_called_once_with(owner, desired_plan) |
2181 | 2202 |
|
2182 | 2203 | set_default_plan_data.assert_not_called() |
|
0 commit comments