test_api.py 7.9 KB

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