diff --git a/homeassistant/components/http/__init__.py b/homeassistant/components/http/__init__.py index 069fc42c884..42d388ffa85 100644 --- a/homeassistant/components/http/__init__.py +++ b/homeassistant/components/http/__init__.py @@ -133,7 +133,7 @@ class ApiConfig: def base_url(self) -> str: """Proxy property to find caller of this deprecated property.""" found_frame = None - for frame in reversed(extract_stack()): + for frame in reversed(extract_stack()[:-1]): for path in ("custom_components/", "homeassistant/components/"): try: index = frame.filename.index(path) diff --git a/tests/components/http/test_init.py b/tests/components/http/test_init.py index 18ec9ccf471..2c95d03a9ef 100644 --- a/tests/components/http/test_init.py +++ b/tests/components/http/test_init.py @@ -1,7 +1,6 @@ """The tests for the Home Assistant HTTP component.""" from ipaddress import ip_network import logging -import unittest import pytest @@ -12,6 +11,32 @@ from homeassistant.util.ssl import server_context_intermediate, server_context_m from tests.async_mock import Mock, patch +@pytest.fixture +def mock_stack(): + """Mock extract stack.""" + with patch( + "homeassistant.components.http.extract_stack", + return_value=[ + Mock( + filename="/home/paulus/core/homeassistant/core.py", + lineno="23", + line="do_something()", + ), + Mock( + filename="/home/paulus/core/homeassistant/components/hue/light.py", + lineno="23", + line="self.light.is_on", + ), + Mock( + filename="/home/paulus/core/homeassistant/components/http/__init__.py", + lineno="157", + line="base_url", + ), + ], + ): + yield + + class TestView(http.HomeAssistantView): """Test the HTTP views.""" @@ -36,110 +61,73 @@ async def test_registering_view_while_running( hass.http.register_view(TestView) -class TestApiConfig(unittest.TestCase): - """Test API configuration methods.""" - - def test_api_base_url_with_domain(hass): - """Test setting API URL with domain.""" - api_config = http.ApiConfig("127.0.0.1", "example.com") - assert api_config.base_url == "http://example.com:8123" - - def test_api_base_url_with_ip(hass): - """Test setting API URL with IP.""" - api_config = http.ApiConfig("127.0.0.1", "1.1.1.1") - assert api_config.base_url == "http://1.1.1.1:8123" - - def test_api_base_url_with_ip_and_port(hass): - """Test setting API URL with IP and port.""" - api_config = http.ApiConfig("127.0.0.1", "1.1.1.1", 8124) - assert api_config.base_url == "http://1.1.1.1:8124" - - def test_api_base_url_with_protocol(hass): - """Test setting API URL with protocol.""" - api_config = http.ApiConfig("127.0.0.1", "https://example.com") - assert api_config.base_url == "https://example.com:8123" - - def test_api_base_url_with_protocol_and_port(hass): - """Test setting API URL with protocol and port.""" - api_config = http.ApiConfig("127.0.0.1", "https://example.com", 433) - assert api_config.base_url == "https://example.com:433" - - def test_api_base_url_with_ssl_enable(hass): - """Test setting API URL with use_ssl enabled.""" - api_config = http.ApiConfig("127.0.0.1", "example.com", use_ssl=True) - assert api_config.base_url == "https://example.com:8123" - - def test_api_base_url_with_ssl_enable_and_port(hass): - """Test setting API URL with use_ssl enabled and port.""" - api_config = http.ApiConfig("127.0.0.1", "1.1.1.1", use_ssl=True, port=8888) - assert api_config.base_url == "https://1.1.1.1:8888" - - def test_api_base_url_with_protocol_and_ssl_enable(hass): - """Test setting API URL with specific protocol and use_ssl enabled.""" - api_config = http.ApiConfig("127.0.0.1", "http://example.com", use_ssl=True) - assert api_config.base_url == "http://example.com:8123" - - def test_api_base_url_removes_trailing_slash(hass): - """Test a trialing slash is removed when setting the API URL.""" - api_config = http.ApiConfig("127.0.0.1", "http://example.com/") - assert api_config.base_url == "http://example.com:8123" - - def test_api_local_ip(hass): - """Test a trialing slash is removed when setting the API URL.""" - api_config = http.ApiConfig("127.0.0.1", "http://example.com/") - assert api_config.local_ip == "127.0.0.1" +def test_api_base_url_with_domain(mock_stack): + """Test setting API URL with domain.""" + api_config = http.ApiConfig("127.0.0.1", "example.com") + assert api_config.base_url == "http://example.com:8123" -async def test_api_base_url_with_domain(hass): - """Test setting API URL.""" - result = await async_setup_component( - hass, "http", {"http": {"base_url": "example.com"}} - ) - assert result - assert hass.config.api.base_url == "http://example.com" +def test_api_base_url_with_ip(mock_stack): + """Test setting API URL with IP.""" + api_config = http.ApiConfig("127.0.0.1", "1.1.1.1") + assert api_config.base_url == "http://1.1.1.1:8123" -async def test_api_base_url_with_ip(hass): - """Test setting api url.""" - result = await async_setup_component( - hass, "http", {"http": {"server_host": "1.1.1.1"}} - ) - assert result - assert hass.config.api.base_url == "http://1.1.1.1:8123" +def test_api_base_url_with_ip_and_port(mock_stack): + """Test setting API URL with IP and port.""" + api_config = http.ApiConfig("127.0.0.1", "1.1.1.1", 8124) + assert api_config.base_url == "http://1.1.1.1:8124" -async def test_api_base_url_with_ip_port(hass): - """Test setting api url.""" - result = await async_setup_component( - hass, "http", {"http": {"base_url": "1.1.1.1:8124"}} - ) - assert result - assert hass.config.api.base_url == "http://1.1.1.1:8124" +def test_api_base_url_with_protocol(mock_stack): + """Test setting API URL with protocol.""" + api_config = http.ApiConfig("127.0.0.1", "https://example.com") + assert api_config.base_url == "https://example.com:8123" -async def test_api_no_base_url(hass): +def test_api_base_url_with_protocol_and_port(mock_stack): + """Test setting API URL with protocol and port.""" + api_config = http.ApiConfig("127.0.0.1", "https://example.com", 433) + assert api_config.base_url == "https://example.com:433" + + +def test_api_base_url_with_ssl_enable(mock_stack): + """Test setting API URL with use_ssl enabled.""" + api_config = http.ApiConfig("127.0.0.1", "example.com", use_ssl=True) + assert api_config.base_url == "https://example.com:8123" + + +def test_api_base_url_with_ssl_enable_and_port(mock_stack): + """Test setting API URL with use_ssl enabled and port.""" + api_config = http.ApiConfig("127.0.0.1", "1.1.1.1", use_ssl=True, port=8888) + assert api_config.base_url == "https://1.1.1.1:8888" + + +def test_api_base_url_with_protocol_and_ssl_enable(mock_stack): + """Test setting API URL with specific protocol and use_ssl enabled.""" + api_config = http.ApiConfig("127.0.0.1", "http://example.com", use_ssl=True) + assert api_config.base_url == "http://example.com:8123" + + +def test_api_base_url_removes_trailing_slash(mock_stack): + """Test a trialing slash is removed when setting the API URL.""" + api_config = http.ApiConfig("127.0.0.1", "http://example.com/") + assert api_config.base_url == "http://example.com:8123" + + +def test_api_local_ip(mock_stack): + """Test a trialing slash is removed when setting the API URL.""" + api_config = http.ApiConfig("127.0.0.1", "http://example.com/") + assert api_config.local_ip == "127.0.0.1" + + +async def test_api_no_base_url(hass, mock_stack): """Test setting api url.""" result = await async_setup_component(hass, "http", {"http": {}}) assert result assert hass.config.api.base_url == "http://127.0.0.1:8123" -async def test_api_local_ip(hass): - """Test setting api url.""" - result = await async_setup_component(hass, "http", {"http": {}}) - assert result - assert hass.config.api.local_ip == "127.0.0.1" - - -async def test_api_base_url_removes_trailing_slash(hass): - """Test setting api url.""" - result = await async_setup_component( - hass, "http", {"http": {"base_url": "https://example.com/"}} - ) - assert result - assert hass.config.api.base_url == "https://example.com" - - async def test_not_log_password(hass, aiohttp_client, caplog, legacy_auth): """Test access with password doesn't get logged.""" assert await async_setup_component(hass, "api", {"http": {}}) diff --git a/tests/components/image_processing/test_init.py b/tests/components/image_processing/test_init.py index 9763dbf9415..72edcd7160a 100644 --- a/tests/components/image_processing/test_init.py +++ b/tests/components/image_processing/test_init.py @@ -62,7 +62,7 @@ class TestImageProcessing: self.hass.block_till_done() state = self.hass.states.get("camera.demo_camera") - self.url = f"{self.hass.config.api.base_url}{state.attributes.get(ATTR_ENTITY_PICTURE)}" + self.url = f"{self.hass.config.internal_url}{state.attributes.get(ATTR_ENTITY_PICTURE)}" def teardown_method(self): """Stop everything that was started.""" @@ -117,7 +117,7 @@ class TestImageProcessingAlpr: self.hass.block_till_done() state = self.hass.states.get("camera.demo_camera") - self.url = f"{self.hass.config.api.base_url}{state.attributes.get(ATTR_ENTITY_PICTURE)}" + self.url = f"{self.hass.config.internal_url}{state.attributes.get(ATTR_ENTITY_PICTURE)}" self.alpr_events = [] @@ -223,7 +223,7 @@ class TestImageProcessingFace: self.hass.block_till_done() state = self.hass.states.get("camera.demo_camera") - self.url = f"{self.hass.config.api.base_url}{state.attributes.get(ATTR_ENTITY_PICTURE)}" + self.url = f"{self.hass.config.internal_url}{state.attributes.get(ATTR_ENTITY_PICTURE)}" self.face_events = [] diff --git a/tests/components/microsoft_face_detect/test_image_processing.py b/tests/components/microsoft_face_detect/test_image_processing.py index b6f828318bc..d2e0eb19d57 100644 --- a/tests/components/microsoft_face_detect/test_image_processing.py +++ b/tests/components/microsoft_face_detect/test_image_processing.py @@ -118,7 +118,7 @@ class TestMicrosoftFaceDetect: self.hass.block_till_done() state = self.hass.states.get("camera.demo_camera") - url = f"{self.hass.config.api.base_url}{state.attributes.get(ATTR_ENTITY_PICTURE)}" + url = f"{self.hass.config.internal_url}{state.attributes.get(ATTR_ENTITY_PICTURE)}" face_events = [] diff --git a/tests/components/microsoft_face_identify/test_image_processing.py b/tests/components/microsoft_face_identify/test_image_processing.py index cc45220153e..856d308816c 100644 --- a/tests/components/microsoft_face_identify/test_image_processing.py +++ b/tests/components/microsoft_face_identify/test_image_processing.py @@ -119,7 +119,7 @@ class TestMicrosoftFaceIdentify: self.hass.block_till_done() state = self.hass.states.get("camera.demo_camera") - url = f"{self.hass.config.api.base_url}{state.attributes.get(ATTR_ENTITY_PICTURE)}" + url = f"{self.hass.config.internal_url}{state.attributes.get(ATTR_ENTITY_PICTURE)}" face_events = [] diff --git a/tests/components/openalpr_local/test_image_processing.py b/tests/components/openalpr_local/test_image_processing.py index bcf4fb8aafd..d98c27490e8 100644 --- a/tests/components/openalpr_local/test_image_processing.py +++ b/tests/components/openalpr_local/test_image_processing.py @@ -107,7 +107,7 @@ class TestOpenAlprLocal: self.hass.block_till_done() state = self.hass.states.get("camera.demo_camera") - self.url = f"{self.hass.config.api.base_url}{state.attributes.get(ATTR_ENTITY_PICTURE)}" + self.url = f"{self.hass.config.internal_url}{state.attributes.get(ATTR_ENTITY_PICTURE)}" self.alpr_events = []