test_attachmentview.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import os
  2. from django.urls import reverse
  3. from misago.acl.models import Role
  4. from misago.acl.test import patch_user_acl
  5. from misago.categories.models import Category
  6. from misago.conf import settings
  7. from misago.threads import testutils
  8. from misago.threads.models import Attachment, AttachmentType
  9. from misago.users.testutils import AuthenticatedUserTestCase
  10. TESTFILES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'testfiles')
  11. TEST_DOCUMENT_PATH = os.path.join(TESTFILES_DIR, 'document.pdf')
  12. TEST_SMALLJPG_PATH = os.path.join(TESTFILES_DIR, 'small.jpg')
  13. def patch_attachments_acl(acl_patch=None):
  14. acl_patch = acl_patch or {}
  15. acl_patch.setdefault("max_attachment_size", 1024)
  16. acl_patch.setdefault("can_download_other_users_attachments", True)
  17. return patch_user_acl(acl_patch)
  18. class AttachmentViewTestCase(AuthenticatedUserTestCase):
  19. def setUp(self):
  20. super().setUp()
  21. AttachmentType.objects.all().delete()
  22. self.category = Category.objects.get(slug='first-category')
  23. self.post = testutils.post_thread(category=self.category).first_post
  24. self.api_link = reverse('misago:api:attachment-list')
  25. self.attachment_type_jpg = AttachmentType.objects.create(
  26. name="JPG",
  27. extensions='jpeg,jpg',
  28. )
  29. self.attachment_type_pdf = AttachmentType.objects.create(
  30. name="PDF",
  31. extensions='pdf',
  32. )
  33. def upload_document(self, is_orphaned=False, by_other_user=False):
  34. with open(TEST_DOCUMENT_PATH, 'rb') as upload:
  35. response = self.client.post(
  36. self.api_link, data={
  37. 'upload': upload,
  38. }
  39. )
  40. self.assertEqual(response.status_code, 200)
  41. attachment = Attachment.objects.order_by('id').last()
  42. if not is_orphaned:
  43. attachment.post = self.post
  44. attachment.save()
  45. if by_other_user:
  46. attachment.uploader = None
  47. attachment.save()
  48. return attachment
  49. def upload_image(self):
  50. with open(TEST_SMALLJPG_PATH, 'rb') as upload:
  51. response = self.client.post(
  52. self.api_link, data={
  53. 'upload': upload,
  54. }
  55. )
  56. self.assertEqual(response.status_code, 200)
  57. return Attachment.objects.order_by('id').last()
  58. @patch_attachments_acl()
  59. def assertIs404(self, response):
  60. self.assertEqual(response.status_code, 302)
  61. self.assertTrue(response['location'].endswith(settings.MISAGO_404_IMAGE))
  62. @patch_attachments_acl()
  63. def assertIs403(self, response):
  64. self.assertEqual(response.status_code, 302)
  65. self.assertTrue(response['location'].endswith(settings.MISAGO_403_IMAGE))
  66. @patch_attachments_acl()
  67. def assertSuccess(self, response):
  68. self.assertEqual(response.status_code, 302)
  69. self.assertFalse(response['location'].endswith(settings.MISAGO_404_IMAGE))
  70. self.assertFalse(response['location'].endswith(settings.MISAGO_403_IMAGE))
  71. @patch_attachments_acl()
  72. def test_nonexistant_file(self):
  73. """user tries to retrieve nonexistant file"""
  74. response = self.client.get(
  75. reverse('misago:attachment', kwargs={
  76. 'pk': 123,
  77. 'secret': 'qwertyuiop',
  78. })
  79. )
  80. self.assertIs404(response)
  81. @patch_attachments_acl()
  82. def test_invalid_secret(self):
  83. """user tries to retrieve existing file using invalid secret"""
  84. attachment = self.upload_document()
  85. response = self.client.get(
  86. reverse('misago:attachment', kwargs={
  87. 'pk': attachment.pk,
  88. 'secret': 'qwertyuiop',
  89. })
  90. )
  91. self.assertIs404(response)
  92. @patch_attachments_acl({"can_download_other_users_attachments": False})
  93. def test_other_user_file_no_permission(self):
  94. """user tries to retrieve other user's file without perm"""
  95. attachment = self.upload_document(by_other_user=True)
  96. response = self.client.get(attachment.get_absolute_url())
  97. self.assertIs403(response)
  98. @patch_attachments_acl({"can_download_other_users_attachments": False})
  99. def test_other_user_orphaned_file(self):
  100. """user tries to retrieve other user's orphaned file"""
  101. attachment = self.upload_document(is_orphaned=True, by_other_user=True)
  102. response = self.client.get(attachment.get_absolute_url())
  103. self.assertIs404(response)
  104. response = self.client.get(attachment.get_absolute_url() + '?shva=1')
  105. self.assertIs404(response)
  106. @patch_attachments_acl()
  107. def test_document_thumbnail(self):
  108. """user tries to retrieve thumbnail from non-image attachment"""
  109. attachment = self.upload_document()
  110. response = self.client.get(
  111. reverse(
  112. 'misago:attachment-thumbnail',
  113. kwargs={
  114. 'pk': attachment.pk,
  115. 'secret': attachment.secret,
  116. }
  117. )
  118. )
  119. self.assertIs404(response)
  120. @patch_attachments_acl()
  121. def test_no_role(self):
  122. """user tries to retrieve attachment without perm to its type"""
  123. attachment = self.upload_document()
  124. user_roles = (r.pk for r in self.user.get_roles())
  125. self.attachment_type_pdf.limit_downloads_to.set(Role.objects.exclude(id__in=user_roles))
  126. response = self.client.get(attachment.get_absolute_url())
  127. self.assertIs403(response)
  128. @patch_attachments_acl()
  129. def test_type_disabled(self):
  130. """user tries to retrieve attachment the type disabled downloads"""
  131. attachment = self.upload_document()
  132. self.attachment_type_pdf.status = AttachmentType.DISABLED
  133. self.attachment_type_pdf.save()
  134. response = self.client.get(attachment.get_absolute_url())
  135. self.assertIs403(response)
  136. @patch_attachments_acl()
  137. def test_locked_type(self):
  138. """user retrieves own locked file"""
  139. attachment = self.upload_document()
  140. self.attachment_type_pdf.status = AttachmentType.LOCKED
  141. self.attachment_type_pdf.save()
  142. response = self.client.get(attachment.get_absolute_url())
  143. self.assertSuccess(response)
  144. @patch_attachments_acl()
  145. def test_own_file(self):
  146. """user retrieves own file"""
  147. attachment = self.upload_document()
  148. response = self.client.get(attachment.get_absolute_url())
  149. self.assertSuccess(response)
  150. @patch_attachments_acl()
  151. def test_other_user_file(self):
  152. """user retrieves other user's file with perm"""
  153. attachment = self.upload_document(by_other_user=True)
  154. response = self.client.get(attachment.get_absolute_url())
  155. self.assertSuccess(response)
  156. @patch_attachments_acl()
  157. def test_other_user_orphaned_file_is_staff(self):
  158. """user retrieves other user's orphaned file because he is staff"""
  159. attachment = self.upload_document(is_orphaned=True, by_other_user=True)
  160. self.user.is_staff = True
  161. self.user.save()
  162. response = self.client.get(attachment.get_absolute_url())
  163. self.assertIs404(response)
  164. response = self.client.get(attachment.get_absolute_url() + '?shva=1')
  165. self.assertSuccess(response)
  166. @patch_attachments_acl()
  167. def test_orphaned_file_is_uploader(self):
  168. """user retrieves orphaned file because he is its uploader"""
  169. attachment = self.upload_document(is_orphaned=True)
  170. response = self.client.get(attachment.get_absolute_url())
  171. self.assertIs404(response)
  172. response = self.client.get(attachment.get_absolute_url() + '?shva=1')
  173. self.assertSuccess(response)
  174. @patch_attachments_acl()
  175. def test_has_role(self):
  176. """user retrieves file he has roles to download"""
  177. attachment = self.upload_document()
  178. user_roles = self.user.get_roles()
  179. self.attachment_type_pdf.limit_downloads_to.set(user_roles)
  180. response = self.client.get(attachment.get_absolute_url() + '?shva=1')
  181. self.assertSuccess(response)
  182. @patch_attachments_acl()
  183. def test_image(self):
  184. """user retrieves """
  185. attachment = self.upload_image()
  186. response = self.client.get(attachment.get_absolute_url() + '?shva=1')
  187. self.assertSuccess(response)
  188. @patch_attachments_acl()
  189. def test_image_thumb(self):
  190. """user retrieves image's thumbnail"""
  191. attachment = self.upload_image()
  192. response = self.client.get(attachment.get_absolute_url() + '?shva=1')
  193. self.assertSuccess(response)