tests.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. from django.conf import settings
  2. from django.shortcuts import reverse
  3. from django.utils import timezone
  4. from rest_framework.test import APITestCase
  5. from model_bakery import baker
  6. from glitchtip import test_utils # pylint: disable=unused-import
  7. from organizations_ext.models import OrganizationUserRole
  8. from ..models import ProjectKey, Project
  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("projects.Project", organization=organization, name="A Project")
  30. projectZ = baker.make("projects.Project", organization=organization, name="Z Project")
  31. projectB = baker.make("projects.Project", organization=organization, name="B Project")
  32. res = self.client.get(self.url)
  33. self.assertEqual(res.data[0]["name"], projectA.name)
  34. self.assertEqual(res.data[2]["name"], projectZ.name)
  35. def test_projects_api_retrieve(self):
  36. organization = baker.make("organizations_ext.Organization")
  37. organization.add_user(self.user, role=OrganizationUserRole.OWNER)
  38. project = baker.make(
  39. "projects.Project", organization=organization, first_event=timezone.now()
  40. )
  41. res = self.client.get(
  42. reverse(
  43. "project-detail", kwargs={"pk": organization.slug + "/" + project.slug}
  44. )
  45. )
  46. self.assertTrue(res.data["firstEvent"])
  47. def test_projects_pagination(self):
  48. """
  49. Test link header pagination
  50. """
  51. page_size = settings.REST_FRAMEWORK.get("PAGE_SIZE")
  52. organization = baker.make("organizations_ext.Organization")
  53. organization.add_user(self.user, role=OrganizationUserRole.OWNER)
  54. firstProject = projects = baker.make(
  55. "projects.Project", organization=organization, name="Alphabetically First"
  56. )
  57. baker.make(
  58. "projects.Project", organization=organization, name="B", _quantity=page_size
  59. )
  60. lastProject = projects = baker.make(
  61. "projects.Project", organization=organization, name="Last Alphabetically"
  62. )
  63. res = self.client.get(self.url)
  64. self.assertNotContains(res, lastProject.name)
  65. self.assertContains(res, firstProject.name)
  66. link_header = res.get("Link")
  67. self.assertIn('results="true"', link_header)
  68. def test_project_isolation(self):
  69. """ Users should only access projects in their organization """
  70. user1 = self.user
  71. user2 = baker.make("users.user")
  72. org1 = baker.make("organizations_ext.Organization")
  73. org2 = baker.make("organizations_ext.Organization")
  74. org1.add_user(user1)
  75. org2.add_user(user2)
  76. project1 = baker.make("projects.Project", organization=org1)
  77. project2 = baker.make("projects.Project", organization=org2)
  78. res = self.client.get(self.url)
  79. self.assertContains(res, project1.name)
  80. self.assertNotContains(res, project2.name)
  81. def test_project_delete(self):
  82. organization = baker.make("organizations_ext.Organization")
  83. organization.add_user(self.user, OrganizationUserRole.ADMIN)
  84. team = baker.make("teams.Team", organization=organization)
  85. project = baker.make("projects.Project", organization=organization, team=team)
  86. url = reverse(
  87. "project-detail", kwargs={"pk": f"{organization.slug}/{project.slug}"}
  88. )
  89. res = self.client.delete(url)
  90. self.assertEqual(res.status_code, 204)
  91. self.assertEqual(Project.objects.all().count(), 0)
  92. def test_project_invalid_delete(self):
  93. """ Cannot delete projects that are not in the organization the user is an admin of """
  94. organization = baker.make("organizations_ext.Organization")
  95. organization.add_user(self.user, OrganizationUserRole.ADMIN)
  96. project = baker.make("projects.Project")
  97. url = reverse(
  98. "project-detail", kwargs={"pk": f"{organization.slug}/{project.slug}"}
  99. )
  100. res = self.client.delete(url)
  101. self.assertEqual(res.status_code, 404)
  102. class TeamProjectsAPITestCase(APITestCase):
  103. def setUp(self):
  104. self.user = baker.make("users.user")
  105. self.organization = baker.make("organizations_ext.Organization")
  106. self.organization.add_user(self.user, OrganizationUserRole.ADMIN)
  107. self.team = baker.make("teams.Team", organization=self.organization)
  108. self.client.force_login(self.user)
  109. self.url = reverse(
  110. "team-projects-list",
  111. kwargs={"team_pk": f"{self.organization.slug}/{self.team.slug}"},
  112. )
  113. def test_list(self):
  114. project = baker.make("projects.Project", organization=self.organization)
  115. project.team_set.add(self.team)
  116. not_my_project = baker.make("projects.Project")
  117. res = self.client.get(self.url)
  118. self.assertContains(res, project.name)
  119. self.assertNotContains(res, not_my_project.name)
  120. """
  121. If a user is in multiple orgs, that user will have multiple org users.
  122. Make sure endpoint doesn't show projects from other orgs
  123. """
  124. second_org = baker.make("organizations_ext.Organization")
  125. second_org.add_user(self.user, OrganizationUserRole.ADMIN)
  126. project_in_second_org = baker.make("projects.Project", organization=second_org)
  127. res = self.client.get(self.url)
  128. self.assertNotContains(res, project_in_second_org.name)
  129. """
  130. Only show projects that are associated with the team in the URL.
  131. If a project is on another team in the same org, it should not show
  132. """
  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. res = self.client.post(self.url, data)
  150. self.assertContains(res, name, status_code=201)
  151. projects = Project.objects.all()
  152. self.assertNotEqual(projects[0].slug, projects[1].slug)
  153. self.assertEqual(ProjectKey.objects.all().count(), 2)
  154. org2 = baker.make("organizations_ext.Organization")
  155. org2_project = Project.objects.create(name=name, organization=org2)
  156. # The same slug can exist between multiple organizations
  157. self.assertEqual(projects[0].slug, org2_project.slug)
  158. """
  159. The frontend UI requires you to assign a new project to a team, so make sure
  160. that the new project has a team associated with it
  161. """
  162. def test_projects_api_project_has_team(self):
  163. name = "test project"
  164. data = {"name": name}
  165. res = self.client.post(self.url, data)
  166. project = Project.objects.first()
  167. self.assertEqual(project.team_set.all().count(), 1)