test_api.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. from unittest.mock import AsyncMock, patch
  2. from django.test import TestCase
  3. from django.urls import reverse
  4. from model_bakery import baker
  5. class StripeAPITestCase(TestCase):
  6. @classmethod
  7. def setUpTestData(cls):
  8. cls.user = baker.make("users.user")
  9. cls.organization = baker.make(
  10. "organizations_ext.Organization", stripe_customer_id="cust_1"
  11. )
  12. cls.org_user = cls.organization.add_user(cls.user)
  13. cls.product = baker.make("stripe.StripeProduct", is_public=True, events=5)
  14. cls.price = baker.make("stripe.StripePrice", product=cls.product, price=0)
  15. def setUp(self):
  16. self.client.force_login(self.user)
  17. def test_list_stripe_products(self):
  18. url = reverse("api:list_stripe_products")
  19. res = self.client.get(url)
  20. self.assertContains(res, self.product.name)
  21. def test_get_stripe_subscription(self):
  22. sub = baker.make(
  23. "stripe.StripeSubscription", organization=self.organization, is_active=True
  24. )
  25. url = reverse("api:get_stripe_subscription", args=[self.organization.slug])
  26. res = self.client.get(url)
  27. self.assertContains(res, sub.stripe_id)
  28. @patch("apps.stripe.api.create_session")
  29. def test_create_stripe_session(self, mock_create_session):
  30. url = reverse("api:create_stripe_session", args=[self.organization.slug])
  31. mock_create_session.return_value = {}
  32. res = self.client.post(
  33. url, {"price": self.price.stripe_id}, content_type="application/json"
  34. )
  35. self.assertEqual(res.status_code, 200)
  36. @patch("apps.stripe.api.create_portal_session", new_callable=AsyncMock)
  37. def test_manage_billing(self, mock_create_portal_session):
  38. mock_create_portal_session.return_value = {}
  39. url = reverse(
  40. "api:stripe_billing_portal_session", args=[self.organization.slug]
  41. )
  42. res = self.client.post(url, {}, content_type="application/json")
  43. self.assertEqual(res.status_code, 200)
  44. mock_create_portal_session.assert_called_once()
  45. @patch("apps.stripe.api.create_subscription")
  46. def test_stripe_create_subscription(self, mock_create_subscription):
  47. url = reverse("api:stripe_create_subscription")
  48. res = self.client.post(
  49. url,
  50. {"organization": self.organization.id, "price": self.price.stripe_id},
  51. content_type="application/json",
  52. )
  53. self.assertEqual(res.status_code, 200)
  54. mock_create_subscription.assert_called_once()