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.

95 lines
3.4 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. r = self.run_create_plan()
  22. self.assertRedirects(r, "/pricing/")
  23. # Subscription should be filled out:
  24. sub = Subscription.objects.get(user=self.alice)
  25. self.assertEqual(sub.customer_id, "test-customer-id")
  26. self.assertEqual(sub.payment_method_token, "t-token")
  27. self.assertEqual(sub.subscription_id, "t-sub-id")
  28. self.assertEqual(sub.plan_id, "P5")
  29. # User's profile should have a higher ping log limit:
  30. profile = Profile.objects.get(user=self.alice)
  31. self.assertEqual(profile.ping_log_limit, 1000)
  32. # braintree.Subscription.cancel should have not been called
  33. assert not mock.Subscription.cancel.called
  34. def test_bad_plan_id(self):
  35. r = self.run_create_plan(plan_id="this-is-wrong")
  36. self.assertEqual(r.status_code, 400)
  37. @patch("hc.payments.views.braintree")
  38. def test_it_cancels_previous_subscription(self, mock):
  39. self._setup_mock(mock)
  40. sub = Subscription(user=self.alice)
  41. sub.subscription_id = "prev-sub"
  42. sub.save()
  43. r = self.run_create_plan()
  44. self.assertRedirects(r, "/pricing/")
  45. assert mock.Subscription.cancel.called
  46. @patch("hc.payments.views.braintree")
  47. def test_customer_creation_failure(self, mock):
  48. self._setup_mock(mock)
  49. mock.Customer.create.return_value.is_success = False
  50. mock.Customer.create.return_value.message = "Test Failure"
  51. r = self.run_create_plan()
  52. self.assertRedirects(r, "/pricing/")
  53. self.assertContains(r, "Test Failure")
  54. @patch("hc.payments.views.braintree")
  55. def test_pm_creation_failure(self, mock):
  56. self._setup_mock(mock)
  57. mock.PaymentMethod.create.return_value.is_success = False
  58. mock.PaymentMethod.create.return_value.message = "pm failure"
  59. r = self.run_create_plan()
  60. self.assertRedirects(r, "/pricing/")
  61. self.assertContains(r, "pm failure")
  62. @patch("hc.payments.views.braintree")
  63. def test_subscription_creation_failure(self, mock):
  64. self._setup_mock(mock)
  65. mock.Subscription.create.return_value.is_success = False
  66. mock.Subscription.create.return_value.message = "sub failure"
  67. r = self.run_create_plan()
  68. self.assertRedirects(r, "/pricing/")
  69. self.assertContains(r, "sub failure")