Skip to content

Commit

Permalink
Improve tests and error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
fsbraun committed Sep 25, 2023
1 parent d463838 commit b5d2c80
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
22 changes: 11 additions & 11 deletions filer/admin/clipboardadmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,31 +145,31 @@ def ajax_upload(request, folder_id=None):
height: int = max(1, file_obj.height)
width: int = max(1, file_obj.width)
pixels: int = width * height
aspect: float = file_obj.width / file_obj.height
aspect: float = width / height
res_x: int = int((FILER_MAX_IMAGE_PIXELS * aspect) ** 0.5)
res_y: int = int(res_x / aspect)
if pixels > 2 * FILER_MAX_IMAGE_PIXELS:
msg = _(
"Image size (%(pixels)d pixels) exceeds limit of %(max_pixels)d "
"pixels by a factor of two or more. Resize image to (%(width)d, "
"%(height)d) resolution or lower."
).format(pixels=pixels, max_pixels=FILER_MAX_IMAGE_PIXELS,
width=res_x, height=res_y)
if pixels > 2 * FILER_MAX_IMAGE_PIXELS:
msg = _(
"Image size (%(pixels)d million pixels) exceeds limit of "
"%(max_pixels)d million pixels by a factor of two or more. Resize "
"image to %(width)d x %(height)d) resolution or lower."
) % dict(pixels=pixels / 1000000, max_pixels=FILER_MAX_IMAGE_PIXELS / 1000000,
width=res_x, height=res_y)
message = str(msg)
add_message(request, ERROR, message)
return JsonResponse({'error': message})

if pixels > FILER_MAX_IMAGE_PIXELS:
msg = _(
"Image size (%(pixels)d pixels) exceeds limit of %(max_pixels)d "
"pixels. Consider resizing image to (%(width)d, %(height)d) resolution "
"Image size (%(pixels)d million pixels) exceeds limit of %(max_pixels)d "
"million pixels. Consider resizing image to %(width)d x %(height)d resolution "
"or lower."
).format(pixels=pixels, max_pixels=FILER_MAX_IMAGE_PIXELS,
) % dict(pixels=pixels / 1000000, max_pixels=FILER_MAX_IMAGE_PIXELS / 1000000,
width=res_x, height=res_y)

message = str(msg)
add_message(request, WARNING, message)
# return JsonResponse({'warning': message})
file_obj.folder = folder
file_obj.save()
else:
Expand Down
30 changes: 29 additions & 1 deletion tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.contrib.admin import helpers
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Permission
from django.contrib.messages import get_messages, ERROR, WARNING
from django.forms.models import model_to_dict as model_to_dict_django
from django.http import HttpRequest, HttpResponseForbidden
from django.test import RequestFactory, TestCase
Expand Down Expand Up @@ -479,7 +480,7 @@ def test_filer_ajax_upload_file(self):
def test_filer_ajax_decompression_bomb(self):
from filer.admin import clipboardadmin
DEFAULT_MAX_IMAGE_PIXELS = clipboardadmin.FILER_MAX_IMAGE_PIXELS
clipboardadmin.FILER_MAX_IMAGE_PIXELS = 800 * 200
clipboardadmin.FILER_MAX_IMAGE_PIXELS = 800 * 200 # Triggers error
self.assertEqual(Image.objects.count(), 0)
folder = Folder.objects.create(name='foo')
with open(self.filename, 'rb') as fh:
Expand All @@ -496,6 +497,33 @@ def test_filer_ajax_decompression_bomb(self):
)
self.assertEqual(Image.objects.count(), 0)
self.assertIn("error", json.loads(response.content.decode("utf-8")))
messages = list(get_messages(response.wsgi_request))
self.assertEqual(len(messages), 1)
self.assertEqual(messages[0].level, ERROR)

clipboardadmin.FILER_MAX_IMAGE_PIXELS = 800 * 300 # Triggers warning
folder = Folder.objects.create(name='foo')
with open(self.filename, 'rb') as fh:
file_obj = django.core.files.File(fh)
url = reverse(
'admin:filer-ajax_upload',
kwargs={'folder_id': folder.pk}
) + '?filename=%s' % self.image_name
response = self.client.post(
url,
data=file_obj.read(),
content_type='image/jpeg',
**{'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'}
)
self.assertEqual(response.status_code, 200)
messages = list(get_messages(response.wsgi_request))
self.assertEqual(len(messages), 2) # One more message
self.assertEqual(messages[1].level, WARNING)
self.assertEqual(Image.objects.count(), 1)
stored_image = Image.objects.first()
self.assertEqual(stored_image.original_filename, self.image_name)
self.assertEqual(stored_image.mime_type, 'image/jpeg')

clipboardadmin.FILER_MAX_IMAGE_PIXELS = DEFAULT_MAX_IMAGE_PIXELS

def test_filer_ajax_upload_file_using_content_type(self):
Expand Down

0 comments on commit b5d2c80

Please sign in to comment.