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.

104 lines
3.7 KiB

9 years ago
9 years ago
9 years ago
9 years ago
  1. from mock import patch
  2. from hc.accounts.models import Profile
  3. from hc.payments.models import Subscription
  4. from hc.test import BaseTestCase
  5. class CreatePlanTestCase(BaseTestCase):
  6. def _setup_mock(self, mock):
  7. """ Set up Braintree calls that the controller will use. """
  8. mock.Customer.create.return_value.is_success = True
  9. mock.Customer.create.return_value.customer.id = "test-customer-id"
  10. mock.PaymentMethod.create.return_value.is_success = True
  11. mock.PaymentMethod.create.return_value.payment_method.token = "t-token"
  12. mock.Subscription.create.return_value.is_success = True
  13. mock.Subscription.create.return_value.subscription.id = "t-sub-id"
  14. def run_create_plan(self, plan_id="P5"):
  15. form = {"plan_id": plan_id, "payment_method_nonce": "test-nonce"}
  16. self.client.login(username="[email protected]", password="password")
  17. return self.client.post("/pricing/create_plan/", form, follow=True)
  18. @patch("hc.payments.views.braintree")
  19. def test_it_works(self, mock):
  20. self._setup_mock(mock)
  21. self.profile.team_access_allowed = False
  22. self.profile.sms_limit = 0
  23. self.profile.sms_sent = 1
  24. self.profile.save()
  25. r = self.run_create_plan()
  26. self.assertRedirects(r, "/pricing/")
  27. # Subscription should be filled out:
  28. sub = Subscription.objects.get(user=self.alice)
  29. self.assertEqual(sub.customer_id, "test-customer-id")
  30. self.assertEqual(sub.payment_method_token, "t-token")
  31. self.assertEqual(sub.subscription_id, "t-sub-id")
  32. self.assertEqual(sub.plan_id, "P5")
  33. # User's profile should have a higher limits
  34. self.profile.refresh_from_db()
  35. self.assertEqual(self.profile.ping_log_limit, 1000)
  36. self.assertEqual(self.profile.check_limit, 500)
  37. self.assertEqual(self.profile.sms_limit, 50)
  38. self.assertEqual(self.profile.sms_sent, 0)
  39. self.assertTrue(self.profile.team_access_allowed)
  40. # braintree.Subscription.cancel should have not been called
  41. assert not mock.Subscription.cancel.called
  42. def test_bad_plan_id(self):
  43. r = self.run_create_plan(plan_id="this-is-wrong")
  44. self.assertEqual(r.status_code, 400)
  45. @patch("hc.payments.views.braintree")
  46. def test_it_cancels_previous_subscription(self, mock):
  47. self._setup_mock(mock)
  48. sub = Subscription(user=self.alice)
  49. sub.subscription_id = "prev-sub"
  50. sub.save()
  51. r = self.run_create_plan()
  52. self.assertRedirects(r, "/pricing/")
  53. assert mock.Subscription.cancel.called
  54. @patch("hc.payments.views.braintree")
  55. def test_customer_creation_failure(self, mock):
  56. self._setup_mock(mock)
  57. mock.Customer.create.return_value.is_success = False
  58. mock.Customer.create.return_value.message = "Test Failure"
  59. r = self.run_create_plan()
  60. self.assertRedirects(r, "/pricing/")
  61. self.assertContains(r, "Test Failure")
  62. @patch("hc.payments.views.braintree")
  63. def test_pm_creation_failure(self, mock):
  64. self._setup_mock(mock)
  65. mock.PaymentMethod.create.return_value.is_success = False
  66. mock.PaymentMethod.create.return_value.message = "pm failure"
  67. r = self.run_create_plan()
  68. self.assertRedirects(r, "/pricing/")
  69. self.assertContains(r, "pm failure")
  70. @patch("hc.payments.views.braintree")
  71. def test_subscription_creation_failure(self, mock):
  72. self._setup_mock(mock)
  73. mock.Subscription.create.return_value.is_success = False
  74. mock.Subscription.create.return_value.message = "sub failure"
  75. r = self.run_create_plan()
  76. self.assertRedirects(r, "/pricing/")
  77. self.assertContains(r, "sub failure")