test_models.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. from unittest.mock import patch
  2. from asgiref.sync import sync_to_async
  3. from django.test import TestCase
  4. from model_bakery import baker
  5. from ..models import StripeProduct, StripeSubscription
  6. from ..schema import (
  7. Customer,
  8. Items,
  9. Price,
  10. ProductExpandedPrice,
  11. SubscriptionExpandCustomer,
  12. )
  13. test_price = Price(
  14. object="price",
  15. id="price_1",
  16. active=True,
  17. unit_amount=1000,
  18. currency="usd",
  19. type="one_time",
  20. metadata={},
  21. billing_scheme="per_unit",
  22. created=1678886400,
  23. livemode=False,
  24. lookup_key=None,
  25. nickname=None,
  26. product="prod_1",
  27. recurring=None,
  28. tax_behavior=None,
  29. tiers_mode=None,
  30. unit_amount_decimal="1000",
  31. )
  32. class StripeTestCase(TestCase):
  33. @classmethod
  34. def setUpTestData(cls):
  35. cls.org = baker.make("organizations_ext.Organization")
  36. @patch("apps.stripe.models.list_products")
  37. async def test_sync_product(self, mock_list_products):
  38. mock_products_page_1 = [
  39. ProductExpandedPrice(
  40. object="product",
  41. id="prod_1",
  42. active=True,
  43. attributes=[],
  44. created=1678886400,
  45. default_price=test_price,
  46. description="Description 1",
  47. images=[],
  48. livemode=False,
  49. marketing_features=[],
  50. metadata={"events": "123", "is_public": "true"},
  51. name="Product 1",
  52. package_dimensions=None,
  53. shippable=None,
  54. statement_descriptor=None,
  55. tax_code=None,
  56. type="service",
  57. unit_label=None,
  58. updated=1678886400,
  59. url=None,
  60. ),
  61. ]
  62. async def mock_products_generator():
  63. yield mock_products_page_1
  64. mock_list_products.return_value = mock_products_generator()
  65. await StripeProduct.sync_from_stripe()
  66. self.assertEqual(
  67. await StripeProduct.objects.acount(), len(mock_products_page_1)
  68. )
  69. @patch("apps.stripe.models.list_subscriptions")
  70. async def test_sync_subscription(self, mock_list_subscriptions):
  71. await sync_to_async(baker.make)(
  72. "stripe.StripeProduct", stripe_id=test_price.product
  73. )
  74. subscriptions_page_1 = [
  75. SubscriptionExpandCustomer(
  76. object="subscription",
  77. id="sub_1",
  78. customer=Customer(
  79. object="customer",
  80. id="cus_1",
  81. email="foo@example.com",
  82. metadata={"organization_id": str(self.org.id)},
  83. name="",
  84. ),
  85. items=Items(object="list", data=[{"price": test_price.dict()}]),
  86. created=1678886400,
  87. current_period_end=1678886400 + 2592000, # +30 days
  88. current_period_start=1678886400,
  89. status="active",
  90. livemode=False,
  91. metadata={},
  92. cancel_at_period_end=False,
  93. )
  94. ]
  95. async def mock_subscriptions_generator():
  96. yield subscriptions_page_1
  97. mock_list_subscriptions.return_value = mock_subscriptions_generator()
  98. await StripeSubscription.sync_from_stripe()
  99. self.assertEqual(
  100. await StripeSubscription.objects.acount(), len(subscriptions_page_1)
  101. )