tests.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. from unittest.mock import patch
  2. from django.shortcuts import reverse
  3. from django.utils import timezone
  4. from rest_framework.test import APITestCase
  5. from model_bakery import baker
  6. from model_bakery.random_gen import gen_slug, gen_datetime, gen_integer
  7. from glitchtip import test_utils # pylint: disable=unused-import
  8. baker.generators.add("djstripe.fields.StripeIdField", gen_slug)
  9. baker.generators.add("djstripe.fields.StripeDateTimeField", gen_datetime)
  10. baker.generators.add("djstripe.fields.StripeQuantumCurrencyAmountField", gen_integer)
  11. class SubscriptionAPITestCase(APITestCase):
  12. def setUp(self):
  13. self.user = baker.make("users.user")
  14. self.organization = baker.make("organizations_ext.Organization")
  15. self.organization.add_user(self.user)
  16. self.client.force_login(self.user)
  17. self.url = reverse("subscription-list")
  18. def test_list(self):
  19. customer = baker.make("djstripe.Customer", subscriber=self.organization)
  20. subscription = baker.make(
  21. "djstripe.Subscription", customer=customer, livemode=False
  22. )
  23. subscription2 = baker.make("djstripe.Subscription", livemode=False)
  24. subscription3 = baker.make(
  25. "djstripe.Subscription", customer=customer, livemode=True
  26. )
  27. res = self.client.get(self.url)
  28. self.assertContains(res, subscription.id)
  29. self.assertNotContains(res, subscription2.id)
  30. self.assertNotContains(res, subscription3.id)
  31. def test_detail(self):
  32. customer = baker.make("djstripe.Customer", subscriber=self.organization)
  33. subscription = baker.make(
  34. "djstripe.Subscription",
  35. customer=customer,
  36. livemode=False,
  37. created=timezone.make_aware(timezone.datetime(2020, 1, 2)),
  38. )
  39. # Should only get most recent
  40. baker.make(
  41. "djstripe.Subscription",
  42. customer=customer,
  43. livemode=False,
  44. created=timezone.make_aware(timezone.datetime(2020, 1, 1)),
  45. )
  46. baker.make("djstripe.Subscription")
  47. url = reverse("subscription-detail", args=[self.organization.slug])
  48. res = self.client.get(url)
  49. self.assertContains(res, subscription.id)
  50. @patch("djstripe.models.Customer.subscribe")
  51. def test_create(self, djstripe_customer_subscribe_mock):
  52. customer = baker.make("djstripe.Customer", subscriber=self.organization)
  53. plan = baker.make("djstripe.Plan", amount=0)
  54. subscription = baker.make(
  55. "djstripe.Subscription", customer=customer, livemode=False,
  56. )
  57. djstripe_customer_subscribe_mock.return_value = subscription
  58. data = {"plan": plan.id, "organization": self.organization.id}
  59. res = self.client.post(self.url, data)
  60. self.assertEqual(res.data["plan"], plan.id)
  61. def test_create_invalid_org(self):
  62. """ Only owners may create subscriptions """
  63. user = baker.make("users.user") # Non owner member
  64. plan = baker.make("djstripe.Plan", amount=0)
  65. self.organization.add_user(user)
  66. self.client.force_login(user)
  67. data = {"plan": plan.id, "organization": self.organization.id}
  68. res = self.client.post(self.url, data)
  69. self.assertEqual(res.status_code, 400)
  70. class SubscriptionIntegrationTestCase(APITestCase):
  71. def setUp(self):
  72. self.user = baker.make("users.user")
  73. self.organization = baker.make("organizations_ext.Organization")
  74. self.organization.add_user(self.user)
  75. # Make these in this manner to avoid syncing data to stripe actual
  76. self.plan = baker.make("djstripe.Plan", active=True, amount=0)
  77. self.customer = baker.make("djstripe.Customer", subscriber=self.organization)
  78. self.client.force_login(self.user)
  79. self.list_url = reverse("subscription-list")
  80. self.detail_url = reverse("subscription-detail", args=[self.organization.slug])
  81. @patch("djstripe.models.Customer.subscribe")
  82. def test_new_org_flow(self, djstripe_customer_subscribe_mock):
  83. """ Test checking if subscription exists and when not, creating a free tier one """
  84. res = self.client.get(self.detail_url)
  85. self.assertFalse(res.data["id"]) # No subscription, user should create one
  86. subscription = baker.make(
  87. "djstripe.Subscription", customer=self.customer, livemode=False,
  88. )
  89. djstripe_customer_subscribe_mock.return_value = subscription
  90. data = {"plan": self.plan.id, "organization": self.organization.id}
  91. res = self.client.post(self.list_url, data)
  92. self.assertContains(res, self.plan.id, status_code=201)
  93. djstripe_customer_subscribe_mock.assert_called_once()
  94. res = self.client.get(self.detail_url)
  95. self.assertEqual(res.data["id"], subscription.id)