1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 21:06:19 +00:00

Add ssl_verify option to imap integration (#93811)

* Add ssl_verify option to imap integration

* Add test
This commit is contained in:
Jan Bouwhuis
2023-05-30 19:48:47 +02:00
committed by GitHub
parent 46d8885023
commit 1e0770ff8a
5 changed files with 89 additions and 12 deletions

View File

@@ -533,12 +533,14 @@ async def test_import_flow_connection_error(hass: HomeAssistant) -> None:
@pytest.mark.parametrize("cipher_list", ["python_default", "modern", "intermediate"])
async def test_config_flow_with_cipherlist(
hass: HomeAssistant, mock_setup_entry: AsyncMock, cipher_list: str
@pytest.mark.parametrize("verify_ssl", [False, True])
async def test_config_flow_with_cipherlist_and_ssl_verify(
hass: HomeAssistant, mock_setup_entry: AsyncMock, cipher_list: str, verify_ssl: True
) -> None:
"""Test with alternate cipherlist."""
"""Test with alternate cipherlist or disabled ssl verification."""
config = MOCK_CONFIG.copy()
config["ssl_cipher_list"] = cipher_list
config["verify_ssl"] = verify_ssl
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_USER, "show_advanced_options": True},
@@ -562,3 +564,49 @@ async def test_config_flow_with_cipherlist(
assert result2["title"] == "email@email.com"
assert result2["data"] == config
assert len(mock_setup_entry.mock_calls) == 1
async def test_config_flow_from_with_advanced_settings(
hass: HomeAssistant, mock_setup_entry: AsyncMock
) -> None:
"""Test if advanced settings show correctly."""
config = MOCK_CONFIG.copy()
config["ssl_cipher_list"] = "python_default"
config["verify_ssl"] = True
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_USER, "show_advanced_options": True},
)
assert result["type"] == FlowResultType.FORM
assert result["errors"] is None
with patch(
"homeassistant.components.imap.config_flow.connect_to_server",
side_effect=asyncio.TimeoutError,
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], config
)
await hass.async_block_till_done()
assert result2["type"] == FlowResultType.FORM
assert result2["errors"]["base"] == "cannot_connect"
assert "ssl_cipher_list" in result2["data_schema"].schema
config["ssl_cipher_list"] = "modern"
with patch(
"homeassistant.components.imap.config_flow.connect_to_server"
) as mock_client:
mock_client.return_value.search.return_value = (
"OK",
[b""],
)
result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"], config
)
await hass.async_block_till_done()
assert result3["type"] == FlowResultType.CREATE_ENTRY
assert result3["title"] == "email@email.com"
assert result3["data"] == config
assert len(mock_setup_entry.mock_calls) == 1

View File

@@ -34,16 +34,28 @@ from tests.common import MockConfigEntry, async_capture_events, async_fire_time_
@pytest.mark.parametrize(
"cipher_list", [None, "python_default", "modern", "intermediate"]
("cipher_list", "verify_ssl"),
[
(None, None),
("python_default", True),
("python_default", False),
("modern", True),
("intermediate", True),
],
)
@pytest.mark.parametrize("imap_has_capability", [True, False], ids=["push", "poll"])
async def test_entry_startup_and_unload(
hass: HomeAssistant, mock_imap_protocol: MagicMock, cipher_list: str
hass: HomeAssistant,
mock_imap_protocol: MagicMock,
cipher_list: str | None,
verify_ssl: bool | None,
) -> None:
"""Test imap entry startup and unload with push and polling coordinator and alternate ciphers."""
config = MOCK_CONFIG.copy()
if cipher_list:
if cipher_list is not None:
config["ssl_cipher_list"] = cipher_list
if verify_ssl is not None:
config["verify_ssl"] = verify_ssl
config_entry = MockConfigEntry(domain=DOMAIN, data=config)
config_entry.add_to_hass(hass)