mirror of
https://github.com/home-assistant/core.git
synced 2026-11-19 12:12:30 -04:00
Add config flow to qBittorrent (#82560)
* qbittorrent: implement config_flow Signed-off-by: Chris Xiao <30990835+chrisx8@users.noreply.github.com> * qbittorrent: add English translations Signed-off-by: Chris Xiao <30990835+chrisx8@users.noreply.github.com> * qbittorrent: create sensors with config_flow Signed-off-by: Chris Xiao <30990835+chrisx8@users.noreply.github.com> * qbittorrent: set unique_id and icon Signed-off-by: Chris Xiao <30990835+chrisx8@users.noreply.github.com> * qbittorrent: add tests for config_flow Signed-off-by: Chris Xiao <30990835+chrisx8@users.noreply.github.com> * qbittorrent: detect duplicate config entries Signed-off-by: Chris Xiao <30990835+chrisx8@users.noreply.github.com> * qbittorrent: import YAML config Signed-off-by: Chris Xiao <30990835+chrisx8@users.noreply.github.com> * qbittorrent: update coveragerc Signed-off-by: Chris Xiao <30990835+chrisx8@users.noreply.github.com> * qbittorrent: delete translations file * create `deprecated_yaml` issue in `setup_platform` * move qbittorrent test fixtures to conftest.py * improve code quality & remove wrong unique_id * keep PLATFORM_SCHEMA until YAML support is removed * remove CONF_NAME in config entry, fix setup_entry * improve test suite * clean up QBittorrentSensor class * improve user flow tests * explicit result assertion & minor tweaks in tests Co-authored-by: epenet <epenet@users.noreply.github.com> * implement entry unloading Co-authored-by: epenet <epenet@users.noreply.github.com> * add type hints * tweak config_flow data handling --------- Signed-off-by: Chris Xiao <30990835+chrisx8@users.noreply.github.com> Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1 @@
|
||||
"""Tests for the qBittorrent integration."""
|
||||
@@ -0,0 +1,25 @@
|
||||
"""Fixtures for testing qBittorrent component."""
|
||||
from collections.abc import Generator
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
import requests_mock
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_setup_entry() -> Generator[AsyncMock, None, None]:
|
||||
"""Mock qbittorrent entry setup."""
|
||||
with patch(
|
||||
"homeassistant.components.qbittorrent.async_setup_entry", return_value=True
|
||||
) as mock_setup_entry:
|
||||
yield mock_setup_entry
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_api() -> Generator[requests_mock.Mocker, None, None]:
|
||||
"""Mock the qbittorrent API."""
|
||||
with requests_mock.Mocker() as mocker:
|
||||
mocker.get("http://localhost:8080/api/v2/app/preferences", status_code=403)
|
||||
mocker.get("http://localhost:8080/api/v2/transfer/speedLimitsMode")
|
||||
mocker.post("http://localhost:8080/api/v2/auth/login", text="Ok.")
|
||||
yield mocker
|
||||
@@ -0,0 +1,136 @@
|
||||
"""Test the qBittorrent config flow."""
|
||||
import pytest
|
||||
from requests.exceptions import RequestException
|
||||
import requests_mock
|
||||
|
||||
from homeassistant.components.qbittorrent.const import DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER
|
||||
from homeassistant.const import (
|
||||
CONF_PASSWORD,
|
||||
CONF_SOURCE,
|
||||
CONF_URL,
|
||||
CONF_USERNAME,
|
||||
CONF_VERIFY_SSL,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
pytestmark = pytest.mark.usefixtures("mock_setup_entry")
|
||||
|
||||
USER_INPUT = {
|
||||
CONF_URL: "http://localhost:8080",
|
||||
CONF_USERNAME: "user",
|
||||
CONF_PASSWORD: "pass",
|
||||
CONF_VERIFY_SSL: True,
|
||||
}
|
||||
|
||||
YAML_IMPORT = {
|
||||
CONF_URL: "http://localhost:8080",
|
||||
CONF_USERNAME: "user",
|
||||
CONF_PASSWORD: "pass",
|
||||
}
|
||||
|
||||
|
||||
async def test_flow_user(hass: HomeAssistant, mock_api: requests_mock.Mocker) -> None:
|
||||
"""Test the user flow."""
|
||||
# Open flow as USER with no input
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={CONF_SOURCE: SOURCE_USER}
|
||||
)
|
||||
assert result["type"] == FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
|
||||
# Test flow with connection failure, fail with cannot_connect
|
||||
with requests_mock.Mocker() as mock:
|
||||
mock.get(
|
||||
f"{USER_INPUT[CONF_URL]}/api/v2/app/preferences",
|
||||
exc=RequestException,
|
||||
)
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], USER_INPUT
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert result["type"] == FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
assert result["errors"] == {"base": "cannot_connect"}
|
||||
|
||||
# Test flow with wrong creds, fail with invalid_auth
|
||||
with requests_mock.Mocker() as mock:
|
||||
mock.get(f"{USER_INPUT[CONF_URL]}/api/v2/transfer/speedLimitsMode")
|
||||
mock.get(f"{USER_INPUT[CONF_URL]}/api/v2/app/preferences", status_code=403)
|
||||
mock.post(
|
||||
f"{USER_INPUT[CONF_URL]}/api/v2/auth/login",
|
||||
text="Wrong username/password",
|
||||
)
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], USER_INPUT
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert result["type"] == FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
assert result["errors"] == {"base": "invalid_auth"}
|
||||
|
||||
# Test flow with proper input, succeed
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], USER_INPUT
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert result["type"] == FlowResultType.CREATE_ENTRY
|
||||
assert result["data"] == {
|
||||
CONF_URL: "http://localhost:8080",
|
||||
CONF_USERNAME: "user",
|
||||
CONF_PASSWORD: "pass",
|
||||
CONF_VERIFY_SSL: True,
|
||||
}
|
||||
|
||||
|
||||
async def test_flow_user_already_configured(hass: HomeAssistant) -> None:
|
||||
"""Test user initialized flow with duplicate server."""
|
||||
entry = MockConfigEntry(domain=DOMAIN, data=USER_INPUT)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
# Open flow as USER with no input
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={CONF_SOURCE: SOURCE_USER}
|
||||
)
|
||||
assert result["type"] == FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
|
||||
# Test flow with duplicate config
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], USER_INPUT
|
||||
)
|
||||
assert result["type"] == FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_flow_import(hass: HomeAssistant) -> None:
|
||||
"""Test import step."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={CONF_SOURCE: SOURCE_IMPORT},
|
||||
data=YAML_IMPORT,
|
||||
)
|
||||
assert result["type"] == FlowResultType.CREATE_ENTRY
|
||||
assert result["data"] == {
|
||||
CONF_URL: "http://localhost:8080",
|
||||
CONF_USERNAME: "user",
|
||||
CONF_PASSWORD: "pass",
|
||||
CONF_VERIFY_SSL: True,
|
||||
}
|
||||
|
||||
|
||||
async def test_flow_import_already_configured(hass: HomeAssistant) -> None:
|
||||
"""Test import step already configured."""
|
||||
entry = MockConfigEntry(domain=DOMAIN, data=USER_INPUT)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={CONF_SOURCE: SOURCE_IMPORT},
|
||||
data=YAML_IMPORT,
|
||||
)
|
||||
assert result["type"] == FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
Reference in New Issue
Block a user