Fix #11716: Failure in Dynamic Image ServeView

Added back a line that was removed in a previous commit,"rendition.file.open("rb")" in serve.py.
Also, created a test in admin/tests/viewsets , test_image_presence.py that confirms that
the issue has been resolved. The test looks for a status error code 500 in the display of an image,
something that happenned when the dynamic serve view failed. If it finds it, the test fails.
The test also passes when we dont't use a dynamic serve view.
pull/11825/head
Afonso 2024-04-05 14:06:49 +01:00
rodzic 73d4b8383c
commit 80dffaf107
2 zmienionych plików z 50 dodań i 0 usunięć

Wyświetl plik

@ -0,0 +1,49 @@
import io
from urllib.parse import urljoin
import requests
from bs4 import BeautifulSoup
from django.test import TestCase
from wagtail.test.utils import WagtailTestUtils
# This test was made in order to test the Dynamic Image Serve View.
# For this test you will need to change your "mysite" in order for its home_page.html to show
# an image.
# To do that, you need to ensure that you have (it can be dynamic) serve view in the template and update
# the urls.py. Problably an addition of "image = models.ForeignKey( get_image_model(), ...)"
# field to the HomePage class will also be needed
class TestImagePresence(WagtailTestUtils, TestCase):
def test_image_presence(self):
# Make request to localhost
response = requests.get("http://localhost:8000")
# Check if the request was successful
self.assertEqual(response.status_code, 200)
# Parse the HTML content of the response
soup = BeautifulSoup(response.content, "html.parser")
# Find img elements with src="/images/"
images = soup.find_all("img", src=lambda x: x and "/images/" in x)
# Check if at least one image was found
self.assertTrue(images)
# Check for images with error (status code 500)
for img in images:
img_src = img.get("src")
full_img_url = urljoin(
"http://localhost:8000", img_src
) # Construct full image URL
# Request the image
img_response = requests.get(full_img_url, stream=True)
# Check if the response is a properly opened file
if img_response.ok:
self.assertTrue(isinstance(img_response.raw, io.IOBase))
else:
self.fail(f"Error accessing image: {img_src}")

Wyświetl plik

@ -65,6 +65,7 @@ class ServeView(View):
mime_type = willow_image.mime_type
# Serve the file
rendition.file.open("rb")
return FileResponse(rendition.file, content_type=mime_type)
def redirect(self, rendition):