From 3a38ce08fdee43b59781c5a15ce2ae54818d5738 Mon Sep 17 00:00:00 2001 From: Sage Abdullah Date: Fri, 21 Jul 2023 11:36:25 +0100 Subject: [PATCH] Ensure file names in tests are unique Prevent files from being renamed with a unique suffix when running tests in parallel --- wagtail/documents/tests/test_models.py | 21 ++++++++++------- wagtail/documents/tests/test_serializers.py | 12 +++++++--- wagtail/documents/tests/test_views.py | 26 ++++++++++++++++----- wagtail/images/tests/test_models.py | 10 ++++---- 4 files changed, 47 insertions(+), 22 deletions(-) diff --git a/wagtail/documents/tests/test_models.py b/wagtail/documents/tests/test_models.py index cd31489b1f29..e7a88ab8d91a 100644 --- a/wagtail/documents/tests/test_models.py +++ b/wagtail/documents/tests/test_models.py @@ -103,22 +103,27 @@ def test_user_cant_edit(self): class TestDocumentFilenameProperties(TestCase): def setUp(self): self.document = models.Document(title="Test document") - self.document.file.save("example.doc", ContentFile("A boring example document")) + self.document.file.save( + "sample_name.doc", + ContentFile("A boring example document"), + ) self.pdf_document = models.Document(title="Test document") self.pdf_document.file.save( - "example.pdf", ContentFile("A boring example document") + "sample_name.pdf", + ContentFile("A boring example document"), ) self.extensionless_document = models.Document(title="Test document") self.extensionless_document.file.save( - "example", ContentFile("A boring example document") + "sample_name", + ContentFile("A boring example document"), ) def test_filename(self): - self.assertEqual("example.doc", self.document.filename) - self.assertEqual("example.pdf", self.pdf_document.filename) - self.assertEqual("example", self.extensionless_document.filename) + self.assertEqual("sample_name.doc", self.document.filename) + self.assertEqual("sample_name.pdf", self.pdf_document.filename) + self.assertEqual("sample_name", self.extensionless_document.filename) def test_file_extension(self): self.assertEqual("doc", self.document.file_extension) @@ -147,12 +152,12 @@ def test_file_hash(self): def test_content_disposition(self): self.assertEqual( - """attachment; filename=example.doc; filename*=UTF-8''example.doc""", + """attachment; filename=sample_name.doc; filename*=UTF-8''sample_name.doc""", self.document.content_disposition, ) self.assertEqual("inline", self.pdf_document.content_disposition) self.assertEqual( - """attachment; filename=example; filename*=UTF-8''example""", + """attachment; filename=sample_name; filename*=UTF-8''sample_name""", self.extensionless_document.content_disposition, ) diff --git a/wagtail/documents/tests/test_serializers.py b/wagtail/documents/tests/test_serializers.py index cf2e9b48fb2a..532426b3cf16 100644 --- a/wagtail/documents/tests/test_serializers.py +++ b/wagtail/documents/tests/test_serializers.py @@ -14,7 +14,10 @@ class TestCorrectDownloadUrlSerialization(TestCase): def setUp(self): self.document = models.Document(title="Test document", file_hash="123456") - self.document.file.save("example.doc", ContentFile("A boring example document")) + self.document.file.save( + "serialization.doc", + ContentFile("A boring example document"), + ) def tearDown(self): # delete the FieldFile directly because the TestCase does not commit @@ -38,7 +41,9 @@ def test_serializer_wagtaildocs_serve_redirect(self): meta = data["meta"] self.assertIn("download_url", meta) download_url = meta["download_url"] - expected_url = "http://example.com/documents/%d/example.doc" % self.document.pk + expected_url = ( + f"http://example.com/documents/{self.document.pk}/serialization.doc" + ) self.assertEqual(download_url, expected_url) @override_settings( @@ -55,5 +60,6 @@ def test_serializer_wagtaildocs_serve_direct(self): self.assertIn("download_url", meta) download_url = meta["download_url"] self.assertEqual( - download_url, "http://remotestorage.com/media/documents/example.doc" + download_url, + "http://remotestorage.com/media/documents/serialization.doc", ) diff --git a/wagtail/documents/tests/test_views.py b/wagtail/documents/tests/test_views.py index 3a1097fd71d1..40a04d33f6d7 100644 --- a/wagtail/documents/tests/test_views.py +++ b/wagtail/documents/tests/test_views.py @@ -17,10 +17,12 @@ class TestServeView(TestCase): def setUp(self): self.document = models.Document(title="Test document", file_hash="123456") - self.document.file.save("example.doc", ContentFile("A boring example document")) + self.document.file.save( + "serve_view.doc", ContentFile("A boring example document") + ) self.pdf_document = models.Document(title="Test document", file_hash="123456") self.pdf_document.file.save( - "example.pdf", ContentFile("A boring example document") + "serve_view.pdf", ContentFile("A boring example document") ) def tearDown(self): @@ -179,7 +181,10 @@ def clear_sendfile_cache(self): class TestServeViewWithRedirect(TestCase): def setUp(self): self.document = models.Document(title="Test document") - self.document.file.save("example.doc", ContentFile("A boring example document")) + self.document.file.save( + "serve_view_with_redirect.doc", + ContentFile("A boring example document"), + ) self.serve_view_url = reverse( "wagtaildocs_serve", args=(self.document.id, self.document.filename) ) @@ -208,7 +213,10 @@ def test_redirect(self): class TestDirectDocumentUrls(TestCase): def setUp(self): self.document = models.Document(title="Test document") - self.document.file.save("example.doc", ContentFile("A boring example document")) + self.document.file.save( + "direct_document_urls.doc", + ContentFile("A boring example document"), + ) def tearDown(self): self.document.delete() @@ -244,7 +252,10 @@ class TestServeWithExternalStorage(TestCase): def setUp(self): self.document = models.Document(title="Test document") - self.document.file.save("example.doc", ContentFile("A boring example document")) + self.document.file.save( + "serve_with_external_storage.doc", + ContentFile("A boring example document"), + ) self.serve_view_url = reverse( "wagtaildocs_serve", args=(self.document.id, self.document.filename) ) @@ -274,7 +285,10 @@ def setUp(self): raise unittest.SkipTest("django-sendfile not installed") self.document = models.Document(title="Test document") - self.document.file.save("example.doc", ContentFile("A boring example document")) + self.document.file.save( + "serve_view_with_sendfile.doc", + ContentFile("A boring example document"), + ) def tearDown(self): # delete the FieldFile directly because the TestCase does not commit diff --git a/wagtail/images/tests/test_models.py b/wagtail/images/tests/test_models.py index e5b1f9204c20..0126768fedba 100644 --- a/wagtail/images/tests/test_models.py +++ b/wagtail/images/tests/test_models.py @@ -785,7 +785,7 @@ def test_issue_573(self): image = Image.objects.create( title="Test image", file=get_test_image_file( - "thisisaverylongfilename-abcdefghijklmnopqrstuvwxyz-supercalifragilisticexpialidocious.png" + "thisisaverylongfilename-cdefghijklmnopqrstuvwxyzab-supercalifragilisticexpialidocious.png" ), focal_point_x=1000, focal_point_y=1000, @@ -931,14 +931,14 @@ def test_filename_reduction_no_ext(self): image = Image.objects.create( title="Test image", file=get_test_image_file( - "thisisaverylongfilename-abcdefghijklmnopqrstuvwxyz-supercalifragilisticexpialidocioussuperlong" + "thisisaverylongfilename-defghijklmnopqrstuvwxyzabc-supercalifragilisticexpialidocioussuperlong" ), ) # Saving file will result in infinite loop when bug is present image.save() self.assertEqual( - "original_images/thisisaverylongfilename-abcdefghijklmnopqrstuvwxyz-supercalifragilisticexpiali", + "original_images/thisisaverylongfilename-defghijklmnopqrstuvwxyzabc-supercalifragilisticexpiali", image.file.name, ) @@ -948,13 +948,13 @@ def test_filename_reduction_ext(self): image = Image.objects.create( title="Test image", file=get_test_image_file( - "thisisaverylongfilename-abcdefghijklmnopqrstuvwxyz-supercalifragilisticexpialidocioussuperlong.png" + "thisisaverylongfilename-efghijklmnopqrstuvwxyzabcd-supercalifragilisticexpialidocioussuperlong.png" ), ) image.save() self.assertEqual( - "original_images/thisisaverylongfilename-abcdefghijklmnopqrstuvwxyz-supercalifragilisticexp.png", + "original_images/thisisaverylongfilename-efghijklmnopqrstuvwxyzabcd-supercalifragilisticexp.png", image.file.name, )