test_attachmentview.py 8.1 KB

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