Skip to content

Commit

Permalink
link_customer: create missing customers
Browse files Browse the repository at this point in the history
This also fixes some event fixtures.

Fixes pinax#467.
  • Loading branch information
blueyed committed Dec 4, 2017
1 parent 4013376 commit 094d588
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 15 deletions.
18 changes: 12 additions & 6 deletions pinax/stripe/actions/customers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
23 changes: 18 additions & 5 deletions pinax/stripe/tests/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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):
Expand Down
39 changes: 35 additions & 4 deletions pinax/stripe/tests/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,21 @@ 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,
"email": "[email protected]",
"id": "cus_xxxxxxxxxxxxxxx",
"livemode": True,
"object": "customer",
"subscription": None
"sources": {
"data": [],
},
"subscriptions": {
"data": [],
},
}
},
"id": "evt_xxxxxxxxxxxxx",
Expand All @@ -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 = {
Expand Down Expand Up @@ -88,14 +98,21 @@ def test_link_customer_customer_updated(self):
"type": "MasterCard"
},
"created": 1346855596,
"currency": None,
"default_source": None,
"delinquent": False,
"description": None,
"discount": None,
"email": "[email protected]",
"id": "cus_xxxxxxxxxxxxxxx",
"livemode": True,
"object": "customer",
"subscription": None
"sources": {
"data": [],
},
"subscriptions": {
"data": [],
},
},
"previous_attributes": {
"active_card": None
Expand Down Expand Up @@ -125,14 +142,21 @@ 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,
"email": "[email protected]",
"id": "cus_xxxxxxxxxxxxxxx",
"livemode": True,
"object": "customer",
"subscription": None
"sources": {
"data": [],
},
"subscriptions": {
"data": [],
},
}
},
"id": "evt_xxxxxxxxxxxxx",
Expand Down Expand Up @@ -162,14 +186,21 @@ 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,
"email": "[email protected]",
"id": "cus_xxxxxxxxxxxxxxx",
"livemode": True,
"object": "customer",
"subscription": None
"sources": {
"data": [],
},
"subscriptions": {
"data": [],
}
}
},
"id": "evt_xxxxxxxxxxxxx",
Expand Down

0 comments on commit 094d588

Please sign in to comment.