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.

159 lines
5.6 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.save()
  86. self.profile.sms_limit = 1
  87. self.profile.sms_sent = 1
  88. self.profile.save()
  89. r = self.run_set_plan("")
  90. self.assertRedirects(r, "/accounts/profile/billing/")
  91. # Subscription should be cleared
  92. sub = Subscription.objects.get(user=self.alice)
  93. self.assertEqual(sub.subscription_id, "")
  94. self.assertEqual(sub.plan_id, "")
  95. # User's profile should have standard limits
  96. self.profile.refresh_from_db()
  97. self.assertEqual(self.profile.ping_log_limit, 100)
  98. self.assertEqual(self.profile.check_limit, 20)
  99. self.assertEqual(self.profile.team_limit, 2)
  100. self.assertEqual(self.profile.sms_limit, 0)
  101. assert mock.Subscription.cancel.called
  102. def test_bad_plan_id(self):
  103. r = self.run_set_plan(plan_id="this-is-wrong")
  104. self.assertEqual(r.status_code, 400)
  105. @patch("hc.payments.models.braintree")
  106. def test_it_cancels_previous_subscription(self, mock):
  107. self._setup_mock(mock)
  108. sub = Subscription(user=self.alice)
  109. sub.subscription_id = "prev-sub"
  110. sub.save()
  111. r = self.run_set_plan()
  112. self.assertRedirects(r, "/accounts/profile/billing/")
  113. assert mock.Subscription.cancel.called
  114. @patch("hc.payments.models.braintree")
  115. def test_subscription_creation_failure(self, mock):
  116. self._setup_mock(mock)
  117. mock.Subscription.create.return_value.is_success = False
  118. mock.Subscription.create.return_value.message = "sub failure"
  119. r = self.run_set_plan()
  120. self.assertRedirects(r, "/accounts/profile/billing/")
  121. self.assertContains(r, "sub failure")