test_api_permissions.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. from django.urls import reverse
  2. from model_bakery import baker
  3. from glitchtip.test_utils.test_case import APIPermissionTestCase
  4. class EnvironmentAPIPermissionTests(APIPermissionTestCase):
  5. def setUp(self):
  6. self.create_user_org()
  7. self.set_client_credentials(self.auth_token.token)
  8. self.environment = baker.make(
  9. "environments.Environment", organization=self.organization
  10. )
  11. baker.make(
  12. "environments.EnvironmentProject",
  13. environment=self.environment,
  14. is_hidden=False,
  15. )
  16. self.list_url = reverse(
  17. "api:list_environments",
  18. kwargs={"organization_slug": self.organization.slug},
  19. )
  20. def test_list(self):
  21. self.assertGetReqStatusCode(self.list_url, 403)
  22. self.auth_token.add_permission("org:read")
  23. self.assertGetReqStatusCode(self.list_url, 200)
  24. class EnvironmentProjectAPIPermissionTests(APIPermissionTestCase):
  25. def setUp(self):
  26. self.create_user_org()
  27. self.set_client_credentials(self.auth_token.token)
  28. self.project = baker.make("projects.Project", organization=self.organization)
  29. self.environment_project = baker.make(
  30. "environments.EnvironmentProject",
  31. environment__organization=self.organization,
  32. project=self.project,
  33. )
  34. self.list_url = reverse(
  35. "api:list_environment_projects",
  36. args=[self.organization.slug, self.project.slug],
  37. )
  38. self.detail_url = reverse(
  39. "api:update_environment_project",
  40. args=[
  41. self.organization.slug,
  42. self.project.slug,
  43. self.environment_project.environment.name,
  44. ],
  45. )
  46. def test_list(self):
  47. self.assertGetReqStatusCode(self.list_url, 403)
  48. self.auth_token.add_permission("project:read")
  49. self.assertGetReqStatusCode(self.list_url, 200)
  50. def test_update(self):
  51. self.auth_token.add_permission("project:read")
  52. data = {"name": "a", "isHidden": True}
  53. self.assertPutReqStatusCode(self.detail_url, data, 403)
  54. self.auth_token.add_permission("project:write")
  55. self.assertPutReqStatusCode(self.detail_url, data, 200)