test_api_permissions.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. from django.urls import reverse
  2. from model_bakery import baker
  3. from apps.organizations_ext.constants import OrganizationUserRole
  4. from glitchtip.test_utils.test_case import APIPermissionTestCase
  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.teams.add(self.team)
  12. self.list_url = reverse("api:list_projects")
  13. self.team_list_url = reverse(
  14. "api:list_team_projects", args=[self.organization.slug, self.team.slug]
  15. )
  16. self.detail_url = reverse(
  17. "api:get_project", args=[self.organization.slug, self.project.slug]
  18. )
  19. def test_list(self):
  20. self.assertGetReqStatusCode(self.list_url, 403)
  21. self.assertGetReqStatusCode(self.team_list_url, 403)
  22. self.auth_token.add_permission("project:read")
  23. self.assertGetReqStatusCode(self.list_url, 200)
  24. self.assertGetReqStatusCode(self.team_list_url, 200)
  25. def test_retrieve(self):
  26. self.assertGetReqStatusCode(self.detail_url, 403)
  27. self.auth_token.add_permission("project:read")
  28. self.assertGetReqStatusCode(self.detail_url, 200)
  29. def test_create(self):
  30. self.auth_token.add_permission("project:read")
  31. data = {"name": "new project"}
  32. self.assertPostReqStatusCode(self.list_url, data, 405)
  33. self.assertPostReqStatusCode(self.team_list_url, data, 403)
  34. self.auth_token.add_permission("project:write")
  35. self.assertPostReqStatusCode(
  36. self.list_url,
  37. data,
  38. 405,
  39. "Post to project endpoint should have no way to select organization",
  40. )
  41. self.assertPostReqStatusCode(self.team_list_url, data, 201)
  42. def test_destroy(self):
  43. self.auth_token.add_permissions(["project:read", "project:write"])
  44. self.assertDeleteReqStatusCode(self.detail_url, 403)
  45. self.auth_token.add_permission("project:admin")
  46. self.assertDeleteReqStatusCode(self.detail_url, 204)
  47. def test_user_destroy(self):
  48. self.set_client_credentials(None)
  49. self.client.force_login(self.user)
  50. self.set_user_role(OrganizationUserRole.MEMBER)
  51. self.assertDeleteReqStatusCode(self.detail_url, 404)
  52. self.set_user_role(OrganizationUserRole.OWNER)
  53. self.assertDeleteReqStatusCode(self.detail_url, 204)
  54. def test_update(self):
  55. self.auth_token.add_permission("project:read")
  56. data = {"name": "new name"}
  57. self.assertPutReqStatusCode(self.detail_url, data, 403)
  58. self.auth_token.add_permission("project:write")
  59. self.assertPutReqStatusCode(self.detail_url, data, 200)
  60. class ProjectKeyAPIPermissionTests(APIPermissionTestCase):
  61. def setUp(self):
  62. self.create_user_org()
  63. self.set_client_credentials(self.auth_token.token)
  64. self.team = baker.make("teams.Team", organization=self.organization)
  65. self.project = baker.make("projects.Project", organization=self.organization)
  66. self.project_key = baker.make("projects.ProjectKey", project=self.project)
  67. self.list_url = reverse(
  68. "api:list_project_keys", args=[self.organization.slug, self.project.slug]
  69. )
  70. self.detail_url = reverse(
  71. "api:get_project_key",
  72. args=[
  73. self.organization.slug,
  74. self.project.slug,
  75. self.project_key.public_key,
  76. ],
  77. )
  78. def test_list(self):
  79. self.assertGetReqStatusCode(self.list_url, 403)
  80. self.auth_token.add_permission("project:read")
  81. self.assertGetReqStatusCode(self.list_url, 200)
  82. def test_retrieve(self):
  83. self.assertGetReqStatusCode(self.detail_url, 403)
  84. self.auth_token.add_permission("project:read")
  85. self.assertGetReqStatusCode(self.detail_url, 200)
  86. def test_create(self):
  87. self.auth_token.add_permission("project:read")
  88. data = {"name": "new project key"}
  89. self.assertPostReqStatusCode(self.list_url, data, 403)
  90. self.auth_token.add_permission("project:write")
  91. self.assertPostReqStatusCode(self.list_url, data, 201)
  92. def test_destroy(self):
  93. self.auth_token.add_permissions(["project:read", "project:write"])
  94. self.assertDeleteReqStatusCode(self.detail_url, 403)
  95. self.auth_token.add_permission("project:admin")
  96. self.assertDeleteReqStatusCode(self.detail_url, 204)
  97. def test_update(self):
  98. self.auth_token.add_permission("project:read")
  99. data = {"name": "new label"}
  100. self.assertPutReqStatusCode(self.detail_url, data, 403)
  101. self.auth_token.add_permission("project:write")
  102. self.assertPutReqStatusCode(self.detail_url, data, 200)