test_api_permissions.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 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. self.set_client_credentials(self.auth_token.token)
  53. self.list_url = reverse(
  54. "organization-members-list",
  55. kwargs={"organization_slug": self.organization.slug},
  56. )
  57. self.detail_url = reverse(
  58. "organization-members-detail",
  59. kwargs={
  60. "organization_slug": self.organization.slug,
  61. "pk": self.org_user.pk,
  62. },
  63. )
  64. def test_list(self):
  65. self.assertGetReqStatusCode(self.list_url, 403)
  66. self.auth_token.add_permission("member:read")
  67. self.assertGetReqStatusCode(self.list_url, 200)
  68. def test_retrieve(self):
  69. self.assertGetReqStatusCode(self.detail_url, 403)
  70. self.auth_token.add_permission("member:read")
  71. self.assertGetReqStatusCode(self.detail_url, 200)
  72. def test_create(self):
  73. self.auth_token.add_permission("member:read")
  74. data = {"email": "lol@example.com", "role": "member"}
  75. self.assertPostReqStatusCode(self.list_url, data, 403)
  76. self.auth_token.add_permission("member:write")
  77. self.assertPostReqStatusCode(self.list_url, data, 201)
  78. def test_destroy(self):
  79. self.auth_token.add_permissions(["member:read", "member:write"])
  80. self.assertDeleteReqStatusCode(self.detail_url, 403)
  81. self.auth_token.add_permission("member:admin")
  82. self.assertDeleteReqStatusCode(self.detail_url, 204)
  83. def test_user_destroy(self):
  84. self.client.force_login(self.user)
  85. self.set_user_role(OrganizationUserRole.MEMBER)
  86. self.assertDeleteReqStatusCode(self.detail_url, 403)
  87. self.set_user_role(OrganizationUserRole.OWNER)
  88. self.assertDeleteReqStatusCode(self.detail_url, 204)
  89. def test_update(self):
  90. self.auth_token.add_permission("member:read")
  91. data = {"email": "lol@example.com", "role": "member"}
  92. self.assertPutReqStatusCode(self.detail_url, data, 403)
  93. self.auth_token.add_permission("member:write")
  94. self.assertPutReqStatusCode(self.detail_url, data, 200)
  95. def test_teams_add(self):
  96. self.team = baker.make("teams.Team", organization=self.organization)
  97. url = self.detail_url + "teams/" + self.team.slug + "/"
  98. data = {}
  99. self.assertPostReqStatusCode(url, data, 403)
  100. self.auth_token.add_permissions(["org:read", "org:write"])
  101. self.assertPostReqStatusCode(url, data, 201)
  102. def test_teams_remove(self):
  103. self.team = baker.make("teams.Team", organization=self.organization)
  104. url = self.detail_url + "teams/" + self.team.slug + "/"
  105. self.assertDeleteReqStatusCode(url, 403)
  106. self.auth_token.add_permissions(["org:read", "org:write"])
  107. self.assertDeleteReqStatusCode(url, 200)