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.

74 lines
2.6 KiB

  1. from mock import Mock, patch
  2. from unittest import skipIf
  3. from django.core import mail
  4. from django.utils.timezone import now
  5. from hc.payments.models import Subscription
  6. from hc.test import BaseTestCase
  7. try:
  8. import reportlab
  9. except ImportError:
  10. reportlab = None
  11. class ChargeWebhookTestCase(BaseTestCase):
  12. def setUp(self):
  13. super(ChargeWebhookTestCase, self).setUp()
  14. self.sub = Subscription(user=self.alice)
  15. self.sub.subscription_id = "test-id"
  16. self.sub.customer_id = "test-customer-id"
  17. self.sub.send_invoices = True
  18. self.sub.save()
  19. self.tx = Mock()
  20. self.tx.id = "abc123"
  21. self.tx.customer_details.id = "test-customer-id"
  22. self.tx.created_at = now()
  23. self.tx.currency_iso_code = "USD"
  24. self.tx.amount = 5
  25. self.tx.subscription_details.billing_period_start_date = now()
  26. self.tx.subscription_details.billing_period_end_date = now()
  27. @skipIf(reportlab is None, "reportlab not installed")
  28. @patch("hc.payments.views.Subscription.objects.by_braintree_webhook")
  29. def test_it_works(self, mock_getter):
  30. mock_getter.return_value = self.sub, self.tx
  31. r = self.client.post("/pricing/charge/")
  32. self.assertEqual(r.status_code, 200)
  33. # See if email was sent
  34. self.assertEqual(len(mail.outbox), 1)
  35. msg = mail.outbox[0]
  36. self.assertEqual(msg.subject, "Invoice from Mychecks")
  37. self.assertEqual(msg.to, ["[email protected]"])
  38. self.assertEqual(msg.attachments[0][0], "MS-HC-ABC123.pdf")
  39. @patch("hc.payments.views.Subscription.objects.by_braintree_webhook")
  40. def test_it_obeys_send_invoices_flag(self, mock_getter):
  41. mock_getter.return_value = self.sub, self.tx
  42. self.sub.send_invoices = False
  43. self.sub.save()
  44. r = self.client.post("/pricing/charge/")
  45. self.assertEqual(r.status_code, 200)
  46. # It should not send the email
  47. self.assertEqual(len(mail.outbox), 0)
  48. @skipIf(reportlab is None, "reportlab not installed")
  49. @patch("hc.payments.views.Subscription.objects.by_braintree_webhook")
  50. def test_it_uses_invoice_email(self, mock_getter):
  51. mock_getter.return_value = self.sub, self.tx
  52. self.sub.invoice_email = "[email protected]"
  53. self.sub.save()
  54. r = self.client.post("/pricing/charge/")
  55. self.assertEqual(r.status_code, 200)
  56. # See if the email was sent to Alice's accountant:
  57. self.assertEqual(len(mail.outbox), 1)
  58. self.assertEqual(mail.outbox[0].to, ["[email protected]"])