test_permissions.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. from io import StringIO
  2. from django.urls import reverse
  3. from django.core.files.uploadedfile import InMemoryUploadedFile
  4. from model_bakery import baker
  5. from organizations_ext.models import OrganizationUserRole
  6. from glitchtip.test_utils.test_case import APIPermissionTestCase
  7. class ReleaseAPIPermissionTests(APIPermissionTestCase):
  8. def setUp(self):
  9. self.create_user_org()
  10. self.set_client_credentials(self.auth_token.token)
  11. self.project = baker.make("projects.Project", organization=self.organization)
  12. self.release = baker.make("releases.Release", organization=self.organization)
  13. self.organization_list_url = reverse(
  14. "organization-releases-list",
  15. kwargs={"organization_slug": self.organization.slug},
  16. )
  17. self.project_list_url = reverse(
  18. "project-releases-list",
  19. kwargs={"project_pk": self.organization.slug + "/" + self.project.slug},
  20. )
  21. self.organization_detail_url = reverse(
  22. "organization-releases-detail",
  23. kwargs={
  24. "organization_slug": self.organization.slug,
  25. "version": self.release.version,
  26. },
  27. )
  28. self.project_detail_url = reverse(
  29. "project-releases-detail",
  30. kwargs={
  31. "project_pk": self.organization.slug + "/" + self.project.slug,
  32. "version": self.release.version,
  33. },
  34. )
  35. def test_list(self):
  36. self.assertGetReqStatusCode(self.organization_list_url, 403)
  37. self.assertGetReqStatusCode(self.project_list_url, 403)
  38. self.auth_token.add_permission("project:read")
  39. self.assertGetReqStatusCode(self.organization_list_url, 200)
  40. self.assertGetReqStatusCode(self.project_list_url, 200)
  41. def test_retrieve(self):
  42. self.assertGetReqStatusCode(self.organization_detail_url, 403)
  43. self.assertGetReqStatusCode(self.project_detail_url, 403)
  44. self.auth_token.add_permission("project:read")
  45. self.assertGetReqStatusCode(self.organization_detail_url, 200)
  46. self.assertGetReqStatusCode(self.project_detail_url, 200)
  47. def test_assemble(self):
  48. url = self.organization_detail_url + "assemble/"
  49. data = {
  50. "checksum": "94bc085fe32db9b4b1b82236214d65eeeeeeeeee",
  51. "chunks": ["94bc085fe32db9b4b1b82236214d65eeeeeeeeee"],
  52. }
  53. self.assertPostReqStatusCode(url, data, 403)
  54. self.auth_token.add_permission("project:write")
  55. self.assertPostReqStatusCode(url, data, 200)
  56. def test_create(self):
  57. self.auth_token.add_permission("project:read")
  58. data = {"version": "new-version"}
  59. self.assertPostReqStatusCode(self.organization_list_url, data, 403)
  60. self.assertPostReqStatusCode(self.project_list_url, data, 403)
  61. self.auth_token.add_permission("project:releases")
  62. # Unsure if this should be supported
  63. # self.assertPostReqStatusCode(self.organization_list_url, data, 201)
  64. self.assertPostReqStatusCode(self.project_list_url, data, 201)
  65. def test_destroy(self):
  66. self.auth_token.add_permissions(["project:read", "project:write"])
  67. self.assertDeleteReqStatusCode(self.project_detail_url, 403)
  68. self.auth_token.add_permission("project:releases")
  69. self.assertDeleteReqStatusCode(self.project_detail_url, 204)
  70. def test_user_destroy(self):
  71. self.client.force_login(self.user)
  72. self.set_user_role(OrganizationUserRole.MEMBER)
  73. self.assertDeleteReqStatusCode(self.project_detail_url, 204)
  74. def test_update(self):
  75. self.auth_token.add_permission("project:read")
  76. data = {"version": "newer-version"}
  77. self.assertPutReqStatusCode(self.organization_detail_url, data, 403)
  78. self.auth_token.add_permission("project:releases")
  79. self.assertPutReqStatusCode(self.organization_detail_url, data, 200)
  80. class ReleaseFileAPIPermissionTests(APIPermissionTestCase):
  81. def setUp(self):
  82. self.create_user_org()
  83. self.set_client_credentials(self.auth_token.token)
  84. self.project = baker.make("projects.Project", organization=self.organization)
  85. self.release = baker.make(
  86. "releases.Release", organization=self.organization, projects=[self.project]
  87. )
  88. self.release_file = baker.make("releases.ReleaseFile", release=self.release)
  89. self.list_url = reverse(
  90. "files-list",
  91. kwargs={
  92. "project_pk": self.organization.slug + "/" + self.project.slug,
  93. "release_version": self.release.version,
  94. },
  95. )
  96. self.detail_url = reverse(
  97. "files-detail",
  98. kwargs={
  99. "project_pk": self.organization.slug + "/" + self.project.slug,
  100. "release_version": self.release.version,
  101. "pk": self.release_file.pk,
  102. },
  103. )
  104. def test_list(self):
  105. self.assertGetReqStatusCode(self.list_url, 403)
  106. self.auth_token.add_permission("project:read")
  107. self.assertGetReqStatusCode(self.list_url, 200)
  108. def test_retrieve(self):
  109. self.assertGetReqStatusCode(self.detail_url, 403)
  110. self.auth_token.add_permission("project:read")
  111. self.assertGetReqStatusCode(self.detail_url, 200)
  112. def test_create(self):
  113. self.auth_token.add_permission("project:read")
  114. im_io = StringIO()
  115. file = InMemoryUploadedFile(
  116. im_io, None, "name.txt", "text/plain", len(im_io.getvalue()), None
  117. )
  118. data = {"name": "name", "file": file}
  119. self.assertPostReqStatusCode(self.list_url, data, 403)
  120. self.auth_token.add_permission("project:releases")
  121. self.assertPostReqStatusCode(self.list_url, data, 201)
  122. def test_destroy(self):
  123. self.auth_token.add_permissions(["project:read", "project:write"])
  124. self.assertDeleteReqStatusCode(self.detail_url, 403)
  125. self.auth_token.add_permission("project:releases")
  126. self.assertDeleteReqStatusCode(self.detail_url, 204)