Skip to content

Commit 73133df

Browse files
committed
Added one extra check and test for an unlikely error
1 parent cd43466 commit 73133df

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

invenio_oauthclient/contrib/globus.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,15 @@ def get_user_id(remote, email):
131131
132132
See the docs here for v2/api/identities
133133
https://docs.globus.org/api/auth/reference/"""
134-
url = '{}?usernames={}'.format(GLOBUS_USER_ID_URL, email)
135-
user_id = get_dict_from_response(remote.get(url))
136-
return user_id['identities'][0]['id']
134+
try:
135+
url = '{}?usernames={}'.format(GLOBUS_USER_ID_URL, email)
136+
user_id = get_dict_from_response(remote.get(url))
137+
return user_id['identities'][0]['id']
138+
except KeyError:
139+
# If we got here the response was successful but the data was invalid.
140+
# It's likely the URL is wrong but possible the API has changed.
141+
raise OAuthResponseError('Failed to fetch user id, likely server '
142+
'mis-configuration', None, remote)
137143

138144

139145
def account_info(remote, resp):

tests/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def base_app(request):
9494
DEBUG=False,
9595
SECRET_KEY='TEST',
9696
SECURITY_DEPRECATED_PASSWORD_SCHEMES=[],
97+
SQLALCHEMY_TRACK_MODIFICATIONS=False,
9798
SECURITY_PASSWORD_HASH='plaintext',
9899
SECURITY_PASSWORD_SCHEMES=['plaintext'],
99100
)

tests/test_contrib_globus.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,16 +241,40 @@ class MockResponse:
241241
remote_app='globus'))
242242
assert resp.status_code == 302
243243

244-
example_info, example_token, example_account_id = example_globus
244+
_, example_token, _ = example_globus
245245
mock_response(app.extensions['oauthlib.client'], 'globus',
246246
example_token)
247-
example_info.update(example_account_id)
248247
oauth_resp = OAuthResponse(resp=MockResponse(), content=None,
249248
content_type='application/json')
250249
mock_remote_get(ioc, 'globus', oauth_resp)
251250

252251
with pytest.raises(OAuthResponseError):
253-
resp = c.get(
252+
c.get(
253+
url_for('invenio_oauthclient.authorized',
254+
remote_app='globus', code='test',
255+
state=_get_state()))
256+
257+
258+
def test_invalid_user_id_response(app, example_globus):
259+
with app.test_client() as c:
260+
261+
# User login with email 'info'
262+
ioc = app.extensions['oauthlib.client']
263+
264+
# Ensure remote apps have been loaded (due to before first request)
265+
resp = c.get(url_for('invenio_oauthclient.login',
266+
remote_app='globus'))
267+
assert resp.status_code == 302
268+
269+
example_info, example_token, _ = example_globus
270+
mock_response(app.extensions['oauthlib.client'], 'globus',
271+
example_token)
272+
oauth_resp = OAuthResponse(resp=None, content=json.dumps(example_info),
273+
content_type='application/json')
274+
mock_remote_get(ioc, 'globus', oauth_resp)
275+
276+
with pytest.raises(OAuthResponseError):
277+
c.get(
254278
url_for('invenio_oauthclient.authorized',
255279
remote_app='globus', code='test',
256280
state=_get_state()))

0 commit comments

Comments
 (0)