|
@@ -3,19 +3,25 @@ import os
|
|
from django.urls import reverse
|
|
from django.urls import reverse
|
|
|
|
|
|
from misago.acl.models import Role
|
|
from misago.acl.models import Role
|
|
-from misago.acl.testutils import override_acl
|
|
|
|
|
|
+from misago.acl.test import patch_user_acl
|
|
from misago.categories.models import Category
|
|
from misago.categories.models import Category
|
|
from misago.conf import settings
|
|
from misago.conf import settings
|
|
from misago.threads import testutils
|
|
from misago.threads import testutils
|
|
from misago.threads.models import Attachment, AttachmentType
|
|
from misago.threads.models import Attachment, AttachmentType
|
|
from misago.users.testutils import AuthenticatedUserTestCase
|
|
from misago.users.testutils import AuthenticatedUserTestCase
|
|
|
|
|
|
-
|
|
|
|
TESTFILES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'testfiles')
|
|
TESTFILES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'testfiles')
|
|
TEST_DOCUMENT_PATH = os.path.join(TESTFILES_DIR, 'document.pdf')
|
|
TEST_DOCUMENT_PATH = os.path.join(TESTFILES_DIR, 'document.pdf')
|
|
TEST_SMALLJPG_PATH = os.path.join(TESTFILES_DIR, 'small.jpg')
|
|
TEST_SMALLJPG_PATH = os.path.join(TESTFILES_DIR, 'small.jpg')
|
|
|
|
|
|
|
|
|
|
|
|
+def patch_attachments_acl(acl_patch=None):
|
|
|
|
+ acl_patch = acl_patch or {}
|
|
|
|
+ acl_patch.setdefault("max_attachment_size", 1024)
|
|
|
|
+ acl_patch.setdefault("can_download_other_users_attachments", True)
|
|
|
|
+ return patch_user_acl(acl_patch)
|
|
|
|
+
|
|
|
|
+
|
|
class AttachmentViewTestCase(AuthenticatedUserTestCase):
|
|
class AttachmentViewTestCase(AuthenticatedUserTestCase):
|
|
def setUp(self):
|
|
def setUp(self):
|
|
super().setUp()
|
|
super().setUp()
|
|
@@ -36,16 +42,6 @@ class AttachmentViewTestCase(AuthenticatedUserTestCase):
|
|
extensions='pdf',
|
|
extensions='pdf',
|
|
)
|
|
)
|
|
|
|
|
|
- self.override_acl()
|
|
|
|
-
|
|
|
|
- def override_acl(self, allow_download=True):
|
|
|
|
- acl = self.user.acl_cache.copy()
|
|
|
|
- acl.update({
|
|
|
|
- 'max_attachment_size': 1000,
|
|
|
|
- 'can_download_other_users_attachments': allow_download,
|
|
|
|
- })
|
|
|
|
- override_acl(self.user, acl)
|
|
|
|
-
|
|
|
|
def upload_document(self, is_orphaned=False, by_other_user=False):
|
|
def upload_document(self, is_orphaned=False, by_other_user=False):
|
|
with open(TEST_DOCUMENT_PATH, 'rb') as upload:
|
|
with open(TEST_DOCUMENT_PATH, 'rb') as upload:
|
|
response = self.client.post(
|
|
response = self.client.post(
|
|
@@ -64,8 +60,6 @@ class AttachmentViewTestCase(AuthenticatedUserTestCase):
|
|
attachment.uploader = None
|
|
attachment.uploader = None
|
|
attachment.save()
|
|
attachment.save()
|
|
|
|
|
|
- self.override_acl()
|
|
|
|
-
|
|
|
|
return attachment
|
|
return attachment
|
|
|
|
|
|
def upload_image(self):
|
|
def upload_image(self):
|
|
@@ -77,25 +71,25 @@ class AttachmentViewTestCase(AuthenticatedUserTestCase):
|
|
)
|
|
)
|
|
self.assertEqual(response.status_code, 200)
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
- attachment = Attachment.objects.order_by('id').last()
|
|
|
|
-
|
|
|
|
- self.override_acl()
|
|
|
|
-
|
|
|
|
- return attachment
|
|
|
|
|
|
+ return Attachment.objects.order_by('id').last()
|
|
|
|
|
|
|
|
+ @patch_attachments_acl()
|
|
def assertIs404(self, response):
|
|
def assertIs404(self, response):
|
|
self.assertEqual(response.status_code, 302)
|
|
self.assertEqual(response.status_code, 302)
|
|
self.assertTrue(response['location'].endswith(settings.MISAGO_404_IMAGE))
|
|
self.assertTrue(response['location'].endswith(settings.MISAGO_404_IMAGE))
|
|
|
|
|
|
|
|
+ @patch_attachments_acl()
|
|
def assertIs403(self, response):
|
|
def assertIs403(self, response):
|
|
self.assertEqual(response.status_code, 302)
|
|
self.assertEqual(response.status_code, 302)
|
|
self.assertTrue(response['location'].endswith(settings.MISAGO_403_IMAGE))
|
|
self.assertTrue(response['location'].endswith(settings.MISAGO_403_IMAGE))
|
|
|
|
|
|
|
|
+ @patch_attachments_acl()
|
|
def assertSuccess(self, response):
|
|
def assertSuccess(self, response):
|
|
self.assertEqual(response.status_code, 302)
|
|
self.assertEqual(response.status_code, 302)
|
|
self.assertFalse(response['location'].endswith(settings.MISAGO_404_IMAGE))
|
|
self.assertFalse(response['location'].endswith(settings.MISAGO_404_IMAGE))
|
|
self.assertFalse(response['location'].endswith(settings.MISAGO_403_IMAGE))
|
|
self.assertFalse(response['location'].endswith(settings.MISAGO_403_IMAGE))
|
|
|
|
|
|
|
|
+ @patch_attachments_acl()
|
|
def test_nonexistant_file(self):
|
|
def test_nonexistant_file(self):
|
|
"""user tries to retrieve nonexistant file"""
|
|
"""user tries to retrieve nonexistant file"""
|
|
response = self.client.get(
|
|
response = self.client.get(
|
|
@@ -107,6 +101,7 @@ class AttachmentViewTestCase(AuthenticatedUserTestCase):
|
|
|
|
|
|
self.assertIs404(response)
|
|
self.assertIs404(response)
|
|
|
|
|
|
|
|
+ @patch_attachments_acl()
|
|
def test_invalid_secret(self):
|
|
def test_invalid_secret(self):
|
|
"""user tries to retrieve existing file using invalid secret"""
|
|
"""user tries to retrieve existing file using invalid secret"""
|
|
attachment = self.upload_document()
|
|
attachment = self.upload_document()
|
|
@@ -120,15 +115,15 @@ class AttachmentViewTestCase(AuthenticatedUserTestCase):
|
|
|
|
|
|
self.assertIs404(response)
|
|
self.assertIs404(response)
|
|
|
|
|
|
|
|
+ @patch_attachments_acl({"can_download_other_users_attachments": False})
|
|
def test_other_user_file_no_permission(self):
|
|
def test_other_user_file_no_permission(self):
|
|
"""user tries to retrieve other user's file without perm"""
|
|
"""user tries to retrieve other user's file without perm"""
|
|
attachment = self.upload_document(by_other_user=True)
|
|
attachment = self.upload_document(by_other_user=True)
|
|
|
|
|
|
- self.override_acl(False)
|
|
|
|
-
|
|
|
|
response = self.client.get(attachment.get_absolute_url())
|
|
response = self.client.get(attachment.get_absolute_url())
|
|
self.assertIs403(response)
|
|
self.assertIs403(response)
|
|
|
|
|
|
|
|
+ @patch_attachments_acl({"can_download_other_users_attachments": False})
|
|
def test_other_user_orphaned_file(self):
|
|
def test_other_user_orphaned_file(self):
|
|
"""user tries to retrieve other user's orphaned file"""
|
|
"""user tries to retrieve other user's orphaned file"""
|
|
attachment = self.upload_document(is_orphaned=True, by_other_user=True)
|
|
attachment = self.upload_document(is_orphaned=True, by_other_user=True)
|
|
@@ -139,6 +134,7 @@ class AttachmentViewTestCase(AuthenticatedUserTestCase):
|
|
response = self.client.get(attachment.get_absolute_url() + '?shva=1')
|
|
response = self.client.get(attachment.get_absolute_url() + '?shva=1')
|
|
self.assertIs404(response)
|
|
self.assertIs404(response)
|
|
|
|
|
|
|
|
+ @patch_attachments_acl()
|
|
def test_document_thumbnail(self):
|
|
def test_document_thumbnail(self):
|
|
"""user tries to retrieve thumbnail from non-image attachment"""
|
|
"""user tries to retrieve thumbnail from non-image attachment"""
|
|
attachment = self.upload_document()
|
|
attachment = self.upload_document()
|
|
@@ -154,6 +150,7 @@ class AttachmentViewTestCase(AuthenticatedUserTestCase):
|
|
)
|
|
)
|
|
self.assertIs404(response)
|
|
self.assertIs404(response)
|
|
|
|
|
|
|
|
+ @patch_attachments_acl()
|
|
def test_no_role(self):
|
|
def test_no_role(self):
|
|
"""user tries to retrieve attachment without perm to its type"""
|
|
"""user tries to retrieve attachment without perm to its type"""
|
|
attachment = self.upload_document()
|
|
attachment = self.upload_document()
|
|
@@ -164,6 +161,7 @@ class AttachmentViewTestCase(AuthenticatedUserTestCase):
|
|
response = self.client.get(attachment.get_absolute_url())
|
|
response = self.client.get(attachment.get_absolute_url())
|
|
self.assertIs403(response)
|
|
self.assertIs403(response)
|
|
|
|
|
|
|
|
+ @patch_attachments_acl()
|
|
def test_type_disabled(self):
|
|
def test_type_disabled(self):
|
|
"""user tries to retrieve attachment the type disabled downloads"""
|
|
"""user tries to retrieve attachment the type disabled downloads"""
|
|
attachment = self.upload_document()
|
|
attachment = self.upload_document()
|
|
@@ -174,6 +172,7 @@ class AttachmentViewTestCase(AuthenticatedUserTestCase):
|
|
response = self.client.get(attachment.get_absolute_url())
|
|
response = self.client.get(attachment.get_absolute_url())
|
|
self.assertIs403(response)
|
|
self.assertIs403(response)
|
|
|
|
|
|
|
|
+ @patch_attachments_acl()
|
|
def test_locked_type(self):
|
|
def test_locked_type(self):
|
|
"""user retrieves own locked file"""
|
|
"""user retrieves own locked file"""
|
|
attachment = self.upload_document()
|
|
attachment = self.upload_document()
|
|
@@ -184,6 +183,7 @@ class AttachmentViewTestCase(AuthenticatedUserTestCase):
|
|
response = self.client.get(attachment.get_absolute_url())
|
|
response = self.client.get(attachment.get_absolute_url())
|
|
self.assertSuccess(response)
|
|
self.assertSuccess(response)
|
|
|
|
|
|
|
|
+ @patch_attachments_acl()
|
|
def test_own_file(self):
|
|
def test_own_file(self):
|
|
"""user retrieves own file"""
|
|
"""user retrieves own file"""
|
|
attachment = self.upload_document()
|
|
attachment = self.upload_document()
|
|
@@ -191,6 +191,7 @@ class AttachmentViewTestCase(AuthenticatedUserTestCase):
|
|
response = self.client.get(attachment.get_absolute_url())
|
|
response = self.client.get(attachment.get_absolute_url())
|
|
self.assertSuccess(response)
|
|
self.assertSuccess(response)
|
|
|
|
|
|
|
|
+ @patch_attachments_acl()
|
|
def test_other_user_file(self):
|
|
def test_other_user_file(self):
|
|
"""user retrieves other user's file with perm"""
|
|
"""user retrieves other user's file with perm"""
|
|
attachment = self.upload_document(by_other_user=True)
|
|
attachment = self.upload_document(by_other_user=True)
|
|
@@ -198,6 +199,7 @@ class AttachmentViewTestCase(AuthenticatedUserTestCase):
|
|
response = self.client.get(attachment.get_absolute_url())
|
|
response = self.client.get(attachment.get_absolute_url())
|
|
self.assertSuccess(response)
|
|
self.assertSuccess(response)
|
|
|
|
|
|
|
|
+ @patch_attachments_acl()
|
|
def test_other_user_orphaned_file_is_staff(self):
|
|
def test_other_user_orphaned_file_is_staff(self):
|
|
"""user retrieves other user's orphaned file because he is staff"""
|
|
"""user retrieves other user's orphaned file because he is staff"""
|
|
attachment = self.upload_document(is_orphaned=True, by_other_user=True)
|
|
attachment = self.upload_document(is_orphaned=True, by_other_user=True)
|
|
@@ -211,6 +213,7 @@ class AttachmentViewTestCase(AuthenticatedUserTestCase):
|
|
response = self.client.get(attachment.get_absolute_url() + '?shva=1')
|
|
response = self.client.get(attachment.get_absolute_url() + '?shva=1')
|
|
self.assertSuccess(response)
|
|
self.assertSuccess(response)
|
|
|
|
|
|
|
|
+ @patch_attachments_acl()
|
|
def test_orphaned_file_is_uploader(self):
|
|
def test_orphaned_file_is_uploader(self):
|
|
"""user retrieves orphaned file because he is its uploader"""
|
|
"""user retrieves orphaned file because he is its uploader"""
|
|
attachment = self.upload_document(is_orphaned=True)
|
|
attachment = self.upload_document(is_orphaned=True)
|
|
@@ -221,6 +224,7 @@ class AttachmentViewTestCase(AuthenticatedUserTestCase):
|
|
response = self.client.get(attachment.get_absolute_url() + '?shva=1')
|
|
response = self.client.get(attachment.get_absolute_url() + '?shva=1')
|
|
self.assertSuccess(response)
|
|
self.assertSuccess(response)
|
|
|
|
|
|
|
|
+ @patch_attachments_acl()
|
|
def test_has_role(self):
|
|
def test_has_role(self):
|
|
"""user retrieves file he has roles to download"""
|
|
"""user retrieves file he has roles to download"""
|
|
attachment = self.upload_document()
|
|
attachment = self.upload_document()
|
|
@@ -231,6 +235,7 @@ class AttachmentViewTestCase(AuthenticatedUserTestCase):
|
|
response = self.client.get(attachment.get_absolute_url() + '?shva=1')
|
|
response = self.client.get(attachment.get_absolute_url() + '?shva=1')
|
|
self.assertSuccess(response)
|
|
self.assertSuccess(response)
|
|
|
|
|
|
|
|
+ @patch_attachments_acl()
|
|
def test_image(self):
|
|
def test_image(self):
|
|
"""user retrieves """
|
|
"""user retrieves """
|
|
attachment = self.upload_image()
|
|
attachment = self.upload_image()
|
|
@@ -238,6 +243,7 @@ class AttachmentViewTestCase(AuthenticatedUserTestCase):
|
|
response = self.client.get(attachment.get_absolute_url() + '?shva=1')
|
|
response = self.client.get(attachment.get_absolute_url() + '?shva=1')
|
|
self.assertSuccess(response)
|
|
self.assertSuccess(response)
|
|
|
|
|
|
|
|
+ @patch_attachments_acl()
|
|
def test_image_thumb(self):
|
|
def test_image_thumb(self):
|
|
"""user retrieves image's thumbnail"""
|
|
"""user retrieves image's thumbnail"""
|
|
attachment = self.upload_image()
|
|
attachment = self.upload_image()
|