Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dropbox] fix saving files #1168

Merged
merged 1 commit into from
Aug 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ django-storages CHANGELOG
Dropbox
-------

- Strip off the root path when saving files to fix saving with upgraded versions of Django (`#1168`_)
- Update ``DropBoxStorage`` constructor parameter order to be backwards compatible (`#1167`_)

.. _#1167: https://github.com/jschneier/django-storages/pull/1167
.. _#1168: https://github.com/jschneier/django-storages/pull/1168

1.13 (2022-08-05)
*****************
Expand Down
4 changes: 3 additions & 1 deletion storages/backends/dropbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ def _save(self, name, content):
else:
self._chunked_upload(content, self._full_path(name))
content.close()
return name
# .save() validates the filename isn't absolute but Dropbox requires an absolute filename.
# Work with the absolute name internally but strip it off before passing up-the-chain.
return name.lstrip(self.root_path)

def _chunked_upload(self, content, dest_path):
upload_session = self.client.files_upload_session_start(
Expand Down
14 changes: 11 additions & 3 deletions tests/test_dropbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,12 @@ def test_open(self, *args):
obj = self.storage._open('foo')
self.assertIsInstance(obj, File)

@mock.patch('dropbox.Dropbox.files_upload',
return_value='foo')
@mock.patch('dropbox.Dropbox.files_upload', return_value='foo')
@mock.patch('dropbox.Dropbox.files_get_metadata', return_value=None)
def test_save(self, files_upload, *args):
self.storage._save('foo', File(io.BytesIO(b'bar'), 'foo'))
name = self.storage.save('foo', File(io.BytesIO(b'bar'), 'foo'))
self.assertTrue(files_upload.called)
self.assertEqual(name, 'foo')

@mock.patch('dropbox.Dropbox.files_upload')
@mock.patch('dropbox.Dropbox.files_upload_session_finish')
Expand Down Expand Up @@ -192,6 +193,13 @@ def test_jailed(self, *args):
self.assertFalse(dirs)
self.assertFalse(files)

@mock.patch('dropbox.Dropbox.files_upload', return_value='foo')
@mock.patch('dropbox.Dropbox.files_get_metadata', return_value=None)
def test_saves(self, *args):
self.storage = dropbox.DropBoxStorage('foo', root_path='/bar')
name = self.storage.save('xyz', File(io.BytesIO(b'abc'), 'def'))
self.assertEqual(name, 'xyz')

def test_suspicious(self, *args):
self.storage = dropbox.DropBoxStorage('foo', root_path='/bar')
with self.assertRaises((SuspiciousFileOperation, ValueError)):
Expand Down