test_api_permissions.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 AlertsAPIPermissionTests(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.team.members.add(self.org_user)
  11. self.project = baker.make("projects.Project", organization=self.organization)
  12. self.project.teams.add(self.team)
  13. self.alert = baker.make("alerts.ProjectAlert", project=self.project)
  14. self.list_url = reverse(
  15. "api:list_project_alerts", args=[self.organization.slug, self.project.slug]
  16. )
  17. self.detail_url = reverse(
  18. "api:update_project_alert",
  19. args=[self.organization.slug, self.project.slug, self.alert.id],
  20. )
  21. def test_list(self):
  22. self.assertGetReqStatusCode(self.list_url, 403)
  23. self.auth_token.add_permission("project:read")
  24. self.assertGetReqStatusCode(self.list_url, 200)
  25. def test_create(self):
  26. self.auth_token.add_permission("project:read")
  27. data = {"timespan_minutes": 1, "quantity": 1}
  28. self.assertPostReqStatusCode(self.list_url, data, 403)
  29. self.auth_token.add_permission("project:write")
  30. self.assertPostReqStatusCode(self.list_url, data, 201)
  31. def test_destroy(self):
  32. self.auth_token.add_permissions(["project:read", "project:write"])
  33. self.assertDeleteReqStatusCode(self.detail_url, 403)
  34. self.auth_token.add_permission("project:admin")
  35. self.assertDeleteReqStatusCode(self.detail_url, 204)
  36. def test_user_destroy(self):
  37. self.set_client_credentials(None)
  38. self.client.force_login(self.user)
  39. self.set_user_role(OrganizationUserRole.MEMBER)
  40. self.assertDeleteReqStatusCode(self.detail_url, 404)
  41. self.set_user_role(OrganizationUserRole.OWNER)
  42. self.assertDeleteReqStatusCode(self.detail_url, 204)
  43. def test_update(self):
  44. self.auth_token.add_permission("project:read")
  45. data = {"timespan_minutes": 1, "quantity": 1}
  46. self.assertPutReqStatusCode(self.detail_url, data, 403)
  47. self.auth_token.add_permission("project:write")
  48. self.assertPutReqStatusCode(self.detail_url, data, 200)