diff options
author | Marcelo Galigniana <marcelogaligniana@gmail.com> | 2022-10-16 23:02:56 -0300 |
---|---|---|
committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-10-25 16:53:52 +0200 |
commit | c0fc1b5302f5d9d82e2255fb5758321fbac34949 (patch) | |
tree | d1f3990e58c227fb5338e4ce613a2dd50e459fd8 /tests/admin_widgets | |
parent | 64e5ef1f17cd18cb8ca24f4e7107dfd28c18b378 (diff) | |
download | django-c0fc1b5302f5d9d82e2255fb5758321fbac34949.tar.gz |
Fixed #19215 -- Fixed rendering ClearableFileInput when editing with invalid files.
Thanks Michael Cardillo for the initial patch.
Diffstat (limited to 'tests/admin_widgets')
-rw-r--r-- | tests/admin_widgets/models.py | 8 | ||||
-rw-r--r-- | tests/admin_widgets/tests.py | 53 | ||||
-rw-r--r-- | tests/admin_widgets/widgetadmin.py | 2 |
3 files changed, 63 insertions, 0 deletions
diff --git a/tests/admin_widgets/models.py b/tests/admin_widgets/models.py index 15d7a3022e..26eaf5d243 100644 --- a/tests/admin_widgets/models.py +++ b/tests/admin_widgets/models.py @@ -1,8 +1,13 @@ +import tempfile import uuid from django.contrib.auth.models import User +from django.core.files.storage import FileSystemStorage from django.db import models +temp_storage_dir = tempfile.mkdtemp() +temp_storage = FileSystemStorage(temp_storage_dir) + class MyFileField(models.FileField): pass @@ -177,6 +182,9 @@ class Advisor(models.Model): class Student(models.Model): name = models.CharField(max_length=255) + photo = models.ImageField( + storage=temp_storage, upload_to="photos", blank=True, null=True + ) class Meta: ordering = ("name",) diff --git a/tests/admin_widgets/tests.py b/tests/admin_widgets/tests.py index 2b35546341..bf26c1edc1 100644 --- a/tests/admin_widgets/tests.py +++ b/tests/admin_widgets/tests.py @@ -1772,3 +1772,56 @@ class RelatedFieldWidgetSeleniumTests(AdminWidgetSeleniumTestCase): profiles = Profile.objects.all() self.assertEqual(len(profiles), 1) self.assertEqual(profiles[0].user.username, username_value) + + +class ImageFieldWidgetsSeleniumTests(AdminWidgetSeleniumTestCase): + def test_clearablefileinput_widget(self): + from selenium.webdriver.common.by import By + + self.admin_login(username="super", password="secret", login_url="/") + self.selenium.get( + self.live_server_url + reverse("admin:admin_widgets_student_add"), + ) + + photo_input_id = "id_photo" + save_and_edit_button_css_selector = "input[value='Save and continue editing']" + tests_files_folder = "%s/files" % os.getcwd() + clear_checkbox_id = "photo-clear_id" + + def _submit_and_wait(): + with self.wait_page_loaded(): + self.selenium.find_element( + By.CSS_SELECTOR, save_and_edit_button_css_selector + ).click() + + # Add a student. + title_input = self.selenium.find_element(By.ID, "id_name") + title_input.send_keys("Joe Doe") + photo_input = self.selenium.find_element(By.ID, photo_input_id) + photo_input.send_keys(f"{tests_files_folder}/test.png") + _submit_and_wait() + student = Student.objects.last() + self.assertEqual(student.name, "Joe Doe") + self.assertEqual(student.photo.name, "photos/test.png") + # Uploading non-image files is not supported by Safari with Selenium, + # so upload a broken one instead. + photo_input = self.selenium.find_element(By.ID, photo_input_id) + photo_input.send_keys(f"{tests_files_folder}/brokenimg.png") + _submit_and_wait() + self.assertEqual( + self.selenium.find_element(By.CSS_SELECTOR, ".errorlist li").text, + ( + "Upload a valid image. The file you uploaded was either not an image " + "or a corrupted image." + ), + ) + # "Currently" with "Clear" checkbox and "Change" still shown. + cover_field_row = self.selenium.find_element(By.CSS_SELECTOR, ".field-photo") + self.assertIn("Currently", cover_field_row.text) + self.assertIn("Change", cover_field_row.text) + # "Clear" box works. + self.selenium.find_element(By.ID, clear_checkbox_id).click() + _submit_and_wait() + student.refresh_from_db() + self.assertEqual(student.name, "Joe Doe") + self.assertEqual(student.photo.name, "") diff --git a/tests/admin_widgets/widgetadmin.py b/tests/admin_widgets/widgetadmin.py index d7bb62be99..deb07adecc 100644 --- a/tests/admin_widgets/widgetadmin.py +++ b/tests/admin_widgets/widgetadmin.py @@ -13,6 +13,7 @@ from .models import ( Profile, ReleaseEvent, School, + Student, User, VideoStream, ) @@ -72,5 +73,6 @@ site.register(Bee) site.register(Advisor) site.register(School, SchoolAdmin) +site.register(Student) site.register(Profile) |