@@ -1643,6 +1643,119 @@ class MockPaymentMethod:
16431643 "sub_123" , default_payment_method = payment_method_retrieve_mock .return_value
16441644 )
16451645
1646+ @patch ("services.billing.stripe.PaymentMethod.attach" )
1647+ @patch ("services.billing.stripe.Customer.modify" )
1648+ @patch ("services.billing.stripe.Subscription.modify" )
1649+ @patch ("services.billing.stripe.PaymentMethod.retrieve" )
1650+ def test_check_and_handle_delayed_notification_payment_methods_no_subscription (
1651+ self ,
1652+ payment_method_retrieve_mock ,
1653+ subscription_modify_mock ,
1654+ customer_modify_mock ,
1655+ payment_method_attach_mock ,
1656+ ):
1657+ class MockPaymentMethod :
1658+ type = "us_bank_account"
1659+ us_bank_account = {}
1660+ id = "pm_123"
1661+
1662+ payment_method_retrieve_mock .return_value = MockPaymentMethod ()
1663+
1664+ self .owner .stripe_subscription_id = None
1665+ self .owner .stripe_customer_id = "cus_123"
1666+ self .owner .save ()
1667+
1668+ handler = StripeWebhookHandler ()
1669+ handler ._check_and_handle_delayed_notification_payment_methods (
1670+ "cus_123" , "pm_123"
1671+ )
1672+
1673+ payment_method_retrieve_mock .assert_called_once_with ("pm_123" )
1674+ payment_method_attach_mock .assert_not_called ()
1675+ customer_modify_mock .assert_not_called ()
1676+ subscription_modify_mock .assert_not_called ()
1677+
1678+ @patch ("services.billing.stripe.PaymentMethod.attach" )
1679+ @patch ("services.billing.stripe.Customer.modify" )
1680+ @patch ("services.billing.stripe.Subscription.modify" )
1681+ @patch ("services.billing.stripe.PaymentMethod.retrieve" )
1682+ def test_check_and_handle_delayed_notification_payment_methods_no_customer (
1683+ self ,
1684+ payment_method_retrieve_mock ,
1685+ subscription_modify_mock ,
1686+ customer_modify_mock ,
1687+ payment_method_attach_mock ,
1688+ ):
1689+ class MockPaymentMethod :
1690+ type = "us_bank_account"
1691+ us_bank_account = {}
1692+ id = "pm_123"
1693+
1694+ payment_method_retrieve_mock .return_value = MockPaymentMethod ()
1695+
1696+ handler = StripeWebhookHandler ()
1697+ handler ._check_and_handle_delayed_notification_payment_methods (
1698+ "cus_123" , "pm_123"
1699+ )
1700+
1701+ payment_method_retrieve_mock .assert_called_once_with ("pm_123" )
1702+ payment_method_attach_mock .assert_not_called ()
1703+ customer_modify_mock .assert_not_called ()
1704+ subscription_modify_mock .assert_not_called ()
1705+
1706+ @patch ("logging.Logger.error" )
1707+ @patch ("services.billing.stripe.PaymentMethod.attach" )
1708+ @patch ("services.billing.stripe.Customer.modify" )
1709+ @patch ("services.billing.stripe.Subscription.modify" )
1710+ @patch ("services.billing.stripe.PaymentMethod.retrieve" )
1711+ def test_check_and_handle_delayed_notification_payment_methods_multiple_subscriptions (
1712+ self ,
1713+ payment_method_retrieve_mock ,
1714+ subscription_modify_mock ,
1715+ customer_modify_mock ,
1716+ payment_method_attach_mock ,
1717+ ):
1718+ class MockPaymentMethod :
1719+ type = "us_bank_account"
1720+ us_bank_account = {}
1721+ id = "pm_123"
1722+
1723+ payment_method_retrieve_mock .return_value = MockPaymentMethod ()
1724+
1725+ self .owner .stripe_subscription_id = "sub_123"
1726+ self .owner .stripe_customer_id = "cus_123"
1727+ self .owner .save ()
1728+
1729+ OwnerFactory (stripe_subscription_id = "sub_124" , stripe_customer_id = "cus_123" )
1730+
1731+ handler = StripeWebhookHandler ()
1732+ handler ._check_and_handle_delayed_notification_payment_methods (
1733+ "cus_123" , "pm_123"
1734+ )
1735+
1736+ payment_method_retrieve_mock .assert_called_once_with ("pm_123" )
1737+ payment_method_attach_mock .assert_called_once_with (
1738+ payment_method_retrieve_mock .return_value , customer = "cus_123"
1739+ )
1740+ customer_modify_mock .assert_called_once_with (
1741+ "cus_123" ,
1742+ invoice_settings = {
1743+ "default_payment_method" : payment_method_retrieve_mock .return_value
1744+ },
1745+ )
1746+ subscription_modify_mock .assert_has_calls (
1747+ [
1748+ call (
1749+ "sub_123" ,
1750+ default_payment_method = payment_method_retrieve_mock .return_value ,
1751+ ),
1752+ call (
1753+ "sub_124" ,
1754+ default_payment_method = payment_method_retrieve_mock .return_value ,
1755+ ),
1756+ ]
1757+ )
1758+
16461759 @patch (
16471760 "billing.views.StripeWebhookHandler._check_and_handle_delayed_notification_payment_methods"
16481761 )
0 commit comments