def unsubscribe_payments_on_user_delete(sender, instance, **kwargs): """Cancel Stripe subscriptions before deleting User.""" user = instance if user.stripe_customer_id: # This may raise an exception if the Stripe API call fails. # This will stop User deletion while an admin investigates. cancel_stripe_customer_subscription(user.stripe_customer_id)
def scrub_user(): # Before doing anything, cancel any active subscriptions first. if user.stripe_customer_id: for subscription in cancel_stripe_customer_subscription( user.stripe_customer_id): UserSubscription.set_canceled(request.user, subscription.id) # From the User abstract class user.first_name = "" user.last_name = "" user.email = "" # All User attributes user.timezone = "" user.locale = "" user.homepage = "" user.title = "" user.fullname = "" user.organization = "" user.location = "" user.bio = "" user.irc_nickname = "" user.website_url = "" user.github_url = "" user.mozillians_url = "" user.twitter_url = "" user.linkedin_url = "" user.facebook_url = "" user.stackoverflow_url = "" user.discourse_url = "" user.stripe_customer_id = "" user.save() user.socialaccount_set.all().delete() user.key_set.all().delete()
def test_cancel_stripe_customer_no_subscriptions(mock_sub, mock_cust): '''A stripe customer with no subscriptions returns True.''' cancel_stripe_customer_subscription('cust_id') assert not mock_sub.called
def test_cancel_stripe_customer_multiple_subscriptions(mock_sub, mock_cust): '''A stripe customer's subscriptions can be cancelled.''' cancel_stripe_customer_subscription('cust_id') assert mock_sub.call_count == 2
def test_cancel_stripe_customer_subscription(mock_sub, mock_cust): '''A stripe customer's subscriptions can be cancelled.''' cancel_stripe_customer_subscription('cust_id') mock_sub.return_value.delete.assert_called_once_with()
def test_cancel_stripe_customer_no_subscriptions(mock_sub, mock_cust): """A stripe customer with no subscriptions returns True.""" cancel_stripe_customer_subscription("cust_id") assert not mock_sub.called
def test_cancel_stripe_customer_multiple_subscriptions(mock_sub, mock_cust): """A stripe customer's subscriptions can be cancelled.""" cancel_stripe_customer_subscription("cust_id") assert mock_sub.call_count == 2
def test_cancel_stripe_customer_subscription(mock_sub, mock_cust): """A stripe customer's subscriptions can be cancelled.""" cancel_stripe_customer_subscription("cust_id") mock_sub.return_value.delete.assert_called_once_with()