Skip to content

Commit

Permalink
[BE] fix refresh_customers command
Browse files Browse the repository at this point in the history
  • Loading branch information
poxip committed Jun 11, 2018
1 parent 2eca82f commit b995c94
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log
All notable changes to this project will be documented in this file.
## [0.6.6]
### Fixed
- refresh_customers command

## [0.6.5]
### Fixed
- setting StripeCustomer.default_source when creating a new customer from API
Expand Down
2 changes: 1 addition & 1 deletion aa_stripe/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
__title__ = "Arabella Stripe"
__version__ = "0.6.5"
__version__ = "0.6.6"
__author__ = "Jacek Ostanski"
__license__ = "MIT"
__copyright__ = "Copyright 2017 Arabella"
Expand Down
2 changes: 1 addition & 1 deletion aa_stripe/management/commands/refresh_customers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def handle(self, *args, **options):

for stripe_customer in response["data"]:
updated_count += StripeCustomer.objects.filter(stripe_customer_id=stripe_customer["id"]).update(
sources=stripe_customer["sources"], default_source=stripe_customer["default_source"]
sources=stripe_customer["sources"]["data"], default_source=stripe_customer["default_source"]
)

if not response["has_more"]:
Expand Down
45 changes: 22 additions & 23 deletions tests/test_customers.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,37 +147,36 @@ def setUp(self):

@requests_mock.Mocker()
def test_command(self, m):
customer1_data = {
"id": "cus_xyz",
"object": "customer",
"sources": [
{"id": "card_1"}
],
"default_source": "card_1"
}
customer2_data = customer1_data.copy()
customer2_data["id"] = "cus_2"
customer2_data["sources"] = [{"id": "card_2"}]
customer2_data["default_source"] = "card_2"
customer2_data = customer1_data.copy()
customer2_data["id"] = "cus_2"
customer2_data["sources"] = []
customer2_data["default_source"] = None
def get_customer_data(customer_id, sources, default_source=None):
return {
"id": customer_id,
"object": "customer",
"sources": {
"object": "list",
"data": sources,
"has_more": False,
"total_count": 1,
"url": f"/v1/customers/{customer_id}/sources"
},
"default_source": default_source
}

stripe_response_part1 = {
"object": "list",
"url": "/v1/customers",
"has_more": True,
"data": [get_customer_data("cus_xyz", [{"id": "card_1"}], default_source="card_1")]
}
stripe_response_part2 = {
"object": "list",
"url": "/v1/customers",
"has_more": False,
"data": [
customer1_data
]
"data": [get_customer_data("cus_b", [{"id": "card_2"}])]
}
stripe_response_part2 = stripe_response_part1.copy()
stripe_response_part2["has_more"] = False
stripe_response_part2["data"] = [customer2_data]
m.register_uri("GET", "https://api.stripe.com/v1/customers", text=json.dumps(stripe_response_part1))
m.register_uri("GET", "https://api.stripe.com/v1/customers?starting_after=cus_xyz", [
{"text": "", "status_code": 500}, # make sure the command will try again
{"text": json.dumps(stripe_response_part2), "status_code": "200"}
{"text": json.dumps(stripe_response_part2), "status_code": 200}
])
call_command("refresh_customers")
self.active_customer.refresh_from_db()
Expand Down

0 comments on commit b995c94

Please sign in to comment.