test_attachmentview.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import os
  2. from django.conf import settings
  3. from django.core.urlresolvers import reverse
  4. from misago.acl.models import Role
  5. from misago.acl.testutils import override_acl
  6. from misago.categories.models import Category
  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. 'uuid': 'qwertyuiop'
  75. }))
  76. self.assertIs404(response)
  77. def test_invalid_uuid(self):
  78. """user tries to retrieve existing file using invalid uuid"""
  79. attachment = self.upload_document()
  80. response = self.client.get(reverse('misago:attachment', kwargs={
  81. 'pk': attachment.pk,
  82. 'uuid': '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.assertIs403(response)
  96. def test_document_thumbnail(self):
  97. """user tries to retrieve thumbnail from non-image attachment"""
  98. attachment = self.upload_document()
  99. response = self.client.get(reverse('misago:attachment-thumbnail', kwargs={
  100. 'pk': attachment.pk,
  101. 'uuid': attachment.uuid
  102. }))
  103. self.assertIs404(response)
  104. def test_no_role(self):
  105. """user tries to retrieve attachment without perm to its type"""
  106. attachment = self.upload_document()
  107. user_roles = (r.pk for r in self.user.get_roles())
  108. self.attachment_type_pdf.limit_downloaders_to.set(Role.objects.exclude(id__in=user_roles))
  109. response = self.client.get(attachment.get_absolute_url())
  110. self.assertIs403(response)
  111. def test_type_disabled(self):
  112. """user tries to retrieve attachment the type disabled downloads"""
  113. attachment = self.upload_document()
  114. self.attachment_type_pdf.status = AttachmentType.DISABLED
  115. self.attachment_type_pdf.save()
  116. response = self.client.get(attachment.get_absolute_url())
  117. self.assertIs403(response)
  118. def test_locked_type(self):
  119. """user retrieves own locked file"""
  120. attachment = self.upload_document()
  121. self.attachment_type_pdf.status = AttachmentType.LOCKED
  122. self.attachment_type_pdf.save()
  123. response = self.client.get(attachment.get_absolute_url())
  124. self.assertSuccess(response)
  125. def test_own_file(self):
  126. """user retrieves own file"""
  127. attachment = self.upload_document()
  128. response = self.client.get(attachment.get_absolute_url())
  129. self.assertSuccess(response)
  130. def test_other_user_file(self):
  131. """user retrieves other user's file with perm"""
  132. attachment = self.upload_document(by_other_user=True)
  133. response = self.client.get(attachment.get_absolute_url())
  134. self.assertSuccess(response)
  135. def test_other_user_orphaned_file_is_staff(self):
  136. """user retrieves other user's orphaned file because he is staff"""
  137. attachment = self.upload_document(is_orphaned=True, by_other_user=True)
  138. self.user.is_staff = True
  139. self.user.save()
  140. response = self.client.get(attachment.get_absolute_url())
  141. self.assertSuccess(response)
  142. def test_orphaned_file_is_uploader(self):
  143. """user retrieves orphaned file because he is its uploader"""
  144. attachment = self.upload_document(is_orphaned=True)
  145. response = self.client.get(attachment.get_absolute_url())
  146. self.assertSuccess(response)
  147. def test_has_role(self):
  148. """user retrieves file he has roles to download"""
  149. attachment = self.upload_document()
  150. user_roles = self.user.get_roles()
  151. self.attachment_type_pdf.limit_downloaders_to.set(user_roles)
  152. response = self.client.get(attachment.get_absolute_url())
  153. self.assertSuccess(response)
  154. def test_image(self):
  155. """user retrieves """
  156. attachment = self.upload_image()
  157. response = self.client.get(attachment.get_absolute_url())
  158. self.assertSuccess(response)
  159. def test_image_thumb(self):
  160. """user retrieves image's thumbnail"""
  161. attachment = self.upload_image()
  162. response = self.client.get(attachment.get_thumbnail_url())
  163. self.assertSuccess(response)