test_api_permissions.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 OrganizationAPIPermissionTests(APIPermissionTestCase):
  6. def setUp(self):
  7. self.create_user_org()
  8. self.set_client_credentials(self.auth_token.token)
  9. self.list_url = reverse("organization-list")
  10. self.detail_url = reverse("organization-detail", args=[self.organization.slug])
  11. def test_list(self):
  12. self.assertGetReqStatusCode(self.list_url, 403)
  13. self.auth_token.add_permission("org:read")
  14. self.assertGetReqStatusCode(self.list_url, 200)
  15. def test_retrieve(self):
  16. self.assertGetReqStatusCode(self.detail_url, 403)
  17. self.auth_token.add_permission("org:read")
  18. self.assertGetReqStatusCode(self.detail_url, 200)
  19. def test_create(self):
  20. self.auth_token.add_permission("org:read")
  21. data = {"name": "new org"}
  22. self.assertPostReqStatusCode(self.list_url, data, 403)
  23. self.auth_token.add_permission("org:write")
  24. self.assertPostReqStatusCode(self.list_url, data, 201)
  25. def test_destroy(self):
  26. self.auth_token.add_permissions(["org:read", "org:write"])
  27. self.assertDeleteReqStatusCode(self.detail_url, 403)
  28. self.auth_token.add_permission("org:admin")
  29. self.assertDeleteReqStatusCode(self.detail_url, 204)
  30. def test_user_destroy(self):
  31. self.client.force_login(self.user)
  32. self.set_user_role(OrganizationUserRole.MEMBER)
  33. self.assertDeleteReqStatusCode(self.detail_url, 403)
  34. self.set_user_role(OrganizationUserRole.OWNER)
  35. self.assertDeleteReqStatusCode(self.detail_url, 204)
  36. def test_update(self):
  37. self.auth_token.add_permission("org:read")
  38. data = {"name": "new name"}
  39. self.assertPutReqStatusCode(self.detail_url, data, 403)
  40. self.auth_token.add_permission("org:write")
  41. self.assertPutReqStatusCode(self.detail_url, data, 200)
  42. def test_user_update(self):
  43. self.client.force_login(self.user)
  44. self.set_user_role(OrganizationUserRole.MEMBER)
  45. data = {"name": "new name"}
  46. self.assertPutReqStatusCode(self.detail_url, data, 403)
  47. self.set_user_role(OrganizationUserRole.MANAGER)
  48. self.assertPutReqStatusCode(self.detail_url, data, 200)
  49. class OrganizationMemberAPIPermissionTests(APIPermissionTestCase):
  50. def setUp(self):
  51. self.create_user_org()
  52. # Change owner to avoid restrictions on org owners
  53. # deleting their own organization
  54. new_user = baker.make("users.User")
  55. new_owner = self.organization.add_user(new_user)
  56. self.organization.change_owner(new_owner)
  57. self.set_client_credentials(self.auth_token.token)
  58. self.list_url = reverse(
  59. "organization-members-list",
  60. kwargs={"organization_slug": self.organization.slug},
  61. )
  62. self.detail_url = reverse(
  63. "organization-members-detail",
  64. kwargs={
  65. "organization_slug": self.organization.slug,
  66. "pk": self.org_user.pk,
  67. },
  68. )
  69. def test_list(self):
  70. self.assertGetReqStatusCode(self.list_url, 403)
  71. self.auth_token.add_permission("member:read")
  72. self.assertGetReqStatusCode(self.list_url, 200)
  73. def test_retrieve(self):
  74. self.assertGetReqStatusCode(self.detail_url, 403)
  75. self.auth_token.add_permission("member:read")
  76. self.assertGetReqStatusCode(self.detail_url, 200)
  77. def test_create(self):
  78. self.auth_token.add_permission("member:read")
  79. data = {"email": "lol@example.com", "role": "member"}
  80. self.assertPostReqStatusCode(self.list_url, data, 403)
  81. self.auth_token.add_permission("member:write")
  82. self.assertPostReqStatusCode(self.list_url, data, 201)
  83. def test_destroy(self):
  84. self.auth_token.add_permissions(["member:read", "member:write"])
  85. self.assertDeleteReqStatusCode(self.detail_url, 403)
  86. self.auth_token.add_permission("member:admin")
  87. self.assertDeleteReqStatusCode(self.detail_url, 204)
  88. def test_user_destroy(self):
  89. self.client.force_login(self.user)
  90. self.set_user_role(OrganizationUserRole.MEMBER)
  91. self.assertDeleteReqStatusCode(self.detail_url, 403)
  92. self.set_user_role(OrganizationUserRole.OWNER)
  93. self.assertDeleteReqStatusCode(self.detail_url, 204)
  94. def test_update(self):
  95. self.auth_token.add_permission("member:read")
  96. data = {"email": "lol@example.com", "role": "member"}
  97. self.assertPutReqStatusCode(self.detail_url, data, 403)
  98. self.auth_token.add_permission("member:write")
  99. self.assertPutReqStatusCode(self.detail_url, data, 200)
  100. def test_teams_add(self):
  101. self.team = baker.make("teams.Team", organization=self.organization)
  102. url = self.detail_url + "teams/" + self.team.slug + "/"
  103. data = {}
  104. self.assertPostReqStatusCode(url, data, 403)
  105. self.auth_token.add_permissions(["org:read", "org:write"])
  106. self.assertPostReqStatusCode(url, data, 201)
  107. def test_teams_remove(self):
  108. self.team = baker.make("teams.Team", organization=self.organization)
  109. url = self.detail_url + "teams/" + self.team.slug + "/"
  110. self.assertDeleteReqStatusCode(url, 403)
  111. self.auth_token.add_permissions(["org:read", "org:write"])
  112. self.assertDeleteReqStatusCode(url, 200)