From 34f329ecac79d2d40df5ed83233ea8e42a5f530f Mon Sep 17 00:00:00 2001 From: nessita <124304+nessita@users.noreply.github.com> Date: Fri, 10 May 2024 08:51:47 -0300 Subject: [PATCH] Added tests for Storage's generate_filename method. --- tests/file_storage/test_generate_filename.py | 37 ++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/file_storage/test_generate_filename.py b/tests/file_storage/test_generate_filename.py index 30d76c1a78..9631705fc8 100644 --- a/tests/file_storage/test_generate_filename.py +++ b/tests/file_storage/test_generate_filename.py @@ -37,11 +37,46 @@ class AWSS3Storage(Storage): return self.prefix + self.get_valid_name(filename) +class StorageGenerateFilenameTests(SimpleTestCase): + """Tests for base Storage's generate_filename method.""" + + storage_class = Storage + + def test_valid_names(self): + storage = self.storage_class() + name = "UnTRIVĂ­AL @fil$ena#me!" + valid_name = storage.get_valid_name(name) + candidates = [ + (name, valid_name), + (f"././././././{name}", valid_name), + (f"some/path/{name}", f"some/path/{valid_name}"), + (f"some/./path/./{name}", f"some/path/{valid_name}"), + (f"././some/././path/./{name}", f"some/path/{valid_name}"), + (f".\\.\\.\\.\\.\\.\\{name}", valid_name), + (f"some\\path\\{name}", f"some/path/{valid_name}"), + (f"some\\.\\path\\.\\{name}", f"some/path/{valid_name}"), + (f".\\.\\some\\.\\.\\path\\.\\{name}", f"some/path/{valid_name}"), + ] + for name, expected in candidates: + with self.subTest(name=name): + result = storage.generate_filename(name) + self.assertEqual(result, os.path.normpath(expected)) + + +class FileSystemStorageGenerateFilenameTests(StorageGenerateFilenameTests): + + storage_class = FileSystemStorage + + class GenerateFilenameStorageTests(SimpleTestCase): def test_storage_dangerous_paths(self): candidates = [ ("/tmp/..", ".."), + ("\\tmp\\..", ".."), ("/tmp/.", "."), + ("\\tmp\\.", "."), + ("..", ".."), + (".", "."), ("", ""), ] s = FileSystemStorage() @@ -55,6 +90,8 @@ class GenerateFilenameStorageTests(SimpleTestCase): def test_storage_dangerous_paths_dir_name(self): candidates = [ + ("../path", ".."), + ("..\\path", ".."), ("tmp/../path", "tmp/.."), ("tmp\\..\\path", "tmp/.."), ("/tmp/../path", "/tmp/.."),