forked from dj-stripe/dj-stripe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_plan.py
280 lines (223 loc) · 8.79 KB
/
test_plan.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
"""
dj-stripe Plan Model Tests.
"""
from copy import deepcopy
from decimal import Decimal
from unittest.mock import patch
from django.contrib.admin.sites import AdminSite
from django.test import TestCase
from djstripe.admin import PlanAdmin
from djstripe.enums import PlanUsageType
from djstripe.models import Plan, Product
from djstripe.settings import STRIPE_SECRET_KEY
from . import (
FAKE_PLAN, FAKE_PLAN_II, FAKE_PLAN_METERED,
FAKE_PRODUCT, FAKE_TIER_PLAN, AssertStripeFksMixin
)
class TestPlanAdmin(TestCase):
class FakeForm(object):
cleaned_data = {}
class FakeRequest(object):
pass
def setUp(self):
with patch(
"stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT), autospec=True
):
self.plan = Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN))
self.site = AdminSite()
self.plan_admin = PlanAdmin(Plan, self.site)
@patch("stripe.Plan.retrieve", autospec=True)
def test_update_name(self, plan_retrieve_mock):
new_name = "Updated Plan Name"
self.plan.name = new_name
self.plan.update_name()
# Would throw DoesNotExist if it didn't work
Plan.objects.get(name="Updated Plan Name")
@patch("stripe.Plan.create", return_value=FAKE_PLAN_II, autospec=True)
@patch("stripe.Plan.retrieve", autospec=True)
def test_that_admin_save_does_create_new_object(
self, plan_retrieve_mock, plan_create_mock
):
fake_form = self.FakeForm()
plan_data = Plan._stripe_object_to_record(deepcopy(FAKE_PLAN_II))
fake_form.cleaned_data = plan_data
self.plan_admin.save_model(
request=self.FakeRequest(), obj=None, form=fake_form, change=False
)
# Would throw DoesNotExist if it didn't work
Plan.objects.get(id=plan_data["id"])
@patch("stripe.Plan.create", autospec=True)
@patch("stripe.Plan.retrieve", autospec=True)
def test_that_admin_save_does_update_object(
self, plan_retrieve_mock, plan_create_mock
):
self.plan.name = "A new name (again)"
self.plan_admin.save_model(
request=self.FakeRequest(), obj=self.plan, form=self.FakeForm(), change=True
)
# Would throw DoesNotExist if it didn't work
Plan.objects.get(name=self.plan.name)
class PlanCreateTest(AssertStripeFksMixin, TestCase):
def setUp(self):
with patch(
"stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT), autospec=True
):
self.stripe_product = Product(id=FAKE_PRODUCT["id"]).api_retrieve()
@patch("stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT), autospec=True)
@patch("stripe.Plan.create", return_value=deepcopy(FAKE_PLAN), autospec=True)
def test_create_from_product_id(self, plan_create_mock, product_retrieve_mock):
fake_plan = deepcopy(FAKE_PLAN)
fake_plan["amount"] = fake_plan["amount"] / 100
self.assertIsInstance(fake_plan["product"], str)
plan = Plan.create(**fake_plan)
expected_create_kwargs = deepcopy(FAKE_PLAN)
expected_create_kwargs["api_key"] = STRIPE_SECRET_KEY
plan_create_mock.assert_called_once_with(**expected_create_kwargs)
self.assert_fks(plan, expected_blank_fks={"djstripe.Customer.coupon"})
@patch("stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT), autospec=True)
@patch("stripe.Plan.create", return_value=deepcopy(FAKE_PLAN), autospec=True)
def test_create_from_stripe_product(self, plan_create_mock, product_retrieve_mock):
fake_plan = deepcopy(FAKE_PLAN)
fake_plan["product"] = self.stripe_product
fake_plan["amount"] = fake_plan["amount"] / 100
self.assertIsInstance(fake_plan["product"], dict)
plan = Plan.create(**fake_plan)
expected_create_kwargs = deepcopy(FAKE_PLAN)
expected_create_kwargs["product"] = self.stripe_product
plan_create_mock.assert_called_once_with(
api_key=STRIPE_SECRET_KEY, **expected_create_kwargs
)
self.assert_fks(plan, expected_blank_fks={"djstripe.Customer.coupon"})
@patch("stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT), autospec=True)
@patch("stripe.Plan.create", return_value=deepcopy(FAKE_PLAN), autospec=True)
def test_create_from_djstripe_product(self, plan_create_mock, product_retrieve_mock):
fake_plan = deepcopy(FAKE_PLAN)
fake_plan["product"] = Product.sync_from_stripe_data(self.stripe_product)
fake_plan["amount"] = fake_plan["amount"] / 100
self.assertIsInstance(fake_plan["product"], Product)
plan = Plan.create(**fake_plan)
plan_create_mock.assert_called_once_with(api_key=STRIPE_SECRET_KEY, **FAKE_PLAN)
self.assert_fks(plan, expected_blank_fks={"djstripe.Customer.coupon"})
@patch("stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT), autospec=True)
@patch("stripe.Plan.create", return_value=deepcopy(FAKE_PLAN), autospec=True)
def test_create_with_metadata(self, plan_create_mock, product_retrieve_mock):
metadata = {"other_data": "more_data"}
fake_plan = deepcopy(FAKE_PLAN)
fake_plan["amount"] = fake_plan["amount"] / 100
fake_plan["metadata"] = metadata
self.assertIsInstance(fake_plan["product"], str)
plan = Plan.create(**fake_plan)
expected_create_kwargs = deepcopy(FAKE_PLAN)
expected_create_kwargs["metadata"] = metadata
plan_create_mock.assert_called_once_with(
api_key=STRIPE_SECRET_KEY, **expected_create_kwargs
)
self.assert_fks(plan, expected_blank_fks={"djstripe.Customer.coupon"})
class PlanTest(AssertStripeFksMixin, TestCase):
def setUp(self):
self.plan_data = deepcopy(FAKE_PLAN)
with patch(
"stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT), autospec=True
):
self.plan = Plan.sync_from_stripe_data(self.plan_data)
def test_str(self):
self.assertEqual(str(self.plan), self.plan_data["nickname"])
@patch("stripe.Plan.retrieve", return_value=FAKE_PLAN, autospec=True)
def test_stripe_plan(self, plan_retrieve_mock):
stripe_plan = self.plan.api_retrieve()
plan_retrieve_mock.assert_called_once_with(
id=self.plan_data["id"], api_key=STRIPE_SECRET_KEY, expand=[]
)
plan = Plan.sync_from_stripe_data(stripe_plan)
assert plan.amount_in_cents == plan.amount * 100
assert isinstance(plan.amount_in_cents, int)
self.assert_fks(plan, expected_blank_fks={"djstripe.Customer.coupon"})
@patch("stripe.Product.retrieve", autospec=True)
def test_stripe_plan_null_product(self, product_retrieve_mock):
"""
assert that plan.Product can be null for backwards compatibility
though note that it is a Stripe required field
"""
plan_data = deepcopy(FAKE_PLAN_II)
del plan_data["product"]
plan = Plan.sync_from_stripe_data(plan_data)
self.assert_fks(
plan, expected_blank_fks={"djstripe.Customer.coupon", "djstripe.Plan.product"}
)
@patch("stripe.Plan.retrieve", autospec=True)
def test_stripe_tier_plan(self, plan_retrieve_mock):
tier_plan_data = deepcopy(FAKE_TIER_PLAN)
plan = Plan.sync_from_stripe_data(tier_plan_data)
self.assertEqual(plan.id, tier_plan_data["id"])
self.assertIsNone(plan.amount)
self.assertIsNotNone(plan.tiers)
self.assert_fks(plan, expected_blank_fks={"djstripe.Customer.coupon"})
@patch("stripe.Plan.retrieve", autospec=True)
def test_stripe_metered_plan(self, plan_retrieve_mock):
plan_data = deepcopy(FAKE_PLAN_METERED)
plan = Plan.sync_from_stripe_data(plan_data)
self.assertEqual(plan.id, plan_data["id"])
self.assertEqual(plan.usage_type, PlanUsageType.metered)
self.assertIsNotNone(plan.amount)
self.assert_fks(plan, expected_blank_fks={"djstripe.Customer.coupon"})
class HumanReadablePlanTest(TestCase):
def test_human_readable_free_usd_daily(self):
plan = Plan.objects.create(
id="plan-test-free-usd-daily",
active=True,
amount=0,
currency="usd",
interval="day",
interval_count=1,
)
self.assertEqual(plan.human_readable_price, "$0.00 USD/day")
def test_human_readable_10_usd_weekly(self):
plan = Plan.objects.create(
id="plan-test-10-usd-weekly",
active=True,
amount=10,
currency="usd",
interval="week",
interval_count=1,
)
self.assertEqual(plan.human_readable_price, "$10.00 USD/week")
def test_human_readable_10_usd_2weeks(self):
plan = Plan.objects.create(
id="plan-test-10-usd-2w",
active=True,
amount=10,
currency="usd",
interval="week",
interval_count=2,
)
self.assertEqual(plan.human_readable_price, "$10.00 USD every 2 weeks")
def test_human_readable_499_usd_monthly(self):
plan = Plan.objects.create(
id="plan-test-499-usd-monthly",
active=True,
amount=Decimal("4.99"),
currency="usd",
interval="month",
interval_count=1,
)
self.assertEqual(plan.human_readable_price, "$4.99 USD/month")
def test_human_readable_25_usd_6months(self):
plan = Plan.objects.create(
id="plan-test-25-usd-6m",
active=True,
amount=25,
currency="usd",
interval="month",
interval_count=6,
)
self.assertEqual(plan.human_readable_price, "$25.00 USD every 6 months")
def test_human_readable_10_usd_yearly(self):
plan = Plan.objects.create(
id="plan-test-10-usd-yearly",
active=True,
amount=10,
currency="usd",
interval="year",
interval_count=1,
)
self.assertEqual(plan.human_readable_price, "$10.00 USD/year")