-
Notifications
You must be signed in to change notification settings - Fork 286
/
test_customer.py
281 lines (259 loc) · 11.2 KB
/
test_customer.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
# pylint: disable=C0301
import decimal
from django.test import TestCase
from mock import patch
from ..models import Customer, Charge
from ..utils import get_user_model
class TestCustomer(TestCase):
def setUp(self):
self.User = get_user_model()
self.user = self.User.objects.create_user(
username="patrick",
email="paltman@eldarion.com"
)
self.customer = Customer.objects.create(
user=self.user,
stripe_id="cus_xxxxxxxxxxxxxxx",
card_fingerprint="YYYYYYYY",
card_last_4="2342",
card_kind="Visa"
)
@patch("stripe.Customer.retrieve")
@patch("stripe.Customer.create")
def test_customer_create_user_only(self, CreateMock, RetrieveMock):
self.customer.delete()
stripe_customer = CreateMock()
stripe_customer.active_card = None
stripe_customer.subscription = None
stripe_customer.id = "cus_YYYYYYYYYYYYY"
customer = Customer.create(self.user)
self.assertEqual(customer.user, self.user)
self.assertEqual(customer.stripe_id, "cus_YYYYYYYYYYYYY")
_, kwargs = CreateMock.call_args
self.assertEqual(kwargs["email"], self.user.email)
self.assertIsNone(kwargs["card"])
self.assertIsNone(kwargs["plan"])
self.assertIsNone(kwargs["trial_end"])
@patch("stripe.Invoice.create")
@patch("stripe.Customer.retrieve")
@patch("stripe.Customer.create")
def test_customer_create_user_with_plan(self, CreateMock, RetrieveMock, PayMock):
self.customer.delete()
stripe_customer = CreateMock()
stripe_customer.active_card = None
stripe_customer.subscription.plan.id = "pro-monthly"
stripe_customer.subscription.current_period_start = 1348876800
stripe_customer.subscription.current_period_end = 1349876800
stripe_customer.subscription.plan.amount = 9999
stripe_customer.subscription.status = "active"
stripe_customer.subscription.cancel_at_period_end = False
stripe_customer.subscription.start = 1348876800
stripe_customer.subscription.quantity = 1
stripe_customer.subscription.trial_start = 1348876800
stripe_customer.subscription.trial_end = 1349876800
stripe_customer.id = "cus_YYYYYYYYYYYYY"
customer = Customer.create(self.user, card="token232323", plan="pro")
self.assertEqual(customer.user, self.user)
self.assertEqual(customer.stripe_id, "cus_YYYYYYYYYYYYY")
_, kwargs = CreateMock.call_args
self.assertEqual(kwargs["email"], self.user.email)
self.assertEqual(kwargs["card"], "token232323")
self.assertEqual(kwargs["plan"], "pro-monthly")
self.assertIsNotNone(kwargs["trial_end"])
self.assertTrue(PayMock.called)
self.assertTrue(customer.current_subscription.plan, "pro")
# @@@ Need to figure out a way to tempmorarily set DEFAULT_PLAN to "entry" for this test
# @patch("stripe.Invoice.create")
# @patch("stripe.Customer.retrieve")
# @patch("stripe.Customer.create")
# def test_customer_create_user_with_card_default_plan(self, CreateMock, RetrieveMock, PayMock):
# self.customer.delete()
# stripe_customer = CreateMock()
# stripe_customer.active_card = None
# stripe_customer.subscription.plan.id = "entry-monthly"
# stripe_customer.subscription.current_period_start = 1348876800
# stripe_customer.subscription.current_period_end = 1349876800
# stripe_customer.subscription.plan.amount = 9999
# stripe_customer.subscription.status = "active"
# stripe_customer.subscription.cancel_at_period_end = False
# stripe_customer.subscription.start = 1348876800
# stripe_customer.subscription.quantity = 1
# stripe_customer.subscription.trial_start = 1348876800
# stripe_customer.subscription.trial_end = 1349876800
# stripe_customer.id = "cus_YYYYYYYYYYYYY"
# customer = Customer.create(self.user, card="token232323")
# self.assertEqual(customer.user, self.user)
# self.assertEqual(customer.stripe_id, "cus_YYYYYYYYYYYYY")
# _, kwargs = CreateMock.call_args
# self.assertEqual(kwargs["email"], self.user.email)
# self.assertEqual(kwargs["card"], "token232323")
# self.assertEqual(kwargs["plan"], "entry-monthly")
# self.assertIsNotNone(kwargs["trial_end"])
# self.assertTrue(PayMock.called)
# self.assertTrue(customer.current_subscription.plan, "entry")
@patch("stripe.Customer.retrieve")
def test_customer_subscribe_with_specified_quantity(self, CustomerRetrieveMock):
customer = CustomerRetrieveMock()
customer.subscription.plan.id = "entry-monthly"
customer.subscription.current_period_start = 1348360173
customer.subscription.current_period_end = 1375603198
customer.subscription.plan.amount = decimal.Decimal("9.57")
customer.subscription.status = "active"
customer.subscription.cancel_at_period_end = True
customer.subscription.start = 1348360173
customer.subscription.quantity = 1
customer.subscription.trial_start = None
customer.subscription.trial_end = None
self.customer.subscribe("entry", quantity=3, charge_immediately=False)
_, kwargs = customer.update_subscription.call_args
self.assertEqual(kwargs["quantity"], 3)
@patch("stripe.Customer.retrieve")
def test_customer_subscribe_with_callback_quantity(self, CustomerRetrieveMock):
customer = CustomerRetrieveMock()
customer.subscription.plan.id = "entry-monthly"
customer.subscription.current_period_start = 1348360173
customer.subscription.current_period_end = 1375603198
customer.subscription.plan.amount = decimal.Decimal("9.57")
customer.subscription.status = "active"
customer.subscription.cancel_at_period_end = True
customer.subscription.start = 1348360173
customer.subscription.quantity = 1
customer.subscription.trial_start = None
customer.subscription.trial_end = None
self.customer.subscribe("entry", charge_immediately=False)
_, kwargs = customer.update_subscription.call_args
self.assertEqual(kwargs["quantity"], 4)
@patch("stripe.Customer.retrieve")
def test_customer_purge_leaves_customer_record(self, CustomerRetrieveMock):
self.customer.purge()
customer = Customer.objects.get(stripe_id=self.customer.stripe_id)
self.assertTrue(customer.user is None)
self.assertTrue(customer.card_fingerprint == "")
self.assertTrue(customer.card_last_4 == "")
self.assertTrue(customer.card_kind == "")
self.assertTrue(self.User.objects.filter(pk=self.user.pk).exists())
@patch("stripe.Customer.retrieve")
def test_customer_delete_same_as_purge(self, CustomerRetrieveMock):
self.customer.delete()
customer = Customer.objects.get(stripe_id=self.customer.stripe_id)
self.assertTrue(customer.user is None)
self.assertTrue(customer.card_fingerprint == "")
self.assertTrue(customer.card_last_4 == "")
self.assertTrue(customer.card_kind == "")
self.assertTrue(self.User.objects.filter(pk=self.user.pk).exists())
def test_change_charge(self):
self.assertTrue(self.customer.can_charge())
@patch("stripe.Customer.retrieve")
def test_cannot_charge(self, CustomerRetrieveMock):
self.customer.delete()
self.assertFalse(self.customer.can_charge())
def test_charge_accepts_only_decimals(self):
with self.assertRaises(ValueError):
self.customer.charge(10)
@patch("stripe.Charge.retrieve")
def test_record_charge(self, RetrieveMock):
RetrieveMock.return_value = {
"id": "ch_XXXXXX",
"card": {
"last4": "4323",
"type": "Visa"
},
"amount": 1000,
"paid": True,
"refunded": False,
"fee": 499,
"dispute": None,
"created": 1363911708,
"customer": "cus_xxxxxxxxxxxxxxx"
}
obj = self.customer.record_charge("ch_XXXXXX")
self.assertEquals(Charge.objects.get(stripe_id="ch_XXXXXX").pk, obj.pk)
self.assertEquals(obj.paid, True)
self.assertEquals(obj.disputed, False)
self.assertEquals(obj.refunded, False)
self.assertEquals(obj.amount_refunded, None)
@patch("stripe.Charge.retrieve")
def test_refund_charge(self, RetrieveMock):
charge = Charge.objects.create(
stripe_id="ch_XXXXXX",
customer=self.customer,
card_last_4="4323",
card_kind="Visa",
amount=decimal.Decimal("10.00"),
paid=True,
refunded=False,
fee=decimal.Decimal("4.99"),
disputed=False
)
RetrieveMock.return_value.refund.return_value = {
"id": "ch_XXXXXX",
"card": {
"last4": "4323",
"type": "Visa"
},
"amount": 1000,
"paid": True,
"refunded": True,
"amount_refunded": 1000,
"fee": 499,
"dispute": None,
"created": 1363911708,
"customer": "cus_xxxxxxxxxxxxxxx"
}
charge.refund()
charge2 = Charge.objects.get(stripe_id="ch_XXXXXX")
self.assertEquals(charge2.refunded, True)
self.assertEquals(charge2.amount_refunded, decimal.Decimal("10.00"))
def test_calculate_refund_amount_full_refund(self):
charge = Charge(
stripe_id="ch_111111",
customer=self.customer,
amount=decimal.Decimal("500.00")
)
self.assertEquals(
charge.calculate_refund_amount(),
50000
)
def test_calculate_refund_amount_partial_refund(self):
charge = Charge(
stripe_id="ch_111111",
customer=self.customer,
amount=decimal.Decimal("500.00")
)
self.assertEquals(
charge.calculate_refund_amount(amount=decimal.Decimal("300.00")),
30000
)
def test_calculate_refund_above_max_refund(self):
charge = Charge(
stripe_id="ch_111111",
customer=self.customer,
amount=decimal.Decimal("500.00")
)
self.assertEquals(
charge.calculate_refund_amount(amount=decimal.Decimal("600.00")),
50000
)
@patch("stripe.Charge.retrieve")
@patch("stripe.Charge.create")
def test_charge_converts_dollars_into_cents(self, ChargeMock, RetrieveMock):
ChargeMock.return_value.id = "ch_XXXXX"
RetrieveMock.return_value = {
"id": "ch_XXXXXX",
"card": {
"last4": "4323",
"type": "Visa"
},
"amount": 1000,
"paid": True,
"refunded": False,
"fee": 499,
"dispute": None,
"created": 1363911708,
"customer": "cus_xxxxxxxxxxxxxxx"
}
self.customer.charge(
amount=decimal.Decimal("10.00")
)
_, kwargs = ChargeMock.call_args
self.assertEquals(kwargs["amount"], 1000)