Repository: jschneier/django-storages

Issue:
Is it correct in the `get_available_overwrite_name` function?
Hi,

Please tell me what the following code.

When `name`'s length equals `max_length` in the `get_available_overwrite_name`, `get_available_overwrite_name` returns overwritten `name`.
The `name` must be less than or equal to `max_length` isn't it?

https://github.com/jschneier/django-storages/blob/master/storages/utils.py#L105


Regards,

Chihiro


Discussion:

Resolving patch:
diff --git a/storages/utils.py b/storages/utils.py
--- a/storages/utils.py
+++ b/storages/utils.py
@@ -102,7 +102,7 @@ def lookup_env(names):
 
 
 def get_available_overwrite_name(name, max_length):
-    if max_length is None or len(name) < max_length:
+    if max_length is None or len(name) <= max_length:
         return name
 
     # Adapted from Django


Test patch:
diff --git a/tests/test_gcloud.py b/tests/test_gcloud.py
--- a/tests/test_gcloud.py
+++ b/tests/test_gcloud.py
@@ -8,9 +8,7 @@
 import mimetypes
 from datetime import datetime, timedelta
 
-from django.core.exceptions import (
-    ImproperlyConfigured, SuspiciousFileOperation,
-)
+from django.core.exceptions import ImproperlyConfigured
 from django.core.files.base import ContentFile
 from django.test import TestCase
 from django.utils import timezone
@@ -376,14 +374,6 @@ def test_get_available_name_unicode(self):
         filename = 'ủⓝï℅ⅆℇ.txt'
         self.assertEqual(self.storage.get_available_name(filename), filename)
 
-    def test_get_available_name_overwrite_maxlength(self):
-        self.storage.file_overwrite = True
-
-        self.assertEqual(self.storage.get_available_name('test/foo.txt', 11), 'test/fo.txt')
-        self.assertEqual(self.storage.get_available_name('test_a/foobar.txt', None), 'test_a/foobar.txt')
-        with self.assertRaises(SuspiciousFileOperation):
-            self.storage.get_available_name('test_a/foobar.txt', 10)
-
     def test_cache_control(self):
         data = 'This is some test content.'
         filename = 'cache_control_file.txt'
diff --git a/tests/test_utils.py b/tests/test_utils.py
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -1,9 +1,11 @@
 import datetime
 
 from django.conf import settings
+from django.core.exceptions import SuspiciousFileOperation
 from django.test import TestCase
 
 from storages import utils
+from storages.utils import get_available_overwrite_name as gaon
 
 
 class SettingTest(TestCase):
@@ -108,3 +110,26 @@ def test_join_nothing(self):
     def test_with_base_url_join_nothing(self):
         path = utils.safe_join('base_url')
         self.assertEqual(path, 'base_url/')
+
+
+class TestGetAvailableOverwriteName(TestCase):
+    def test_maxlength_is_none(self):
+        name = 'superlong/file/with/path.txt'
+        self.assertEqual(gaon(name, None), name)
+
+    def test_maxlength_equals_name(self):
+        name = 'parent/child.txt'
+        self.assertEqual(gaon(name, len(name)), name)
+
+    def test_maxlength_is_greater_than_name(self):
+        name = 'parent/child.txt'
+        self.assertEqual(gaon(name, len(name) + 1), name)
+
+    def test_maxlength_less_than_name(self):
+        name = 'parent/child.txt'
+        self.assertEqual(gaon(name, len(name) - 1), 'parent/chil.txt')
+
+    def test_truncates_away_filename_raises(self):
+        name = 'parent/child.txt'
+        with self.assertRaises(SuspiciousFileOperation):
+            gaon(name, len(name) - 5)