Skip to content

Commit eb6b4a3

Browse files
committed
test: enhance filename length validation and edge case handling
Integration of the suggestion from the community and sourcery-ai
1 parent 14a2745 commit eb6b4a3

File tree

1 file changed

+51
-25
lines changed

1 file changed

+51
-25
lines changed

tests/test_files.py

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import string
2-
32
from django.test import TestCase
4-
53
from filer.utils.files import get_valid_filename
64

75

@@ -17,17 +15,13 @@ def test_short_filename_remains_unchanged(self):
1715
def test_long_filename_is_truncated_and_suffix_appended(self):
1816
"""
1917
Test that a filename longer than the maximum allowed length is truncated and a random
20-
hexadecimal suffix is appended. The final filename must not exceed 255 characters.
18+
hexadecimal suffix of length 16 is appended, resulting in exactly 255 characters.
2119
"""
22-
# Create a filename that is much longer than 255 characters.
23-
base = "a" * 300 # 300 characters
20+
base = "a" * 300 # 300 characters base
2421
original = f"{base}.jpg"
2522
result = get_valid_filename(original)
26-
# Assert that the result is within the maximum allowed length.
27-
self.assertTrue(len(result) <= 255, "Filename exceeds 255 characters.")
28-
29-
# When truncated, the filename should end with a random hexadecimal suffix of length 16.
30-
# We check that the suffix contains only hexadecimal digits.
23+
self.assertEqual(len(result), 255, "Filename length should be exactly 255 characters.")
24+
# Verify that the last 16 characters form a valid hexadecimal string.
3125
random_suffix = result[-16:]
3226
valid_hex_chars = set(string.hexdigits)
3327
self.assertTrue(all(c in valid_hex_chars for c in random_suffix),
@@ -39,33 +33,65 @@ def test_filename_with_extension_preserved(self):
3933
"""
4034
original = "This is a test IMAGE.JPG"
4135
result = get_valid_filename(original)
42-
# Since slugification converts characters to lowercase, we expect ".jpg"
4336
self.assertTrue(result.endswith(".jpg"),
4437
"File extension was not preserved correctly.")
4538

4639
def test_unicode_characters(self):
4740
"""
48-
Test that filenames with Unicode characters are handled correctly and result in a valid filename.
41+
Test that filenames with Unicode characters are handled correctly.
4942
"""
5043
original = "fiłęñâmé_üñîçødé.jpeg"
5144
result = get_valid_filename(original)
52-
# Verify that the result ends with the expected extension and contains only allowed characters.
53-
self.assertTrue(result.endswith(".jpeg"), "File extension is not preserved for unicode filename.")
54-
# Optionally, check that no unexpected characters remain (depends on your slugify behavior).
45+
self.assertTrue(result.endswith(".jpeg"),
46+
"File extension is not preserved for unicode filename.")
47+
# Verify that the resulting filename contains only allowed characters.
48+
allowed_chars = set(string.ascii_lowercase + string.digits + "._-")
5549
for char in result:
56-
# Allow only alphanumeric characters, underscores, dashes, and the dot.
57-
self.assertIn(char, string.ascii_lowercase + string.digits + "._-",
50+
self.assertIn(char, allowed_chars,
5851
f"Unexpected character '{char}' found in filename.")
5952

6053
def test_edge_case_exact_length(self):
6154
"""
62-
Test an edge case where the filename is exactly the maximum allowed length.
63-
The function should leave such a filename unchanged.
55+
Test that a filename exactly at the maximum allowed length remains unchanged.
56+
"""
57+
extension = ".png"
58+
base_length = 255 - len(extension)
59+
base = "b" * base_length
60+
original = f"{base}{extension}"
61+
result = get_valid_filename(original)
62+
self.assertEqual(len(result), 255,
63+
"Filename with length exactly 255 should remain unchanged.")
64+
self.assertEqual(result, original,
65+
"Filename with length exactly 255 should not be modified.")
66+
67+
def test_edge_case_filenames(self):
68+
"""
69+
Test filenames at various boundary conditions to ensure correct behavior.
6470
"""
65-
# Create a filename that is exactly 255 characters long.
66-
base = "b" * 251 # 251 characters for base
67-
original = f"{base}.png" # This may reach exactly or slightly above 255 depending on slugification
71+
max_length = 255
72+
random_suffix_length = 16
73+
extension = ".jpg"
74+
75+
# Test case 1: Filename with length exactly max_length - 1.
76+
base_length = max_length - 1 - len(extension)
77+
base = "c" * base_length
78+
original = f"{base}{extension}"
79+
result = get_valid_filename(original)
80+
self.assertEqual(result, original,
81+
"Filename with length max_length-1 should remain unchanged.")
82+
83+
# Test case 2: Filename with length exactly equal to max_length - random_suffix_length.
84+
base_length = max_length - random_suffix_length - len(extension)
85+
base = "d" * base_length
86+
original = f"{base}{extension}"
87+
result = get_valid_filename(original)
88+
self.assertEqual(result, original,
89+
"Filename with length equal to max_length - random_suffix_length should remain unchanged.")
90+
91+
# Test case 3: Filename with length exactly equal to max_length - random_suffix_length - 1.
92+
base_length = max_length - random_suffix_length - 1 - len(extension)
93+
base = "e" * base_length
94+
original = f"{base}{extension}"
6895
result = get_valid_filename(original)
69-
# We check that the final result does not exceed 255 characters.
70-
self.assertTrue(len(result) <= 255,
71-
"Edge case filename exceeds the maximum allowed length.")
96+
self.assertEqual(result, original,
97+
"Filename with length equal to max_length - random_suffix_length - 1 should remain unchanged.")

0 commit comments

Comments
 (0)