tests.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. from django.conf import settings
  2. from django.urls import reverse
  3. from django.utils import timezone
  4. from model_bakery import baker
  5. from rest_framework.test import APITestCase
  6. from apps.organizations_ext.models import OrganizationUserRole
  7. from ..models import Project, ProjectKey
  8. from ..views import ProjectViewSet
  9. class ProjectsAPITestCase(APITestCase):
  10. def setUp(self):
  11. self.user = baker.make("users.user")
  12. self.client.force_login(self.user)
  13. self.url = reverse("project-list")
  14. def test_projects_api_create(self):
  15. """This endpoint can't be used to create"""
  16. data = {"name": "test"}
  17. res = self.client.post(self.url, data)
  18. # Must specify organization and team
  19. self.assertEqual(res.status_code, 405)
  20. def test_projects_api_list(self):
  21. organization = baker.make("organizations_ext.Organization")
  22. organization.add_user(self.user, role=OrganizationUserRole.OWNER)
  23. project = baker.make("projects.Project", organization=organization)
  24. res = self.client.get(self.url)
  25. self.assertContains(res, project.name)
  26. def test_default_ordering(self):
  27. organization = baker.make("organizations_ext.Organization")
  28. organization.add_user(self.user, role=OrganizationUserRole.OWNER)
  29. projectA = baker.make(
  30. "projects.Project", organization=organization, name="A Project"
  31. )
  32. projectZ = baker.make(
  33. "projects.Project", organization=organization, name="Z Project"
  34. )
  35. baker.make("projects.Project", organization=organization, name="B Project")
  36. res = self.client.get(self.url)
  37. self.assertEqual(res.data[0]["name"], projectA.name)
  38. self.assertEqual(res.data[2]["name"], projectZ.name)
  39. def test_projects_api_retrieve(self):
  40. organization = baker.make("organizations_ext.Organization")
  41. organization.add_user(self.user, role=OrganizationUserRole.OWNER)
  42. project = baker.make(
  43. "projects.Project", organization=organization, first_event=timezone.now()
  44. )
  45. res = self.client.get(
  46. reverse(
  47. "project-detail", kwargs={"pk": organization.slug + "/" + project.slug}
  48. )
  49. )
  50. self.assertTrue(res.data["firstEvent"])
  51. def test_projects_pagination(self):
  52. """
  53. Test link header pagination
  54. """
  55. page_size = settings.REST_FRAMEWORK.get("PAGE_SIZE")
  56. organization = baker.make("organizations_ext.Organization")
  57. organization.add_user(self.user, role=OrganizationUserRole.OWNER)
  58. firstProject = baker.make(
  59. "projects.Project", organization=organization, name="Alphabetically First"
  60. )
  61. baker.make(
  62. "projects.Project", organization=organization, name="B", _quantity=page_size
  63. )
  64. lastProject = baker.make(
  65. "projects.Project", organization=organization, name="Last Alphabetically"
  66. )
  67. res = self.client.get(self.url)
  68. self.assertNotContains(res, lastProject.name)
  69. self.assertContains(res, firstProject.name)
  70. link_header = res.get("Link")
  71. self.assertIn('results="true"', link_header)
  72. def test_project_isolation(self):
  73. """Users should only access projects in their organization"""
  74. user1 = self.user
  75. user2 = baker.make("users.user")
  76. org1 = baker.make("organizations_ext.Organization")
  77. org2 = baker.make("organizations_ext.Organization")
  78. org1.add_user(user1)
  79. org2.add_user(user2)
  80. project1 = baker.make("projects.Project", organization=org1)
  81. project2 = baker.make("projects.Project", organization=org2)
  82. res = self.client.get(self.url)
  83. self.assertContains(res, project1.name)
  84. self.assertNotContains(res, project2.name)
  85. def test_project_delete(self):
  86. organization = baker.make("organizations_ext.Organization")
  87. organization.add_user(self.user, OrganizationUserRole.ADMIN)
  88. team = baker.make("teams.Team", organization=organization)
  89. project = baker.make("projects.Project", organization=organization, team=team)
  90. url = reverse(
  91. "project-detail", kwargs={"pk": f"{organization.slug}/{project.slug}"}
  92. )
  93. res = self.client.delete(url)
  94. self.assertEqual(res.status_code, 204)
  95. self.assertEqual(ProjectViewSet.queryset.count(), 0)
  96. def test_project_invalid_delete(self):
  97. """Cannot delete projects that are not in the organization the user is an admin of"""
  98. organization = baker.make("organizations_ext.Organization")
  99. organization.add_user(self.user, OrganizationUserRole.ADMIN)
  100. project = baker.make("projects.Project")
  101. url = reverse(
  102. "project-detail", kwargs={"pk": f"{organization.slug}/{project.slug}"}
  103. )
  104. res = self.client.delete(url)
  105. self.assertEqual(res.status_code, 404)
  106. class TeamProjectsAPITestCase(APITestCase):
  107. def setUp(self):
  108. self.user = baker.make("users.user")
  109. self.organization = baker.make("organizations_ext.Organization")
  110. self.organization.add_user(self.user, OrganizationUserRole.ADMIN)
  111. self.team = baker.make("teams.Team", organization=self.organization)
  112. self.client.force_login(self.user)
  113. self.url = reverse(
  114. "team-projects-list",
  115. kwargs={"team_pk": f"{self.organization.slug}/{self.team.slug}"},
  116. )
  117. def test_list(self):
  118. project = baker.make("projects.Project", organization=self.organization)
  119. project.team_set.add(self.team)
  120. not_my_project = baker.make("projects.Project")
  121. res = self.client.get(self.url)
  122. self.assertContains(res, project.name)
  123. self.assertNotContains(res, not_my_project.name)
  124. # If a user is in multiple orgs, that user will have multiple org users.
  125. # Make sure endpoint doesn't show projects from other orgs
  126. second_org = baker.make("organizations_ext.Organization")
  127. second_org.add_user(self.user, OrganizationUserRole.ADMIN)
  128. project_in_second_org = baker.make("projects.Project", organization=second_org)
  129. res = self.client.get(self.url)
  130. self.assertNotContains(res, project_in_second_org.name)
  131. # Only show projects that are associated with the team in the URL.
  132. # If a project is on another team in the same org, it should not show
  133. project_teamless = baker.make(
  134. "projects.Project", organization=self.organization
  135. )
  136. res = self.client.get(self.url)
  137. self.assertNotContains(res, project_teamless)
  138. def test_create(self):
  139. data = {"name": "test-team"}
  140. res = self.client.post(self.url, data)
  141. self.assertContains(res, data["name"], status_code=201)
  142. res = self.client.get(self.url)
  143. self.assertContains(res, data["name"])
  144. self.assertEqual(ProjectKey.objects.all().count(), 1)
  145. def test_projects_api_create_unique_slug(self):
  146. name = "test project"
  147. data = {"name": name}
  148. res = self.client.post(self.url, data)
  149. first_project = Project.objects.get()
  150. res = self.client.post(self.url, data)
  151. self.assertContains(res, name, status_code=201)
  152. projects = Project.objects.all()
  153. self.assertNotEqual(projects[0].slug, projects[1].slug)
  154. self.assertEqual(ProjectKey.objects.all().count(), 2)
  155. org2 = baker.make("organizations_ext.Organization")
  156. org2_project = Project.objects.create(name=name, organization=org2)
  157. # The same slug can exist between multiple organizations
  158. self.assertEqual(first_project.slug, org2_project.slug)
  159. def test_projects_api_project_has_team(self):
  160. """
  161. The frontend UI requires you to assign a new project to a team, so make sure
  162. that the new project has a team associated with it
  163. """
  164. name = "test project"
  165. data = {"name": name}
  166. self.client.post(self.url, data)
  167. project = Project.objects.first()
  168. self.assertEqual(project.team_set.all().count(), 1)
  169. def test_project_reserved_words(self):
  170. data = {"name": "new"}
  171. res = self.client.post(self.url, data)
  172. self.assertContains(res, "new-1", status_code=201)
  173. self.client.post(self.url, data)
  174. self.assertFalse(Project.objects.filter(slug="new").exists())