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.

93 lines
3.4 KiB

  1. from mock import patch
  2. from hc.payments.models import Subscription
  3. from hc.test import BaseTestCase
  4. class UpdatePaymentMethodTestCase(BaseTestCase):
  5. def _setup_mock(self, mock):
  6. """ Set up Braintree calls that the controller will use. """
  7. mock.PaymentMethod.create.return_value.is_success = True
  8. mock.PaymentMethod.create.return_value.payment_method.token = "fake"
  9. @patch("hc.payments.models.braintree")
  10. def test_it_retrieves_paypal(self, mock):
  11. self._setup_mock(mock)
  12. mock.paypal_account.PayPalAccount = dict
  13. mock.credit_card.CreditCard = list
  14. mock.PaymentMethod.find.return_value = {"email": "[email protected]"}
  15. self.sub = Subscription(user=self.alice)
  16. self.sub.payment_method_token = "fake-token"
  17. self.sub.save()
  18. self.client.login(username="[email protected]", password="password")
  19. r = self.client.get("/accounts/profile/billing/payment_method/")
  20. self.assertContains(r, "[email protected]")
  21. @patch("hc.payments.models.braintree")
  22. def test_it_retrieves_cc(self, mock):
  23. self._setup_mock(mock)
  24. mock.paypal_account.PayPalAccount = list
  25. mock.credit_card.CreditCard = dict
  26. mock.PaymentMethod.find.return_value = {"masked_number": "1***2"}
  27. self.sub = Subscription(user=self.alice)
  28. self.sub.payment_method_token = "fake-token"
  29. self.sub.save()
  30. self.client.login(username="[email protected]", password="password")
  31. r = self.client.get("/accounts/profile/billing/payment_method/")
  32. self.assertContains(r, "1***2")
  33. @patch("hc.payments.models.braintree")
  34. def test_it_creates_payment_method(self, mock):
  35. self._setup_mock(mock)
  36. self.sub = Subscription(user=self.alice)
  37. self.sub.customer_id = "test-customer"
  38. self.sub.save()
  39. self.client.login(username="[email protected]", password="password")
  40. form = {"payment_method_nonce": "test-nonce"}
  41. r = self.client.post("/accounts/profile/billing/payment_method/", form)
  42. self.assertRedirects(r, "/accounts/profile/billing/")
  43. @patch("hc.payments.models.braintree")
  44. def test_it_creates_customer(self, mock):
  45. self._setup_mock(mock)
  46. mock.Customer.create.return_value.is_success = True
  47. mock.Customer.create.return_value.customer.id = "test-customer-id"
  48. self.sub = Subscription(user=self.alice)
  49. self.sub.save()
  50. self.client.login(username="[email protected]", password="password")
  51. form = {"payment_method_nonce": "test-nonce"}
  52. self.client.post("/accounts/profile/billing/payment_method/", form)
  53. self.sub.refresh_from_db()
  54. self.assertEqual(self.sub.customer_id, "test-customer-id")
  55. @patch("hc.payments.models.braintree")
  56. def test_it_updates_subscription(self, mock):
  57. self._setup_mock(mock)
  58. self.sub = Subscription(user=self.alice)
  59. self.sub.customer_id = "test-customer"
  60. self.sub.subscription_id = "fake-id"
  61. self.sub.save()
  62. mock.Customer.create.return_value.is_success = True
  63. mock.Customer.create.return_value.customer.id = "test-customer-id"
  64. self.client.login(username="[email protected]", password="password")
  65. form = {"payment_method_nonce": "test-nonce"}
  66. self.client.post("/accounts/profile/billing/payment_method/", form)
  67. self.assertTrue(mock.Subscription.update.called)