test_api_permissions.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. from django.urls import reverse
  2. from model_bakery import baker
  3. from apps.organizations_ext.models import OrganizationUserRole
  4. from glitchtip.test_utils.test_case import APIPermissionTestCase
  5. class TeamAPIPermissionTests(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.list_url = reverse(
  12. "api:list_teams",
  13. kwargs={"organization_slug": self.organization.slug},
  14. )
  15. self.project_list_url = reverse(
  16. "api:list_project_teams",
  17. kwargs={
  18. "organization_slug": self.organization.slug,
  19. "project_slug": self.project.slug,
  20. },
  21. )
  22. self.detail_url = reverse(
  23. "api:get_team",
  24. kwargs={
  25. "organization_slug": self.organization.slug,
  26. "team_slug": self.team.slug,
  27. },
  28. )
  29. self.create_team_url = reverse(
  30. "api:create_team",
  31. kwargs={
  32. "organization_slug": self.organization.slug,
  33. },
  34. )
  35. def test_list(self):
  36. self.assertGetReqStatusCode(self.list_url, 403)
  37. self.assertGetReqStatusCode(self.project_list_url, 403)
  38. self.auth_token.add_permission("team:read")
  39. self.assertGetReqStatusCode(self.list_url, 200)
  40. self.assertGetReqStatusCode(self.project_list_url, 200)
  41. def test_retrieve(self):
  42. self.assertGetReqStatusCode(self.detail_url, 403)
  43. self.auth_token.add_permission("team:read")
  44. self.assertGetReqStatusCode(self.detail_url, 200)
  45. def test_create(self):
  46. self.auth_token.add_permission("team:read")
  47. data = {"slug": "new-team"}
  48. self.assertPostReqStatusCode(self.create_team_url, data, 403)
  49. self.auth_token.add_permission("team:write")
  50. self.assertPostReqStatusCode(self.create_team_url, data, 201)
  51. def test_destroy(self):
  52. self.auth_token.add_permissions(["team:read", "team:write"])
  53. self.assertDeleteReqStatusCode(self.detail_url, 403)
  54. self.auth_token.add_permission("team:admin")
  55. self.assertDeleteReqStatusCode(self.detail_url, 204)
  56. def test_user_destroy(self):
  57. self.set_client_credentials(None)
  58. self.client.force_login(self.user)
  59. self.set_user_role(OrganizationUserRole.MEMBER)
  60. self.assertDeleteReqStatusCode(self.detail_url, 404)
  61. self.set_user_role(OrganizationUserRole.OWNER)
  62. self.assertDeleteReqStatusCode(self.detail_url, 204)
  63. def test_update(self):
  64. self.auth_token.add_permission("team:read")
  65. data = {"slug": "new-slug"}
  66. self.assertPutReqStatusCode(self.detail_url, data, 403)
  67. self.auth_token.add_permission("team:write")
  68. self.assertPutReqStatusCode(self.detail_url, data, 200)