Browse Source

Make sure account limits are reset when user cancels their subscription.

pull/287/head
Pēteris Caune 5 years ago
parent
commit
4c39aeea83
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
2 changed files with 35 additions and 8 deletions
  1. +26
    -1
      hc/payments/tests/test_set_plan.py
  2. +9
    -7
      hc/payments/views.py

+ 26
- 1
hc/payments/tests/test_set_plan.py View File

@ -150,11 +150,36 @@ class SetPlanTestCase(BaseTestCase):
@patch("hc.payments.models.braintree")
def test_subscription_creation_failure(self, mock):
self._setup_mock(mock)
mock.Subscription.create.return_value.is_success = False
mock.Subscription.create.return_value.message = "sub failure"
r = self.run_set_plan()
self.assertRedirects(r, "/accounts/profile/billing/")
self.assertContains(r, "sub failure")
@patch("hc.payments.models.braintree")
def test_failed_plan_change_resets_limits(self, mock):
# Initial state: the user has a subscription and a high check limit:
sub = Subscription.objects.for_user(self.alice)
sub.subscription_id = "old-sub-id"
sub.save()
self.profile.check_limit = 1000
self.profile.save()
# Simulate a subscription creation failure:
mock.Subscription.create.return_value.is_success = False
mock.Subscription.create.return_value.message = "sub failure"
r = self.run_set_plan()
# It should cancel the current plan
self.assertTrue(mock.Subscription.cancel.called)
# It should clear out the limits:
self.profile.refresh_from_db()
self.assertEqual(self.profile.check_limit, 20)
# And it should show the error message from API:
self.assertRedirects(r, "/accounts/profile/billing/")
self.assertContains(r, "sub failure")

+ 9
- 7
hc/payments/views.py View File

@ -105,15 +105,17 @@ def set_plan(request):
if sub.plan_id == plan_id:
return redirect("hc-billing")
# Cancel the previous plan
# Cancel the previous plan and reset limits:
sub.cancel()
profile = request.user.profile
profile.ping_log_limit = 100
profile.check_limit = 20
profile.team_limit = 2
profile.sms_limit = 0
profile.save()
if plan_id == "":
profile = request.user.profile
profile.ping_log_limit = 100
profile.check_limit = 20
profile.team_limit = 2
profile.sms_limit = 0
profile.save()
return redirect("hc-billing")
result = sub.setup(plan_id)


Loading…
Cancel
Save