tests.py 8.0 KB

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