Skip to content

Commit

Permalink
[BE] add feature to change customer description
Browse files Browse the repository at this point in the history
  • Loading branch information
poxip committed May 4, 2018
1 parent bf8636e commit 30185d8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 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.5.0]
### Added
- changing customer description

## [0.4.1]
### Fixed
- stripe API key was not set for Refunds.
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.4.1"
__version__ = "0.5.0"
__author__ = "Jacek Ostanski"
__license__ = "MIT"
__copyright__ = "Copyright 2017 Arabella"
Expand Down
7 changes: 7 additions & 0 deletions aa_stripe/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ def get_latest_active_customer_for_user(cls, user):
customer = cls.objects.filter(user_id=user.id, is_active=True).last()
return customer

def change_description(self, description):
stripe.api_key = stripe_settings.API_KEY
customer = stripe.Customer.retrieve(self.stripe_customer_id)
customer.description = description
customer.save()
return customer

class Meta:
ordering = ["id"]

Expand Down
23 changes: 16 additions & 7 deletions tests/test_customers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@
class TestCreatingUsers(APITestCase):
def setUp(self):
self.user = UserModel.objects.create(email="foo@bar.bar", username="foo", password="dump-password")

def test_user_create(self):
self.assertEqual(StripeCustomer.objects.count(), 0)
url = reverse("stripe-customers")
stripe_js_response = {
self.stripe_js_response = {
"id": "tok_193mTaHSTEMJ0IPXhhZ5vuTX",
"object": "customer",
"client_ip": None,
Expand Down Expand Up @@ -49,6 +45,10 @@ def test_user_create(self):
},
}

def test_user_create(self):
self.assertEqual(StripeCustomer.objects.count(), 0)
url = reverse("stripe-customers")

data = {}
response = self.client.post(url, format="json")
self.assertEqual(response.status_code, 403) # not logged
Expand Down Expand Up @@ -104,7 +104,7 @@ def test_user_create(self):

# test response error
stripe_customer_qs = StripeCustomer.objects.filter(is_created_at_stripe=True)
data = {"stripe_js_response": stripe_js_response}
data = {"stripe_js_response": self.stripe_js_response}
self.client.force_authenticate(user=self.user)
response = self.client.post(url, data, format="json")
self.assertEqual(response.status_code, 400)
Expand All @@ -121,6 +121,15 @@ def test_user_create(self):
customer = stripe_customer_qs.first()
self.assertTrue(customer.is_active)
self.assertEqual(customer.user, self.user)
self.assertEqual(customer.stripe_js_response, stripe_js_response)
self.assertEqual(customer.stripe_js_response, self.stripe_js_response)
self.assertEqual(customer.stripe_customer_id, "cus_9Oop0gQ1R1ATMi")
self.assertEqual(customer.stripe_response["id"], "cus_9Oop0gQ1R1ATMi")

def test_change_description(self):
customer_id = self.stripe_js_response["id"]
customer = StripeCustomer(user=self.user, stripe_customer_id=customer_id)
api_url = "https://api.stripe.com/v1/customers/{customer_id}".format(customer_id=customer_id)
with requests_mock.Mocker() as m:
m.register_uri("GET", api_url, text=json.dumps(self.stripe_js_response))
m.register_uri("POST", api_url, text=json.dumps(self.stripe_js_response))
customer.change_description("abc")

0 comments on commit 30185d8

Please sign in to comment.