Skip to content

Commit

Permalink
Add support for OpenSRP v2 user details
Browse files Browse the repository at this point in the history
OpenSRP v2 returns the username as "username" instead of the olf
"userName".  This commit adds support for OpenSRP v2, while keeping
support for the old version.

Finally, this PR adds additional settings for isort, flake8 and pycodestyle.
  • Loading branch information
moshthepitt committed Jun 8, 2020
1 parent becbdda commit 292d569
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
13 changes: 12 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,15 @@ known_first_party = superset_patchup
known_flask = flask
known_flask_appbuilder = flask_appbuilder
known_superset = superset
sections = FUTURE,STDLIB,FLASK,FLASK_APPBUILDER,SUPERSET,THIRDPARTY,FIRST_PARTY,LOCALFOLDER
sections = FUTURE,STDLIB,FLASK,FLASK_APPBUILDER,SUPERSET,THIRDPARTY,FIRST_PARTY,LOCALFOLDER
multi_line_output = 3
include_trailing_comma = True
force_grid_wrap = 0
use_parentheses = True
line_length = 88

[flake8]
max-line-length=90

[pycodestyle]
max-line-length=90
3 changes: 2 additions & 1 deletion superset_patchup/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,8 @@ def oauth_user_info(self, provider, response=None):
if is_valid_provider(provider, "OpenSRP"):
user_object = (self.appbuilder.sm.oauth_remotes[provider].get(
"user-details").data)
username = user_object["userName"]

username = user_object.get("username") or user_object.get('userName')

result = {"username": username}

Expand Down
15 changes: 15 additions & 0 deletions tests/oauth/test_oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,21 @@ def test_oauth_user_info_opensrp_provider(self):
request_mock.assert_called_once_with("user-details")
assert user_info2 == result_info2

# Sample data returned OpenSRP v2
data3 = {"username": "mosh", "roles": ["Privilege Level: Full"]}

# Expected result
result_info3 = {"email": "[email protected]", "username": "mosh"}

appbuilder3 = MagicMock()
user_mock3 = MagicMock()
user_mock3.data = data3
appbuilder3.sm.oauth_remotes["OpenSRP"].get = MagicMock(
side_effect=[user_mock3])
csm3 = CustomSecurityManager(appbuilder=appbuilder3)
user_info3 = csm3.oauth_user_info(provider="OpenSRP")
assert user_info3 == result_info3

def test_oauth_user_info_no_provider(self):
"""
Test that when no provider is provided
Expand Down

0 comments on commit 292d569

Please sign in to comment.