From 094d58844bde3c1b94f7881ce16e885f09c95a73 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Mon, 4 Dec 2017 16:38:57 +0100 Subject: [PATCH] link_customer: create missing customers This also fixes some event fixtures. Fixes https://github.com/pinax/pinax-stripe/issues/467. --- pinax/stripe/actions/customers.py | 18 +++++++++----- pinax/stripe/tests/test_actions.py | 23 ++++++++++++++---- pinax/stripe/tests/test_event.py | 39 +++++++++++++++++++++++++++--- 3 files changed, 65 insertions(+), 15 deletions(-) diff --git a/pinax/stripe/actions/customers.py b/pinax/stripe/actions/customers.py index 67096b0a5..e0a53c1fb 100644 --- a/pinax/stripe/actions/customers.py +++ b/pinax/stripe/actions/customers.py @@ -171,16 +171,22 @@ def link_customer(event): "customer.updated", "customer.deleted" ] + event_data_object = event.message["data"]["object"] if event.kind in customer_crud_events: - cus_id = event.message["data"]["object"]["id"] + cus_id = event_data_object["id"] else: - cus_id = event.message["data"]["object"].get("customer", None) + cus_id = event_data_object.get("customer", None) if cus_id is not None: - customer = models.Customer.objects.filter(stripe_id=cus_id).first() - if customer is not None: - event.customer = customer - event.save() + customer, created = models.Customer.objects.get_or_create( + stripe_id=cus_id, + stripe_account=event.stripe_account, + ) + if event.kind in customer_crud_events: + sync_customer(customer, event_data_object) + + event.customer = customer + event.save() def set_default_source(customer, source): diff --git a/pinax/stripe/tests/test_actions.py b/pinax/stripe/tests/test_actions.py index 11ed7ce27..7fc1d16bb 100644 --- a/pinax/stripe/tests/test_actions.py +++ b/pinax/stripe/tests/test_actions.py @@ -509,12 +509,14 @@ def test_can_charge_false_no_default_source(self): customer = Customer() self.assertFalse(customers.can_charge(customer)) - def test_link_customer(self): + @patch("pinax.stripe.actions.customers.sync_customer") + def test_link_customer(self, SyncMock): Customer.objects.create(stripe_id="cu_123") message = dict(data=dict(object=dict(id="cu_123"))) event = Event.objects.create(validated_message=message, kind="customer.created") customers.link_customer(event) self.assertEquals(event.customer.stripe_id, "cu_123") + self.assertTrue(SyncMock.called) def test_link_customer_non_customer_event(self): Customer.objects.create(stripe_id="cu_123") @@ -523,18 +525,29 @@ def test_link_customer_non_customer_event(self): customers.link_customer(event) self.assertEquals(event.customer.stripe_id, "cu_123") - def test_link_customer_no_customer(self): + def test_link_customer_non_customer_event_no_customer(self): Customer.objects.create(stripe_id="cu_123") message = dict(data=dict(object=dict())) event = Event.objects.create(validated_message=message, kind="transfer.created") customers.link_customer(event) self.assertIsNone(event.customer, "cu_123") - def test_link_customer_does_not_exist(self): + @patch("pinax.stripe.actions.customers.sync_customer") + def test_link_customer_does_not_exist(self, SyncMock): message = dict(data=dict(object=dict(id="cu_123"))) - event = Event.objects.create(validated_message=message, kind="customer.created") + event = Event.objects.create(stripe_id="evt_1", validated_message=message, kind="customer.created") customers.link_customer(event) - self.assertIsNone(event.customer) + Customer.objects.get(stripe_id="cu_123") + self.assertTrue(SyncMock.called) + + @patch("pinax.stripe.actions.customers.sync_customer") + def test_link_customer_does_not_exist_connected(self, SyncMock): + message = dict(data=dict(object=dict(id="cu_123"))) + account = Account.objects.create(stripe_id="acc_XXX") + event = Event.objects.create(stripe_id="evt_1", validated_message=message, kind="customer.created", stripe_account=account) + customers.link_customer(event) + Customer.objects.get(stripe_id="cu_123", stripe_account=account) + self.assertTrue(SyncMock.called) class CustomersWithConnectTests(TestCase): diff --git a/pinax/stripe/tests/test_event.py b/pinax/stripe/tests/test_event.py index 4a3bc8577..8a320277b 100644 --- a/pinax/stripe/tests/test_event.py +++ b/pinax/stripe/tests/test_event.py @@ -36,6 +36,8 @@ def test_link_customer_customer_created(self): "account_balance": 0, "active_card": None, "created": 1363911708, + "currency": None, + "default_source": None, "delinquent": False, "description": None, "discount": None, @@ -43,7 +45,12 @@ def test_link_customer_customer_created(self): "id": "cus_xxxxxxxxxxxxxxx", "livemode": True, "object": "customer", - "subscription": None + "sources": { + "data": [], + }, + "subscriptions": { + "data": [], + }, } }, "id": "evt_xxxxxxxxxxxxx", @@ -59,8 +66,11 @@ def test_link_customer_customer_created(self): webhook_message=msg, validated_message=msg ) + self.assertIsNone(self.customer.account_balance) customers.link_customer(event) self.assertEquals(event.customer, self.customer) + self.customer.refresh_from_db() + self.assertEquals(self.customer.account_balance, 0) def test_link_customer_customer_updated(self): msg = { @@ -88,6 +98,8 @@ def test_link_customer_customer_updated(self): "type": "MasterCard" }, "created": 1346855596, + "currency": None, + "default_source": None, "delinquent": False, "description": None, "discount": None, @@ -95,7 +107,12 @@ def test_link_customer_customer_updated(self): "id": "cus_xxxxxxxxxxxxxxx", "livemode": True, "object": "customer", - "subscription": None + "sources": { + "data": [], + }, + "subscriptions": { + "data": [], + }, }, "previous_attributes": { "active_card": None @@ -125,6 +142,8 @@ def test_link_customer_customer_deleted(self): "account_balance": 0, "active_card": None, "created": 1348286302, + "currency": None, + "default_source": None, "delinquent": False, "description": None, "discount": None, @@ -132,7 +151,12 @@ def test_link_customer_customer_deleted(self): "id": "cus_xxxxxxxxxxxxxxx", "livemode": True, "object": "customer", - "subscription": None + "sources": { + "data": [], + }, + "subscriptions": { + "data": [], + }, } }, "id": "evt_xxxxxxxxxxxxx", @@ -162,6 +186,8 @@ def test_process_customer_deleted(self, CustomerMock, EventMock): "account_balance": 0, "active_card": None, "created": 1348286302, + "currency": None, + "default_source": None, "delinquent": False, "description": None, "discount": None, @@ -169,7 +195,12 @@ def test_process_customer_deleted(self, CustomerMock, EventMock): "id": "cus_xxxxxxxxxxxxxxx", "livemode": True, "object": "customer", - "subscription": None + "sources": { + "data": [], + }, + "subscriptions": { + "data": [], + } } }, "id": "evt_xxxxxxxxxxxxx",