This is an automated email from the ASF dual-hosted git repository.
ephraimanierobi pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git
The following commit(s) were added to refs/heads/main by this push:
new 980065aaf1 Make `get_url_user_profile()` in auth manager optional
(#32886)
980065aaf1 is described below
commit 980065aaf1b25f1d8d6504446589da33ca699689
Author: Vincent <[email protected]>
AuthorDate: Fri Jul 28 01:34:46 2023 -0400
Make `get_url_user_profile()` in auth manager optional (#32886)
---
airflow/auth/managers/base_auth_manager.py | 2 +-
airflow/auth/managers/fab/fab_auth_manager.py | 4 ++--
airflow/www/templates/appbuilder/navbar_right.html | 7 +++++--
tests/auth/managers/fab/test_fab_auth_manager.py | 13 +++++++++++++
4 files changed, 21 insertions(+), 5 deletions(-)
diff --git a/airflow/auth/managers/base_auth_manager.py
b/airflow/auth/managers/base_auth_manager.py
index 645616069d..17b7b61aeb 100644
--- a/airflow/auth/managers/base_auth_manager.py
+++ b/airflow/auth/managers/base_auth_manager.py
@@ -53,7 +53,7 @@ class BaseAuthManager(LoggingMixin):
...
@abstractmethod
- def get_url_user_profile(self) -> str:
+ def get_url_user_profile(self) -> str | None:
"""Return the url to a page displaying info about the current user."""
...
diff --git a/airflow/auth/managers/fab/fab_auth_manager.py
b/airflow/auth/managers/fab/fab_auth_manager.py
index c009ff6247..ac5ae45c43 100644
--- a/airflow/auth/managers/fab/fab_auth_manager.py
+++ b/airflow/auth/managers/fab/fab_auth_manager.py
@@ -60,8 +60,8 @@ class FabAuthManager(BaseAuthManager):
else:
return url_for(f"{self.security_manager.auth_view.endpoint}.login")
- def get_url_user_profile(self) -> str:
+ def get_url_user_profile(self) -> str | None:
"""Return the url to a page displaying info about the current user."""
if not self.security_manager.user_view:
- raise AirflowException("`user_view` not defined in the security
manager.")
+ return None
return url_for(f"{self.security_manager.user_view.endpoint}.userinfo")
diff --git a/airflow/www/templates/appbuilder/navbar_right.html
b/airflow/www/templates/appbuilder/navbar_right.html
index 7252b9647a..2c16e09d96 100644
--- a/airflow/www/templates/appbuilder/navbar_right.html
+++ b/airflow/www/templates/appbuilder/navbar_right.html
@@ -73,8 +73,11 @@
<b class="caret"></b>
</a>
<ul class="dropdown-menu">
- <li><a href="{{auth_manager.get_url_user_profile()}}"><span
class="material-icons">account_circle</span>{{_("Your Profile")}}</a></li>
- <li role="separator" class="divider"></li>
+ {% set user_profile_url = auth_manager.get_url_user_profile() %}
+ {% if user_profile_url %}
+ <li><a href="{{user_profile_url}}"><span
class="material-icons">account_circle</span>{{_("Your Profile")}}</a></li>
+ <li role="separator" class="divider"></li>
+ {% endif %}
<li><a href="{{appbuilder.get_url_for_logout}}"><span
class="material-icons">exit_to_app</span>{{_("Log Out")}}</a></li>
</ul>
</li>
diff --git a/tests/auth/managers/fab/test_fab_auth_manager.py
b/tests/auth/managers/fab/test_fab_auth_manager.py
index ef8baba455..313165b56b 100644
--- a/tests/auth/managers/fab/test_fab_auth_manager.py
+++ b/tests/auth/managers/fab/test_fab_auth_manager.py
@@ -81,3 +81,16 @@ class TestFabAuthManager:
auth_manager.security_manager.auth_view.endpoint = "test_endpoint"
auth_manager.get_url_login(next_url="next_url")
mock_url_for.assert_called_once_with("test_endpoint.login",
next="next_url")
+
+ def test_get_url_user_profile_when_auth_view_not_defined(self,
auth_manager):
+ assert auth_manager.get_url_user_profile() is None
+
+ @mock.patch("airflow.auth.managers.fab.fab_auth_manager.url_for")
+ def test_get_url_user_profile(self, mock_url_for, auth_manager):
+ expected_url = "test_url"
+ mock_url_for.return_value = expected_url
+ auth_manager.security_manager.user_view = Mock()
+ auth_manager.security_manager.user_view.endpoint = "test_endpoint"
+ actual_url = auth_manager.get_url_user_profile()
+ mock_url_for.assert_called_once_with("test_endpoint.userinfo")
+ assert actual_url == expected_url