From ec5276370633d8f45f99302c562e18634687a7fe Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Sun, 19 Sep 2021 10:24:27 +0200 Subject: [PATCH] Prevent 3rd party lib from opening sockets in samsungtv tests (#56334) --- tests/components/samsungtv/conftest.py | 7 ++++++ .../components/samsungtv/test_config_flow.py | 22 +++++++++++++------ tests/components/samsungtv/test_init.py | 6 +++-- .../components/samsungtv/test_media_player.py | 7 +++--- 4 files changed, 29 insertions(+), 13 deletions(-) diff --git a/tests/components/samsungtv/conftest.py b/tests/components/samsungtv/conftest.py index c3da2652a6d..05c51fdf591 100644 --- a/tests/components/samsungtv/conftest.py +++ b/tests/components/samsungtv/conftest.py @@ -112,3 +112,10 @@ def delay_fixture(): def mock_now(): """Fixture for dtutil.now.""" return dt_util.utcnow() + + +@pytest.fixture(name="no_mac_address") +def mac_address_fixture(): + """Patch getmac.get_mac_address.""" + with patch("getmac.get_mac_address", return_value=None) as mac: + yield mac diff --git a/tests/components/samsungtv/test_config_flow.py b/tests/components/samsungtv/test_config_flow.py index 64d0c95c084..d9c96982aa1 100644 --- a/tests/components/samsungtv/test_config_flow.py +++ b/tests/components/samsungtv/test_config_flow.py @@ -389,7 +389,9 @@ async def test_ssdp_legacy_not_supported(hass: HomeAssistant, remote: Mock): async def test_ssdp_websocket_success_populates_mac_address( - hass: HomeAssistant, remotews: Mock + hass: HomeAssistant, + remote: Mock, + remotews: Mock, ): """Test starting a flow from ssdp for a supported device populates the mac.""" result = await hass.config_entries.flow.async_init( @@ -441,7 +443,9 @@ async def test_ssdp_model_not_supported(hass: HomeAssistant, remote: Mock): assert result["reason"] == RESULT_NOT_SUPPORTED -async def test_ssdp_not_successful(hass: HomeAssistant, remote: Mock): +async def test_ssdp_not_successful( + hass: HomeAssistant, remote: Mock, no_mac_address: Mock +): """Test starting a flow from discovery but no device found.""" with patch( "homeassistant.components.samsungtv.bridge.Remote", @@ -469,7 +473,9 @@ async def test_ssdp_not_successful(hass: HomeAssistant, remote: Mock): assert result["reason"] == RESULT_CANNOT_CONNECT -async def test_ssdp_not_successful_2(hass: HomeAssistant, remote: Mock): +async def test_ssdp_not_successful_2( + hass: HomeAssistant, remote: Mock, no_mac_address: Mock +): """Test starting a flow from discovery but no device found.""" with patch( "homeassistant.components.samsungtv.bridge.Remote", @@ -564,7 +570,9 @@ async def test_import_legacy(hass: HomeAssistant, remote: Mock): assert entries[0].data[CONF_PORT] == LEGACY_PORT -async def test_import_legacy_without_name(hass: HomeAssistant, remote: Mock): +async def test_import_legacy_without_name( + hass: HomeAssistant, remote: Mock, no_mac_address: Mock +): """Test importing from yaml without a name.""" with patch( "homeassistant.components.samsungtv.config_flow.socket.gethostbyname", @@ -651,7 +659,7 @@ async def test_import_unknown_host(hass: HomeAssistant, remotews: Mock): assert result["reason"] == RESULT_UNKNOWN_HOST -async def test_dhcp(hass: HomeAssistant, remotews: Mock): +async def test_dhcp(hass: HomeAssistant, remote: Mock, remotews: Mock): """Test starting a flow from dhcp.""" # confirm to add the entry result = await hass.config_entries.flow.async_init( @@ -677,7 +685,7 @@ async def test_dhcp(hass: HomeAssistant, remotews: Mock): assert result["result"].unique_id == "be9554b9-c9fb-41f4-8920-22da015376a4" -async def test_zeroconf(hass: HomeAssistant, remotews: Mock): +async def test_zeroconf(hass: HomeAssistant, remote: Mock, remotews: Mock): """Test starting a flow from zeroconf.""" result = await hass.config_entries.flow.async_init( DOMAIN, @@ -715,7 +723,7 @@ async def test_zeroconf_ignores_soundbar(hass: HomeAssistant, remotews_soundbar: async def test_zeroconf_no_device_info( - hass: HomeAssistant, remotews_no_device_info: Mock + hass: HomeAssistant, remote: Mock, remotews_no_device_info: Mock ): """Test starting a flow from zeroconf where device_info returns None.""" result = await hass.config_entries.flow.async_init( diff --git a/tests/components/samsungtv/test_init.py b/tests/components/samsungtv/test_init.py index c5c1519556d..1f6c13809cb 100644 --- a/tests/components/samsungtv/test_init.py +++ b/tests/components/samsungtv/test_init.py @@ -53,7 +53,7 @@ REMOTE_CALL = { } -async def test_setup(hass: HomeAssistant, remote: Mock): +async def test_setup(hass: HomeAssistant, remote: Mock, no_mac_address: Mock): """Test Samsung TV integration is setup.""" with patch("homeassistant.components.samsungtv.bridge.Remote") as remote, patch( "homeassistant.components.samsungtv.config_flow.socket.gethostbyname", @@ -129,7 +129,9 @@ async def test_setup_duplicate_config(hass: HomeAssistant, remote: Mock, caplog) assert "duplicate host entries found" in caplog.text -async def test_setup_duplicate_entries(hass: HomeAssistant, remote: Mock, caplog): +async def test_setup_duplicate_entries( + hass: HomeAssistant, remote: Mock, no_mac_address: Mock, caplog +): """Test duplicate setup of platform.""" await async_setup_component(hass, SAMSUNGTV_DOMAIN, MOCK_CONFIG) await hass.async_block_till_done() diff --git a/tests/components/samsungtv/test_media_player.py b/tests/components/samsungtv/test_media_player.py index de9183915a2..1d81769ad8b 100644 --- a/tests/components/samsungtv/test_media_player.py +++ b/tests/components/samsungtv/test_media_player.py @@ -127,10 +127,9 @@ def delay_fixture(): yield delay -@pytest.fixture -def mock_now(): - """Fixture for dtutil.now.""" - return dt_util.utcnow() +@pytest.fixture(autouse=True) +def mock_no_mac_address(no_mac_address): + """Fake mac address in all mediaplayer tests.""" async def setup_samsungtv(hass, config):