From b0b9e2e0cb67641834e4f7d6c58d57b6aa2a0c11 Mon Sep 17 00:00:00 2001 From: Lee Skillen Date: Thu, 7 Jul 2016 20:55:52 +0100 Subject: [PATCH] Fix subscription update for empty/false-like values --- djstripe/stripe_objects.py | 2 +- tests/test_subscription.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/djstripe/stripe_objects.py b/djstripe/stripe_objects.py index 0354e53a4e..e1e0238b01 100644 --- a/djstripe/stripe_objects.py +++ b/djstripe/stripe_objects.py @@ -1481,7 +1481,7 @@ def update(self, plan=None, application_fee_percent=None, coupon=None, prorate=N stripe_subscription = self.api_retrieve() for kwarg, value in kwargs.items(): - if value: + if value is not None: setattr(stripe_subscription, kwarg, value) return stripe_subscription.save() diff --git a/tests/test_subscription.py b/tests/test_subscription.py index a0900fa789..a573195a3a 100644 --- a/tests/test_subscription.py +++ b/tests/test_subscription.py @@ -8,6 +8,7 @@ import calendar from copy import deepcopy +from decimal import Decimal from django.contrib.auth import get_user_model from django.test import TestCase @@ -145,6 +146,21 @@ def test_update(self, customer_retrieve_mock, subscription_retrieve_mock, plan_r self.assertEqual(4, new_subscription.quantity) + @patch("stripe.Plan.retrieve", return_value=deepcopy(FAKE_PLAN)) + @patch("stripe.Subscription.retrieve") + @patch("stripe.Customer.retrieve", return_value=deepcopy(FAKE_CUSTOMER)) + def test_update_set_empty_value(self, customer_retrieve_mock, subscription_retrieve_mock, plan_retrieve_mock): + subscription_fake = deepcopy(FAKE_SUBSCRIPTION) + subscription_fake.update({'tax_percent': Decimal(20.0)}) + subscription_retrieve_mock.return_value = subscription_fake + subscription = Subscription.sync_from_stripe_data(subscription_fake) + + self.assertEqual(Decimal(20.0), subscription.tax_percent) + + new_subscription = subscription.update(tax_percent=Decimal(0.0)) + + self.assertEqual(Decimal(0.0), new_subscription.tax_percent) + @patch("stripe.Plan.retrieve", return_value=deepcopy(FAKE_PLAN)) @patch("stripe.Subscription.retrieve", return_value=deepcopy(FAKE_SUBSCRIPTION)) @patch("stripe.Customer.retrieve", return_value=deepcopy(FAKE_CUSTOMER))