forked from dj-stripe/dj-stripe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_transfer_reversal.py
179 lines (156 loc) · 5.8 KB
/
test_transfer_reversal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
"""
dj-stripe TransferReversal model tests
"""
from copy import deepcopy
from unittest.mock import PropertyMock, patch
import pytest
from django.test.testcases import TestCase
from djstripe.models import Transfer, TransferReversal
from djstripe.settings import djstripe_settings
from . import (
FAKE_BALANCE_TRANSACTION_II,
FAKE_PLATFORM_ACCOUNT,
FAKE_TRANSFER,
FAKE_TRANSFER_WITH_1_REVERSAL,
AssertStripeFksMixin,
)
pytestmark = pytest.mark.django_db
class TestTransferReversalStr(TestCase):
@patch.object(Transfer, "_attach_objects_post_save_hook")
@patch(
"stripe.Account.retrieve",
return_value=deepcopy(FAKE_PLATFORM_ACCOUNT),
autospec=True,
)
@patch(
"stripe.BalanceTransaction.retrieve",
return_value=deepcopy(FAKE_BALANCE_TRANSACTION_II),
autospec=True,
)
@patch(
"stripe.Transfer.retrieve_reversal",
autospec=True,
return_value=deepcopy(FAKE_TRANSFER_WITH_1_REVERSAL),
)
def test___str__(
self,
transfer_reversal_retrieve_mock,
balance_transaction_retrieve_mock,
account_retrieve_mock,
transfer__attach_object_post_save_hook_mock,
):
transfer_reversal = TransferReversal.sync_from_stripe_data(
deepcopy(FAKE_TRANSFER_WITH_1_REVERSAL["reversals"]["data"][0])
)
self.assertEqual(str(f"{transfer_reversal.transfer}"), str(transfer_reversal))
class TestTransfer(AssertStripeFksMixin, TestCase):
@patch.object(Transfer, "_attach_objects_post_save_hook")
@patch(
"stripe.Account.retrieve",
return_value=deepcopy(FAKE_PLATFORM_ACCOUNT),
autospec=True,
)
@patch(
"stripe.BalanceTransaction.retrieve",
return_value=deepcopy(FAKE_BALANCE_TRANSACTION_II),
autospec=True,
)
@patch(
"stripe.Transfer.retrieve_reversal",
autospec=True,
return_value=deepcopy(FAKE_TRANSFER_WITH_1_REVERSAL["reversals"]["data"][0]),
)
def test_sync_from_stripe_data(
self,
transfer_reversal_retrieve_mock,
balance_transaction_retrieve_mock,
account_retrieve_mock,
transfer__attach_object_post_save_hook_mock,
):
transfer_reversal = TransferReversal.sync_from_stripe_data(
deepcopy(FAKE_TRANSFER_WITH_1_REVERSAL["reversals"]["data"][0])
)
balance_transaction_retrieve_mock.assert_not_called()
transfer_reversal_retrieve_mock.assert_not_called()
assert (
transfer_reversal.balance_transaction.id
== FAKE_TRANSFER["balance_transaction"]["id"]
)
assert (
transfer_reversal.transfer.id
== FAKE_TRANSFER_WITH_1_REVERSAL["reversals"]["data"][0]["transfer"]["id"]
)
self.assert_fks(transfer_reversal, expected_blank_fks="")
@patch.object(Transfer, "_attach_objects_post_save_hook")
@patch(
"stripe.Account.retrieve",
return_value=deepcopy(FAKE_PLATFORM_ACCOUNT),
autospec=True,
)
@patch(
"stripe.BalanceTransaction.retrieve",
return_value=deepcopy(FAKE_BALANCE_TRANSACTION_II),
autospec=True,
)
@patch(
"stripe.Transfer.retrieve_reversal",
autospec=True,
return_value=deepcopy(FAKE_TRANSFER_WITH_1_REVERSAL),
)
def test_api_retrieve(
self,
transfer_reversal_retrieve_mock,
balance_transaction_retrieve_mock,
account_retrieve_mock,
transfer__attach_object_post_save_hook_mock,
):
transfer_reversal = TransferReversal.sync_from_stripe_data(
deepcopy(FAKE_TRANSFER_WITH_1_REVERSAL["reversals"]["data"][0])
)
transfer_reversal.api_retrieve()
transfer_reversal_retrieve_mock.assert_called_once_with(
id=FAKE_TRANSFER_WITH_1_REVERSAL["id"],
nested_id=FAKE_TRANSFER_WITH_1_REVERSAL["reversals"]["data"][0]["id"],
api_key=djstripe_settings.STRIPE_SECRET_KEY,
expand=["balance_transaction", "transfer"],
stripe_account=transfer_reversal.djstripe_owner_account.id,
)
@patch.object(Transfer, "_attach_objects_post_save_hook")
# we are returning any value for the Transfer.objects.get as we only need to avoid the Transfer.DoesNotExist error
@patch(
"djstripe.models.connect.Transfer.objects.get",
return_value=deepcopy(FAKE_TRANSFER),
)
@patch(
"stripe.Transfer.create_reversal",
autospec=True,
return_value=deepcopy(FAKE_TRANSFER_WITH_1_REVERSAL),
)
def test__api_create(
self,
transfer_reversal_create_mock,
transfer_get_mock,
transfer__attach_object_post_save_hook_mock,
):
TransferReversal._api_create(
id=FAKE_TRANSFER_WITH_1_REVERSAL["reversals"]["data"][0]["transfer"]["id"]
)
transfer_reversal_create_mock.assert_called_once_with(
id=FAKE_TRANSFER_WITH_1_REVERSAL["reversals"]["data"][0]["transfer"]["id"],
api_key=djstripe_settings.STRIPE_SECRET_KEY,
)
@patch("stripe.Transfer.list_reversals", autospec=True)
def test_api_list(self, transfer_reversal_list_mock):
p = PropertyMock(return_value=deepcopy(FAKE_TRANSFER_WITH_1_REVERSAL))
type(transfer_reversal_list_mock).auto_paging_iter = p
TransferReversal.api_list(
id=FAKE_TRANSFER_WITH_1_REVERSAL["reversals"]["data"][0]["transfer"]["id"]
)
transfer_reversal_list_mock.assert_called_once_with(
id=FAKE_TRANSFER_WITH_1_REVERSAL["reversals"]["data"][0]["transfer"]["id"],
api_key=djstripe_settings.STRIPE_SECRET_KEY,
)
def test_is_valid_object(self):
assert TransferReversal.is_valid_object(
deepcopy(FAKE_TRANSFER_WITH_1_REVERSAL["reversals"]["data"][0])
)