Skip to content

Commit

Permalink
Replace final deprecated dropbox methods, fix the tests, upgrade the …
Browse files Browse the repository at this point in the history
…required version
  • Loading branch information
jschneier committed Jun 20, 2017
1 parent a1b2fc8 commit f819489
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 24 deletions.
2 changes: 1 addition & 1 deletion requirements-tests.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
boto3>=1.2.3
boto>=2.32.0
dropbox>=7.21
Django>=1.8
dropbox>=3.24
flake8
google-cloud>=0.25.0
mock
Expand Down
6 changes: 3 additions & 3 deletions storages/backends/dropbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def __init__(self, name, storage):
@property
def file(self):
if not hasattr(self, '_file'):
response = self._storage.client.get_file(self.name)
response = self._storage.client.files_download(self.name)
self._file = SpooledTemporaryFile()
copyfileobj(response, self._file)
self._file.seek(0)
Expand Down Expand Up @@ -101,8 +101,8 @@ def accessed_time(self, name):
return acc_time

def url(self, name):
media = self.client.media(self._full_path(name))
return media['url']
media = self.client.files_get_temporary_link(self._full_path(name))
return media['link']

def _open(self, name, mode='rb'):
remote_file = DropBoxFile(self._full_path(name), self)
Expand Down
34 changes: 14 additions & 20 deletions tests/test_dropbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from django.test import TestCase
from django.core.files.base import File, ContentFile
from django.core.exceptions import ImproperlyConfigured, \
SuspiciousFileOperation
SuspiciousFileOperation

from storages.backends import dropbox

Expand Down Expand Up @@ -51,7 +51,7 @@
'thumb_exists': False
}
FILE_MEDIA_FIXTURE = {
'url': 'https://dl.dropboxusercontent.com/1/view/foo',
'link': 'https://dl.dropboxusercontent.com/1/view/foo',
'expires': 'Fri, 16 Sep 2011 01:01:25 +0000',
}

Expand All @@ -67,24 +67,24 @@ def test_no_access_token(self, *args):
with self.assertRaises(ImproperlyConfigured):
dropbox.DropBoxStorage(None)

@mock.patch('dropbox.client.DropboxClient.file_delete',
@mock.patch('dropbox.Dropbox.files_delete',
return_value=FILE_FIXTURE)
def test_delete(self, *args):
self.storage.delete('foo')

@mock.patch('dropbox.client.DropboxClient.metadata',
@mock.patch('dropbox.Dropbox.files_get_metadata',
return_value=[FILE_FIXTURE])
def test_exists(self, *args):
exists = self.storage.exists('foo')
self.assertTrue(exists)

@mock.patch('dropbox.client.DropboxClient.metadata',
@mock.patch('dropbox.Dropbox.files_get_metadata',
return_value=[])
def test_not_exists(self, *args):
exists = self.storage.exists('bar')
self.assertFalse(exists)

@mock.patch('dropbox.client.DropboxClient.metadata',
@mock.patch('dropbox.Dropbox.files_get_metadata',
return_value=FILES_FIXTURE)
def test_listdir(self, *args):
dirs, files = self.storage.listdir('/')
Expand All @@ -93,19 +93,19 @@ def test_listdir(self, *args):
self.assertEqual(dirs[0], 'bar')
self.assertEqual(files[0], 'foo.txt')

@mock.patch('dropbox.client.DropboxClient.metadata',
@mock.patch('dropbox.Dropbox.files_get_metadata',
return_value=FILE_FIXTURE)
def test_size(self, *args):
size = self.storage.size('foo')
self.assertEqual(size, FILE_FIXTURE['bytes'])

@mock.patch('dropbox.client.DropboxClient.metadata',
@mock.patch('dropbox.Dropbox.files_get_metadata',
return_value=FILE_FIXTURE)
def test_modified_time(self, *args):
mtime = self.storage.modified_time('foo')
self.assertEqual(mtime, FILE_DATE)

@mock.patch('dropbox.client.DropboxClient.metadata',
@mock.patch('dropbox.Dropbox.files_get_metadata',
return_value=FILE_FIXTURE)
def test_accessed_time(self, *args):
mtime = self.storage.accessed_time('foo')
Expand All @@ -115,16 +115,16 @@ def test_open(self, *args):
obj = self.storage._open('foo')
self.assertIsInstance(obj, File)

@mock.patch('dropbox.client.DropboxClient.put_file',
@mock.patch('dropbox.Dropbox.files_upload',
return_value='foo')
def test_save(self, *args):
self.storage._save('foo', b'bar')

@mock.patch('dropbox.client.DropboxClient.media',
@mock.patch('dropbox.Dropbox.files_get_temporary_link',
return_value=FILE_MEDIA_FIXTURE)
def test_url(self, *args):
url = self.storage.url('foo')
self.assertEqual(url, FILE_MEDIA_FIXTURE['url'])
self.assertEqual(url, FILE_MEDIA_FIXTURE['link'])

def test_formats(self, *args):
self.storage = dropbox.DropBoxStorage('foo')
Expand All @@ -136,24 +136,18 @@ def test_formats(self, *args):


class DropBoxFileTest(TestCase):
@mock.patch('dropbox.client._OAUTH2_ACCESS_TOKEN_PATTERN',
re.compile(r'.*'))
@mock.patch('dropbox.client.DropboxOAuth2Session')
def setUp(self, *args):
self.storage = dropbox.DropBoxStorage('foo')
self.file = dropbox.DropBoxFile('/foo.txt', self.storage)

@mock.patch('dropbox.client.DropboxClient.get_file',
@mock.patch('dropbox.Dropbox.files_download',
return_value=ContentFile(b'bar'))
def test_read(self, *args):
file = self.storage._open(b'foo')
self.assertEqual(file.read(), b'bar')


@mock.patch('dropbox.client._OAUTH2_ACCESS_TOKEN_PATTERN',
re.compile(r'.*'))
@mock.patch('dropbox.client.DropboxOAuth2Session')
@mock.patch('dropbox.client.DropboxClient.metadata',
@mock.patch('dropbox.Dropbox.files_get_metadata',
return_value={'contents': []})
class DropBoxRootPathTest(TestCase):
def test_jailed(self, *args):
Expand Down

0 comments on commit f819489

Please sign in to comment.