From d1711d3def0f02c050149a5553cfaac15b889fe6 Mon Sep 17 00:00:00 2001 From: Olivier Bellone Date: Wed, 11 Oct 2017 11:38:46 +0200 Subject: [PATCH] detach method for detaching sources from customers --- stripe/resource.py | 14 ++++++++++---- stripe/test/resources/test_sources.py | 23 +++++++++++++++++++---- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/stripe/resource.py b/stripe/resource.py index 4f6dc499d..43ee36aa2 100644 --- a/stripe/resource.py +++ b/stripe/resource.py @@ -1184,7 +1184,7 @@ def class_url(cls): class Source(CreateableAPIResource, UpdateableAPIResource, VerifyMixin): - def delete(self, **params): + def detach(self, **params): if hasattr(self, 'customer') and self.customer: extn = urllib.quote_plus(util.utf8(self.id)) customer = util.utf8(self.customer) @@ -1197,6 +1197,12 @@ def delete(self, **params): else: raise NotImplementedError( - 'Source objects cannot be deleted, they can only be detached ' - 'from customer objects. This source object does not appear to ' - 'be currently attached to a customer object.') + "This source object does not appear to be currently attached " + "to a customer object.") + + def delete(self, **params): + warnings.warn("The `Source.delete` method is deprecated and will " + "be removed in future versions. Please use the " + "`Source.detach` method instead", + DeprecationWarning) + self.detach(**params) diff --git a/stripe/test/resources/test_sources.py b/stripe/test/resources/test_sources.py index edcbbf49b..5d41c104d 100644 --- a/stripe/test/resources/test_sources.py +++ b/stripe/test/resources/test_sources.py @@ -1,3 +1,5 @@ +import warnings + import stripe from stripe.test.helper import StripeResourceTest @@ -46,18 +48,18 @@ def test_update_source(self): None ) - def test_delete_source_unattached(self): + def test_detach_source_unattached(self): source = stripe.Source.construct_from({ 'id': 'src_foo', }, 'api_key') - self.assertRaises(NotImplementedError, source.delete) + self.assertRaises(NotImplementedError, source.detach) - def test_delete_source_attached(self): + def test_detach_source_attached(self): source = stripe.Source.construct_from({ 'id': 'src_foo', 'customer': 'cus_bar', }, 'api_key') - source.delete() + source.detach() self.requestor_mock.request.assert_called_with( 'delete', @@ -66,6 +68,19 @@ def test_delete_source_attached(self): None ) + def test_delete_source(self): + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter('always') + + source = stripe.Source.construct_from({ + 'id': 'src_foo', + 'customer': 'cus_bar', + }, 'api_key') + source.delete() + + self.assertEqual(1, len(w)) + self.assertEqual(w[0].category, DeprecationWarning) + def test_verify_source(self): source = stripe.Source.construct_from({ 'id': 'src_foo',