test_api_permissions.py 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. from django.urls import reverse
  2. from model_bakery import baker
  3. from organizations_ext.models import OrganizationUserRole
  4. from glitchtip.test_utils.test_case import APIPermissionTestCase
  5. class TeamAPIPermissionTests(APIPermissionTestCase):
  6. def setUp(self):
  7. self.create_user_org()
  8. self.set_client_credentials(self.auth_token.token)
  9. self.team = baker.make("teams.Team", organization=self.organization)
  10. self.project = baker.make("projects.Project", organization=self.organization)
  11. self.list_url = reverse("team-list")
  12. self.organization_list_url = reverse(
  13. "organization-teams-list",
  14. kwargs={"organization_slug": self.organization.slug},
  15. )
  16. self.project_list_url = reverse(
  17. "project-teams-list",
  18. kwargs={"project_pk": self.organization.slug + "/" + self.project.slug},
  19. )
  20. self.detail_url = reverse(
  21. "team-detail", kwargs={"pk": self.organization.slug + "/" + self.team.slug}
  22. )
  23. self.organization_detail_url = reverse(
  24. "organization-teams-detail",
  25. kwargs={"organization_slug": self.organization.slug, "pk": self.team.pk},
  26. )
  27. self.project_detail_url = reverse(
  28. "project-teams-detail",
  29. kwargs={
  30. "project_pk": self.organization.slug + "/" + self.project.slug,
  31. "pk": self.team.pk,
  32. },
  33. )
  34. def test_list(self):
  35. self.assertGetReqStatusCode(self.list_url, 403)
  36. self.assertGetReqStatusCode(self.organization_list_url, 403)
  37. self.assertGetReqStatusCode(self.project_list_url, 403)
  38. self.auth_token.add_permission("team:read")
  39. self.assertGetReqStatusCode(self.list_url, 200)
  40. self.assertGetReqStatusCode(self.organization_list_url, 200)
  41. self.assertGetReqStatusCode(self.project_list_url, 200)
  42. def test_retrieve(self):
  43. self.assertGetReqStatusCode(self.detail_url, 403)
  44. self.assertGetReqStatusCode(self.organization_detail_url, 403)
  45. self.assertGetReqStatusCode(self.project_detail_url, 403)
  46. self.auth_token.add_permission("team:read")
  47. self.assertGetReqStatusCode(self.detail_url, 200)
  48. self.assertGetReqStatusCode(self.organization_detail_url, 200)
  49. # ProjectTeamViewSet does not allow GET
  50. self.assertGetReqStatusCode(self.project_detail_url, 405)
  51. def test_create(self):
  52. self.auth_token.add_permission("team:read")
  53. data = {"slug": "new-team"}
  54. self.assertPostReqStatusCode(self.list_url, data, 403)
  55. self.assertPostReqStatusCode(self.organization_list_url, data, 403)
  56. self.assertPostReqStatusCode(self.project_list_url, data, 403)
  57. self.auth_token.add_permission("team:write")
  58. # Specifying organization from url slug is required
  59. self.assertPostReqStatusCode(self.list_url, data, 400)
  60. self.assertPostReqStatusCode(self.organization_list_url, data, 201)
  61. self.assertPostReqStatusCode(self.project_list_url, data, 400)
  62. data = {"slug": "new-team2"}
  63. self.assertPostReqStatusCode(self.project_list_url, data, 201)
  64. def test_destroy(self):
  65. self.auth_token.add_permissions(["team:read", "team:write"])
  66. self.assertDeleteReqStatusCode(self.detail_url, 403)
  67. self.auth_token.add_permission("team:admin")
  68. self.assertDeleteReqStatusCode(self.detail_url, 204)
  69. def test_user_destroy(self):
  70. self.client.force_login(self.user)
  71. self.set_user_role(OrganizationUserRole.MEMBER)
  72. self.assertDeleteReqStatusCode(self.detail_url, 403)
  73. self.set_user_role(OrganizationUserRole.OWNER)
  74. self.assertDeleteReqStatusCode(self.detail_url, 204)
  75. def test_update(self):
  76. self.auth_token.add_permission("team:read")
  77. data = {"slug": "new-slug"}
  78. self.assertPutReqStatusCode(self.detail_url, data, 403)
  79. self.assertPutReqStatusCode(self.organization_detail_url, data, 403)
  80. self.auth_token.add_permission("team:write")
  81. self.assertPutReqStatusCode(self.detail_url, data, 200)
  82. self.assertPutReqStatusCode(self.organization_detail_url, data, 200)