You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
7.5 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. from unittest.mock import patch
  2. from hc.payments.models import Subscription
  3. from hc.test import BaseTestCase
  4. class UpdateSubscriptionTestCase(BaseTestCase):
  5. def _setup_mock(self, mock):
  6. """ Set up Braintree calls that the controller will use. """
  7. mock.Subscription.create.return_value.is_success = True
  8. mock.Subscription.create.return_value.subscription.id = "t-sub-id"
  9. def run_update(self, plan_id="P20", nonce="fake-nonce"):
  10. form = {"plan_id": plan_id, "nonce": nonce}
  11. self.client.login(username="[email protected]", password="password")
  12. return self.client.post("/pricing/update/", form, follow=True)
  13. @patch("hc.payments.models.braintree")
  14. def test_it_works(self, mock):
  15. self._setup_mock(mock)
  16. self.profile.sms_limit = 0
  17. self.profile.sms_sent = 1
  18. self.profile.save()
  19. r = self.run_update()
  20. self.assertRedirects(r, "/accounts/profile/billing/")
  21. self.assertContains(r, "Your billing plan has been updated!")
  22. # Subscription should be filled out:
  23. sub = Subscription.objects.get(user=self.alice)
  24. self.assertEqual(sub.subscription_id, "t-sub-id")
  25. self.assertEqual(sub.plan_id, "P20")
  26. self.assertEqual(sub.plan_name, "Business ($20 / month)")
  27. # User's profile should have a higher limits
  28. self.profile.refresh_from_db()
  29. self.assertEqual(self.profile.ping_log_limit, 1000)
  30. self.assertEqual(self.profile.check_limit, 100)
  31. self.assertEqual(self.profile.team_limit, 9)
  32. self.assertEqual(self.profile.sms_limit, 50)
  33. self.assertEqual(self.profile.sms_sent, 0)
  34. # braintree.Subscription.cancel should have not been called
  35. # because there was no previous subscription
  36. self.assertFalse(mock.Subscription.cancel.called)
  37. self.assertTrue(mock.Subscription.create.called)
  38. @patch("hc.payments.models.braintree")
  39. def test_yearly_works(self, mock):
  40. self._setup_mock(mock)
  41. self.profile.sms_limit = 0
  42. self.profile.sms_sent = 1
  43. self.profile.save()
  44. r = self.run_update("Y192")
  45. self.assertRedirects(r, "/accounts/profile/billing/")
  46. # Subscription should be filled out:
  47. sub = Subscription.objects.get(user=self.alice)
  48. self.assertEqual(sub.subscription_id, "t-sub-id")
  49. self.assertEqual(sub.plan_id, "Y192")
  50. self.assertEqual(sub.plan_name, "Business ($192 / year)")
  51. # User's profile should have a higher limits
  52. self.profile.refresh_from_db()
  53. self.assertEqual(self.profile.ping_log_limit, 1000)
  54. self.assertEqual(self.profile.check_limit, 100)
  55. self.assertEqual(self.profile.team_limit, 9)
  56. self.assertEqual(self.profile.sms_limit, 50)
  57. self.assertEqual(self.profile.sms_sent, 0)
  58. # braintree.Subscription.cancel should have not been called
  59. assert not mock.Subscription.cancel.called
  60. @patch("hc.payments.models.braintree")
  61. def test_plus_works(self, mock):
  62. self._setup_mock(mock)
  63. self.profile.sms_limit = 0
  64. self.profile.sms_sent = 1
  65. self.profile.save()
  66. r = self.run_update("P80")
  67. self.assertRedirects(r, "/accounts/profile/billing/")
  68. # Subscription should be filled out:
  69. sub = Subscription.objects.get(user=self.alice)
  70. self.assertEqual(sub.subscription_id, "t-sub-id")
  71. self.assertEqual(sub.plan_id, "P80")
  72. self.assertEqual(sub.plan_name, "Business Plus ($80 / month)")
  73. # User's profile should have a higher limits
  74. self.profile.refresh_from_db()
  75. self.assertEqual(self.profile.ping_log_limit, 1000)
  76. self.assertEqual(self.profile.check_limit, 1000)
  77. self.assertEqual(self.profile.team_limit, 500)
  78. self.assertEqual(self.profile.sms_limit, 500)
  79. self.assertEqual(self.profile.sms_sent, 0)
  80. # braintree.Subscription.cancel should have not been called
  81. assert not mock.Subscription.cancel.called
  82. @patch("hc.payments.models.braintree")
  83. def test_it_cancels(self, mock):
  84. self._setup_mock(mock)
  85. self.sub = Subscription(user=self.alice)
  86. self.sub.subscription_id = "test-id"
  87. self.sub.plan_id = "P20"
  88. self.sub.plan_name = "Business ($20/mo)"
  89. self.sub.save()
  90. self.profile.sms_limit = 1
  91. self.profile.sms_sent = 1
  92. self.profile.save()
  93. r = self.run_update("")
  94. self.assertRedirects(r, "/accounts/profile/billing/")
  95. self.assertContains(r, "Your billing plan has been updated!")
  96. # Subscription should be cleared
  97. sub = Subscription.objects.get(user=self.alice)
  98. self.assertEqual(sub.subscription_id, "")
  99. self.assertEqual(sub.plan_id, "")
  100. self.assertEqual(sub.plan_name, "")
  101. # User's profile should have standard limits
  102. self.profile.refresh_from_db()
  103. self.assertEqual(self.profile.ping_log_limit, 100)
  104. self.assertEqual(self.profile.check_limit, 20)
  105. self.assertEqual(self.profile.team_limit, 2)
  106. self.assertEqual(self.profile.sms_limit, 0)
  107. self.assertTrue(mock.Subscription.cancel.called)
  108. def test_bad_plan_id(self):
  109. r = self.run_update(plan_id="this-is-wrong")
  110. self.assertEqual(r.status_code, 400)
  111. @patch("hc.payments.models.braintree")
  112. def test_it_cancels_previous_subscription(self, mock):
  113. self._setup_mock(mock)
  114. sub = Subscription(user=self.alice)
  115. sub.subscription_id = "prev-sub"
  116. sub.save()
  117. r = self.run_update()
  118. self.assertRedirects(r, "/accounts/profile/billing/")
  119. self.assertTrue(mock.Subscription.cancel.called)
  120. @patch("hc.payments.models.braintree")
  121. def test_subscription_creation_failure(self, mock):
  122. mock.Subscription.create.return_value.is_success = False
  123. mock.Subscription.create.return_value.message = "sub failure"
  124. r = self.run_update()
  125. self.assertRedirects(r, "/accounts/profile/billing/")
  126. self.assertContains(r, "sub failure")
  127. @patch("hc.payments.models.braintree")
  128. def test_failed_plan_change_resets_limits(self, mock):
  129. # Initial state: the user has a subscription and a high check limit:
  130. sub = Subscription.objects.for_user(self.alice)
  131. sub.subscription_id = "old-sub-id"
  132. sub.save()
  133. self.profile.check_limit = 1000
  134. self.profile.save()
  135. # Simulate a subscription creation failure:
  136. mock.Subscription.create.return_value.is_success = False
  137. mock.Subscription.create.return_value.message = "sub failure"
  138. r = self.run_update()
  139. # It should cancel the current plan
  140. self.assertTrue(mock.Subscription.cancel.called)
  141. # It should clear out the limits:
  142. self.profile.refresh_from_db()
  143. self.assertEqual(self.profile.check_limit, 20)
  144. # And it should show the error message from API:
  145. self.assertRedirects(r, "/accounts/profile/billing/")
  146. self.assertContains(r, "sub failure")
  147. @patch("hc.payments.models.braintree")
  148. def test_it_updates_payment_method(self, mock):
  149. # Initial state: the user has a subscription and a high check limit:
  150. sub = Subscription.objects.for_user(self.alice)
  151. sub.plan_id = "P20"
  152. sub.subscription_id = "old-sub-id"
  153. sub.save()
  154. r = self.run_update()
  155. # It should update the existing subscription
  156. self.assertTrue(mock.Subscription.update.called)
  157. self.assertRedirects(r, "/accounts/profile/billing/")
  158. self.assertContains(r, "Your payment method has been updated!")