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

View File

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