2019-10-23 20:09:11 +00:00
|
|
|
"""Tests for Transmission config flow."""
|
2024-03-08 13:44:56 +00:00
|
|
|
|
2022-08-19 10:10:34 +00:00
|
|
|
from unittest.mock import MagicMock, patch
|
2019-09-26 09:14:57 +00:00
|
|
|
|
|
|
|
import pytest
|
2023-05-05 06:42:51 +00:00
|
|
|
from transmission_rpc.error import (
|
|
|
|
TransmissionAuthError,
|
|
|
|
TransmissionConnectError,
|
|
|
|
TransmissionError,
|
|
|
|
)
|
2019-09-26 09:14:57 +00:00
|
|
|
|
2022-08-19 10:10:34 +00:00
|
|
|
from homeassistant import config_entries
|
2020-02-27 11:19:54 +00:00
|
|
|
from homeassistant.components import transmission
|
2022-08-19 10:10:34 +00:00
|
|
|
from homeassistant.components.transmission.const import DOMAIN
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.data_entry_flow import FlowResultType
|
2019-09-26 09:14:57 +00:00
|
|
|
|
2022-08-19 10:10:34 +00:00
|
|
|
from . import MOCK_CONFIG_DATA
|
2019-09-26 09:14:57 +00:00
|
|
|
|
2022-08-19 10:10:34 +00:00
|
|
|
from tests.common import MockConfigEntry
|
2019-09-26 09:14:57 +00:00
|
|
|
|
|
|
|
|
2022-08-19 10:10:34 +00:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def mock_api():
|
2019-09-26 09:14:57 +00:00
|
|
|
"""Mock an api."""
|
2022-12-14 15:36:37 +00:00
|
|
|
with patch("transmission_rpc.Client") as api:
|
2022-08-19 10:10:34 +00:00
|
|
|
yield api
|
2019-09-26 09:14:57 +00:00
|
|
|
|
2020-02-27 11:19:54 +00:00
|
|
|
|
2022-08-19 10:10:34 +00:00
|
|
|
async def test_form(hass: HomeAssistant) -> None:
|
|
|
|
"""Test we get the form."""
|
2020-02-27 11:19:54 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
2022-08-19 10:10:34 +00:00
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
2019-09-26 09:14:57 +00:00
|
|
|
)
|
2024-04-02 21:21:50 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2019-09-26 09:14:57 +00:00
|
|
|
|
2022-08-19 10:10:34 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.components.transmission.async_setup_entry",
|
|
|
|
return_value=True,
|
|
|
|
) as mock_setup_entry:
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
MOCK_CONFIG_DATA,
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2024-04-02 21:21:50 +00:00
|
|
|
assert result2["type"] is FlowResultType.CREATE_ENTRY
|
2022-08-19 10:10:34 +00:00
|
|
|
assert result2["title"] == "Transmission"
|
|
|
|
assert result2["data"] == MOCK_CONFIG_DATA
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
|
|
|
|
|
|
|
async def test_device_already_configured(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
) -> None:
|
|
|
|
"""Test aborting if the device is already configured."""
|
|
|
|
entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG_DATA)
|
|
|
|
entry.add_to_hass(hass)
|
2020-02-27 11:19:54 +00:00
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2022-08-19 10:10:34 +00:00
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
2020-02-27 11:19:54 +00:00
|
|
|
)
|
2024-04-02 21:21:50 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2019-09-26 09:14:57 +00:00
|
|
|
|
2022-08-19 10:10:34 +00:00
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
MOCK_CONFIG_DATA,
|
2019-09-26 09:14:57 +00:00
|
|
|
)
|
2022-08-19 10:10:34 +00:00
|
|
|
await hass.async_block_till_done()
|
2019-09-26 09:14:57 +00:00
|
|
|
|
2024-04-02 21:21:50 +00:00
|
|
|
assert result2["type"] is FlowResultType.ABORT
|
2022-08-19 10:10:34 +00:00
|
|
|
assert result2["reason"] == "already_configured"
|
2019-09-26 09:14:57 +00:00
|
|
|
|
|
|
|
|
2022-08-19 10:10:34 +00:00
|
|
|
async def test_options(hass: HomeAssistant) -> None:
|
|
|
|
"""Test updating options."""
|
2019-10-23 20:09:11 +00:00
|
|
|
entry = MockConfigEntry(
|
2020-02-27 11:19:54 +00:00
|
|
|
domain=transmission.DOMAIN,
|
2022-08-19 10:10:34 +00:00
|
|
|
data=MOCK_CONFIG_DATA,
|
2023-10-24 12:17:46 +00:00
|
|
|
options={"limit": 10, "order": "oldest_first"},
|
2019-10-23 20:09:11 +00:00
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
2022-08-19 10:10:34 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.components.transmission.async_setup_entry",
|
|
|
|
return_value=True,
|
|
|
|
):
|
|
|
|
assert await hass.config_entries.async_setup(entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
2019-09-26 09:14:57 +00:00
|
|
|
|
2022-08-19 10:10:34 +00:00
|
|
|
result = await hass.config_entries.options.async_init(entry.entry_id)
|
2019-09-26 09:14:57 +00:00
|
|
|
|
2024-04-02 21:21:50 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2022-08-19 10:10:34 +00:00
|
|
|
assert result["step_id"] == "init"
|
2019-09-26 09:14:57 +00:00
|
|
|
|
2022-08-19 10:10:34 +00:00
|
|
|
result = await hass.config_entries.options.async_configure(
|
2023-10-24 12:17:46 +00:00
|
|
|
result["flow_id"], user_input={"limit": 20}
|
2019-09-26 09:14:57 +00:00
|
|
|
)
|
|
|
|
|
2024-04-02 21:21:50 +00:00
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
2023-10-24 12:17:46 +00:00
|
|
|
assert result["data"]["limit"] == 20
|
|
|
|
assert result["data"]["order"] == "oldest_first"
|
2019-09-26 09:14:57 +00:00
|
|
|
|
|
|
|
|
2022-08-19 10:10:34 +00:00
|
|
|
async def test_error_on_wrong_credentials(
|
|
|
|
hass: HomeAssistant, mock_api: MagicMock
|
|
|
|
) -> None:
|
|
|
|
"""Test we handle invalid credentials."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
2019-09-26 09:14:57 +00:00
|
|
|
)
|
|
|
|
|
2023-05-05 06:42:51 +00:00
|
|
|
mock_api.side_effect = TransmissionAuthError()
|
2022-08-19 10:10:34 +00:00
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
MOCK_CONFIG_DATA,
|
|
|
|
)
|
2024-04-02 21:21:50 +00:00
|
|
|
assert result2["type"] is FlowResultType.FORM
|
2022-08-19 10:10:34 +00:00
|
|
|
assert result2["errors"] == {
|
|
|
|
"username": "invalid_auth",
|
|
|
|
"password": "invalid_auth",
|
|
|
|
}
|
2019-09-26 09:14:57 +00:00
|
|
|
|
|
|
|
|
2023-05-05 06:42:51 +00:00
|
|
|
async def test_unexpected_error(hass: HomeAssistant, mock_api: MagicMock) -> None:
|
|
|
|
"""Test we handle unexpected error."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
|
|
|
mock_api.side_effect = TransmissionError()
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
MOCK_CONFIG_DATA,
|
|
|
|
)
|
2024-04-02 21:21:50 +00:00
|
|
|
assert result2["type"] is FlowResultType.FORM
|
2023-05-05 06:42:51 +00:00
|
|
|
assert result2["errors"] == {"base": "cannot_connect"}
|
|
|
|
|
|
|
|
|
2022-08-19 10:10:34 +00:00
|
|
|
async def test_error_on_connection_failure(
|
|
|
|
hass: HomeAssistant, mock_api: MagicMock
|
|
|
|
) -> None:
|
|
|
|
"""Test we handle cannot connect error."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
2019-09-26 09:14:57 +00:00
|
|
|
)
|
2022-08-19 10:10:34 +00:00
|
|
|
|
2023-05-05 06:42:51 +00:00
|
|
|
mock_api.side_effect = TransmissionConnectError()
|
2022-08-19 10:10:34 +00:00
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
MOCK_CONFIG_DATA,
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:21:50 +00:00
|
|
|
assert result2["type"] is FlowResultType.FORM
|
2022-08-19 10:10:34 +00:00
|
|
|
assert result2["errors"] == {"base": "cannot_connect"}
|
2022-06-20 14:09:58 +00:00
|
|
|
|
|
|
|
|
2022-08-19 10:10:34 +00:00
|
|
|
async def test_reauth_success(hass: HomeAssistant) -> None:
|
2022-06-20 14:09:58 +00:00
|
|
|
"""Test we can reauth."""
|
2024-03-21 10:05:36 +00:00
|
|
|
entry = MockConfigEntry(domain=transmission.DOMAIN, data=MOCK_CONFIG_DATA)
|
2022-06-20 14:09:58 +00:00
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
transmission.DOMAIN,
|
|
|
|
context={
|
|
|
|
"source": config_entries.SOURCE_REAUTH,
|
|
|
|
"entry_id": entry.entry_id,
|
|
|
|
},
|
2022-08-19 10:10:34 +00:00
|
|
|
data=MOCK_CONFIG_DATA,
|
2022-06-20 14:09:58 +00:00
|
|
|
)
|
|
|
|
|
2024-04-02 21:21:50 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2022-06-20 14:09:58 +00:00
|
|
|
assert result["step_id"] == "reauth_confirm"
|
2022-08-19 10:10:34 +00:00
|
|
|
assert result["description_placeholders"] == {"username": "user"}
|
2022-06-20 14:09:58 +00:00
|
|
|
|
2022-08-19 10:10:34 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.components.transmission.async_setup_entry",
|
|
|
|
return_value=True,
|
|
|
|
) as mock_setup_entry:
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{
|
|
|
|
"password": "test-password",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:21:50 +00:00
|
|
|
assert result2["type"] is FlowResultType.ABORT
|
2022-06-20 14:09:58 +00:00
|
|
|
assert result2["reason"] == "reauth_successful"
|
2022-08-19 10:10:34 +00:00
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
2022-06-20 14:09:58 +00:00
|
|
|
|
|
|
|
|
2022-08-19 10:10:34 +00:00
|
|
|
async def test_reauth_failed(hass: HomeAssistant, mock_api: MagicMock) -> None:
|
|
|
|
"""Test we can't reauth due to invalid password."""
|
2022-06-20 14:09:58 +00:00
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=transmission.DOMAIN,
|
2022-08-19 10:10:34 +00:00
|
|
|
data=MOCK_CONFIG_DATA,
|
2022-06-20 14:09:58 +00:00
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
transmission.DOMAIN,
|
|
|
|
context={
|
|
|
|
"source": config_entries.SOURCE_REAUTH,
|
|
|
|
"entry_id": entry.entry_id,
|
|
|
|
},
|
2022-08-19 10:10:34 +00:00
|
|
|
data=MOCK_CONFIG_DATA,
|
2022-06-20 14:09:58 +00:00
|
|
|
)
|
|
|
|
|
2024-04-02 21:21:50 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2022-06-20 14:09:58 +00:00
|
|
|
assert result["step_id"] == "reauth_confirm"
|
2022-08-19 10:10:34 +00:00
|
|
|
assert result["description_placeholders"] == {"username": "user"}
|
2022-06-20 14:09:58 +00:00
|
|
|
|
2023-05-05 06:42:51 +00:00
|
|
|
mock_api.side_effect = TransmissionAuthError()
|
2022-06-20 14:09:58 +00:00
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{
|
2022-08-19 10:10:34 +00:00
|
|
|
"password": "wrong-password",
|
2022-06-20 14:09:58 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:21:50 +00:00
|
|
|
assert result2["type"] is FlowResultType.FORM
|
2022-08-19 10:10:34 +00:00
|
|
|
assert result2["errors"] == {"password": "invalid_auth"}
|
2022-06-20 14:09:58 +00:00
|
|
|
|
|
|
|
|
2022-08-19 10:10:34 +00:00
|
|
|
async def test_reauth_failed_connection_error(
|
|
|
|
hass: HomeAssistant, mock_api: MagicMock
|
|
|
|
) -> None:
|
|
|
|
"""Test we can't reauth due to connection error."""
|
2022-06-20 14:09:58 +00:00
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=transmission.DOMAIN,
|
2022-08-19 10:10:34 +00:00
|
|
|
data=MOCK_CONFIG_DATA,
|
2022-06-20 14:09:58 +00:00
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
transmission.DOMAIN,
|
|
|
|
context={
|
|
|
|
"source": config_entries.SOURCE_REAUTH,
|
|
|
|
"entry_id": entry.entry_id,
|
|
|
|
},
|
2022-08-19 10:10:34 +00:00
|
|
|
data=MOCK_CONFIG_DATA,
|
2022-06-20 14:09:58 +00:00
|
|
|
)
|
|
|
|
|
2024-04-02 21:21:50 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2022-06-20 14:09:58 +00:00
|
|
|
assert result["step_id"] == "reauth_confirm"
|
2022-08-19 10:10:34 +00:00
|
|
|
assert result["description_placeholders"] == {"username": "user"}
|
2022-06-20 14:09:58 +00:00
|
|
|
|
2023-05-05 06:42:51 +00:00
|
|
|
mock_api.side_effect = TransmissionConnectError()
|
2022-06-20 14:09:58 +00:00
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{
|
2022-08-19 10:10:34 +00:00
|
|
|
"password": "test-password",
|
2022-06-20 14:09:58 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:21:50 +00:00
|
|
|
assert result2["type"] is FlowResultType.FORM
|
2022-06-20 14:09:58 +00:00
|
|
|
assert result2["errors"] == {"base": "cannot_connect"}
|