From ef12245351b243a705fa4ce4213904cd7c5f4883 Mon Sep 17 00:00:00 2001 From: Joshua Munn Date: Tue, 3 Jan 2023 17:31:23 +0000 Subject: [PATCH] Add more SVG tests --- wagtail/images/tests/test_admin_views.py | 21 ++++++++++- wagtail/images/tests/test_image_operations.py | 35 +++++++++++++++++++ wagtail/images/tests/test_templatetags.py | 6 ++++ wagtail/test/settings.py | 2 ++ 4 files changed, 63 insertions(+), 1 deletion(-) diff --git a/wagtail/images/tests/test_admin_views.py b/wagtail/images/tests/test_admin_views.py index f2bb343268..819c68e550 100644 --- a/wagtail/images/tests/test_admin_views.py +++ b/wagtail/images/tests/test_admin_views.py @@ -31,7 +31,7 @@ from wagtail.test.testapp.models import ( ) from wagtail.test.utils import WagtailTestUtils -from .utils import Image, get_test_image_file +from .utils import Image, get_test_image_file, get_test_image_file_svg # Get the chars that Django considers safe to leave unescaped in a URL urlquote_safechars = RFC3986_SUBDELIMS + str("/~:@") @@ -524,6 +524,25 @@ class TestImageAddView(WagtailTestUtils, TestCase): root_collection = Collection.get_first_root_node() self.assertEqual(image.collection, root_collection) + def test_add_svg(self): + response = self.post( + { + "title": "Test image", + "file": SimpleUploadedFile( + "test.svg", + get_test_image_file_svg().file.getvalue(), + content_type="text/html", + ), + } + ) + + # Should redirect back to index + self.assertRedirects(response, reverse("wagtailimages:index")) + + # Check that the image was created + images = Image.objects.filter(title="Test image") + self.assertEqual(images.count(), 1) + @override_settings( DEFAULT_FILE_STORAGE="wagtail.test.dummy_external_storage.DummyExternalStorage" ) diff --git a/wagtail/images/tests/test_image_operations.py b/wagtail/images/tests/test_image_operations.py index 78c0bfe9f3..7dc7f34fe4 100644 --- a/wagtail/images/tests/test_image_operations.py +++ b/wagtail/images/tests/test_image_operations.py @@ -913,3 +913,38 @@ class TestWebpFormatConversion(TestCase): out = fil.run(image, BytesIO()) self.assertEqual(out.format_name, "webp") + + +class TestCheckSize(TestCase): + def test_check_size_when_floats_allowed(self): + sizes = [ + (1.5, 1.5), + (1.5, 1), + (1, 1.5), + (1, 1), + ] + for size in sizes: + with self.subTest(size=size): + self.assertIsNone( + image_operations.ImageTransform._check_size( + size, allow_floating_point=True + ) + ) + + def test_check_size_when_floats_forbidden(self): + fail_sizes = [ + (1.5, 1.5), + (1.5, 1), + (1, 1.5), + ] + for size in fail_sizes: + with self.subTest(size=size): + with self.assertRaises(TypeError): + image_operations.ImageTransform._check_size( + size, allow_floating_point=False + ) + self.assertIsNone( + image_operations.ImageTransform._check_size( + (1, 1), allow_floating_point=False + ) + ) diff --git a/wagtail/images/tests/test_templatetags.py b/wagtail/images/tests/test_templatetags.py index f1ba7a8afe..57b9c03653 100644 --- a/wagtail/images/tests/test_templatetags.py +++ b/wagtail/images/tests/test_templatetags.py @@ -88,6 +88,12 @@ class ImageNodeTestCase(TestCase): ["fill-400x400", "format-webp", "webpquality-50"], "fill-400x400|format-webp|webpquality-50", ), + (self.svg_image, ["max-400x400"], "max-400x400"), + (self.svg_image, ["min-400x400"], "min-400x400"), + (self.svg_image, ["width-300"], "width-300"), + (self.svg_image, ["height-300"], "height-300"), + (self.svg_image, ["scale-50"], "scale-50"), + (self.svg_image, ["fill-400x400"], "fill-400x400"), ] for image, filter_specs, expected in params: with self.subTest(img=image, filter_specs=filter_specs, expected=expected): diff --git a/wagtail/test/settings.py b/wagtail/test/settings.py index db375c9983..1efcdc67ae 100644 --- a/wagtail/test/settings.py +++ b/wagtail/test/settings.py @@ -257,3 +257,5 @@ MESSAGE_TAGS = { message_constants.WARNING: "my-custom-tag", message_constants.ERROR: "my-custom-tag", } + +WAGTAILIMAGES_ALLOW_SVG = True