Skip to content

Commit

Permalink
Merge pull request #349 from stripe/ob-detach-sources
Browse files Browse the repository at this point in the history
detach method for detaching sources from customers
  • Loading branch information
brandur-stripe authored Oct 11, 2017
2 parents 8654ee6 + d1711d3 commit db8e4bf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
14 changes: 10 additions & 4 deletions stripe/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
23 changes: 19 additions & 4 deletions stripe/test/resources/test_sources.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import warnings

import stripe
from stripe.test.helper import StripeResourceTest

Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand Down

0 comments on commit db8e4bf

Please sign in to comment.