test_api.py 7.9 KB

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