Detect use of deprecated base_url (#35353)

* Detect use of deprecated base_url

* Update get_url helper

* Update core migration

* Migrate all tests
This commit is contained in:
Franck Nijhof authored and GitHub committed 2020-05-08 17:52:32 +02:00
1 parent 1be41b9de8
commit e56dd8ed50
21 files changed
+308 -73

No files matched your search

+4 -1
View File
@@ -2,6 +2,7 @@
import pytest
from homeassistant import config_entries, data_entry_flow, setup
from homeassistant.config import async_process_ha_core_config
from homeassistant.helpers import config_entry_flow
from tests.async_mock import Mock, patch
@@ -232,7 +233,9 @@ async def test_webhook_config_flow_registers_webhook(hass, webhook_flow_conf):
flow = config_entries.HANDLERS["test_single"]()
flow.hass = hass
hass.config.api = Mock(base_url="http://example.com")
await async_process_ha_core_config(
hass, {"external_url": "https://example.com"},
)
result = await flow.async_step_user(user_input={})
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
+15 -4
View File
@@ -6,6 +6,7 @@ import time
import pytest
from homeassistant import config_entries, data_entry_flow, setup
from homeassistant.config import async_process_ha_core_config
from homeassistant.helpers import config_entry_oauth2_flow
from tests.async_mock import patch
@@ -124,7 +125,9 @@ async def test_abort_if_authorization_timeout(hass, flow_handler, local_impl):
async def test_step_discovery(hass, flow_handler, local_impl):
"""Check flow triggers from discovery."""
hass.config.api.base_url = "https://example.com"
await async_process_ha_core_config(
hass, {"external_url": "https://example.com"},
)
flow_handler.async_register_implementation(hass, local_impl)
config_entry_oauth2_flow.async_register_implementation(
hass, TEST_DOMAIN, MockOAuth2Implementation()
@@ -140,7 +143,10 @@ async def test_step_discovery(hass, flow_handler, local_impl):
async def test_abort_discovered_multiple(hass, flow_handler, local_impl):
"""Test if aborts when discovered multiple times."""
hass.config.api.base_url = "https://example.com"
await async_process_ha_core_config(
hass, {"external_url": "https://example.com"},
)
flow_handler.async_register_implementation(hass, local_impl)
config_entry_oauth2_flow.async_register_implementation(
hass, TEST_DOMAIN, MockOAuth2Implementation()
@@ -163,7 +169,9 @@ async def test_abort_discovered_multiple(hass, flow_handler, local_impl):
async def test_abort_discovered_existing_entries(hass, flow_handler, local_impl):
"""Test if abort discovery when entries exists."""
hass.config.api.base_url = "https://example.com"
await async_process_ha_core_config(
hass, {"external_url": "https://example.com"},
)
flow_handler.async_register_implementation(hass, local_impl)
config_entry_oauth2_flow.async_register_implementation(
hass, TEST_DOMAIN, MockOAuth2Implementation()
@@ -184,7 +192,10 @@ async def test_full_flow(
hass, flow_handler, local_impl, aiohttp_client, aioclient_mock
):
"""Check full flow."""
hass.config.api.base_url = "https://example.com"
await async_process_ha_core_config(
hass, {"external_url": "https://example.com"},
)
flow_handler.async_register_implementation(hass, local_impl)
config_entry_oauth2_flow.async_register_implementation(
hass, TEST_DOMAIN, MockOAuth2Implementation()
+24 -20
View File
@@ -119,7 +119,7 @@ async def test_get_url_internal_fallback(hass: HomeAssistant):
assert hass.config.internal_url is None
hass.config.api = Mock(
use_ssl=False, port=8123, base_url=None, local_ip="192.168.123.123"
use_ssl=False, port=8123, deprecated_base_url=None, local_ip="192.168.123.123"
)
assert _async_get_internal_url(hass) == "http://192.168.123.123:8123"
@@ -133,7 +133,7 @@ async def test_get_url_internal_fallback(hass: HomeAssistant):
_async_get_internal_url(hass, require_ssl=True)
hass.config.api = Mock(
use_ssl=False, port=80, base_url=None, local_ip="192.168.123.123"
use_ssl=False, port=80, deprecated_base_url=None, local_ip="192.168.123.123"
)
assert _async_get_internal_url(hass) == "http://192.168.123.123"
assert (
@@ -147,7 +147,7 @@ async def test_get_url_internal_fallback(hass: HomeAssistant):
with pytest.raises(NoURLAvailableError):
_async_get_internal_url(hass, require_ssl=True)
hass.config.api = Mock(use_ssl=True, port=443, base_url=None)
hass.config.api = Mock(use_ssl=True, port=443, deprecated_base_url=None)
with pytest.raises(NoURLAvailableError):
_async_get_internal_url(hass)
@@ -161,7 +161,9 @@ async def test_get_url_internal_fallback(hass: HomeAssistant):
_async_get_internal_url(hass, require_ssl=True)
# Do no accept any local loopback address as fallback
hass.config.api = Mock(use_ssl=False, port=80, base_url=None, local_ip="127.0.0.1")
hass.config.api = Mock(
use_ssl=False, port=80, deprecated_base_url=None, local_ip="127.0.0.1"
)
with pytest.raises(NoURLAvailableError):
_async_get_internal_url(hass)
@@ -367,7 +369,7 @@ async def test_get_url(hass: HomeAssistant):
async_get_url(hass)
hass.config.api = Mock(
use_ssl=False, port=8123, base_url=None, local_ip="192.168.123.123"
use_ssl=False, port=8123, deprecated_base_url=None, local_ip="192.168.123.123"
)
assert async_get_url(hass) == "http://192.168.123.123:8123"
assert async_get_url(hass, prefer_external=True) == "http://192.168.123.123:8123"
@@ -409,7 +411,7 @@ async def test_get_url(hass: HomeAssistant):
async def test_get_deprecated_base_url_internal(hass: HomeAssistant):
"""Test getting an internal instance URL from the deprecated base_url."""
# Test with SSL local URL
hass.config.api = Mock(base_url="https://example.local")
hass.config.api = Mock(deprecated_base_url="https://example.local")
assert (
_async_get_deprecated_base_url(hass, internal=True) == "https://example.local"
)
@@ -427,7 +429,7 @@ async def test_get_deprecated_base_url_internal(hass: HomeAssistant):
)
# Test with no SSL, local IP URL
hass.config.api = Mock(base_url="http://10.10.10.10:8123")
hass.config.api = Mock(deprecated_base_url="http://10.10.10.10:8123")
assert (
_async_get_deprecated_base_url(hass, internal=True) == "http://10.10.10.10:8123"
)
@@ -442,7 +444,7 @@ async def test_get_deprecated_base_url_internal(hass: HomeAssistant):
_async_get_deprecated_base_url(hass, internal=True, require_standard_port=True)
# Test with SSL, local IP URL
hass.config.api = Mock(base_url="https://10.10.10.10")
hass.config.api = Mock(deprecated_base_url="https://10.10.10.10")
assert _async_get_deprecated_base_url(hass, internal=True) == "https://10.10.10.10"
assert (
_async_get_deprecated_base_url(hass, internal=True, require_ssl=True)
@@ -454,7 +456,7 @@ async def test_get_deprecated_base_url_internal(hass: HomeAssistant):
)
# Test external URL
hass.config.api = Mock(base_url="https://example.com")
hass.config.api = Mock(deprecated_base_url="https://example.com")
with pytest.raises(NoURLAvailableError):
_async_get_deprecated_base_url(hass, internal=True)
@@ -468,7 +470,7 @@ async def test_get_deprecated_base_url_internal(hass: HomeAssistant):
_async_get_deprecated_base_url(hass, internal=True, allow_ip=False)
# Test with loopback
hass.config.api = Mock(base_url="https://127.0.0.42")
hass.config.api = Mock(deprecated_base_url="https://127.0.0.42")
with pytest.raises(NoURLAvailableError):
assert _async_get_deprecated_base_url(hass, internal=True)
@@ -485,7 +487,7 @@ async def test_get_deprecated_base_url_internal(hass: HomeAssistant):
async def test_get_deprecated_base_url_external(hass: HomeAssistant):
"""Test getting an external instance URL from the deprecated base_url."""
# Test with SSL and external domain on standard port
hass.config.api = Mock(base_url="https://example.com:443/")
hass.config.api = Mock(deprecated_base_url="https://example.com:443/")
assert _async_get_deprecated_base_url(hass) == "https://example.com"
assert (
_async_get_deprecated_base_url(hass, require_ssl=True) == "https://example.com"
@@ -496,7 +498,7 @@ async def test_get_deprecated_base_url_external(hass: HomeAssistant):
)
# Test without SSL and external domain on non-standard port
hass.config.api = Mock(base_url="http://example.com:8123/")
hass.config.api = Mock(deprecated_base_url="http://example.com:8123/")
assert _async_get_deprecated_base_url(hass) == "http://example.com:8123"
with pytest.raises(NoURLAvailableError):
@@ -506,7 +508,7 @@ async def test_get_deprecated_base_url_external(hass: HomeAssistant):
_async_get_deprecated_base_url(hass, require_standard_port=True)
# Test SSL on external IP
hass.config.api = Mock(base_url="https://1.1.1.1")
hass.config.api = Mock(deprecated_base_url="https://1.1.1.1")
assert _async_get_deprecated_base_url(hass) == "https://1.1.1.1"
assert _async_get_deprecated_base_url(hass, require_ssl=True) == "https://1.1.1.1"
assert (
@@ -518,7 +520,7 @@ async def test_get_deprecated_base_url_external(hass: HomeAssistant):
_async_get_deprecated_base_url(hass, allow_ip=False)
# Test with private IP
hass.config.api = Mock(base_url="https://10.10.10.10")
hass.config.api = Mock(deprecated_base_url="https://10.10.10.10")
with pytest.raises(NoURLAvailableError):
assert _async_get_deprecated_base_url(hass)
@@ -532,7 +534,7 @@ async def test_get_deprecated_base_url_external(hass: HomeAssistant):
_async_get_deprecated_base_url(hass, require_standard_port=True)
# Test with local domain
hass.config.api = Mock(base_url="https://example.local")
hass.config.api = Mock(deprecated_base_url="https://example.local")
with pytest.raises(NoURLAvailableError):
assert _async_get_deprecated_base_url(hass)
@@ -546,7 +548,7 @@ async def test_get_deprecated_base_url_external(hass: HomeAssistant):
_async_get_deprecated_base_url(hass, require_standard_port=True)
# Test with loopback
hass.config.api = Mock(base_url="https://127.0.0.42")
hass.config.api = Mock(deprecated_base_url="https://127.0.0.42")
with pytest.raises(NoURLAvailableError):
assert _async_get_deprecated_base_url(hass)
@@ -563,7 +565,7 @@ async def test_get_deprecated_base_url_external(hass: HomeAssistant):
async def test_get_internal_url_with_base_url_fallback(hass: HomeAssistant):
"""Test getting an internal instance URL with the deprecated base_url fallback."""
hass.config.api = Mock(
use_ssl=False, port=8123, base_url=None, local_ip="192.168.123.123"
use_ssl=False, port=8123, deprecated_base_url=None, local_ip="192.168.123.123"
)
assert hass.config.internal_url is None
assert _async_get_internal_url(hass) == "http://192.168.123.123:8123"
@@ -578,7 +580,9 @@ async def test_get_internal_url_with_base_url_fallback(hass: HomeAssistant):
_async_get_internal_url(hass, require_ssl=True)
# Add base_url
hass.config.api = Mock(use_ssl=False, port=8123, base_url="https://example.local")
hass.config.api = Mock(
use_ssl=False, port=8123, deprecated_base_url="https://example.local"
)
assert _async_get_internal_url(hass) == "https://example.local"
assert _async_get_internal_url(hass, allow_ip=False) == "https://example.local"
assert (
@@ -626,14 +630,14 @@ async def test_get_internal_url_with_base_url_fallback(hass: HomeAssistant):
async def test_get_external_url_with_base_url_fallback(hass: HomeAssistant):
"""Test getting an external instance URL with the deprecated base_url fallback."""
hass.config.api = Mock(use_ssl=False, port=8123, base_url=None)
hass.config.api = Mock(use_ssl=False, port=8123, deprecated_base_url=None)
assert hass.config.internal_url is None
with pytest.raises(NoURLAvailableError):
_async_get_external_url(hass)
# Test with SSL and external domain on standard port
hass.config.api = Mock(base_url="https://example.com:443/")
hass.config.api = Mock(deprecated_base_url="https://example.com:443/")
assert _async_get_external_url(hass) == "https://example.com"
assert _async_get_external_url(hass, allow_ip=False) == "https://example.com"
assert _async_get_external_url(hass, require_ssl=True) == "https://example.com"