Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions providers/smtp/src/airflow/providers/smtp/hooks/smtp.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,10 @@ def test_connection(self) -> tuple[bool, str]:
"""Test SMTP connectivity from UI."""
try:
smtp_client = self.get_conn()._smtp_client
if smtp_client:
if smtp_client is not None:
smtp_client = cast("smtplib.SMTP_SSL | smtplib.SMTP", smtp_client)
status = smtp_client.noop()
if status == 250:
if status[0] == 250:
return True, "Connection successfully tested"
except Exception as e:
return False, str(e)
Expand Down
23 changes: 23 additions & 0 deletions providers/smtp/tests/unit/smtp/hooks/test_smtp.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,29 @@ def test_ehlo_called_after_starttls(self, mock_smtplib):
assert ehlo_call == call.ehlo()
assert login_call == call.login(SMTP_LOGIN, SMTP_PASSWORD)

@pytest.mark.parametrize(
("noop_response", "expected_result"),
[
pytest.param(
(250, b"2.0.0 Ok"),
(True, "Connection successfully tested"),
id="success",
),
pytest.param(
(421, b"4.3.0 Service not available"),
(False, "Failed to establish connection"),
id="failure",
),
],
)
@patch(smtplib_string)
def test_test_connection_handles_noop_responses(self, mock_smtplib, noop_response, expected_result):
mock_conn = _create_fake_smtp(mock_smtplib)
mock_conn.noop.return_value = noop_response

assert SmtpHook().test_connection() == expected_result
mock_conn.noop.assert_called_once_with()


@pytest.mark.asyncio
@pytest.mark.skipif(not AIRFLOW_V_3_1_PLUS, reason="Async support was added to BaseNotifier in 3.1.0")
Expand Down
Loading