Compare commits

...
2 changed files with 38 additions and 1 deletions

No files matched your search

@@ -1,8 +1,10 @@
import json
import os
from io import BytesIO
from pathlib import Path
from unittest.mock import patch
from django.conf import settings
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import override_settings
from PIL import Image
@@ -507,6 +509,39 @@ class TestApiAppConfig(DirectoriesMixin, APITestCase):
self.assertEqual(stored_text, {})
def test_api_accepts_a_logo_too_large_to_hold_in_memory(self) -> None:
"""
GIVEN:
- A PNG logo larger than Django's in memory upload limit, which is
stored in a temporary file while it is received
WHEN:
- Uploaded via PATCH to app config
THEN:
- The upload is accepted and the stored logo is a valid image
"""
# Random pixels do not compress, so this is a few megabytes as a PNG
image = Image.frombytes("RGB", (1100, 1100), os.urandom(1100 * 1100 * 3))
logo = BytesIO()
image.save(logo, format="PNG")
assert logo.getbuffer().nbytes > settings.FILE_UPLOAD_MAX_MEMORY_SIZE
response = self.client.patch(
f"{self.ENDPOINT}1/",
{
"app_logo": SimpleUploadedFile(
name="large-logo.png",
content=logo.getvalue(),
content_type="image/png",
),
},
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
config = ApplicationConfiguration.objects.first()
assert config is not None
with Image.open(config.app_logo.path) as stored_logo:
self.assertEqual(stored_logo.size, (1100, 1100))
def test_api_accepts_valid_gif_logo(self) -> None:
"""
GIVEN:
+3 -1
View File
@@ -303,7 +303,9 @@ class ApplicationConfigurationSerializer(
return InMemoryUploadedFile(
file=data,
field_name=file.field_name,
# An upload past the in memory limit is a temporary file, which has no
# field name of its own
field_name="app_logo",
name=file.name,
content_type=file.content_type,
size=data.getbuffer().nbytes,