test_api_permissions.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 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("api:list_organizations")
  10. self.detail_url = reverse("api:get_organization", 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.set_client_credentials(None)
  32. self.client.force_login(self.user)
  33. self.set_user_role(OrganizationUserRole.MEMBER)
  34. self.assertDeleteReqStatusCode(self.detail_url, 403)
  35. self.set_user_role(OrganizationUserRole.OWNER)
  36. self.assertDeleteReqStatusCode(self.detail_url, 204)
  37. def test_update(self):
  38. self.auth_token.add_permission("org:read")
  39. data = {"name": "new name"}
  40. self.assertPutReqStatusCode(self.detail_url, data, 403)
  41. self.auth_token.add_permission("org:write")
  42. self.assertPutReqStatusCode(self.detail_url, data, 200)
  43. def test_user_update(self):
  44. user2 = baker.make("users.user")
  45. self.organization.add_user(user2, OrganizationUserRole.MANAGER)
  46. self.set_client_credentials(None)
  47. self.client.force_login(self.user)
  48. self.set_user_role(OrganizationUserRole.MEMBER)
  49. data = {"name": "new name"}
  50. self.assertPutReqStatusCode(self.detail_url, data, 403)
  51. self.set_user_role(OrganizationUserRole.MANAGER)
  52. self.assertPutReqStatusCode(self.detail_url, data, 200)
  53. class OrganizationMemberAPIPermissionTests(APIPermissionTestCase):
  54. @classmethod
  55. def setUpTestData(cls):
  56. cls.create_user_org(cls)
  57. # Change owner to avoid restrictions on org owners
  58. # deleting their own organization
  59. new_user = baker.make("users.User")
  60. new_owner = cls.organization.add_user(new_user)
  61. cls.organization.change_owner(new_owner)
  62. cls.list_url = reverse(
  63. "api:list_organization_members", args=[cls.organization.slug]
  64. )
  65. cls.detail_url = reverse(
  66. "api:get_organization_member",
  67. args=[cls.organization.slug, cls.org_user.pk],
  68. )
  69. def setUp(self):
  70. self.set_client_credentials(self.auth_token.token)
  71. def test_list(self):
  72. self.assertGetReqStatusCode(self.list_url, 403)
  73. self.auth_token.add_permission("member:read")
  74. self.assertGetReqStatusCode(self.list_url, 200)
  75. def test_retrieve(self):
  76. self.assertGetReqStatusCode(self.detail_url, 403)
  77. self.auth_token.add_permission("member:read")
  78. self.assertGetReqStatusCode(self.detail_url, 200)
  79. def test_create(self):
  80. self.auth_token.add_permission("member:read")
  81. data = {"email": "lol@example.com", "orgRole": "member", "teams": []}
  82. self.assertPostReqStatusCode(self.list_url, data, 403)
  83. self.auth_token.add_permission("member:write")
  84. self.assertPostReqStatusCode(self.list_url, data, 201)
  85. def test_destroy(self):
  86. self.auth_token.add_permissions(["member:read", "member:write"])
  87. self.assertDeleteReqStatusCode(self.detail_url, 403)
  88. self.auth_token.add_permission("member:admin")
  89. self.assertDeleteReqStatusCode(self.detail_url, 204)
  90. def test_user_destroy(self):
  91. self.set_client_credentials(None)
  92. self.client.force_login(self.user)
  93. self.set_user_role(OrganizationUserRole.MEMBER)
  94. self.assertDeleteReqStatusCode(self.detail_url, 403)
  95. self.set_user_role(OrganizationUserRole.OWNER)
  96. self.assertDeleteReqStatusCode(self.detail_url, 204)
  97. def test_update(self):
  98. baker.make(
  99. "organizations_ext.OrganizationUser",
  100. role=OrganizationUserRole.OWNER,
  101. organization=self.organization,
  102. ) # Ensure alternative owner exists
  103. self.auth_token.add_permission("member:read")
  104. data = {"email": "lol@example.com", "orgRole": "member"}
  105. self.assertPutReqStatusCode(self.detail_url, data, 403)
  106. self.auth_token.add_permission("member:write")
  107. self.assertPutReqStatusCode(self.detail_url, data, 200)