forked from dj-stripe/dj-stripe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_subscription_item.py
317 lines (296 loc) · 10.4 KB
/
test_subscription_item.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
"""
dj-stripe SubscriptionItem model tests
"""
from copy import deepcopy
from unittest.mock import patch
from django.contrib.auth import get_user_model
from django.test import TestCase
from djstripe.models import SubscriptionItem
from djstripe.models.billing import Invoice
from . import (
FAKE_BALANCE_TRANSACTION,
FAKE_CARD_AS_PAYMENT_METHOD,
FAKE_CHARGE,
FAKE_CUSTOMER,
FAKE_CUSTOMER_II,
FAKE_INVOICE,
FAKE_INVOICEITEM,
FAKE_PAYMENT_INTENT_I,
FAKE_PLAN,
FAKE_PLAN_II,
FAKE_PLAN_METERED,
FAKE_PRICE,
FAKE_PRICE_II,
FAKE_PRICE_METERED,
FAKE_PRODUCT,
FAKE_SUBSCRIPTION,
FAKE_SUBSCRIPTION_II,
FAKE_SUBSCRIPTION_ITEM,
FAKE_SUBSCRIPTION_ITEM_METERED,
FAKE_SUBSCRIPTION_ITEM_MULTI_PLAN,
FAKE_SUBSCRIPTION_ITEM_TAX_RATES,
FAKE_SUBSCRIPTION_METERED,
FAKE_SUBSCRIPTION_MULTI_PLAN,
FAKE_TAX_RATE_EXAMPLE_1_VAT,
AssertStripeFksMixin,
)
class SubscriptionItemTest(AssertStripeFksMixin, TestCase):
@patch(
"stripe.Customer.retrieve",
return_value=deepcopy(FAKE_CUSTOMER),
autospec=True,
)
@patch(
"stripe.BalanceTransaction.retrieve",
return_value=deepcopy(FAKE_BALANCE_TRANSACTION),
autospec=True,
)
@patch(
"stripe.SubscriptionItem.retrieve",
return_value=deepcopy(FAKE_SUBSCRIPTION_ITEM),
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.InvoiceItem.retrieve",
return_value=deepcopy(FAKE_INVOICEITEM),
autospec=True,
)
@patch(
"stripe.Invoice.retrieve", autospec=True, return_value=deepcopy(FAKE_INVOICE)
)
def setUp(
self,
invoice_retrieve_mock,
invoice_item_retrieve_mock,
product_retrieve_mock,
payment_intent_retrieve_mock,
paymentmethod_card_retrieve_mock,
charge_retrieve_mock,
subscription_retrieve_mock,
subscription_item_retrieve_mock,
balance_transaction_retrieve_mock,
customer_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.Product.default_price",
"djstripe.Subscription.default_payment_method",
"djstripe.Subscription.default_source",
"djstripe.Subscription.pending_setup_intent",
"djstripe.Subscription.schedule",
}
# create latest invoice
Invoice.sync_from_stripe_data(deepcopy(FAKE_INVOICE))
@patch(
"stripe.Price.retrieve",
return_value=deepcopy(FAKE_PRICE_METERED),
autospec=True,
)
@patch(
"stripe.Plan.retrieve", return_value=deepcopy(FAKE_PLAN_METERED), autospec=True
)
@patch(
"stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT), autospec=True
)
@patch(
"stripe.Customer.retrieve", return_value=deepcopy(FAKE_CUSTOMER), autospec=True
)
@patch(
"stripe.Subscription.retrieve",
return_value=deepcopy(FAKE_SUBSCRIPTION_METERED),
autospec=True,
)
def test_sync_from_stripe_data_metered_subscription(
self,
subscription_retrieve_mock,
customer_retrieve_mock,
product_retrieve_mock,
plan_retrieve_mock,
price_retrieve_mock,
):
subscription_item_fake = deepcopy(FAKE_SUBSCRIPTION_ITEM_METERED)
subscription_item = SubscriptionItem.sync_from_stripe_data(
subscription_item_fake
)
self.assertEqual(subscription_item.id, FAKE_SUBSCRIPTION_ITEM_METERED["id"])
self.assertEqual(
subscription_item.plan.id, FAKE_SUBSCRIPTION_ITEM_METERED["plan"]["id"]
)
self.assertEqual(
subscription_item.price.id, FAKE_SUBSCRIPTION_ITEM_METERED["price"]["id"]
)
self.assertEqual(
subscription_item.subscription.id,
FAKE_SUBSCRIPTION_ITEM_METERED["subscription"],
)
self.assert_fks(
subscription_item,
expected_blank_fks=(
self.default_expected_blank_fks
| {"djstripe.Subscription.latest_invoice"}
),
)
@patch(
"stripe.Price.retrieve",
return_value=deepcopy(FAKE_PRICE_II),
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(
"stripe.Customer.retrieve", return_value=deepcopy(FAKE_CUSTOMER), autospec=True
)
@patch(
"stripe.Subscription.retrieve",
autospec=True,
)
def test_sync_items_with_tax_rates(
self,
subscription_retrieve_mock,
customer_retrieve_mock,
product_retrieve_mock,
plan_retrieve_mock,
price_retrieve_mock,
):
fake_subscription = deepcopy(FAKE_SUBSCRIPTION_II)
fake_subscription["latest_invoice"] = FAKE_INVOICE["id"]
subscription_retrieve_mock.return_value = fake_subscription
subscription_item_fake = deepcopy(FAKE_SUBSCRIPTION_ITEM_TAX_RATES)
subscription_item = SubscriptionItem.sync_from_stripe_data(
subscription_item_fake
)
self.assertEqual(subscription_item.id, FAKE_SUBSCRIPTION_ITEM_TAX_RATES["id"])
self.assertEqual(
subscription_item.plan.id, FAKE_SUBSCRIPTION_ITEM_TAX_RATES["plan"]["id"]
)
self.assertEqual(
subscription_item.price.id, FAKE_SUBSCRIPTION_ITEM_TAX_RATES["price"]["id"]
)
self.assertEqual(
subscription_item.subscription.id,
FAKE_SUBSCRIPTION_ITEM_TAX_RATES["subscription"],
)
self.assert_fks(
subscription_item,
expected_blank_fks=(
self.default_expected_blank_fks
| {
"djstripe.Charge.latest_upcominginvoice (related name)",
"djstripe.Charge.application_fee",
"djstripe.Charge.dispute",
"djstripe.Charge.on_behalf_of",
"djstripe.Charge.source_transfer",
"djstripe.Charge.transfer",
"djstripe.PaymentIntent.upcominginvoice (related name)",
"djstripe.PaymentIntent.on_behalf_of",
"djstripe.PaymentIntent.payment_method",
"djstripe.Invoice.default_payment_method",
"djstripe.Invoice.default_source",
}
),
)
self.assertEqual(subscription_item.tax_rates.count(), 1)
self.assertEqual(
subscription_item.tax_rates.first().id, FAKE_TAX_RATE_EXAMPLE_1_VAT["id"]
)
@patch(
"stripe.Price.retrieve",
side_effect=[deepcopy(FAKE_PRICE), deepcopy(FAKE_PRICE_II)],
autospec=True,
)
@patch(
"stripe.Plan.retrieve",
side_effect=[deepcopy(FAKE_PLAN), deepcopy(FAKE_PLAN_II)],
autospec=True,
)
@patch(
"stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT), autospec=True
)
@patch(
"stripe.Customer.retrieve",
return_value=deepcopy(FAKE_CUSTOMER_II),
autospec=True,
)
@patch(
"stripe.Subscription.retrieve",
autospec=True,
)
def test_sync_multi_plan_subscription(
self,
subscription_retrieve_mock,
customer_retrieve_mock,
product_retrieve_mock,
plan_retrieve_mock,
price_retrieve_mock,
):
fake_subscription = deepcopy(FAKE_SUBSCRIPTION_MULTI_PLAN)
fake_subscription["latest_invoice"] = FAKE_INVOICE["id"]
subscription_retrieve_mock.return_value = fake_subscription
subscription_item_fake = deepcopy(FAKE_SUBSCRIPTION_ITEM_MULTI_PLAN)
subscription_item = SubscriptionItem.sync_from_stripe_data(
subscription_item_fake
)
self.assertEqual(subscription_item.id, FAKE_SUBSCRIPTION_ITEM_MULTI_PLAN["id"])
self.assertEqual(
subscription_item.plan.id, FAKE_SUBSCRIPTION_ITEM_MULTI_PLAN["plan"]["id"]
)
self.assertEqual(
subscription_item.price.id, FAKE_SUBSCRIPTION_ITEM_MULTI_PLAN["price"]["id"]
)
self.assertEqual(
subscription_item.subscription.id,
FAKE_SUBSCRIPTION_ITEM_MULTI_PLAN["subscription"],
)
# delete pydanny customer as that causes issues with Invoice and Latest_invoice FKs
self.customer.delete()
self.assert_fks(
subscription_item,
expected_blank_fks=(
self.default_expected_blank_fks
| {
"djstripe.Customer.subscriber",
"djstripe.Subscription.plan",
"djstripe.Charge.latest_upcominginvoice (related name)",
"djstripe.Charge.application_fee",
"djstripe.Charge.dispute",
"djstripe.Charge.on_behalf_of",
"djstripe.Charge.source_transfer",
"djstripe.Charge.transfer",
"djstripe.PaymentIntent.upcominginvoice (related name)",
"djstripe.PaymentIntent.on_behalf_of",
"djstripe.PaymentIntent.payment_method",
"djstripe.Invoice.default_payment_method",
"djstripe.Invoice.default_source",
"djstripe.Invoice.charge",
"djstripe.Invoice.customer",
"djstripe.Invoice.payment_intent",
"djstripe.Invoice.subscription",
}
),
)