#35326: OverwritingStorageTests fail if a TemporaryUploadedFile is used
------------------------------------------------+------------------------
               Reporter:  bcail                 |          Owner:  nobody
                   Type:  Bug                   |         Status:  new
              Component:  File uploads/storage  |        Version:  dev
               Severity:  Normal                |       Keywords:
           Triage Stage:  Unreviewed            |      Has patch:  0
    Needs documentation:  0                     |    Needs tests:  0
Patch needs improvement:  0                     |  Easy pickings:  0
                  UI/UX:  0                     |
------------------------------------------------+------------------------
 [https://code.djangoproject.com/ticket/28144 Ticket #28144] added the
 option of using custom flags on a storage object to allow overwriting
 files in storage. However, this doesn't seem to work for temporary
 uploaded files, since the
 
[https://github.com/django/django/blob/main/django/core/files/storage/filesystem.py#L100
 alternate path] is taken in the _save method.

 Here is an example test that fails for me - it loops forever:
 {{{
 diff --git a/tests/file_storage/tests.py b/tests/file_storage/tests.py
 index 420314573d..d404300708 100644
 --- a/tests/file_storage/tests.py
 +++ b/tests/file_storage/tests.py
 @@ -648,6 +648,34 @@ class OverwritingStorageTests(FileStorageTests):
          finally:
              self.storage.delete(name)

 +    def test_save_overwrite_behavior_temp_file(self):
 +        """Saving to same file name twice overwrites the first file."""
 +        name = "test.file"
 +        self.assertFalse(self.storage.exists(name))
 +        content_1 = b"content one"
 +        content_2 = b"second content"
 +        f_1 = TemporaryUploadedFile('tmp1', 'text/plain', 11, 'utf8')
 +        f_1.write(content_1)
 +        f_1.seek(0)
 +        f_2 = TemporaryUploadedFile('tmp2', 'text/plain', 14, 'utf8')
 +        f_2.write(content_2)
 +        f_2.seek(0)
 +        stored_name_1 = self.storage.save(name, f_1)
 +        try:
 +            self.assertEqual(stored_name_1, name)
 +            self.assertTrue(self.storage.exists(name))
 +            self.assertTrue(os.path.exists(os.path.join(self.temp_dir,
 name)))
 +            with self.storage.open(name) as fp:
 +                self.assertEqual(fp.read(), content_1)
 +            stored_name_2 = self.storage.save(name, f_2)
 +            self.assertEqual(stored_name_2, name)
 +            self.assertTrue(self.storage.exists(name))
 +            self.assertTrue(os.path.exists(os.path.join(self.temp_dir,
 name)))
 +            with self.storage.open(name) as fp:
 +                self.assertEqual(fp.read(), content_2)
 +        finally:
 +            self.storage.delete(name)
 }}}
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35326>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018e66e0d43e-545f270f-0301-4201-a26b-93a0f009c162-000000%40eu-central-1.amazonses.com.

Reply via email to