test_attachmentview.py 7.8 KB

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