From 292d569898550eab61190e7490817037c3ef0876 Mon Sep 17 00:00:00 2001 From: Kelvin Jayanoris Date: Mon, 8 Jun 2020 16:28:17 +0300 Subject: [PATCH] Add support for OpenSRP v2 user details 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. --- setup.cfg | 13 ++++++++++++- superset_patchup/oauth.py | 3 ++- tests/oauth/test_oauth.py | 15 +++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/setup.cfg b/setup.cfg index 0933766..55e3c55 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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 \ No newline at end of file +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 diff --git a/superset_patchup/oauth.py b/superset_patchup/oauth.py index 06e51e5..cf37bd9 100644 --- a/superset_patchup/oauth.py +++ b/superset_patchup/oauth.py @@ -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} diff --git a/tests/oauth/test_oauth.py b/tests/oauth/test_oauth.py index 5da1039..cc42f07 100644 --- a/tests/oauth/test_oauth.py +++ b/tests/oauth/test_oauth.py @@ -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": "noreply+mosh@example.com", "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