Skip to content

Commit

Permalink
Ensure file names in tests are unique
Browse files Browse the repository at this point in the history
Prevent files from being renamed with a unique suffix when running tests in parallel
  • Loading branch information
laymonage committed Aug 2, 2023
1 parent 19123ce commit 3a38ce0
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 22 deletions.
21 changes: 13 additions & 8 deletions wagtail/documents/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
)

Expand Down
12 changes: 9 additions & 3 deletions wagtail/documents/tests/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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",
)
26 changes: 20 additions & 6 deletions wagtail/documents/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
)
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions wagtail/images/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
)

Expand All @@ -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,
)

Expand Down

0 comments on commit 3a38ce0

Please sign in to comment.