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.

161 lines
5.7 KiB

9 years ago
9 years ago
9 years ago
  1. from mock import patch
  2. from hc.payments.models import Subscription
  3. from hc.test import BaseTestCase
  4. class SetPlanTestCase(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_set_plan(self, plan_id="P20"):
  10. form = {"plan_id": plan_id}
  11. self.client.login(username="[email protected]", password="password")
  12. return self.client.post("/pricing/set_plan/", 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_set_plan()
  20. self.assertRedirects(r, "/accounts/profile/billing/")
  21. # Subscription should be filled out:
  22. sub = Subscription.objects.get(user=self.alice)
  23. self.assertEqual(sub.subscription_id, "t-sub-id")
  24. self.assertEqual(sub.plan_id, "P20")
  25. self.assertEqual(sub.plan_name, "Business ($20 / month)")
  26. # User's profile should have a higher limits
  27. self.profile.refresh_from_db()
  28. self.assertEqual(self.profile.ping_log_limit, 1000)
  29. self.assertEqual(self.profile.check_limit, 100)
  30. self.assertEqual(self.profile.team_limit, 9)
  31. self.assertEqual(self.profile.sms_limit, 50)
  32. self.assertEqual(self.profile.sms_sent, 0)
  33. # braintree.Subscription.cancel should have not been called
  34. assert not mock.Subscription.cancel.called
  35. @patch("hc.payments.models.braintree")
  36. def test_yearly_works(self, mock):
  37. self._setup_mock(mock)
  38. self.profile.sms_limit = 0
  39. self.profile.sms_sent = 1
  40. self.profile.save()
  41. r = self.run_set_plan("Y192")
  42. self.assertRedirects(r, "/accounts/profile/billing/")
  43. # Subscription should be filled out:
  44. sub = Subscription.objects.get(user=self.alice)
  45. self.assertEqual(sub.subscription_id, "t-sub-id")
  46. self.assertEqual(sub.plan_id, "Y192")
  47. self.assertEqual(sub.plan_name, "Business ($192 / year)")
  48. # User's profile should have a higher limits
  49. self.profile.refresh_from_db()
  50. self.assertEqual(self.profile.ping_log_limit, 1000)
  51. self.assertEqual(self.profile.check_limit, 100)
  52. self.assertEqual(self.profile.team_limit, 9)
  53. self.assertEqual(self.profile.sms_limit, 50)
  54. self.assertEqual(self.profile.sms_sent, 0)
  55. # braintree.Subscription.cancel should have not been called
  56. assert not mock.Subscription.cancel.called
  57. @patch("hc.payments.models.braintree")
  58. def test_plus_works(self, mock):
  59. self._setup_mock(mock)
  60. self.profile.sms_limit = 0
  61. self.profile.sms_sent = 1
  62. self.profile.save()
  63. r = self.run_set_plan("P80")
  64. self.assertRedirects(r, "/accounts/profile/billing/")
  65. # Subscription should be filled out:
  66. sub = Subscription.objects.get(user=self.alice)
  67. self.assertEqual(sub.subscription_id, "t-sub-id")
  68. self.assertEqual(sub.plan_id, "P80")
  69. self.assertEqual(sub.plan_name, "Business Plus ($80 / month)")
  70. # User's profile should have a higher limits
  71. self.profile.refresh_from_db()
  72. self.assertEqual(self.profile.ping_log_limit, 1000)
  73. self.assertEqual(self.profile.check_limit, 1000)
  74. self.assertEqual(self.profile.team_limit, 500)
  75. self.assertEqual(self.profile.sms_limit, 500)
  76. self.assertEqual(self.profile.sms_sent, 0)
  77. # braintree.Subscription.cancel should have not been called
  78. assert not mock.Subscription.cancel.called
  79. @patch("hc.payments.models.braintree")
  80. def test_it_cancels(self, mock):
  81. self._setup_mock(mock)
  82. self.sub = Subscription(user=self.alice)
  83. self.sub.subscription_id = "test-id"
  84. self.sub.plan_id = "P20"
  85. self.sub.plan_name = "Business ($20/mo)"
  86. self.sub.save()
  87. self.profile.sms_limit = 1
  88. self.profile.sms_sent = 1
  89. self.profile.save()
  90. r = self.run_set_plan("")
  91. self.assertRedirects(r, "/accounts/profile/billing/")
  92. # Subscription should be cleared
  93. sub = Subscription.objects.get(user=self.alice)
  94. self.assertEqual(sub.subscription_id, "")
  95. self.assertEqual(sub.plan_id, "")
  96. self.assertEqual(sub.plan_name, "")
  97. # User's profile should have standard limits
  98. self.profile.refresh_from_db()
  99. self.assertEqual(self.profile.ping_log_limit, 100)
  100. self.assertEqual(self.profile.check_limit, 20)
  101. self.assertEqual(self.profile.team_limit, 2)
  102. self.assertEqual(self.profile.sms_limit, 0)
  103. assert mock.Subscription.cancel.called
  104. def test_bad_plan_id(self):
  105. r = self.run_set_plan(plan_id="this-is-wrong")
  106. self.assertEqual(r.status_code, 400)
  107. @patch("hc.payments.models.braintree")
  108. def test_it_cancels_previous_subscription(self, mock):
  109. self._setup_mock(mock)
  110. sub = Subscription(user=self.alice)
  111. sub.subscription_id = "prev-sub"
  112. sub.save()
  113. r = self.run_set_plan()
  114. self.assertRedirects(r, "/accounts/profile/billing/")
  115. assert mock.Subscription.cancel.called
  116. @patch("hc.payments.models.braintree")
  117. def test_subscription_creation_failure(self, mock):
  118. self._setup_mock(mock)
  119. mock.Subscription.create.return_value.is_success = False
  120. mock.Subscription.create.return_value.message = "sub failure"
  121. r = self.run_set_plan()
  122. self.assertRedirects(r, "/accounts/profile/billing/")
  123. self.assertContains(r, "sub failure")