-
Notifications
You must be signed in to change notification settings - Fork 286
/
test_event.py
290 lines (269 loc) · 10.6 KB
/
test_event.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
# pylint: disable=C0301
from django.test import TestCase
from django.utils import timezone
from mock import patch, Mock
from ..models import Customer, Event, CurrentSubscription
from payments.signals import WEBHOOK_SIGNALS
from ..utils import get_user_model
class TestEventMethods(TestCase):
def setUp(self):
User = get_user_model()
self.user = User.objects.create_user(username="testuser")
self.user.save()
self.customer = Customer.objects.create(
stripe_id="cus_xxxxxxxxxxxxxxx",
user=self.user
)
def test_link_customer_customer_created(self):
msg = {
"created": 1363911708,
"data": {
"object": {
"account_balance": 0,
"active_card": None,
"created": 1363911708,
"delinquent": False,
"description": None,
"discount": None,
"email": "xxxxxxxxxx@yahoo.com",
"id": "cus_xxxxxxxxxxxxxxx",
"livemode": True,
"object": "customer",
"subscription": None
}
},
"id": "evt_xxxxxxxxxxxxx",
"livemode": True,
"object": "event",
"pending_webhooks": 1,
"type": "customer.created"
}
event = Event.objects.create(
stripe_id=msg["id"],
kind="customer.created",
livemode=True,
webhook_message=msg,
validated_message=msg
)
event.link_customer()
self.assertEquals(event.customer, self.customer)
def test_link_customer_customer_updated(self):
msg = {
"created": 1346855599,
"data": {
"object": {
"account_balance": 0,
"active_card": {
"address_city": None,
"address_country": None,
"address_line1": None,
"address_line1_check": None,
"address_line2": None,
"address_state": None,
"address_zip": None,
"address_zip_check": None,
"country": "MX",
"cvc_check": "pass",
"exp_month": 1,
"exp_year": 2014,
"fingerprint": "XXXXXXXXXXX",
"last4": "7992",
"name": None,
"object": "card",
"type": "MasterCard"
},
"created": 1346855596,
"delinquent": False,
"description": None,
"discount": None,
"email": "xxxxxxxxxx@yahoo.com",
"id": "cus_xxxxxxxxxxxxxxx",
"livemode": True,
"object": "customer",
"subscription": None
},
"previous_attributes": {
"active_card": None
}
},
"id": "evt_xxxxxxxxxxxxx",
"livemode": True,
"object": "event",
"pending_webhooks": 1,
"type": "customer.updated"
}
event = Event.objects.create(
stripe_id=msg["id"],
kind="customer.updated",
livemode=True,
webhook_message=msg,
validated_message=msg
)
event.link_customer()
self.assertEquals(event.customer, self.customer)
def test_link_customer_customer_deleted(self):
msg = {
"created": 1348286560,
"data": {
"object": {
"account_balance": 0,
"active_card": None,
"created": 1348286302,
"delinquent": False,
"description": None,
"discount": None,
"email": "paltman+test@gmail.com",
"id": "cus_xxxxxxxxxxxxxxx",
"livemode": True,
"object": "customer",
"subscription": None
}
},
"id": "evt_xxxxxxxxxxxxx",
"livemode": True,
"object": "event",
"pending_webhooks": 1,
"type": "customer.deleted"
}
event = Event.objects.create(
stripe_id=msg["id"],
kind="customer.deleted",
livemode=True,
webhook_message=msg,
validated_message=msg
)
event.link_customer()
self.assertEquals(event.customer, self.customer)
@patch("stripe.Customer.retrieve")
def test_process_customer_deleted(self, CustomerMock):
msg = {
"created": 1348286560,
"data": {
"object": {
"account_balance": 0,
"active_card": None,
"created": 1348286302,
"delinquent": False,
"description": None,
"discount": None,
"email": "paltman+test@gmail.com",
"id": "cus_xxxxxxxxxxxxxxx",
"livemode": True,
"object": "customer",
"subscription": None
}
},
"id": "evt_xxxxxxxxxxxxx",
"livemode": True,
"object": "event",
"pending_webhooks": 1,
"type": "customer.deleted"
}
event = Event.objects.create(
stripe_id=msg["id"],
kind="customer.deleted",
livemode=True,
webhook_message=msg,
validated_message=msg,
valid=True
)
event.process()
self.assertEquals(event.customer, self.customer)
self.assertEquals(event.customer.user, None)
@staticmethod
def send_signal(customer, kind):
event = Event(customer=customer, kind=kind)
signal = WEBHOOK_SIGNALS.get(kind)
signal.send(sender=Event, event=event)
@staticmethod
def connect_webhook_signal(kind, func, **kwargs):
signal = WEBHOOK_SIGNALS.get(kind)
signal.connect(func, **kwargs)
@staticmethod
def disconnect_webhook_signal(kind, func, **kwargs):
signal = WEBHOOK_SIGNALS.get(kind)
signal.disconnect(func, **kwargs)
@patch("stripe.Customer.retrieve")
def test_customer_subscription_deleted(self, CustomerMock):
"""
Tests to make sure downstream signal handlers do not see stale CurrentSubscription object properties
after a customer.subscription.deleted event occurs. While the delete method is called
on the affected CurrentSubscription object's properties are still accessible (unless the
Customer object for the event gets refreshed before sending the complimentary signal)
"""
kind = "customer.subscription.deleted"
cs = CurrentSubscription(customer=self.customer, quantity=1, start=timezone.now(), amount=0)
cs.save()
customer = Customer.objects.get(pk=self.customer.pk)
# Stripe objects will not have this attribute so we must delete it from the mocked object
del customer.stripe_customer.subscription
self.assertIsNotNone(customer.current_subscription)
# This is the expected format of a customer.subscription.delete message
msg = {
"id": "evt_2eRjeAlnH1XMe8",
"created": 1380317537,
"livemode": True,
"type": kind,
"data": {
"object": {
"id": "su_2ZDdGxJ3EQQc7Q",
"plan": {
"interval": "month",
"name": "xxx",
"amount": 200,
"currency": "usd",
"id": "xxx",
"object": "plan",
"livemode": True,
"interval_count": 1,
"trial_period_days": None
},
"object": "subscription",
"start": 1379111889,
"status": "canceled",
"customer": self.customer.stripe_id,
"cancel_at_period_end": False,
"current_period_start": 1378738246,
"current_period_end": 1381330246,
"ended_at": 1380317537,
"trial_start": None,
"trial_end": None,
"canceled_at": 1380317537,
"quantity": 1,
"application_fee_percent": None
}
},
"object": "event",
"pending_webhooks": 1,
"request": "iar_2eRjQZmn0i3G9M"
}
# Create a test event for the message
test_event = Event.objects.create(
stripe_id=msg["id"],
kind=kind,
livemode=msg["livemode"],
webhook_message=msg,
validated_message=msg,
valid=True,
customer=customer,
)
def signal_handler(sender, event, **kwargs):
# Illustrate and test what signal handlers would experience
self.assertFalse(event.customer.current_subscription.is_valid())
self.assertIsNone(event.customer.current_subscription.plan)
self.assertIsNone(event.customer.current_subscription.status)
self.assertIsNone(event.customer.current_subscription.id)
signal_handler_mock = Mock()
# Let's make the side effect call our real function, the mock is a proxy so we can assert it was called
signal_handler_mock.side_effect = signal_handler
TestEventMethods.connect_webhook_signal(kind, signal_handler_mock, weak=False, sender=Event)
signal_handler_mock.reset_mock()
# Now process the event - at the end of this the signal should get sent
test_event.process()
self.assertFalse(test_event.customer.current_subscription.is_valid())
self.assertIsNone(test_event.customer.current_subscription.plan)
self.assertIsNone(test_event.customer.current_subscription.status)
self.assertIsNone(test_event.customer.current_subscription.id)
# Verify our signal handler was called
self.assertTrue(signal_handler_mock.called)
TestEventMethods.disconnect_webhook_signal(kind, signal_handler_mock, weak=False, sender=Event)