From f50ef221d8311d4f7100cdeb2c8706a9d4352859 Mon Sep 17 00:00:00 2001 From: Jed Cunningham <66968678+jedcunningham@users.noreply.github.com> Date: Fri, 29 Apr 2022 04:29:02 -0600 Subject: [PATCH] Fix connection test button (#23345) The connection test button was always disabled if any of your hooks had import errors, for example because of a missing module. This handles that scenario. (cherry picked from commit f197030cea860351da07894879f647fe76c5751e) --- airflow/www/views.py | 4 ++-- tests/www/views/test_views_connection.py | 13 ++++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/airflow/www/views.py b/airflow/www/views.py index f73b8107b957c..9bdd761022cd2 100644 --- a/airflow/www/views.py +++ b/airflow/www/views.py @@ -3867,8 +3867,8 @@ def field_behaviours(self): def testable_connection_types(self): return [ connection_type - for connection_type, provider_info in ProvidersManager().hooks.items() - if provider_info.connection_testable + for connection_type, hook_info in ProvidersManager().hooks.items() + if hook_info and hook_info.connection_testable ] diff --git a/tests/www/views/test_views_connection.py b/tests/www/views/test_views_connection.py index 8c5ff0d013c06..d71a8a63c0c82 100644 --- a/tests/www/views/test_views_connection.py +++ b/tests/www/views/test_views_connection.py @@ -25,7 +25,7 @@ from airflow.models import Connection from airflow.utils.session import create_session from airflow.www.extensions import init_views -from airflow.www.views import ConnectionModelView +from airflow.www.views import ConnectionFormWidget, ConnectionModelView from tests.test_utils.www import check_content_in_response CONNECTION = { @@ -311,3 +311,14 @@ def test_connection_muldelete(admin_client, connection): assert resp.status_code == 200 with create_session() as session: assert session.query(Connection).filter(Connection.id == conn_id).count() == 0 + + +@mock.patch('airflow.providers_manager.ProvidersManager.hooks', new_callable=PropertyMock) +def test_connection_form_widgets_testable_types(mock_pm_hooks, admin_client): + mock_pm_hooks.return_value = { + "first": mock.MagicMock(connection_testable=True), + "second": mock.MagicMock(connection_testable=False), + "third": None, + } + + assert ["first"] == ConnectionFormWidget().testable_connection_types