From 00ccaf1ff947ededd7b4b39cd9e9550da817fcb9 Mon Sep 17 00:00:00 2001 From: Robin Lintermann Date: Thu, 22 May 2025 08:22:44 +0000 Subject: [PATCH] Fix leaking tests --- homeassistant/components/smarla/__init__.py | 2 +- tests/components/smarla/conftest.py | 16 +++++++++++++--- tests/components/smarla/test_config_flow.py | 12 +++++++++--- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/smarla/__init__.py b/homeassistant/components/smarla/__init__.py index f5c39a274ab..c55b1067735 100644 --- a/homeassistant/components/smarla/__init__.py +++ b/homeassistant/components/smarla/__init__.py @@ -14,7 +14,7 @@ type FederwiegeConfigEntry = ConfigEntry[Federwiege] async def async_setup_entry(hass: HomeAssistant, entry: FederwiegeConfigEntry) -> bool: """Set up this integration using UI.""" - connection = Connection(HOST, token_b64=entry.data.get(CONF_ACCESS_TOKEN)) + connection = Connection(HOST, token_b64=entry.data[CONF_ACCESS_TOKEN]) # Check if token still has access if not await connection.refresh_token(): diff --git a/tests/components/smarla/conftest.py b/tests/components/smarla/conftest.py index 87e64b90329..bfc488822d2 100644 --- a/tests/components/smarla/conftest.py +++ b/tests/components/smarla/conftest.py @@ -1,8 +1,9 @@ -"""Configuration for Sentry tests.""" +"""Configuration for smarla tests.""" from __future__ import annotations -from unittest.mock import patch +from collections.abc import Generator +from unittest.mock import AsyncMock, MagicMock, patch from pysmarlaapi.classes import AuthToken import pytest @@ -27,7 +28,16 @@ def mock_config_entry() -> MockConfigEntry: @pytest.fixture -def mock_connection(): +def mock_setup_entry() -> Generator[AsyncMock]: + """Override async_setup_entry.""" + with patch( + "homeassistant.components.smarla.async_setup_entry", return_value=True + ) as mock_setup_entry: + yield mock_setup_entry + + +@pytest.fixture +def mock_connection() -> Generator[MagicMock]: """Patch Connection object.""" with ( patch( diff --git a/tests/components/smarla/test_config_flow.py b/tests/components/smarla/test_config_flow.py index 73b2634e971..0e96e154adb 100644 --- a/tests/components/smarla/test_config_flow.py +++ b/tests/components/smarla/test_config_flow.py @@ -12,7 +12,9 @@ from . import MOCK_SERIAL_NUMBER, MOCK_USER_INPUT from tests.common import MockConfigEntry -async def test_config_flow(hass: HomeAssistant, mock_connection) -> None: +async def test_config_flow( + hass: HomeAssistant, mock_setup_entry, mock_connection +) -> None: """Test creating a config entry.""" result = await hass.config_entries.flow.async_init( DOMAIN, @@ -33,7 +35,9 @@ async def test_config_flow(hass: HomeAssistant, mock_connection) -> None: assert result["result"].unique_id == MOCK_SERIAL_NUMBER -async def test_malformed_token(hass: HomeAssistant, mock_connection) -> None: +async def test_malformed_token( + hass: HomeAssistant, mock_setup_entry, mock_connection +) -> None: """Test we show user form on malformed token input.""" with patch( "homeassistant.components.smarla.config_flow.Connection", side_effect=ValueError @@ -56,7 +60,9 @@ async def test_malformed_token(hass: HomeAssistant, mock_connection) -> None: assert result["type"] is FlowResultType.CREATE_ENTRY -async def test_invalid_auth(hass: HomeAssistant, mock_connection) -> None: +async def test_invalid_auth( + hass: HomeAssistant, mock_setup_entry, mock_connection +) -> None: """Test we show user form on invalid auth.""" with patch.object( mock_connection, "refresh_token", new=AsyncMock(return_value=False)