forked from dj-stripe/dj-stripe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_webhooks.py
369 lines (308 loc) · 12.9 KB
/
test_webhooks.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
"""
dj-stripe Webhook Tests.
"""
import json
import warnings
from collections import defaultdict
from copy import deepcopy
from importlib import reload
from unittest.mock import Mock, PropertyMock, call, patch
from django.test import TestCase, override_settings
from django.test.client import Client
from django.urls import reverse
from djstripe import settings as djstripe_settings
from djstripe import webhooks
from djstripe.models import Event, WebhookEventTrigger
from djstripe.webhooks import TEST_EVENT_ID, call_handlers, handler, handler_all
from . import (
FAKE_EVENT_TEST_CHARGE_SUCCEEDED, FAKE_EVENT_TRANSFER_CREATED, FAKE_TRANSFER,
IS_ASSERT_CALLED_AUTOSPEC_SUPPORTED, IS_STATICMETHOD_AUTOSPEC_SUPPORTED
)
def mock_webhook_handler(webhook_event_trigger):
webhook_event_trigger.process()
class TestWebhook(TestCase):
def tearDown(self):
reload(djstripe_settings)
def _send_event(self, event_data):
return Client().post(
reverse("djstripe:webhook"),
json.dumps(event_data),
content_type="application/json",
HTTP_STRIPE_SIGNATURE="PLACEHOLDER",
)
def test_webhook_test_event(self):
self.assertEqual(WebhookEventTrigger.objects.count(), 0)
resp = self._send_event(FAKE_EVENT_TEST_CHARGE_SUCCEEDED)
self.assertEqual(resp.status_code, 200)
self.assertFalse(Event.objects.filter(id=TEST_EVENT_ID).exists())
self.assertEqual(WebhookEventTrigger.objects.count(), 1)
event_trigger = WebhookEventTrigger.objects.first()
self.assertTrue(event_trigger.is_test_event)
@override_settings(DJSTRIPE_WEBHOOK_VALIDATION="retrieve_event")
@patch("stripe.Transfer.retrieve", return_value=deepcopy(FAKE_TRANSFER), autospec=True)
@patch(
"stripe.Event.retrieve",
return_value=deepcopy(FAKE_EVENT_TRANSFER_CREATED),
autospec=True,
)
def test_webhook_retrieve_event_fail(
self, event_retrieve_mock, transfer_retrieve_mock
):
reload(djstripe_settings)
invalid_event = deepcopy(FAKE_EVENT_TRANSFER_CREATED)
invalid_event["id"] = "evt_invalid"
invalid_event["data"]["valid"] = "not really"
resp = self._send_event(invalid_event)
self.assertEqual(resp.status_code, 400)
self.assertFalse(Event.objects.filter(id="evt_invalid").exists())
@override_settings(DJSTRIPE_WEBHOOK_VALIDATION="retrieve_event")
@patch("stripe.Transfer.retrieve", return_value=deepcopy(FAKE_TRANSFER), autospec=True)
@patch(
"stripe.Event.retrieve",
return_value=deepcopy(FAKE_EVENT_TRANSFER_CREATED),
autospec=True,
)
def test_webhook_retrieve_event_pass(
self, event_retrieve_mock, transfer_retrieve_mock
):
reload(djstripe_settings)
resp = self._send_event(FAKE_EVENT_TRANSFER_CREATED)
self.assertEqual(resp.status_code, 200)
event_retrieve_mock.assert_called_once_with(
api_key=djstripe_settings.STRIPE_SECRET_KEY, id=FAKE_EVENT_TRANSFER_CREATED["id"]
)
@override_settings(
DJSTRIPE_WEBHOOK_VALIDATION="verify_signature", DJSTRIPE_WEBHOOK_SECRET="whsec_XXXXX"
)
@patch("stripe.Transfer.retrieve", return_value=deepcopy(FAKE_TRANSFER), autospec=True)
@patch(
"stripe.Event.retrieve",
return_value=deepcopy(FAKE_EVENT_TRANSFER_CREATED),
autospec=True,
)
def test_webhook_invalid_verify_signature_fail(
self, event_retrieve_mock, transfer_retrieve_mock
):
reload(djstripe_settings)
invalid_event = deepcopy(FAKE_EVENT_TRANSFER_CREATED)
invalid_event["id"] = "evt_invalid"
invalid_event["data"]["valid"] = "not really"
resp = self._send_event(invalid_event)
self.assertEqual(resp.status_code, 400)
self.assertFalse(Event.objects.filter(id="evt_invalid").exists())
@override_settings(
DJSTRIPE_WEBHOOK_VALIDATION="verify_signature", DJSTRIPE_WEBHOOK_SECRET="whsec_XXXXX"
)
@patch(
"stripe.WebhookSignature.verify_header",
return_value=True,
autospec=IS_STATICMETHOD_AUTOSPEC_SUPPORTED and IS_ASSERT_CALLED_AUTOSPEC_SUPPORTED,
)
@patch("stripe.Transfer.retrieve", return_value=deepcopy(FAKE_TRANSFER), autospec=True)
@patch(
"stripe.Event.retrieve",
return_value=deepcopy(FAKE_EVENT_TRANSFER_CREATED),
autospec=IS_ASSERT_CALLED_AUTOSPEC_SUPPORTED,
)
def test_webhook_verify_signature_pass(
self, event_retrieve_mock, transfer_retrieve_mock, verify_signature_mock
):
reload(djstripe_settings)
resp = self._send_event(FAKE_EVENT_TRANSFER_CREATED)
self.assertEqual(resp.status_code, 200)
self.assertFalse(Event.objects.filter(id="evt_invalid").exists())
verify_signature_mock.called_once_with(FAKE_EVENT_TRANSFER_CREATED, {})
event_retrieve_mock.assert_not_called()
@override_settings(DJSTRIPE_WEBHOOK_VALIDATION=None)
@patch("stripe.WebhookSignature.verify_header", autospec=True)
@patch("stripe.Transfer.retrieve", return_value=deepcopy(FAKE_TRANSFER), autospec=True)
@patch(
"stripe.Event.retrieve",
return_value=deepcopy(FAKE_EVENT_TRANSFER_CREATED),
autospec=IS_ASSERT_CALLED_AUTOSPEC_SUPPORTED,
)
def test_webhook_no_validation_pass(
self, event_retrieve_mock, transfer_retrieve_mock, verify_signature_mock
):
reload(djstripe_settings)
invalid_event = deepcopy(FAKE_EVENT_TRANSFER_CREATED)
invalid_event["id"] = "evt_invalid"
invalid_event["data"]["valid"] = "not really"
resp = self._send_event(invalid_event)
self.assertEqual(resp.status_code, 200)
self.assertTrue(Event.objects.filter(id="evt_invalid").exists())
event_retrieve_mock.assert_not_called()
verify_signature_mock.assert_not_called()
def test_webhook_no_signature(self):
self.assertEqual(WebhookEventTrigger.objects.count(), 0)
resp = Client().post(
reverse("djstripe:webhook"), "{}", content_type="application/json"
)
self.assertEqual(resp.status_code, 400)
self.assertEqual(WebhookEventTrigger.objects.count(), 0)
def test_webhook_no_remote_addr(self):
self.assertEqual(WebhookEventTrigger.objects.count(), 0)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
Client().post(
reverse("djstripe:webhook"),
"{}",
content_type="application/json",
HTTP_STRIPE_SIGNATURE="PLACEHOLDER",
REMOTE_ADDR=None,
)
self.assertEqual(WebhookEventTrigger.objects.count(), 1)
event_trigger = WebhookEventTrigger.objects.first()
self.assertEqual(event_trigger.remote_ip, "0.0.0.0")
@patch(
"djstripe.models.WebhookEventTrigger.validate", return_value=True, autospec=True
)
@patch("djstripe.models.WebhookEventTrigger.process", autospec=True)
def test_webhook_reraise_exception(
self, webhook_event_process_mock, webhook_event_validate_mock
):
class ProcessException(Exception):
pass
exception_message = "process fail"
webhook_event_process_mock.side_effect = ProcessException(exception_message)
self.assertEqual(WebhookEventTrigger.objects.count(), 0)
fake_event = deepcopy(FAKE_EVENT_TRANSFER_CREATED)
with self.assertRaisesMessage(ProcessException, exception_message):
self._send_event(fake_event)
self.assertEqual(WebhookEventTrigger.objects.count(), 1)
event_trigger = WebhookEventTrigger.objects.first()
self.assertEqual(event_trigger.exception, exception_message)
@patch.object(
djstripe_settings, "WEBHOOK_EVENT_CALLBACK", return_value=mock_webhook_handler
)
@patch("stripe.Transfer.retrieve", return_value=deepcopy(FAKE_TRANSFER), autospec=True)
@patch("stripe.Event.retrieve", autospec=True)
def test_webhook_with_custom_callback(
self, event_retrieve_mock, transfer_retrieve_mock, webhook_event_callback_mock
):
fake_event = deepcopy(FAKE_EVENT_TRANSFER_CREATED)
event_retrieve_mock.return_value = fake_event
djstripe_settings.WEBHOOK_SECRET = ""
resp = self._send_event(fake_event)
self.assertEqual(resp.status_code, 200)
webhook_event_trigger = WebhookEventTrigger.objects.get()
webhook_event_callback_mock.called_once_with(webhook_event_trigger)
@patch("stripe.Transfer.retrieve", return_value=deepcopy(FAKE_TRANSFER), autospec=True)
@patch("stripe.Event.retrieve", autospec=True)
def test_webhook_with_transfer_event_duplicate(
self, event_retrieve_mock, transfer_retrieve_mock
):
fake_event = deepcopy(FAKE_EVENT_TRANSFER_CREATED)
event_retrieve_mock.return_value = fake_event
djstripe_settings.WEBHOOK_SECRET = ""
resp = self._send_event(fake_event)
self.assertEqual(resp.status_code, 200)
self.assertTrue(Event.objects.filter(type="transfer.created").exists())
self.assertEqual(1, Event.objects.filter(type="transfer.created").count())
# Duplication
resp = self._send_event(fake_event)
self.assertEqual(resp.status_code, 200)
self.assertEqual(1, Event.objects.filter(type="transfer.created").count())
@patch("stripe.Transfer.retrieve", return_value=deepcopy(FAKE_TRANSFER), autospec=True)
@patch("stripe.Event.retrieve", autospec=True)
def test_webhook_good(self, event_retrieve_mock, transfer_retrieve_mock):
djstripe_settings.WEBHOOK_SECRET = ""
fake_event = deepcopy(FAKE_EVENT_TRANSFER_CREATED)
event_retrieve_mock.return_value = fake_event
resp = self._send_event(fake_event)
self.assertEqual(resp.status_code, 200)
self.assertEqual(Event.objects.count(), 1)
self.assertEqual(WebhookEventTrigger.objects.count(), 1)
event_trigger = WebhookEventTrigger.objects.first()
self.assertEqual(event_trigger.is_test_event, False)
@patch.object(target=Event, attribute="invoke_webhook_handlers", autospec=True)
@patch("stripe.Transfer.retrieve", return_value=deepcopy(FAKE_TRANSFER), autospec=True)
@patch("stripe.Event.retrieve", autospec=True)
def test_webhook_error(
self, event_retrieve_mock, transfer_retrieve_mock, mock_invoke_webhook_handlers
):
"""Test the case where webhook processing fails to ensure we rollback
and do not commit the Event object to the database.
"""
mock_invoke_webhook_handlers.side_effect = KeyError("Test error")
djstripe_settings.WEBHOOK_SECRET = ""
fake_event = deepcopy(FAKE_EVENT_TRANSFER_CREATED)
event_retrieve_mock.return_value = fake_event
with self.assertRaises(KeyError):
self._send_event(fake_event)
self.assertEqual(Event.objects.count(), 0)
self.assertEqual(WebhookEventTrigger.objects.count(), 1)
event_trigger = WebhookEventTrigger.objects.first()
self.assertEqual(event_trigger.is_test_event, False)
self.assertEqual(event_trigger.exception, "'Test error'")
class TestWebhookHandlers(TestCase):
def setUp(self):
# Reset state of registrations per test
patcher = patch.object(
webhooks, "registrations", new_callable=(lambda: defaultdict(list))
)
self.addCleanup(patcher.stop)
self.registrations = patcher.start()
patcher = patch.object(webhooks, "registrations_global", new_callable=list)
self.addCleanup(patcher.stop)
self.registrations_global = patcher.start()
def test_global_handler_registration(self):
func_mock = Mock()
handler_all()(func_mock)
event = self._call_handlers("wib.ble", {"data": "foo"}) # handled
self.assertEqual(1, func_mock.call_count)
func_mock.assert_called_with(event=event)
def test_event_handler_registration(self):
global_func_mock = Mock()
handler_all()(global_func_mock)
func_mock = Mock()
handler("foo")(func_mock)
event = self._call_handlers("foo.bar", {"data": "foo"}) # handled
self._call_handlers("bar.foo", {"data": "foo"}) # not handled
self.assertEqual(2, global_func_mock.call_count) # called each time
self.assertEqual(1, func_mock.call_count)
func_mock.assert_called_with(event=event)
def test_event_subtype_handler_registration(self):
global_func_mock = Mock()
handler_all()(global_func_mock)
func_mock = Mock()
handler("foo.bar")(func_mock)
event1 = self._call_handlers("foo.bar", {"data": "foo"}) # handled
event2 = self._call_handlers("foo.bar.wib", {"data": "foo"}) # handled
self._call_handlers("foo.baz", {"data": "foo"}) # not handled
self.assertEqual(3, global_func_mock.call_count) # called each time
self.assertEqual(2, func_mock.call_count)
func_mock.assert_has_calls([call(event=event1), call(event=event2)])
def test_global_handler_registration_with_function(self):
func_mock = Mock()
handler_all(func_mock)
event = self._call_handlers("wib.ble", {"data": "foo"}) # handled
self.assertEqual(1, func_mock.call_count)
func_mock.assert_called_with(event=event)
def test_event_handle_registation_with_string(self):
func_mock = Mock()
handler("foo")(func_mock)
event = self._call_handlers("foo.bar", {"data": "foo"}) # handled
self.assertEqual(1, func_mock.call_count)
func_mock.assert_called_with(event=event)
def test_event_handle_registation_with_list_of_strings(self):
func_mock = Mock()
handler("foo", "bar")(func_mock)
event1 = self._call_handlers("foo.bar", {"data": "foo"}) # handled
event2 = self._call_handlers("bar.foo", {"data": "bar"}) # handled
self.assertEqual(2, func_mock.call_count)
func_mock.assert_has_calls([call(event=event1), call(event=event2)])
#
# Helpers
#
@staticmethod
def _call_handlers(event_spec, data):
event = Mock(spec=Event)
parts = event_spec.split(".")
category = parts[0]
verb = ".".join(parts[1:])
type(event).parts = PropertyMock(return_value=parts)
type(event).category = PropertyMock(return_value=category)
type(event).verb = PropertyMock(return_value=verb)
call_handlers(event=event)
return event