tests.py 4.6 KB

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