Improve SABnzbd config flow tests (#131234)

pull/131237/head
Jan-Philipp Benecke 2024-11-22 08:44:33 +01:00 committed by GitHub
parent fa3d2a3031
commit caac22f09f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 43 additions and 32 deletions

View File

@ -22,7 +22,7 @@ def mock_setup_entry() -> Generator[AsyncMock]:
yield mock_setup_entry
@pytest.fixture(name="sabnzbd")
@pytest.fixture(name="sabnzbd", autouse=True)
def mock_sabnzbd() -> Generator[AsyncMock]:
"""Mock the Sabnzbd API."""
with patch(
@ -35,7 +35,7 @@ def mock_sabnzbd() -> Generator[AsyncMock]:
@pytest.fixture(name="config_entry")
async def mock_config_entry(hass: HomeAssistant, sabnzbd: AsyncMock) -> MockConfigEntry:
async def mock_config_entry(hass: HomeAssistant) -> MockConfigEntry:
"""Return a MockConfigEntry for testing."""
config_entry = MockConfigEntry(
domain=DOMAIN,
@ -53,7 +53,7 @@ async def mock_config_entry(hass: HomeAssistant, sabnzbd: AsyncMock) -> MockConf
@pytest.fixture(name="setup_integration")
async def mock_setup_integration(
hass: HomeAssistant, config_entry: MockConfigEntry, sabnzbd: AsyncMock
hass: HomeAssistant, config_entry: MockConfigEntry
) -> None:
"""Fixture for setting up the component."""
assert await async_setup_component(hass, DOMAIN, {})

View File

@ -1,6 +1,6 @@
"""Define tests for the Sabnzbd config flow."""
from unittest.mock import AsyncMock, patch
from unittest.mock import AsyncMock
from pysabnzbd import SabnzbdApiException
import pytest
@ -28,35 +28,46 @@ async def test_create_entry(hass: HomeAssistant, mock_setup_entry: AsyncMock) ->
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {}
with patch(
"homeassistant.components.sabnzbd.sab.SabnzbdApi.check_available",
return_value=True,
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
VALID_CONFIG,
)
await hass.async_block_till_done()
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
VALID_CONFIG,
)
await hass.async_block_till_done()
assert result2["type"] is FlowResultType.CREATE_ENTRY
assert result2["title"] == "edc3eee7330e"
assert result2["data"] == {
CONF_API_KEY: "edc3eee7330e4fdda04489e3fbc283d0",
CONF_URL: "http://localhost:8080",
}
assert len(mock_setup_entry.mock_calls) == 1
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == "edc3eee7330e"
assert result["data"] == {
CONF_API_KEY: "edc3eee7330e4fdda04489e3fbc283d0",
CONF_URL: "http://localhost:8080",
}
assert len(mock_setup_entry.mock_calls) == 1
async def test_auth_error(hass: HomeAssistant) -> None:
"""Test that the user step fails."""
with patch(
"homeassistant.components.sabnzbd.sab.SabnzbdApi.check_available",
side_effect=SabnzbdApiException("Some error"),
):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_USER},
data=VALID_CONFIG,
)
async def test_auth_error(hass: HomeAssistant, sabnzbd: AsyncMock) -> None:
"""Test when the user step fails and if we can recover."""
sabnzbd.check_available.side_effect = SabnzbdApiException("Some error")
assert result["errors"] == {"base": "cannot_connect"}
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_USER},
data=VALID_CONFIG,
)
assert result["errors"] == {"base": "cannot_connect"}
# reset side effect and check if we can recover
sabnzbd.check_available.side_effect = None
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
VALID_CONFIG,
)
await hass.async_block_till_done()
assert "errors" not in result
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == "edc3eee7330e"
assert result["data"] == {
CONF_API_KEY: "edc3eee7330e4fdda04489e3fbc283d0",
CONF_URL: "http://localhost:8080",
}