diff --git a/hc/payments/tests/test_set_plan.py b/hc/payments/tests/test_set_plan.py index 50709478..f5fbd696 100644 --- a/hc/payments/tests/test_set_plan.py +++ b/hc/payments/tests/test_set_plan.py @@ -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") diff --git a/hc/payments/views.py b/hc/payments/views.py index aa239ef8..85e5e993 100644 --- a/hc/payments/views.py +++ b/hc/payments/views.py @@ -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)