Skip to content

Commit

Permalink
Add FAKE_PRODUCT to make tests more realistic
Browse files Browse the repository at this point in the history
Also add test_stripe_plan_null_product for backwards compatibility
Since all the other Plan tests now have a product set
  • Loading branch information
therefromhere committed Apr 13, 2019
1 parent 7842ce6 commit 0580d6f
Show file tree
Hide file tree
Showing 15 changed files with 412 additions and 195 deletions.
12 changes: 12 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,14 @@ def capture(self):
}


FAKE_PRODUCT = {
"id": "prod_fake1",
"object": "product",
"active": True,
"name": "Fake Product",
}


FAKE_PLAN = {
"id": "gold21323",
"object": "plan",
Expand All @@ -725,6 +733,7 @@ def capture(self):
"livemode": False,
"metadata": {},
"name": "New plan name",
"product": FAKE_PRODUCT["id"],
"statement_descriptor": None,
"trial_period_days": None,
"usage_type": "licensed",
Expand All @@ -742,6 +751,7 @@ def capture(self):
"livemode": False,
"metadata": {},
"name": "New plan name",
"product": FAKE_PRODUCT["id"],
"statement_descriptor": None,
"trial_period_days": 12,
"usage_type": "licensed",
Expand All @@ -759,6 +769,7 @@ def capture(self):
"livemode": False,
"metadata": {},
"name": "New plan name",
"product": FAKE_PRODUCT["id"],
"statement_descriptor": None,
"trial_period_days": None,
"usage_type": "licensed",
Expand All @@ -783,6 +794,7 @@ def capture(self):
"livemode": False,
"metadata": {},
"nickname": "Sum Metered Plan",
"product": FAKE_PRODUCT["id"],
"tiers": None,
"tiers_mode": None,
"transform_usage": None,
Expand Down
25 changes: 16 additions & 9 deletions tests/test_charge.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

from . import (
FAKE_ACCOUNT, FAKE_BALANCE_TRANSACTION, FAKE_BALANCE_TRANSACTION_REFUND, FAKE_CHARGE,
FAKE_CHARGE_REFUNDED, FAKE_CUSTOMER, FAKE_FILEUPLOAD, FAKE_INVOICE, FAKE_REFUND,
FAKE_SUBSCRIPTION, FAKE_TRANSFER, AssertStripeFksMixin, default_account
FAKE_CHARGE_REFUNDED, FAKE_CUSTOMER, FAKE_FILEUPLOAD, FAKE_INVOICE, FAKE_PRODUCT,
FAKE_REFUND, FAKE_SUBSCRIPTION, FAKE_TRANSFER, AssertStripeFksMixin, default_account
)


Expand Down Expand Up @@ -89,10 +89,12 @@ def test_capture_charge(self, charge_retrieve_mock, default_account_mock):
@patch("stripe.BalanceTransaction.retrieve")
@patch("stripe.Charge.retrieve")
@patch("stripe.Invoice.retrieve", return_value=deepcopy(FAKE_INVOICE))
@patch("stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT))
@patch("stripe.Subscription.retrieve", return_value=deepcopy(FAKE_SUBSCRIPTION))
def test_sync_from_stripe_data(
self,
subscription_retrieve_mock,
product_retrieve_mock,
invoice_retrieve_mock,
charge_retrieve_mock,
balance_transaction_retrieve_mock,
Expand Down Expand Up @@ -126,17 +128,18 @@ def test_sync_from_stripe_data(
"djstripe.Charge.dispute",
"djstripe.Charge.transfer",
"djstripe.Customer.coupon",
"djstripe.Plan.product",
},
)

@patch("djstripe.models.Account.get_default_account")
@patch("stripe.Charge.retrieve")
@patch("stripe.Invoice.retrieve", return_value=deepcopy(FAKE_INVOICE))
@patch("stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT))
@patch("stripe.Subscription.retrieve", return_value=deepcopy(FAKE_SUBSCRIPTION))
def test_sync_from_stripe_data_refunded_on_update(
self,
subscription_retrieve_mock,
product_retrieve_mock,
invoice_retrieve_mock,
charge_retrieve_mock,
default_account_mock,
Expand Down Expand Up @@ -210,7 +213,6 @@ def test_sync_from_stripe_data_refunded_on_update(
"djstripe.Charge.dispute",
"djstripe.Charge.transfer",
"djstripe.Customer.coupon",
"djstripe.Plan.product",
},
)

Expand All @@ -221,10 +223,12 @@ def test_sync_from_stripe_data_refunded_on_update(
)
@patch("stripe.Charge.retrieve")
@patch("stripe.Invoice.retrieve", return_value=deepcopy(FAKE_INVOICE))
@patch("stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT))
@patch("stripe.Subscription.retrieve", return_value=deepcopy(FAKE_SUBSCRIPTION))
def test_sync_from_stripe_data_refunded(
self,
subscription_retrieve_mock,
product_retrieve_mock,
invoice_retrieve_mock,
charge_retrieve_mock,
balance_transaction_retrieve_mock,
Expand Down Expand Up @@ -268,19 +272,20 @@ def test_sync_from_stripe_data_refunded(
"djstripe.Charge.dispute",
"djstripe.Charge.transfer",
"djstripe.Customer.coupon",
"djstripe.Plan.product",
},
)

@patch("stripe.BalanceTransaction.retrieve")
@patch("stripe.Charge.retrieve")
@patch("stripe.Invoice.retrieve", return_value=deepcopy(FAKE_INVOICE))
@patch("stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT))
@patch("stripe.Subscription.retrieve", return_value=deepcopy(FAKE_SUBSCRIPTION))
@patch("djstripe.models.Account.get_default_account")
def test_sync_from_stripe_data_max_amount(
self,
default_account_mock,
subscription_retrieve_mock,
product_retrieve_mock,
invoice_retrieve_mock,
charge_retrieve_mock,
balance_transaction_retrieve_mock,
Expand Down Expand Up @@ -310,19 +315,20 @@ def test_sync_from_stripe_data_max_amount(
"djstripe.Charge.dispute",
"djstripe.Charge.transfer",
"djstripe.Customer.coupon",
"djstripe.Plan.product",
},
)

@patch("djstripe.models.Account.get_default_account")
@patch("stripe.BalanceTransaction.retrieve")
@patch("stripe.Charge.retrieve")
@patch("stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT))
@patch("stripe.Subscription.retrieve", return_value=deepcopy(FAKE_SUBSCRIPTION))
@patch("stripe.Invoice.retrieve", return_value=deepcopy(FAKE_INVOICE))
def test_sync_from_stripe_data_unsupported_source(
self,
invoice_retrieve_mock,
subscription_retrieve_mock,
product_retrieve_mock,
charge_retrieve_mock,
balance_transaction_retrieve_mock,
default_account_mock,
Expand Down Expand Up @@ -350,7 +356,6 @@ def test_sync_from_stripe_data_unsupported_source(
"djstripe.Charge.dispute",
"djstripe.Charge.transfer",
"djstripe.Customer.coupon",
"djstripe.Plan.product",
},
)

Expand Down Expand Up @@ -393,12 +398,14 @@ def test_sync_from_stripe_data_no_customer(
@patch("stripe.Charge.retrieve")
@patch("stripe.Invoice.retrieve", return_value=deepcopy(FAKE_INVOICE))
@patch("stripe.Transfer.retrieve")
@patch("stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT))
@patch("stripe.Subscription.retrieve", return_value=deepcopy(FAKE_SUBSCRIPTION))
@patch("djstripe.models.Account.get_default_account")
def test_sync_from_stripe_data_with_transfer(
self,
default_account_mock,
subscription_retrieve_mock,
product_retrieve_mock,
transfer_retrieve_mock,
invoice_retrieve_mock,
charge_retrieve_mock,
Expand Down Expand Up @@ -431,13 +438,13 @@ def test_sync_from_stripe_data_with_transfer(
"djstripe.Account.business_logo",
"djstripe.Charge.dispute",
"djstripe.Customer.coupon",
"djstripe.Plan.product",
},
)

@patch("stripe.Charge.retrieve")
@patch("stripe.Account.retrieve")
@patch("stripe.BalanceTransaction.retrieve")
@patch("stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT))
@patch("stripe.Subscription.retrieve", return_value=deepcopy(FAKE_SUBSCRIPTION))
@patch("stripe.Invoice.retrieve", return_value=deepcopy(FAKE_INVOICE))
@patch("stripe.File.retrieve", return_value=deepcopy(FAKE_FILEUPLOAD))
Expand All @@ -446,6 +453,7 @@ def test_sync_from_stripe_data_with_destination(
file_retrive_mock,
invoice_retrieve_mock,
subscription_retrieve_mock,
product_retrieve_mock,
balance_transaction_retrieve_mock,
account_retrieve_mock,
charge_retrieve_mock,
Expand Down Expand Up @@ -474,6 +482,5 @@ def test_sync_from_stripe_data_with_destination(
"djstripe.Charge.dispute",
"djstripe.Charge.transfer",
"djstripe.Customer.coupon",
"djstripe.Plan.product",
},
)
12 changes: 8 additions & 4 deletions tests/test_contrib/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from djstripe.enums import SubscriptionStatus
from djstripe.models import Plan

from .. import FAKE_CUSTOMER, FAKE_PLAN
from .. import FAKE_CUSTOMER, FAKE_PLAN, FAKE_PRODUCT


class SubscriptionSerializerTest(TestCase):
Expand All @@ -29,7 +29,9 @@ def setUp(self):
username="pydanny", email="[email protected]"
)
self.customer = FAKE_CUSTOMER.create_for_user(self.user)
self.plan = Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN))

with patch("stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT)):
self.plan = Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN))

def test_valid_serializer(self):
now = timezone.now()
Expand Down Expand Up @@ -63,7 +65,8 @@ def test_valid_serializer(self):
)
self.assertEqual(serializer.errors, {})

def test_invalid_serializer(self):
@patch("stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT))
def test_invalid_serializer(self, product_retrieve_mock):
now = timezone.now()
serializer = SubscriptionSerializer(
data={
Expand All @@ -83,7 +86,8 @@ def test_invalid_serializer(self):

class CreateSubscriptionSerializerTest(TestCase):
def setUp(self):
self.plan = Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN))
with patch("stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT)):
self.plan = Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN))

@patch("stripe.Token.create", return_value=PropertyMock(id="token_test"))
def test_valid_serializer(self, stripe_token_mock):
Expand Down
9 changes: 6 additions & 3 deletions tests/test_contrib/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from djstripe.enums import SubscriptionStatus
from djstripe.models import Customer, Plan, Subscription

from .. import FAKE_CUSTOMER, FAKE_PLAN, FAKE_SUBSCRIPTION
from .. import FAKE_CUSTOMER, FAKE_PLAN, FAKE_PRODUCT, FAKE_SUBSCRIPTION


class RestSubscriptionTest(APITestCase):
Expand Down Expand Up @@ -98,7 +98,8 @@ def test_get_subscription(self):
Should return the correct data.
"""
plan = Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN))
with patch("stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT)):
plan = Plan.sync_from_stripe_data(deepcopy(FAKE_PLAN))
subscription = Subscription.sync_from_stripe_data(deepcopy(FAKE_SUBSCRIPTION))

response = self.client.get(self.url)
Expand All @@ -125,7 +126,9 @@ def _cancel_sub(*args, **kwargs):
return subscription

fake_cancelled_subscription = deepcopy(FAKE_SUBSCRIPTION)
Subscription.sync_from_stripe_data(fake_cancelled_subscription)

with patch("stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT)):
Subscription.sync_from_stripe_data(fake_cancelled_subscription)

cancel_subscription_mock.side_effect = _cancel_sub

Expand Down
Loading

0 comments on commit 0580d6f

Please sign in to comment.