8000 Add Support for Multi-Auth (#371) · dropbox/dropbox-sdk-python@10d92da · GitHub
[go: up one dir, main page]

Skip to content

Commit 10d92da

Browse files
authored
Add Support for Multi-Auth (#371)
1 parent fa606b8 commit 10d92da

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

dropbox/dropbox_client.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,10 @@ def __init__(self,
182182
refresh will request all available scopes for application
183183
"""
184184

185-
if not (oauth2_access_token or oauth2_refresh_token):
186-
raise BadInputException('OAuth2 access token or refresh token must be set')
185+
if not (oauth2_access_token or oauth2_refresh_token or (app_key and app_secret)):
186+
raise BadInputException(
187+
'OAuth2 access token or refresh token or app key/secret must be set'
188+
)
187189

188190
if headers is not None and not isinstance(headers, dict):
189191
raise BadInputException('Expected dict, got {}'.format(headers))
@@ -544,11 +546,12 @@ def request_json_string(self,
544546
url = self._get_route_url(fq_hostname, func_name)
545547

546548
headers = {'User-Agent': self._user_agent}
547-
if auth_type == USER_AUTH or auth_type == TEAM_AUTH:
549+
auth_types = auth_type.replace(' ', '').split(',')
550+
if (USER_AUTH in auth_types or TEAM_AUTH in auth_types) and self._oauth2_access_token:
548551
headers['Authorization'] = 'Bearer %s' % self._oauth2_access_token
549552
if self._headers:
550553
headers.update(self._headers)
551-
elif auth_type == APP_AUTH:
554+
elif APP_AUTH in auth_types:
552555
if self._app_key is None or self._app_secret is None:
553556
raise BadInputException(
554557
'Client id and client secret are required for routes with app auth')

test/integration/test_dropbox.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
from dropbox.files import (
3333
DeleteResult,
3434
ListFolderError,
35+
PathOrLink,
36+
SharedLinkFileInfo,
3537
)
3638
from dropbox.common import (
3739
PathRoot,
@@ -41,7 +43,7 @@
4143
def _value_from_env_or_die(env_name='DROPBOX_TOKEN'):
4244
value = os.environ.get(env_name)
4345
if value is None:
44-
print('Set {} environment variable to a valid token.'.format(env_name),
46+
print('Set {} environment variable to a valid value.'.format(env_name),
4547
file=sys.stderr)
4648
sys.exit(1)
4749
return value
@@ -72,7 +74,12 @@ def dbx_team_from_env():
7274
def dbx_app_auth_from_env():
7375
app_key = _value_from_env_or_die("DROPBOX_APP_KEY")
7476
app_secret = _value_from_env_or_die("DROPBOX_APP_SECRET")
75-
return Dropbox(oauth2_access_token="foo", app_key=app_key, app_secret=app_secret)
77+
return Dropbox(app_key=app_key, app_secret=app_secret)
78+
79+
80+
@pytest.fixture()
81+
def dbx_share_url_from_env():
82+
return _value_from_env_or_die("DROPBOX_SHARED_LINK")
7683

7784

7885
MALFORMED_TOKEN = 'asdf'
@@ -82,9 +89,10 @@ def dbx_app_auth_from_env():
8289
DUMMY_PAYLOAD = string.ascii_letters.encode('ascii')
8390

8491

85-
@pytest.mark.usefixtures("dbx_from_env", "refresh_dbx_from_env", "dbx_app_auth_from_env")
92+
@pytest.mark.usefixtures(
93+
"dbx_from_env", "refresh_dbx_from_env", "dbx_app_auth_from_env", "dbx_share_url_from_env"
94+
)
8695
class TestDropbox:
87-
8896
def test_default_oauth2_urls(self):
8997
flow_obj = DropboxOAuth2Flow('dummy_app_key', 'dummy_app_secret',
9098
'http://localhost/dummy', 'dummy_session', 'dbx-auth-csrf-token')
@@ -115,6 +123,19 @@ def test_bad_auth(self):
115123
invalid_token_dbx.files_list_folder('')
116124
assert cm.value.error.is_invalid_access_token()
117125

126+
def test_multi_auth(self, dbx_from_env, dbx_app_auth_from_env, dbx_share_url_from_env):
127+
# Test for user (with oa 8000 uth token)
128+
preview_result, resp = dbx_from_env.files_get_thumbnail_v2(
129+
PathOrLink.link(SharedLinkFileInfo(url=dbx_share_url_from_env))
130+
)
131+
assert resp.status_code == 200
132+
133+
# Test for app (with app key and secret)
134+
preview_result, resp = dbx_from_env.files_get_thumbnail_v2(
135+
PathOrLink.link(SharedLinkFileInfo(url=dbx_share_url_from_env))
136+
)
137+
assert resp.status_code == 200
138+
118139
def test_refresh(self, refresh_dbx_from_env):
119140
refresh_dbx_from_env.users_get_current_account()
120141

test/unit/test_dropbox_unit.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,7 @@ def test_Dropbox_with_only_refresh(self, session_instance):
291291
session=session_instance)
292292

293293
def test_Dropbox_with_only_app_key_and_secret(self, session_instance):
294-
with pytest.raises(BadInputException):
295-
Dropbox(app_key=APP_KEY, app_secret=APP_SECRET)
294+
Dropbox(app_key=APP_KEY, app_secret=APP_SECRET)
296295

297296
def test_check_refresh_with_legacy_token(self, session_instance):
298297
dbx = Dropbox(oauth2_access_token=ACCESS_TOKEN, session=session_instance)

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ passenv =
2727
DROPBOX_TEAM_TOKEN
2828
DROPBOX_TOKEN
2929
DROPBOX_WEB_HOST
30+
DROPBOX_SHARED_LINK
3031

3132
deps =
3233
pip

0 commit comments

Comments
 (0)
0