From efa780da6af485f41f1a3f5b3a57b3792f32af1b Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Thu, 21 Sep 2017 18:29:51 +0200 Subject: [PATCH] CustomerUpdatedWebhook: use provided customer object from the event --- pinax/stripe/actions/customers.py | 2 +- pinax/stripe/tests/test_webhooks.py | 13 ++++++++++++- pinax/stripe/webhooks.py | 7 ++++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/pinax/stripe/actions/customers.py b/pinax/stripe/actions/customers.py index bb8e0eccd..0193a00b4 100644 --- a/pinax/stripe/actions/customers.py +++ b/pinax/stripe/actions/customers.py @@ -145,7 +145,7 @@ def set_default_source(customer, source): def sync_customer(customer, cu=None): """ - Syncronizes a local Customer object with details from the Stripe API + Synchronizes a local Customer object with details from the Stripe API Args: customer: a Customer object diff --git a/pinax/stripe/tests/test_webhooks.py b/pinax/stripe/tests/test_webhooks.py index 66aca5cd3..83baf5101 100644 --- a/pinax/stripe/tests/test_webhooks.py +++ b/pinax/stripe/tests/test_webhooks.py @@ -168,7 +168,18 @@ class CustomerUpdatedWebhookTest(TestCase): def test_process_webhook(self, SyncMock): event = Event.objects.create(kind=CustomerUpdatedWebhook.name, webhook_message={}, valid=True, processed=False) CustomerUpdatedWebhook(event).process_webhook() - self.assertTrue(SyncMock.called) + self.assertEquals(SyncMock.call_count, 1) + self.assertEquals(SyncMock.call_args[0], (None, None)) + + @patch("pinax.stripe.actions.customers.sync_customer") + def test_process_webhook_with_data(self, SyncMock): + event = Event.objects.create(kind=CustomerUpdatedWebhook.name, webhook_message={}, valid=True, processed=False) + obj = object() + event.validated_message = dict(data=dict(object=obj)) + CustomerUpdatedWebhook(event).process_webhook() + self.assertEquals(SyncMock.call_count, 1) + self.assertIsNone(SyncMock.call_args[0][0]) + self.assertIs(SyncMock.call_args[0][1], obj) class CustomerSourceCreatedWebhookTest(TestCase): diff --git a/pinax/stripe/webhooks.py b/pinax/stripe/webhooks.py index de6c30f9b..01556a5bd 100644 --- a/pinax/stripe/webhooks.py +++ b/pinax/stripe/webhooks.py @@ -255,7 +255,12 @@ class CustomerUpdatedWebhook(Webhook): description = "Occurs whenever any property of a customer changes." def process_webhook(self): - customers.sync_customer(self.event.customer) + cu = None + try: + cu = self.event.message["data"]["object"] + except (KeyError, TypeError): + pass + customers.sync_customer(self.event.customer, cu) class CustomerDiscountCreatedWebhook(Webhook):