Skip to content

Commit

Permalink
Feature/get stripe customer (#38)
Browse files Browse the repository at this point in the history
* failing test on get stripe customer id

* fix test on get stripe customer id

* fix build

* CR fixes
  • Loading branch information
hash committed Dec 14, 2017
1 parent 262833b commit 957efe7
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 43 deletions.
19 changes: 16 additions & 3 deletions aa_stripe/api.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import simplejson as json
import stripe
from rest_framework import status
from rest_framework.generics import CreateAPIView, RetrieveAPIView
from rest_framework.generics import CreateAPIView, RetrieveAPIView, get_object_or_404
from rest_framework.mixins import RetrieveModelMixin
from rest_framework.permissions import AllowAny, IsAuthenticated
from rest_framework.response import Response

from aa_stripe.models import StripeCoupon, StripeCustomer, StripeWebhook
from aa_stripe.serializers import StripeCouponSerializer, StripeCustomerSerializer, StripeWebhookSerializer
from aa_stripe.serializers import (StripeCouponSerializer, StripeCustomerRetriveSerializer, StripeCustomerSerializer,
StripeWebhookSerializer)
from aa_stripe.settings import stripe_settings


Expand All @@ -17,11 +19,22 @@ class CouponDetailsAPI(RetrieveAPIView):
lookup_field = "coupon_id"


class CustomersAPI(CreateAPIView):
class CustomersAPI(CreateAPIView, RetrieveModelMixin):
queryset = StripeCustomer.objects.all()
serializer_class = StripeCustomerSerializer
permission_classes = (IsAuthenticated,)

def get_object(self):
return get_object_or_404(self.get_queryset(), user=self.request.user)

def get_serializer_class(self):
if hasattr(self, "request") and self.request.method == "GET":
return StripeCustomerRetriveSerializer
return self.serializer_class

def get(self, request):
return self.retrieve(request)


class WebhookAPI(CreateAPIView):
queryset = StripeWebhook.objects.all()
Expand Down
6 changes: 6 additions & 0 deletions aa_stripe/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ class Meta:
]


class StripeCustomerRetriveSerializer(ModelSerializer):
class Meta:
model = StripeCustomer
fields = ["stripe_customer_id"]


class StripeCustomerSerializer(ModelSerializer):
stripe_js_response = JSONField()

Expand Down
108 changes: 68 additions & 40 deletions tests/test_customers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ 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 = {
def get_stripe_js_response(self):
return {
"id": "tok_193mTaHSTEMJ0IPXhhZ5vuTX",
"object": "customer",
"client_ip": None,
Expand Down Expand Up @@ -49,6 +47,43 @@ def test_user_create(self):
},
}

def get_successful_create_stripe_customer_response(self, id="cus_9Oop0gQ1R1ATMi"):
return {
"id": id,
"object": "customer",
"account_balance": 0,
"created": 1476810921,
"currency": "usd",
"default_source": None,
"delinquent": False,
"description": None,
"discount": None,
"email": None,
"livemode": False,
"metadata": {},
"shipping": None,
"sources": {
"object": "list",
"data": [],
"has_more": False,
"total_count": 0,
"url": "/v1/customers/{}/sources".format(id)
},
"subscriptions": {
"object": "list",
"data": [],
"has_more": False,
"total_count": 0,
"url": "/v1/customers/{}/subscriptions".format(id)
}
}

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

stripe_customer_id = "cus_9Oop0gQ1R1ATMi"
data = {}
response = self.client.post(url, format="json")
self.assertEqual(response.status_code, 403) # not logged
Expand All @@ -58,7 +93,7 @@ def test_user_create(self):
self.assertEqual(response.status_code, 400)

with requests_mock.Mocker() as m:
m.register_uri('POST', 'https://api.stripe.com/v1/customers', [
m.register_uri("POST", "https://api.stripe.com/v1/customers", [
{
"text": json.dumps({
"error": {"message": "Your card was declined.", "type": "card_error", "param": "",
Expand All @@ -67,39 +102,7 @@ def test_user_create(self):
"status_code": 400
},
{
"text": json.dumps({
"id": "cus_9Oop0gQ1R1ATMi",
"object": "customer",
"account_balance": 0,
"created": 1476810921,
"currency": "usd",
"default_source": None,
"delinquent": False,
"description": None,
"discount": None,
"email": None,
"livemode": False,
"metadata": {
},
"shipping": None,
"sources": {
"object": "list",
"data": [

],
"has_more": False,
"total_count": 0,
"url": "/v1/customers/cus_9Oop0gQ1R1ATMi/sources"
},
"subscriptions": {
"object": "list",
"data": [

],
"has_more": False,
"total_count": 0,
"url": "/v1/customers/cus_9Oop0gQ1R1ATMi/subscriptions"
}})
"text": json.dumps(self.get_successful_create_stripe_customer_response(stripe_customer_id))
}])

# test response error
Expand All @@ -122,9 +125,34 @@ def test_user_create(self):
self.assertTrue(customer.is_active)
self.assertEqual(customer.user, self.user)
self.assertEqual(customer.stripe_js_response, stripe_js_response)
self.assertEqual(customer.stripe_customer_id, "cus_9Oop0gQ1R1ATMi")
self.assertEqual(customer.stripe_response["id"], "cus_9Oop0gQ1R1ATMi")
self.assertEqual(customer.stripe_customer_id, stripe_customer_id)
self.assertEqual(customer.stripe_response["id"], stripe_customer_id)
self.assertIsNotNone(customer.default_card)
self.assertEqual(customer.default_card.last4, "4242")
self.assertEqual(customer.default_card.exp_month, 8)
self.assertEqual(customer.default_card.exp_year, 2017)

def test_user_get_stripe_customer_id(self):
url = reverse("stripe-customers")
stripe_customer_id = "cus_Bw7eXawkf0zsal"

response = self.client.get(url, format="json")
self.assertEqual(response.status_code, 403) # not logged

self.client.force_authenticate(user=self.user)
response = self.client.get(url, format="json")
self.assertEqual(response.status_code, 404) # logged in but customer is not yet created

with requests_mock.Mocker() as m:
m.register_uri(
"POST", "https://api.stripe.com/v1/customers",
[{
"text": json.dumps(self.get_successful_create_stripe_customer_response(stripe_customer_id))
}])
data = {"stripe_js_response": self.get_stripe_js_response()}
self.client.post(url, data, format="json")

response = self.client.get(url, format="json")
self.assertEqual(response.status_code, 200) # logged in and customer is created
self.assertEqual(set(response.data.keys()), {"stripe_customer_id"})
self.assertEqual(response.data["stripe_customer_id"], stripe_customer_id)

0 comments on commit 957efe7

Please sign in to comment.