test_api.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. from django.test import TestCase
  2. from django.urls import reverse
  3. from model_bakery import baker
  4. from apps.organizations_ext.constants import OrganizationUserRole
  5. from apps.organizations_ext.models import OrganizationUser
  6. class OrganizationsAPITestCase(TestCase):
  7. @classmethod
  8. def setUpTestData(cls):
  9. cls.user = baker.make("users.user")
  10. cls.organization = baker.make("organizations_ext.Organization")
  11. cls.org_user = cls.organization.add_user(cls.user)
  12. cls.url = reverse("api:list_organizations")
  13. def setUp(self):
  14. self.client.force_login(self.user)
  15. def test_organizations_list(self):
  16. not_my_organization = baker.make("organizations_ext.Organization")
  17. res = self.client.get(self.url)
  18. self.assertContains(res, self.organization.slug)
  19. self.assertNotContains(res, not_my_organization.slug)
  20. self.assertFalse(
  21. "teams" in res.json()[0].keys(), "List view shouldn't contain teams"
  22. )
  23. def test_organizations_retrieve(self):
  24. project = baker.make("projects.Project", organization=self.organization)
  25. team = baker.make("teams.Team", organization=self.organization)
  26. url = reverse("api:get_organization", args=[self.organization.slug])
  27. res = self.client.get(url)
  28. self.assertContains(res, self.organization.name)
  29. self.assertContains(res, project.name)
  30. data = res.json()
  31. self.assertTrue("teams" in data.keys(), "Retrieve view should contain teams")
  32. self.assertTrue(
  33. "projects" in data.keys(), "Retrieve view should contain projects"
  34. )
  35. self.assertContains(res, team.slug)
  36. self.assertTrue(
  37. "teams" in data["projects"][0].keys(),
  38. "Org projects should contain teams id/name",
  39. )
  40. def test_organizations_create(self):
  41. data = {"name": "test"}
  42. res = self.client.post(self.url, data, content_type="application/json")
  43. self.assertContains(res, data["name"], status_code=201)
  44. self.assertEqual(
  45. OrganizationUser.objects.filter(organization__name=data["name"]).count(), 1
  46. )
  47. def test_organizations_create_closed_registration_superuser(self):
  48. data = {"name": "test"}
  49. with self.settings(ENABLE_ORGANIZATION_CREATION=False):
  50. res = self.client.post(self.url, data, content_type="application/json")
  51. self.assertEqual(res.status_code, 403)
  52. self.user.is_superuser = True
  53. self.user.save()
  54. with self.settings(ENABLE_ORGANIZATION_CREATION=False):
  55. with self.assertNumQueries(9):
  56. res = self.client.post(self.url, data, content_type="application/json")
  57. self.assertEqual(res.status_code, 201)
  58. def test_organizations_update(self):
  59. data = {"name": "edit"}
  60. url = reverse("api:get_organization", args=[self.organization.slug])
  61. res = self.client.put(url, data, content_type="application/json")
  62. self.assertContains(res, data["name"])
  63. self.assertTrue(
  64. OrganizationUser.objects.filter(organization__name=data["name"]).exists()
  65. )
  66. def test_organizations_update_without_permissions(self):
  67. """
  68. Ensure queryset with role_required checks the correct organization user's role
  69. """
  70. organization_2 = baker.make("organizations_ext.Organization")
  71. org_2_user = organization_2.add_user(self.user)
  72. org_2_user.role = OrganizationUserRole.MEMBER
  73. org_2_user.save()
  74. data = {"name": "edit"}
  75. url = reverse("api:update_organization", args=[organization_2.slug])
  76. res = self.client.put(url, data, content_type="application/json")
  77. self.assertEqual(res.status_code, 403)
  78. org_2_user.role = OrganizationUserRole.OWNER
  79. org_2_user.save()
  80. res = self.client.put(url, data, content_type="application/json")
  81. self.assertContains(res, data["name"])
  82. self.assertTrue(
  83. OrganizationUser.objects.filter(organization__name=data["name"]).exists()
  84. )
  85. def test_organizations_delete_without_permissions(self):
  86. """
  87. Ensure queryset with role_required checks the correct organization user's role
  88. """
  89. organization_2 = baker.make("organizations_ext.Organization")
  90. org_2_user = organization_2.add_user(self.user)
  91. org_2_user.role = OrganizationUserRole.MEMBER
  92. org_2_user.save()
  93. url = reverse("api:delete_organization", args=[organization_2.slug])
  94. res = self.client.delete(url)
  95. self.assertEqual(res.status_code, 403)
  96. org_2_user.role = OrganizationUserRole.OWNER
  97. org_2_user.save()
  98. res = self.client.delete(url)
  99. self.assertEqual(
  100. res.status_code,
  101. 204,
  102. )