forked from dj-stripe/dj-stripe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_subscription_schedule.py
226 lines (208 loc) · 7.63 KB
/
test_subscription_schedule.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
"""
dj-stripe SubscriptionSchedule model tests.
"""
from copy import deepcopy
from unittest.mock import patch
import stripe
from django.contrib.auth import get_user_model
from django.test import TestCase
from djstripe.enums import SubscriptionScheduleStatus
from djstripe.models import Invoice, SubscriptionSchedule
from djstripe.settings import djstripe_settings
from . import (
FAKE_BALANCE_TRANSACTION,
FAKE_CARD_AS_PAYMENT_METHOD,
FAKE_CHARGE,
FAKE_CUSTOMER,
FAKE_INVOICE,
FAKE_PAYMENT_INTENT_I,
FAKE_PLAN,
FAKE_PRODUCT,
FAKE_SUBSCRIPTION,
FAKE_SUBSCRIPTION_SCHEDULE,
AssertStripeFksMixin,
datetime_to_unix,
)
class SubscriptionScheduleTest(AssertStripeFksMixin, TestCase):
@patch(
"stripe.BalanceTransaction.retrieve",
return_value=deepcopy(FAKE_BALANCE_TRANSACTION),
autospec=True,
)
@patch(
"stripe.Subscription.retrieve",
return_value=deepcopy(FAKE_SUBSCRIPTION),
autospec=True,
)
@patch("stripe.Charge.retrieve", return_value=deepcopy(FAKE_CHARGE), autospec=True)
@patch(
"stripe.PaymentMethod.retrieve",
return_value=deepcopy(FAKE_CARD_AS_PAYMENT_METHOD),
autospec=True,
)
@patch(
"stripe.PaymentIntent.retrieve",
return_value=deepcopy(FAKE_PAYMENT_INTENT_I),
autospec=True,
)
@patch(
"stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT), autospec=True
)
@patch(
"stripe.Invoice.retrieve", autospec=True, return_value=deepcopy(FAKE_INVOICE)
)
def setUp(
self,
invoice_retrieve_mock,
product_retrieve_mock,
payment_intent_retrieve_mock,
paymentmethod_card_retrieve_mock,
charge_retrieve_mock,
subscription_retrieve_mock,
balance_transaction_retrieve_mock,
):
self.user = get_user_model().objects.create_user(
username="pydanny", email="[email protected]"
)
self.customer = FAKE_CUSTOMER.create_for_user(self.user)
self.default_expected_blank_fks = {
"djstripe.Customer.coupon",
"djstripe.Customer.default_payment_method",
"djstripe.Charge.application_fee",
"djstripe.Charge.dispute",
"djstripe.Charge.latest_upcominginvoice (related name)",
"djstripe.Charge.on_behalf_of",
"djstripe.Charge.source_transfer",
"djstripe.Charge.transfer",
"djstripe.PaymentIntent.on_behalf_of",
"djstripe.PaymentIntent.payment_method",
"djstripe.PaymentIntent.upcominginvoice (related name)",
"djstripe.Invoice.default_payment_method",
"djstripe.Invoice.default_source",
"djstripe.Subscription.default_payment_method",
"djstripe.Subscription.default_source",
"djstripe.Subscription.pending_setup_intent",
"djstripe.Subscription.schedule",
"djstripe.SubscriptionSchedule.released_subscription",
}
# create latest invoice
Invoice.sync_from_stripe_data(deepcopy(FAKE_INVOICE))
@patch("stripe.Plan.retrieve", return_value=deepcopy(FAKE_PLAN), autospec=True)
@patch(
"stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT), autospec=True
)
@patch(
"stripe.Customer.retrieve", return_value=deepcopy(FAKE_CUSTOMER), autospec=True
)
def test_sync_from_stripe_data(
self,
customer_retrieve_mock,
product_retrieve_mock,
plan_retrieve_mock,
):
canceled_schedule_fake = deepcopy(FAKE_SUBSCRIPTION_SCHEDULE)
canceled_schedule_fake["canceled_at"] = 1624553655
canceled_schedule_fake["status"] = SubscriptionScheduleStatus.canceled
schedule = SubscriptionSchedule.sync_from_stripe_data(canceled_schedule_fake)
self.assert_fks(schedule, expected_blank_fks=self.default_expected_blank_fks)
self.assertEqual(datetime_to_unix(schedule.canceled_at), 1624553655)
self.assertEqual(schedule.subscription.id, FAKE_SUBSCRIPTION["id"])
@patch("stripe.Plan.retrieve", return_value=deepcopy(FAKE_PLAN), autospec=True)
@patch(
"stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT), autospec=True
)
@patch(
"stripe.Customer.retrieve", return_value=deepcopy(FAKE_CUSTOMER), autospec=True
)
def test___str__(
self,
customer_retrieve_mock,
product_retrieve_mock,
plan_retrieve_mock,
):
schedule = SubscriptionSchedule.sync_from_stripe_data(
deepcopy(FAKE_SUBSCRIPTION_SCHEDULE)
)
self.assertEqual(f"<id={FAKE_SUBSCRIPTION_SCHEDULE['id']}>", str(schedule))
self.assert_fks(schedule, expected_blank_fks=self.default_expected_blank_fks)
@patch("stripe.Plan.retrieve", return_value=deepcopy(FAKE_PLAN), autospec=True)
@patch(
"stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT), autospec=True
)
@patch(
"stripe.Customer.retrieve", return_value=deepcopy(FAKE_CUSTOMER), autospec=True
)
def test_release(
self,
customer_retrieve_mock,
product_retrieve_mock,
plan_retrieve_mock,
):
schedule = SubscriptionSchedule.sync_from_stripe_data(
deepcopy(FAKE_SUBSCRIPTION_SCHEDULE)
)
with patch.object(
stripe.SubscriptionSchedule,
"release",
return_value=FAKE_SUBSCRIPTION_SCHEDULE,
) as patched__api_update:
schedule.release()
patched__api_update.assert_called_once_with(
FAKE_SUBSCRIPTION_SCHEDULE["id"],
api_key=djstripe_settings.STRIPE_SECRET_KEY,
stripe_account=schedule.djstripe_owner_account.id,
)
@patch("stripe.Plan.retrieve", return_value=deepcopy(FAKE_PLAN), autospec=True)
@patch(
"stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT), autospec=True
)
@patch(
"stripe.Customer.retrieve", return_value=deepcopy(FAKE_CUSTOMER), autospec=True
)
def test_cancel(
self,
customer_retrieve_mock,
product_retrieve_mock,
plan_retrieve_mock,
):
schedule = SubscriptionSchedule.sync_from_stripe_data(
deepcopy(FAKE_SUBSCRIPTION_SCHEDULE)
)
with patch.object(
stripe.SubscriptionSchedule,
"cancel",
return_value=FAKE_SUBSCRIPTION_SCHEDULE,
) as patched__api_update:
schedule.cancel()
patched__api_update.assert_called_once_with(
FAKE_SUBSCRIPTION_SCHEDULE["id"],
api_key=djstripe_settings.STRIPE_SECRET_KEY,
stripe_account=schedule.djstripe_owner_account.id,
)
@patch("stripe.Plan.retrieve", return_value=deepcopy(FAKE_PLAN), autospec=True)
@patch(
"stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT), autospec=True
)
@patch(
"stripe.Customer.retrieve", return_value=deepcopy(FAKE_CUSTOMER), autospec=True
)
def test_update(
self,
customer_retrieve_mock,
product_retrieve_mock,
plan_retrieve_mock,
):
schedule = SubscriptionSchedule.sync_from_stripe_data(
deepcopy(FAKE_SUBSCRIPTION_SCHEDULE)
)
with patch.object(
stripe.SubscriptionSchedule,
"modify",
return_value=FAKE_SUBSCRIPTION_SCHEDULE,
) as patched__api_update:
schedule.update()
patched__api_update.assert_called_once_with(
FAKE_SUBSCRIPTION_SCHEDULE["id"],
api_key=djstripe_settings.STRIPE_SECRET_KEY,
stripe_account=schedule.djstripe_owner_account.id,
)