test_api_permissions.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. from django.urls import reverse
  2. from model_bakery import baker
  3. from glitchtip.test_utils.test_case import APIPermissionTestCase
  4. from organizations_ext.models import OrganizationUserRole
  5. class ProjectAPIPermissionTests(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.project.team_set.add(self.team)
  12. self.list_url = reverse("project-list")
  13. self.team_list_url = reverse(
  14. "team-projects-list",
  15. kwargs={"team_pk": self.organization.slug + "/" + self.team.slug},
  16. )
  17. self.detail_url = reverse(
  18. "project-detail",
  19. kwargs={"pk": self.organization.slug + "/" + self.project.slug},
  20. )
  21. self.team_detail_url = reverse(
  22. "team-projects-detail",
  23. kwargs={
  24. "team_pk": self.organization.slug + "/" + self.team.slug,
  25. "slug": self.project.slug,
  26. },
  27. )
  28. def test_list(self):
  29. self.assertGetReqStatusCode(self.list_url, 403)
  30. self.assertGetReqStatusCode(self.team_list_url, 403)
  31. self.auth_token.add_permission("project:read")
  32. self.assertGetReqStatusCode(self.list_url, 200)
  33. self.assertGetReqStatusCode(self.team_list_url, 200)
  34. def test_retrieve(self):
  35. self.assertGetReqStatusCode(self.detail_url, 403)
  36. self.assertGetReqStatusCode(self.team_detail_url, 403)
  37. self.auth_token.add_permission("project:read")
  38. self.assertGetReqStatusCode(self.detail_url, 200)
  39. self.assertGetReqStatusCode(self.team_detail_url, 200)
  40. def test_create(self):
  41. self.auth_token.add_permission("project:read")
  42. data = {"name": "new project"}
  43. self.assertPostReqStatusCode(self.list_url, data, 403)
  44. self.assertPostReqStatusCode(self.team_list_url, data, 403)
  45. self.auth_token.add_permission("project:write")
  46. self.assertPostReqStatusCode(
  47. self.list_url,
  48. data,
  49. 405,
  50. "Post to project endpoint should have no way to select organization",
  51. )
  52. self.assertPostReqStatusCode(self.team_list_url, data, 201)
  53. def test_destroy(self):
  54. self.auth_token.add_permissions(["project:read", "project:write"])
  55. self.assertDeleteReqStatusCode(self.detail_url, 403)
  56. self.auth_token.add_permission("project:admin")
  57. self.assertDeleteReqStatusCode(self.detail_url, 204)
  58. def test_user_destroy(self):
  59. self.client.force_login(self.user)
  60. self.set_user_role(OrganizationUserRole.MEMBER)
  61. self.assertDeleteReqStatusCode(self.detail_url, 403)
  62. self.set_user_role(OrganizationUserRole.OWNER)
  63. self.assertDeleteReqStatusCode(self.detail_url, 204)
  64. def test_destory_team_project(self):
  65. self.assertDeleteReqStatusCode(self.team_detail_url, 403)
  66. self.auth_token.add_permission("project:admin")
  67. self.assertDeleteReqStatusCode(self.team_detail_url, 204)
  68. def test_user_destroy_team_project(self):
  69. self.client.force_login(self.user)
  70. self.set_user_role(OrganizationUserRole.MEMBER)
  71. self.assertDeleteReqStatusCode(self.team_detail_url, 403)
  72. self.set_user_role(OrganizationUserRole.OWNER)
  73. self.assertDeleteReqStatusCode(self.team_detail_url, 204)
  74. def test_update(self):
  75. self.auth_token.add_permission("project:read")
  76. data = {"name": "new name"}
  77. self.assertPutReqStatusCode(self.detail_url, data, 403)
  78. self.assertPutReqStatusCode(self.team_detail_url, data, 403)
  79. self.auth_token.add_permission("project:write")
  80. self.assertPutReqStatusCode(self.detail_url, data, 200)
  81. self.assertPutReqStatusCode(self.team_detail_url, data, 200)
  82. class ProjectKeyAPIPermissionTests(APIPermissionTestCase):
  83. def setUp(self):
  84. self.create_user_org()
  85. self.set_client_credentials(self.auth_token.token)
  86. self.team = baker.make("teams.Team", organization=self.organization)
  87. self.project = baker.make("projects.Project", organization=self.organization)
  88. self.project_key = baker.make("projects.ProjectKey", project=self.project)
  89. self.list_url = reverse(
  90. "project-keys-list",
  91. kwargs={"project_pk": f"{self.organization.slug}/{self.project.slug}"},
  92. )
  93. self.detail_url = reverse(
  94. "project-keys-detail",
  95. kwargs={
  96. "project_pk": f"{self.organization.slug}/{self.project.slug}",
  97. "public_key": self.project_key.public_key,
  98. },
  99. )
  100. def test_list(self):
  101. self.assertGetReqStatusCode(self.list_url, 403)
  102. self.auth_token.add_permission("project:read")
  103. self.assertGetReqStatusCode(self.list_url, 200)
  104. def test_retrieve(self):
  105. self.assertGetReqStatusCode(self.detail_url, 403)
  106. self.auth_token.add_permission("project:read")
  107. self.assertGetReqStatusCode(self.detail_url, 200)
  108. def test_create(self):
  109. self.auth_token.add_permission("project:read")
  110. data = {"label": "new project key"}
  111. self.assertPostReqStatusCode(self.list_url, data, 403)
  112. self.auth_token.add_permission("project:write")
  113. self.assertPostReqStatusCode(self.list_url, data, 201)
  114. def test_destroy(self):
  115. self.auth_token.add_permissions(["project:read", "project:write"])
  116. self.assertDeleteReqStatusCode(self.detail_url, 403)
  117. self.auth_token.add_permission("project:admin")
  118. self.assertDeleteReqStatusCode(self.detail_url, 204)
  119. def test_update(self):
  120. self.auth_token.add_permission("project:read")
  121. data = {"label": "new label"}
  122. self.assertPutReqStatusCode(self.detail_url, data, 403)
  123. self.auth_token.add_permission("project:write")
  124. self.assertPutReqStatusCode(self.detail_url, data, 200)