Skip to content

Commit

Permalink
Make test FAKE_CHARGE more realistic
Browse files Browse the repository at this point in the history
balance_transaction isn't expanded in normal retrieve or charge webhook
  • Loading branch information
therefromhere committed May 11, 2019
1 parent 1546a56 commit 8b70981
Show file tree
Hide file tree
Showing 6 changed files with 327 additions and 41 deletions.
4 changes: 2 additions & 2 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
83 changes: 66 additions & 17 deletions tests/test_charge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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())
Expand 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)
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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,
Expand All @@ -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)
Expand All @@ -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,
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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,
Expand All @@ -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(
Expand All @@ -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)
Expand All @@ -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,
Expand Down
Loading

0 comments on commit 8b70981

Please sign in to comment.