diff --git a/tests/__init__.py b/tests/__init__.py index ab836145bb..fbeeaff004 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -527,7 +527,7 @@ def capture(self): "amount": 2200, "amount_refunded": 0, "application_fee": None, - "balance_transaction": deepcopy(FAKE_BALANCE_TRANSACTION), + "balance_transaction": FAKE_BALANCE_TRANSACTION["id"], "captured": True, "created": 1439229084, "currency": "usd", @@ -575,7 +575,7 @@ def capture(self): "amount": 3000, "amount_refunded": 0, "application_fee": None, - "balance_transaction": deepcopy(FAKE_BALANCE_TRANSACTION), + "balance_transaction": FAKE_BALANCE_TRANSACTION["id"], "captured": False, "created": 1439788903, "currency": "usd", diff --git a/tests/test_charge.py b/tests/test_charge.py index c85f989760..2d9d5ef603 100644 --- a/tests/test_charge.py +++ b/tests/test_charge.py @@ -3,7 +3,7 @@ """ from copy import deepcopy from decimal import Decimal -from unittest.mock import patch +from unittest.mock import call, patch from django.contrib.auth import get_user_model from django.test.testcases import TestCase @@ -63,7 +63,14 @@ def test_str(self): autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) @patch("stripe.Charge.retrieve", autospec=True) - def test_capture_charge(self, charge_retrieve_mock, default_account_mock): + @patch( + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, + ) + def test_capture_charge( + self, balance_transaction_retrieve_mock, charge_retrieve_mock, default_account_mock + ): default_account_mock.return_value = self.account fake_charge_no_invoice = deepcopy(FAKE_CHARGE) @@ -94,7 +101,9 @@ def test_capture_charge(self, charge_retrieve_mock, default_account_mock): autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED and IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) @patch( - "stripe.BalanceTransaction.retrieve", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, ) @patch("stripe.Charge.retrieve", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED) @patch("stripe.Invoice.retrieve", return_value=deepcopy(FAKE_INVOICE), autospec=True) @@ -113,6 +122,8 @@ def test_sync_from_stripe_data( balance_transaction_retrieve_mock, default_account_mock, ): + from djstripe.settings import STRIPE_SECRET_KEY + default_account_mock.return_value = self.account fake_charge_copy = deepcopy(FAKE_CHARGE) @@ -132,7 +143,9 @@ def test_sync_from_stripe_data( self.assertEqual(charge.source.type, LegacySourceType.card) charge_retrieve_mock.assert_not_called() - balance_transaction_retrieve_mock.assert_not_called() + balance_transaction_retrieve_mock.assert_called_once_with( + api_key=STRIPE_SECRET_KEY, expand=[], id=FAKE_BALANCE_TRANSACTION["id"] + ) self.assert_fks( charge, @@ -242,7 +255,10 @@ def test_sync_from_stripe_data_refunded_on_update( ) @patch( "stripe.BalanceTransaction.retrieve", - return_value=deepcopy(FAKE_BALANCE_TRANSACTION_REFUND), + side_effect=[ + deepcopy(FAKE_BALANCE_TRANSACTION), + deepcopy(FAKE_BALANCE_TRANSACTION_REFUND), + ], ) @patch("stripe.Charge.retrieve", autospec=IS_ASSERT_CALLED_AUTOSPEC_SUPPORTED) @patch("stripe.Invoice.retrieve", return_value=deepcopy(FAKE_INVOICE), autospec=True) @@ -277,8 +293,15 @@ def test_sync_from_stripe_data_refunded( self.assertEqual(charge.amount, charge.amount_refunded) charge_retrieve_mock.assert_not_called() - balance_transaction_retrieve_mock.assert_called_once_with( - api_key=STRIPE_SECRET_KEY, expand=[], id=FAKE_BALANCE_TRANSACTION_REFUND["id"] + + # We expect two calls - for charge and then for charge.refunds + balance_transaction_retrieve_mock.assert_has_calls( + [ + call(api_key=STRIPE_SECRET_KEY, expand=[], id=FAKE_BALANCE_TRANSACTION["id"]), + call( + api_key=STRIPE_SECRET_KEY, expand=[], id=FAKE_BALANCE_TRANSACTION_REFUND["id"] + ), + ] ) refunds = list(charge.refunds.all()) @@ -303,7 +326,9 @@ def test_sync_from_stripe_data_refunded( ) @patch( - "stripe.BalanceTransaction.retrieve", autospec=IS_ASSERT_CALLED_AUTOSPEC_SUPPORTED + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, ) @patch("stripe.Charge.retrieve", autospec=IS_ASSERT_CALLED_AUTOSPEC_SUPPORTED) @patch("stripe.Invoice.retrieve", return_value=deepcopy(FAKE_INVOICE), autospec=True) @@ -342,7 +367,6 @@ def test_sync_from_stripe_data_max_amount( self.assertEqual(0, charge.amount_refunded) charge_retrieve_mock.assert_not_called() - balance_transaction_retrieve_mock.assert_not_called() self.assert_fks( charge, @@ -359,7 +383,9 @@ def test_sync_from_stripe_data_max_amount( autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) @patch( - "stripe.BalanceTransaction.retrieve", autospec=IS_ASSERT_CALLED_AUTOSPEC_SUPPORTED + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=IS_ASSERT_CALLED_AUTOSPEC_SUPPORTED, ) @patch("stripe.Charge.retrieve", autospec=IS_ASSERT_CALLED_AUTOSPEC_SUPPORTED) @patch("stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT), autospec=True) @@ -378,6 +404,8 @@ def test_sync_from_stripe_data_unsupported_source( balance_transaction_retrieve_mock, default_account_mock, ): + from djstripe.settings import STRIPE_SECRET_KEY + default_account_mock.return_value = self.account fake_charge_copy = deepcopy(FAKE_CHARGE) @@ -392,7 +420,10 @@ def test_sync_from_stripe_data_unsupported_source( self.assertEqual(charge.source, PaymentMethod.objects.get(id="test_id")) charge_retrieve_mock.assert_not_called() - balance_transaction_retrieve_mock.assert_not_called() + + balance_transaction_retrieve_mock.assert_called_once_with( + api_key=STRIPE_SECRET_KEY, expand=[], id=FAKE_BALANCE_TRANSACTION["id"] + ) self.assert_fks( charge, @@ -409,12 +440,16 @@ def test_sync_from_stripe_data_unsupported_source( autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) @patch( - "stripe.BalanceTransaction.retrieve", autospec=IS_ASSERT_CALLED_AUTOSPEC_SUPPORTED + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, ) @patch("stripe.Charge.retrieve", autospec=IS_ASSERT_CALLED_AUTOSPEC_SUPPORTED) def test_sync_from_stripe_data_no_customer( self, charge_retrieve_mock, balance_transaction_retrieve_mock, default_account_mock ): + from djstripe.settings import STRIPE_SECRET_KEY + default_account_mock.return_value = self.account fake_charge_copy = deepcopy(FAKE_CHARGE) @@ -429,7 +464,9 @@ def test_sync_from_stripe_data_no_customer( assert charge.customer is None charge_retrieve_mock.assert_not_called() - balance_transaction_retrieve_mock.assert_not_called() + balance_transaction_retrieve_mock.assert_called_once_with( + api_key=STRIPE_SECRET_KEY, expand=[], id=FAKE_BALANCE_TRANSACTION["id"] + ) self.assert_fks( charge, @@ -445,7 +482,9 @@ def test_sync_from_stripe_data_no_customer( ) @patch( - "stripe.BalanceTransaction.retrieve", autospec=IS_ASSERT_CALLED_AUTOSPEC_SUPPORTED + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, ) @patch("stripe.Charge.retrieve", autospec=IS_ASSERT_CALLED_AUTOSPEC_SUPPORTED) @patch("stripe.Invoice.retrieve", return_value=deepcopy(FAKE_INVOICE), autospec=True) @@ -470,6 +509,8 @@ def test_sync_from_stripe_data_with_transfer( charge_retrieve_mock, balance_transaction_retrieve_mock, ): + from djstripe.settings import STRIPE_SECRET_KEY + default_account_mock.return_value = self.account fake_transfer = deepcopy(FAKE_TRANSFER) @@ -489,7 +530,9 @@ def test_sync_from_stripe_data_with_transfer( self.assertEqual(fake_transfer["id"], charge.transfer.id) charge_retrieve_mock.assert_not_called() - balance_transaction_retrieve_mock.assert_not_called() + balance_transaction_retrieve_mock.assert_called_once_with( + api_key=STRIPE_SECRET_KEY, expand=[], id=FAKE_BALANCE_TRANSACTION["id"] + ) self.assert_fks( charge, @@ -503,7 +546,9 @@ def test_sync_from_stripe_data_with_transfer( @patch("stripe.Charge.retrieve", autospec=IS_ASSERT_CALLED_AUTOSPEC_SUPPORTED) @patch("stripe.Account.retrieve", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED) @patch( - "stripe.BalanceTransaction.retrieve", autospec=IS_ASSERT_CALLED_AUTOSPEC_SUPPORTED + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, ) @patch("stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT), autospec=True) @patch( @@ -523,6 +568,8 @@ def test_sync_from_stripe_data_with_destination( account_retrieve_mock, charge_retrieve_mock, ): + from djstripe.settings import STRIPE_SECRET_KEY + account_retrieve_mock.return_value = FAKE_ACCOUNT fake_charge_copy = deepcopy(FAKE_CHARGE) @@ -539,7 +586,9 @@ def test_sync_from_stripe_data_with_destination( self.assertEqual(account, charge.account) charge_retrieve_mock.assert_not_called() - balance_transaction_retrieve_mock.assert_not_called() + balance_transaction_retrieve_mock.assert_called_once_with( + api_key=STRIPE_SECRET_KEY, expand=[], id=FAKE_BALANCE_TRANSACTION["id"] + ) self.assert_fks( charge, diff --git a/tests/test_customer.py b/tests/test_customer.py index c7a51d9f51..b8596c2ac3 100644 --- a/tests/test_customer.py +++ b/tests/test_customer.py @@ -18,9 +18,9 @@ from djstripe.settings import STRIPE_SECRET_KEY from . import ( - FAKE_ACCOUNT, FAKE_CARD, FAKE_CARD_V, FAKE_CHARGE, FAKE_COUPON, FAKE_CUSTOMER, - FAKE_CUSTOMER_II, FAKE_DISCOUNT_CUSTOMER, FAKE_INVOICE, FAKE_INVOICE_III, - FAKE_INVOICEITEM, FAKE_PLAN, FAKE_PRODUCT, FAKE_SUBSCRIPTION, + FAKE_ACCOUNT, FAKE_BALANCE_TRANSACTION, FAKE_CARD, FAKE_CARD_V, FAKE_CHARGE, + FAKE_COUPON, FAKE_CUSTOMER, FAKE_CUSTOMER_II, FAKE_DISCOUNT_CUSTOMER, FAKE_INVOICE, + FAKE_INVOICE_III, FAKE_INVOICEITEM, FAKE_PLAN, FAKE_PRODUCT, FAKE_SUBSCRIPTION, FAKE_SUBSCRIPTION_II, FAKE_UPCOMING_INVOICE, IS_ASSERT_CALLED_AUTOSPEC_SUPPORTED, IS_EXCEPTION_AUTOSPEC_SUPPORTED, IS_STATICMETHOD_AUTOSPEC_SUPPORTED, AssertStripeFksMixin, StripeList, datetime_to_unix, default_account @@ -350,8 +350,15 @@ def fake_customer_save(self, *args, **kwargs): "djstripe.models.Account.get_default_account", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) + @patch( + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, + ) @patch("stripe.Charge.retrieve", autospec=True) - def test_refund_charge(self, charge_retrieve_mock, default_account_mock): + def test_refund_charge( + self, charge_retrieve_mock, balance_transaction_retrieve_mock, default_account_mock + ): default_account_mock.return_value = self.account fake_charge_no_invoice = deepcopy(FAKE_CHARGE) @@ -398,9 +405,14 @@ def test_refund_charge(self, charge_retrieve_mock, default_account_mock): "djstripe.models.Account.get_default_account", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) + @patch( + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, + ) @patch("stripe.Charge.retrieve", autospec=True) def test_refund_charge_object_returned( - self, charge_retrieve_mock, default_account_mock + self, charge_retrieve_mock, balance_transaction_retrieve_mock, default_account_mock ): default_account_mock.return_value = self.account @@ -464,10 +476,19 @@ def test_calculate_refund_above_max_refund(self): "djstripe.models.Account.get_default_account", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) + @patch( + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, + ) @patch("stripe.Charge.retrieve", autospec=True) @patch("stripe.Charge.create", autospec=True) def test_charge_converts_dollars_into_cents( - self, charge_create_mock, charge_retrieve_mock, default_account_mock + self, + charge_create_mock, + charge_retrieve_mock, + balance_transaction_retrieve_mock, + default_account_mock, ): default_account_mock.return_value = self.account @@ -486,6 +507,11 @@ def test_charge_converts_dollars_into_cents( "djstripe.models.Account.get_default_account", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) + @patch( + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, + ) @patch("stripe.Charge.retrieve", autospec=True) @patch("stripe.Charge.create", autospec=True) @patch("stripe.Invoice.retrieve", autospec=True) @@ -502,6 +528,7 @@ def test_charge_doesnt_require_invoice( invoice_retrieve_mock, charge_create_mock, charge_retrieve_mock, + balance_transaction_retrieve_mock, default_account_mock, ): default_account_mock.return_value = self.account @@ -525,10 +552,19 @@ def test_charge_doesnt_require_invoice( "djstripe.models.Account.get_default_account", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) + @patch( + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, + ) @patch("stripe.Charge.retrieve", autospec=True) @patch("stripe.Charge.create", autospec=True) def test_charge_passes_extra_arguments( - self, charge_create_mock, charge_retrieve_mock, default_account_mock + self, + charge_create_mock, + charge_retrieve_mock, + balance_transaction_retrieve_mock, + default_account_mock, ): default_account_mock.return_value = self.account @@ -550,10 +586,19 @@ def test_charge_passes_extra_arguments( "djstripe.models.Account.get_default_account", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) + @patch( + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, + ) @patch("stripe.Charge.retrieve", autospec=True) @patch("stripe.Charge.create", autospec=True) def test_charge_string_source( - self, charge_create_mock, charge_retrieve_mock, default_account_mock + self, + charge_create_mock, + charge_retrieve_mock, + balance_transaction_retrieve_mock, + default_account_mock, ): default_account_mock.return_value = self.account @@ -569,10 +614,19 @@ def test_charge_string_source( "djstripe.models.Account.get_default_account", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) + @patch( + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, + ) @patch("stripe.Charge.retrieve", autospec=True) @patch("stripe.Charge.create", autospec=True) def test_charge_card_source( - self, charge_create_mock, charge_retrieve_mock, default_account_mock + self, + charge_create_mock, + charge_retrieve_mock, + balance_transaction_retrieve_mock, + default_account_mock, ): default_account_mock.return_value = self.account @@ -588,6 +642,11 @@ def test_charge_card_source( "djstripe.models.Account.get_default_account", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) + @patch( + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, + ) @patch( "stripe.Subscription.retrieve", return_value=deepcopy(FAKE_SUBSCRIPTION), @@ -610,6 +669,7 @@ def test_retry_unpaid_invoices( charge_retrieve_mock, customer_retrieve_mock, subscription_retrive_mock, + balance_transaction_retrieve_mock, default_account_mock, ): default_account_mock.return_value = self.account @@ -623,6 +683,11 @@ def test_retry_unpaid_invoices( "djstripe.models.Account.get_default_account", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) + @patch( + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, + ) @patch( "stripe.Subscription.retrieve", return_value=deepcopy(FAKE_SUBSCRIPTION), @@ -645,6 +710,7 @@ def test_retry_unpaid_invoices_none_unpaid( charge_retrieve_mock, customer_retrieve_mock, subscription_retrieve_mock, + balance_transaction_retrieve_mock, default_account_mock, ): default_account_mock.return_value = self.account diff --git a/tests/test_event_handlers.py b/tests/test_event_handlers.py index 3dd3c9e0dc..00efcbafa2 100644 --- a/tests/test_event_handlers.py +++ b/tests/test_event_handlers.py @@ -14,14 +14,15 @@ ) from . import ( - FAKE_CARD, FAKE_CHARGE, FAKE_CHARGE_II, FAKE_COUPON, FAKE_CUSTOMER, - FAKE_CUSTOMER_II, FAKE_DISPUTE, FAKE_EVENT_ACCOUNT_APPLICATION_DEAUTHORIZED, - FAKE_EVENT_CHARGE_SUCCEEDED, FAKE_EVENT_CUSTOMER_CREATED, - FAKE_EVENT_CUSTOMER_DELETED, FAKE_EVENT_CUSTOMER_DISCOUNT_CREATED, - FAKE_EVENT_CUSTOMER_DISCOUNT_DELETED, FAKE_EVENT_CUSTOMER_SOURCE_CREATED, - FAKE_EVENT_CUSTOMER_SOURCE_DELETED, FAKE_EVENT_CUSTOMER_SOURCE_DELETED_DUPE, - FAKE_EVENT_CUSTOMER_SUBSCRIPTION_CREATED, FAKE_EVENT_CUSTOMER_SUBSCRIPTION_DELETED, - FAKE_EVENT_DISPUTE_CREATED, FAKE_EVENT_INVOICE_CREATED, FAKE_EVENT_INVOICE_DELETED, + FAKE_BALANCE_TRANSACTION, FAKE_CARD, FAKE_CHARGE, FAKE_CHARGE_II, FAKE_COUPON, + FAKE_CUSTOMER, FAKE_CUSTOMER_II, FAKE_DISPUTE, + FAKE_EVENT_ACCOUNT_APPLICATION_DEAUTHORIZED, FAKE_EVENT_CHARGE_SUCCEEDED, + FAKE_EVENT_CUSTOMER_CREATED, FAKE_EVENT_CUSTOMER_DELETED, + FAKE_EVENT_CUSTOMER_DISCOUNT_CREATED, FAKE_EVENT_CUSTOMER_DISCOUNT_DELETED, + FAKE_EVENT_CUSTOMER_SOURCE_CREATED, FAKE_EVENT_CUSTOMER_SOURCE_DELETED, + FAKE_EVENT_CUSTOMER_SOURCE_DELETED_DUPE, FAKE_EVENT_CUSTOMER_SUBSCRIPTION_CREATED, + FAKE_EVENT_CUSTOMER_SUBSCRIPTION_DELETED, FAKE_EVENT_DISPUTE_CREATED, + FAKE_EVENT_INVOICE_CREATED, FAKE_EVENT_INVOICE_DELETED, FAKE_EVENT_INVOICE_UPCOMING, FAKE_EVENT_INVOICEITEM_CREATED, FAKE_EVENT_INVOICEITEM_DELETED, FAKE_EVENT_PLAN_CREATED, FAKE_EVENT_PLAN_DELETED, FAKE_EVENT_PLAN_REQUEST_IS_OBJECT, FAKE_EVENT_TRANSFER_CREATED, @@ -68,6 +69,9 @@ def setUp(self): "djstripe.models.Account.get_default_account", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) + @patch( + "stripe.BalanceTransaction.retrieve", return_value=deepcopy(FAKE_BALANCE_TRANSACTION) + ) @patch("stripe.Charge.retrieve", autospec=True) @patch("stripe.Event.retrieve", autospec=True) @patch("stripe.Invoice.retrieve", return_value=deepcopy(FAKE_INVOICE), autospec=True) @@ -84,6 +88,7 @@ def test_charge_created( invoice_retrieve_mock, event_retrieve_mock, charge_retrieve_mock, + balance_transaction_retrieve_mock, account_mock, ): FAKE_CUSTOMER.create_for_user(self.user) @@ -310,6 +315,11 @@ class TestInvoiceEvents(EventTestCase): "djstripe.models.Account.get_default_account", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) + @patch( + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, + ) @patch( "stripe.Subscription.retrieve", return_value=deepcopy(FAKE_SUBSCRIPTION), @@ -328,6 +338,7 @@ def test_invoice_created_no_existing_customer( charge_retrieve_mock, customer_retrieve_mock, subscription_retrieve_mock, + balance_transaction_retrieve_mock, default_account_mock, ): default_account_mock.return_value = default_account() @@ -348,6 +359,11 @@ def test_invoice_created_no_existing_customer( "djstripe.models.Account.get_default_account", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) + @patch( + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, + ) @patch( "stripe.Subscription.retrieve", return_value=deepcopy(FAKE_SUBSCRIPTION), @@ -366,6 +382,7 @@ def test_invoice_created( charge_retrieve_mock, customer_retrieve_mock, subscription_retrieve_mock, + balance_transaction_retrieve_mock, default_account_mock, ): default_account_mock.return_value = default_account() @@ -394,6 +411,11 @@ def test_invoice_created( "djstripe.models.Account.get_default_account", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) + @patch( + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, + ) @patch( "stripe.Subscription.retrieve", return_value=deepcopy(FAKE_SUBSCRIPTION), @@ -408,6 +430,7 @@ def test_invoice_deleted( invoice_retrieve_mock, charge_retrieve_mock, subscription_retrieve_mock, + balance_transaction_retrieve_mock, default_account_mock, ): default_account_mock.return_value = default_account() @@ -440,6 +463,11 @@ class TestInvoiceItemEvents(EventTestCase): "djstripe.models.Account.get_default_account", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) + @patch( + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, + ) @patch( "stripe.Subscription.retrieve", return_value=deepcopy(FAKE_SUBSCRIPTION_III), @@ -460,6 +488,7 @@ def test_invoiceitem_created( invoice_retrieve_mock, charge_retrieve_mock, subscription_retrieve_mock, + balance_transaction_retrieve_mock, default_account_mock, ): default_account_mock.return_value = default_account() @@ -487,6 +516,11 @@ def test_invoiceitem_created( "djstripe.models.Account.get_default_account", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) + @patch( + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, + ) @patch( "stripe.Subscription.retrieve", return_value=deepcopy(FAKE_SUBSCRIPTION_III), @@ -507,6 +541,7 @@ def test_invoiceitem_deleted( invoice_retrieve_mock, charge_retrieve_mock, subscription_retrieve_mock, + balance_transaction_retrieve_mock, default_account_mock, ): default_account_mock.return_value = default_account() diff --git a/tests/test_invoice.py b/tests/test_invoice.py index bc4db9091f..7e4d22d80e 100644 --- a/tests/test_invoice.py +++ b/tests/test_invoice.py @@ -12,8 +12,9 @@ from djstripe.settings import STRIPE_SECRET_KEY from . import ( - FAKE_CHARGE, FAKE_CUSTOMER, FAKE_INVOICE, FAKE_INVOICEITEM_II, FAKE_PLAN, FAKE_PRODUCT, - FAKE_SUBSCRIPTION, FAKE_UPCOMING_INVOICE, IS_ASSERT_CALLED_AUTOSPEC_SUPPORTED, + FAKE_BALANCE_TRANSACTION, FAKE_CHARGE, FAKE_CUSTOMER, FAKE_INVOICE, + FAKE_INVOICEITEM_II, FAKE_PLAN, FAKE_PRODUCT, FAKE_SUBSCRIPTION, + FAKE_UPCOMING_INVOICE, IS_ASSERT_CALLED_AUTOSPEC_SUPPORTED, IS_STATICMETHOD_AUTOSPEC_SUPPORTED, AssertStripeFksMixin, default_account ) @@ -30,6 +31,11 @@ def setUp(self): "djstripe.models.Account.get_default_account", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) + @patch( + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, + ) @patch( "stripe.Subscription.retrieve", return_value=deepcopy(FAKE_SUBSCRIPTION), @@ -42,6 +48,7 @@ def test_str( product_retrieve_mock, charge_retrieve_mock, subscription_retrive_mock, + balance_transaction_retrieve_mock, default_account_mock, ): default_account_mock.return_value = self.account @@ -66,6 +73,11 @@ def test_str( "djstripe.models.Account.get_default_account", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) + @patch( + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, + ) @patch( "stripe.Subscription.retrieve", return_value=deepcopy(FAKE_SUBSCRIPTION), @@ -78,6 +90,7 @@ def test_retry_true( product_retrieve_mock, charge_retrieve_mock, subscription_retrieve_mock, + balance_transaction_retrieve_mock, default_account_mock, invoice_retrieve_mock, ): @@ -110,6 +123,11 @@ def test_retry_true( "djstripe.models.Account.get_default_account", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) + @patch( + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, + ) @patch( "stripe.Subscription.retrieve", return_value=deepcopy(FAKE_SUBSCRIPTION), @@ -122,6 +140,7 @@ def test_retry_false( product_retrieve_mock, charge_retrieve_mock, subscription_retrieve_mock, + balance_transaction_retrieve_mock, default_account_mock, invoice_retrieve_mock, ): @@ -150,6 +169,11 @@ def test_retry_false( "djstripe.models.Account.get_default_account", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) + @patch( + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, + ) @patch( "stripe.Subscription.retrieve", return_value=deepcopy(FAKE_SUBSCRIPTION), @@ -162,6 +186,7 @@ def test_status_paid( product_retrieve_mock, charge_retrieve_mock, subscription_retrieve_mock, + balance_transaction_retrieve_mock, default_account_mock, ): default_account_mock.return_value = self.account @@ -184,6 +209,11 @@ def test_status_paid( "djstripe.models.Account.get_default_account", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) + @patch( + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, + ) @patch( "stripe.Subscription.retrieve", return_value=deepcopy(FAKE_SUBSCRIPTION), @@ -196,6 +226,7 @@ def test_status_open( product_retrieve_mock, charge_retrieve_mock, subscription_retrieve_mock, + balance_transaction_retrieve_mock, default_account_mock, ): default_account_mock.return_value = self.account @@ -220,6 +251,11 @@ def test_status_open( "djstripe.models.Account.get_default_account", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) + @patch( + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, + ) @patch( "stripe.Subscription.retrieve", return_value=deepcopy(FAKE_SUBSCRIPTION), @@ -232,6 +268,7 @@ def test_status_forgiven( product_retrieve_mock, charge_retrieve_mock, subscription_retrieve_mock, + balance_transaction_retrieve_mock, default_account_mock, ): default_account_mock.return_value = self.account @@ -256,6 +293,11 @@ def test_status_forgiven( "djstripe.models.Account.get_default_account", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) + @patch( + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, + ) @patch( "stripe.Subscription.retrieve", return_value=deepcopy(FAKE_SUBSCRIPTION), @@ -268,6 +310,7 @@ def test_status_forgiven_deprecated( product_retrieve_mock, charge_retrieve_mock, subscription_retrieve_mock, + balance_transaction_retrieve_mock, default_account_mock, ): # forgiven parameter deprecated in API 2018-11-08 - see https://stripe.com/docs/upgrades#2018-11-08 @@ -285,6 +328,11 @@ def test_status_forgiven_deprecated( "djstripe.models.Account.get_default_account", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) + @patch( + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, + ) @patch( "stripe.Subscription.retrieve", return_value=deepcopy(FAKE_SUBSCRIPTION), @@ -297,6 +345,7 @@ def test_status_forgiven_default( product_retrieve_mock, charge_retrieve_mock, subscription_retrieve_mock, + balance_transaction_retrieve_mock, default_account_mock, ): # forgiven parameter deprecated in API 2018-11-08 - see https://stripe.com/docs/upgrades#2018-11-08 @@ -313,6 +362,11 @@ def test_status_forgiven_default( "djstripe.models.Account.get_default_account", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) + @patch( + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, + ) @patch( "stripe.Subscription.retrieve", return_value=deepcopy(FAKE_SUBSCRIPTION), @@ -325,6 +379,7 @@ def test_status_closed( product_retrieve_mock, charge_retrieve_mock, subscription_retrieve_mock, + balance_transaction_retrieve_mock, default_account_mock, ): default_account_mock.return_value = self.account @@ -349,6 +404,11 @@ def test_status_closed( "djstripe.models.Account.get_default_account", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) + @patch( + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, + ) @patch( "stripe.Subscription.retrieve", return_value=deepcopy(FAKE_SUBSCRIPTION), @@ -361,6 +421,7 @@ def test_status_closed_deprecated( product_retrieve_mock, charge_retrieve_mock, subscription_retrieve_mock, + balance_transaction_retrieve_mock, default_account_mock, ): # closed parameter deprecated in API 2018-11-08 - see https://stripe.com/docs/upgrades#2018-11-08 @@ -378,6 +439,11 @@ def test_status_closed_deprecated( "djstripe.models.Account.get_default_account", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) + @patch( + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, + ) @patch( "stripe.Subscription.retrieve", return_value=deepcopy(FAKE_SUBSCRIPTION), @@ -390,6 +456,7 @@ def test_status_closed_default( product_retrieve_mock, charge_retrieve_mock, subscription_retrieve_mock, + balance_transaction_retrieve_mock, default_account_mock, ): # closed parameter deprecated in API 2018-11-08 - see https://stripe.com/docs/upgrades#2018-11-08 @@ -407,6 +474,11 @@ def test_status_closed_default( "djstripe.models.Account.get_default_account", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) + @patch( + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, + ) @patch( "stripe.Plan.retrieve", return_value=deepcopy(FAKE_PLAN), @@ -425,6 +497,7 @@ def test_sync_no_subscription( charge_retrieve_mock, subscription_retrieve_mock, plan_retrieve_mock, + balance_transaction_retrieve_mock, default_account_mock, ): default_account_mock.return_value = self.account @@ -457,6 +530,11 @@ def test_sync_no_subscription( "djstripe.models.Account.get_default_account", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) + @patch( + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, + ) @patch( "stripe.Subscription.retrieve", return_value=deepcopy(FAKE_SUBSCRIPTION), @@ -469,6 +547,7 @@ def test_invoice_with_subscription_invoice_items( product_retrieve_mock, charge_retrieve_mock, subscription_retrieve_mock, + balance_transaction_retrieve_mock, default_account_mock, ): default_account_mock.return_value = self.account @@ -497,6 +576,11 @@ def test_invoice_with_subscription_invoice_items( "djstripe.models.Account.get_default_account", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) + @patch( + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, + ) @patch( "stripe.Subscription.retrieve", return_value=deepcopy(FAKE_SUBSCRIPTION), @@ -509,6 +593,7 @@ def test_invoice_with_no_invoice_items( product_retrieve_mock, charge_retrieve_mock, subscription_retrieve_mock, + balance_transaction_retrieve_mock, default_account_mock, ): default_account_mock.return_value = self.account @@ -534,6 +619,11 @@ def test_invoice_with_no_invoice_items( "djstripe.models.Account.get_default_account", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) + @patch( + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, + ) @patch( "stripe.Subscription.retrieve", return_value=deepcopy(FAKE_SUBSCRIPTION), @@ -546,6 +636,7 @@ def test_invoice_with_non_subscription_invoice_items( product_retrieve_mock, charge_retrieve_mock, subscription_retrieve_mock, + balance_transaction_retrieve_mock, default_account_mock, ): default_account_mock.return_value = self.account @@ -572,6 +663,11 @@ def test_invoice_with_non_subscription_invoice_items( "djstripe.models.Account.get_default_account", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) + @patch( + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, + ) @patch( "stripe.Subscription.retrieve", return_value=deepcopy(FAKE_SUBSCRIPTION), @@ -584,6 +680,7 @@ def test_invoice_plan_from_invoice_items( product_retrieve_mock, charge_retrieve_mock, subscription_retrieve_mock, + balance_transaction_retrieve_mock, default_account_mock, ): default_account_mock.return_value = self.account @@ -608,6 +705,11 @@ def test_invoice_plan_from_invoice_items( "djstripe.models.Account.get_default_account", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) + @patch( + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, + ) @patch( "stripe.Subscription.retrieve", return_value=deepcopy(FAKE_SUBSCRIPTION), @@ -620,6 +722,7 @@ def test_invoice_plan_from_subscription( product_retrieve_mock, charge_retrieve_mock, subscription_retrieve_mock, + balance_transaction_retrieve_mock, default_account_mock, ): default_account_mock.return_value = self.account @@ -644,6 +747,11 @@ def test_invoice_plan_from_subscription( "djstripe.models.Account.get_default_account", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) + @patch( + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, + ) @patch( "stripe.Subscription.retrieve", return_value=deepcopy(FAKE_SUBSCRIPTION), @@ -651,7 +759,11 @@ def test_invoice_plan_from_subscription( ) @patch("stripe.Charge.retrieve", return_value=deepcopy(FAKE_CHARGE), autospec=True) def test_invoice_without_plan( - self, charge_retrieve_mock, subscription_retrieve_mock, default_account_mock + self, + charge_retrieve_mock, + subscription_retrieve_mock, + balance_transaction_retrieve_mock, + default_account_mock, ): default_account_mock.return_value = self.account diff --git a/tests/test_invoiceitem.py b/tests/test_invoiceitem.py index 22cf2b3ff2..eb8de27cab 100644 --- a/tests/test_invoiceitem.py +++ b/tests/test_invoiceitem.py @@ -10,8 +10,8 @@ from djstripe.settings import STRIPE_SECRET_KEY from . import ( - FAKE_CHARGE_II, FAKE_CUSTOMER_II, FAKE_INVOICE_II, FAKE_INVOICEITEM, - FAKE_PLAN_II, FAKE_PRODUCT, FAKE_SUBSCRIPTION_III, + FAKE_BALANCE_TRANSACTION, FAKE_CHARGE_II, FAKE_CUSTOMER_II, FAKE_INVOICE_II, + FAKE_INVOICEITEM, FAKE_PLAN_II, FAKE_PRODUCT, FAKE_SUBSCRIPTION_III, IS_STATICMETHOD_AUTOSPEC_SUPPORTED, AssertStripeFksMixin, default_account ) @@ -24,6 +24,11 @@ def setUp(self): "djstripe.models.Account.get_default_account", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) + @patch( + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, + ) @patch("stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT), autospec=True) @patch( "stripe.Subscription.retrieve", @@ -44,6 +49,7 @@ def test_str( customer_retrieve_mock, subscription_retrieve_mock, product_retrieve_mock, + balance_transaction_retrieve_mock, default_account_mock, ): default_account_mock.return_value = self.account @@ -67,6 +73,11 @@ def test_str( "djstripe.models.Account.get_default_account", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) + @patch( + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, + ) @patch("stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT), autospec=True) @patch( "stripe.Subscription.retrieve", @@ -87,6 +98,7 @@ def test_sync_with_subscription( customer_retrieve_mock, subscription_retrieve_mock, product_retrieve_mock, + balance_transaction_retrieve_mock, default_account_mock, ): default_account_mock.return_value = self.account @@ -119,6 +131,11 @@ def test_sync_with_subscription( "djstripe.models.Account.get_default_account", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) + @patch( + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, + ) @patch("stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT), autospec=True) @patch( "stripe.Subscription.retrieve", @@ -137,6 +154,7 @@ def test_sync_expanded_invoice_with_subscription( customer_retrieve_mock, subscription_retrieve_mock, product_retrieve_mock, + balance_transaction_retrieve_mock, default_account_mock, ): default_account_mock.return_value = self.account @@ -171,6 +189,11 @@ def test_sync_expanded_invoice_with_subscription( "djstripe.models.Account.get_default_account", autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED, ) + @patch( + "stripe.BalanceTransaction.retrieve", + return_value=deepcopy(FAKE_BALANCE_TRANSACTION), + autospec=True, + ) @patch("stripe.Plan.retrieve", return_value=deepcopy(FAKE_PLAN_II), autospec=True) @patch("stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT), autospec=True) @patch( @@ -193,6 +216,7 @@ def test_sync_proration( subscription_retrieve_mock, product_retrieve_mock, plan_retrieve_mock, + balance_transaction_retrieve_mock, default_account_mock, ): default_account_mock.return_value = self.account