test_attachmentview.py 7.9 KB

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