tests.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. from django.conf import settings
  2. from django.shortcuts import reverse
  3. from django.test import TestCase, RequestFactory
  4. from rest_framework.test import APITestCase
  5. from model_bakery import baker
  6. from organizations_ext.models import OrganizationUser, OrganizationUserRole
  7. from glitchtip import test_utils # pylint: disable=unused-import
  8. class OrganizationModelTestCase(TestCase):
  9. def test_email(self):
  10. """Billing email address"""
  11. user = baker.make("users.user")
  12. organization = baker.make("organizations_ext.Organization")
  13. organization.add_user(user)
  14. # Org 1 has two users and only one of which is an owner
  15. user2 = baker.make("users.user")
  16. organization2 = baker.make("organizations_ext.Organization")
  17. organization2.add_user(user2)
  18. organization.add_user(user2)
  19. self.assertEqual(organization.email, user.email)
  20. self.assertEqual(organization.users.count(), 2)
  21. self.assertEqual(organization.owners.count(), 1)
  22. def test_organization_request_callback(self):
  23. user = baker.make("users.user")
  24. organization = baker.make("organizations_ext.Organization")
  25. organization.add_user(user)
  26. factory = RequestFactory()
  27. request = factory.get("/")
  28. request.user = user
  29. callback = settings.DJSTRIPE_SUBSCRIBER_MODEL_REQUEST_CALLBACK
  30. self.assertEqual(callback(request), organization)
  31. def test_slug_reserved_words(self):
  32. """Reserve some words for frontend routing needs"""
  33. word = "login"
  34. organization = baker.make("organizations_ext.Organization", name=word)
  35. self.assertNotEqual(organization.slug, word)
  36. organization = baker.make("organizations_ext.Organization", name=word)
  37. class OrganizationRegistrationSettingQueryTestCase(APITestCase):
  38. def setUp(self):
  39. self.user = baker.make("users.user")
  40. self.client.force_login(self.user)
  41. self.url = reverse("organization-list")
  42. def test_organizations_closed_registration_first_organization_create(self):
  43. data = {"name": "test"}
  44. with self.settings(ENABLE_ORGANIZATION_CREATION=False):
  45. res = self.client.post(self.url, data)
  46. self.assertEqual(res.status_code, 201)
  47. class OrganizationsAPITestCase(APITestCase):
  48. def setUp(self):
  49. self.user = baker.make("users.user")
  50. self.organization = baker.make("organizations_ext.Organization")
  51. self.organization.add_user(self.user)
  52. self.client.force_login(self.user)
  53. self.url = reverse("organization-list")
  54. def test_organizations_list(self):
  55. not_my_organization = baker.make("organizations_ext.Organization")
  56. res = self.client.get(self.url)
  57. self.assertContains(res, self.organization.slug)
  58. self.assertNotContains(res, not_my_organization.slug)
  59. self.assertFalse(
  60. "teams" in res.data[0].keys(), "List view shouldn't contain teams"
  61. )
  62. def test_organizations_retrieve(self):
  63. project = baker.make("projects.Project", organization=self.organization)
  64. url = reverse("organization-detail", args=[self.organization.slug])
  65. res = self.client.get(url)
  66. self.assertContains(res, self.organization.name)
  67. self.assertContains(res, project.name)
  68. self.assertTrue(
  69. "teams" in res.data.keys(), "Retrieve view should contain teams"
  70. )
  71. def test_organizations_create(self):
  72. data = {"name": "test"}
  73. with self.assertNumQueries(7):
  74. res = self.client.post(self.url, data)
  75. self.assertContains(res, data["name"], status_code=201)
  76. self.assertEqual(
  77. OrganizationUser.objects.filter(organization__name=data["name"]).count(), 1
  78. )
  79. def test_organizations_create_closed_registration_superuser(self):
  80. data = {"name": "test"}
  81. with self.settings(ENABLE_ORGANIZATION_CREATION=False):
  82. res = self.client.post(self.url, data)
  83. self.assertEqual(res.status_code, 403)
  84. self.user.is_superuser = True
  85. self.user.save()
  86. with self.settings(ENABLE_ORGANIZATION_CREATION=False):
  87. with self.assertNumQueries(8):
  88. res = self.client.post(self.url, data)
  89. self.assertEqual(res.status_code, 201)
  90. class OrganizationsFilterTestCase(APITestCase):
  91. def setUp(self):
  92. self.user = baker.make("users.user")
  93. self.client.force_login(self.user)
  94. self.url = reverse("organization-list")
  95. def test_default_ordering(self):
  96. organizationA = baker.make(
  97. "organizations_ext.Organization", name="A Organization"
  98. )
  99. organizationZ = baker.make(
  100. "organizations_ext.Organization", name="Z Organization"
  101. )
  102. organizationB = baker.make(
  103. "organizations_ext.Organization", name="B Organization"
  104. )
  105. organizationA.add_user(self.user)
  106. organizationB.add_user(self.user)
  107. organizationZ.add_user(self.user)
  108. res = self.client.get(self.url)
  109. self.assertEqual(res.data[0]["name"], organizationA.name)
  110. self.assertEqual(res.data[2]["name"], organizationZ.name)