tests.py 4.5 KB

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