Default legacy templates to true (#42511)

* Default legacy templates to true

* Disable legacy_templates in tests

Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
Paulus Schoutsen 2020-10-28 16:58:16 +01:00 committed by Franck Nijhof
parent 5b85776566
commit a34d06e363
No known key found for this signature in database
GPG key ID: D62583BA8AB11CA3
3 changed files with 113 additions and 88 deletions

View file

@ -1530,7 +1530,7 @@ class Config:
self.safe_mode: bool = False self.safe_mode: bool = False
# Use legacy template behavior # Use legacy template behavior
self.legacy_templates: bool = False self.legacy_templates: bool = True
def distance(self, lat: float, lon: float) -> Optional[float]: def distance(self, lat: float, lon: float) -> Optional[float]:
"""Calculate distance from Home Assistant. """Calculate distance from Home Assistant.

View file

@ -207,6 +207,7 @@ async def async_test_home_assistant(loop):
hass.config.units = METRIC_SYSTEM hass.config.units = METRIC_SYSTEM
hass.config.media_dirs = {"local": get_test_config_dir("media")} hass.config.media_dirs = {"local": get_test_config_dir("media")}
hass.config.skip_pip = True hass.config.skip_pip = True
hass.config.legacy_templates = False
hass.config_entries = config_entries.ConfigEntries(hass, {}) hass.config_entries = config_entries.ConfigEntries(hass, {})
hass.config_entries._entries = [] hass.config_entries._entries = []

View file

@ -885,108 +885,132 @@ class TestServiceRegistry(unittest.TestCase):
self.hass.block_till_done() self.hass.block_till_done()
class TestConfig(unittest.TestCase): def test_config_defaults():
"""Test configuration methods.""" """Test config defaults."""
hass = Mock()
config = ha.Config(hass)
assert config.hass is hass
assert config.latitude == 0
assert config.longitude == 0
assert config.elevation == 0
assert config.location_name == "Home"
assert config.time_zone == dt_util.UTC
assert config.internal_url is None
assert config.external_url is None
assert config.config_source == "default"
assert config.skip_pip is False
assert config.components == set()
assert config.api is None
assert config.config_dir is None
assert config.allowlist_external_dirs == set()
assert config.allowlist_external_urls == set()
assert config.media_dirs == {}
assert config.safe_mode is False
assert config.legacy_templates is True
# pylint: disable=invalid-name
def setUp(self):
"""Set up things to be run when tests are started."""
self.config = ha.Config(None)
assert self.config.config_dir is None
def test_path_with_file(self): def test_config_path_with_file():
"""Test get_config_path method.""" """Test get_config_path method."""
self.config.config_dir = "/test/ha-config" config = ha.Config(None)
assert self.config.path("test.conf") == "/test/ha-config/test.conf" config.config_dir = "/test/ha-config"
assert config.path("test.conf") == "/test/ha-config/test.conf"
def test_path_with_dir_and_file(self):
"""Test get_config_path method."""
self.config.config_dir = "/test/ha-config"
assert self.config.path("dir", "test.conf") == "/test/ha-config/dir/test.conf"
def test_as_dict(self): def test_config_path_with_dir_and_file():
"""Test as dict.""" """Test get_config_path method."""
self.config.config_dir = "/test/ha-config" config = ha.Config(None)
self.config.hass = MagicMock() config.config_dir = "/test/ha-config"
type(self.config.hass.state).value = PropertyMock(return_value="RUNNING") assert config.path("dir", "test.conf") == "/test/ha-config/dir/test.conf"
expected = {
"latitude": 0,
"longitude": 0,
"elevation": 0,
CONF_UNIT_SYSTEM: METRIC_SYSTEM.as_dict(),
"location_name": "Home",
"time_zone": "UTC",
"components": set(),
"config_dir": "/test/ha-config",
"whitelist_external_dirs": set(),
"allowlist_external_dirs": set(),
"allowlist_external_urls": set(),
"version": __version__,
"config_source": "default",
"safe_mode": False,
"state": "RUNNING",
"external_url": None,
"internal_url": None,
}
assert expected == self.config.as_dict()
def test_is_allowed_path(self): def test_config_as_dict():
"""Test is_allowed_path method.""" """Test as dict."""
with TemporaryDirectory() as tmp_dir: config = ha.Config(None)
# The created dir is in /tmp. This is a symlink on OS X config.config_dir = "/test/ha-config"
# causing this test to fail unless we resolve path first. config.hass = MagicMock()
self.config.allowlist_external_dirs = {os.path.realpath(tmp_dir)} type(config.hass.state).value = PropertyMock(return_value="RUNNING")
expected = {
"latitude": 0,
"longitude": 0,
"elevation": 0,
CONF_UNIT_SYSTEM: METRIC_SYSTEM.as_dict(),
"location_name": "Home",
"time_zone": "UTC",
"components": set(),
"config_dir": "/test/ha-config",
"whitelist_external_dirs": set(),
"allowlist_external_dirs": set(),
"allowlist_external_urls": set(),
"version": __version__,
"config_source": "default",
"safe_mode": False,
"state": "RUNNING",
"external_url": None,
"internal_url": None,
}
test_file = os.path.join(tmp_dir, "test.jpg") assert expected == config.as_dict()
with open(test_file, "w") as tmp_file:
tmp_file.write("test")
valid = [test_file, tmp_dir, os.path.join(tmp_dir, "notfound321")]
for path in valid:
assert self.config.is_allowed_path(path)
self.config.allowlist_external_dirs = {"/home", "/var"} def test_config_is_allowed_path():
"""Test is_allowed_path method."""
config = ha.Config(None)
with TemporaryDirectory() as tmp_dir:
# The created dir is in /tmp. This is a symlink on OS X
# causing this test to fail unless we resolve path first.
config.allowlist_external_dirs = {os.path.realpath(tmp_dir)}
invalid = [ test_file = os.path.join(tmp_dir, "test.jpg")
"/hass/config/secure", with open(test_file, "w") as tmp_file:
"/etc/passwd", tmp_file.write("test")
"/root/secure_file",
"/var/../etc/passwd",
test_file,
]
for path in invalid:
assert not self.config.is_allowed_path(path)
with pytest.raises(AssertionError): valid = [test_file, tmp_dir, os.path.join(tmp_dir, "notfound321")]
self.config.is_allowed_path(None) for path in valid:
assert config.is_allowed_path(path)
def test_is_allowed_external_url(self): config.allowlist_external_dirs = {"/home", "/var"}
"""Test is_allowed_external_url method."""
self.config.allowlist_external_urls = [
"http://x.com/",
"https://y.com/bla/",
"https://z.com/images/1.jpg/",
]
valid = [
"http://x.com/1.jpg",
"http://x.com",
"https://y.com/bla/",
"https://y.com/bla/2.png",
"https://z.com/images/1.jpg",
]
for url in valid:
assert self.config.is_allowed_external_url(url)
invalid = [ invalid = [
"https://a.co", "/hass/config/secure",
"https://y.com/bla_wrong", "/etc/passwd",
"https://y.com/bla/../image.jpg", "/root/secure_file",
"https://z.com/images", "/var/../etc/passwd",
test_file,
] ]
for url in invalid: for path in invalid:
assert not self.config.is_allowed_external_url(url) assert not config.is_allowed_path(path)
with pytest.raises(AssertionError):
config.is_allowed_path(None)
def test_config_is_allowed_external_url():
"""Test is_allowed_external_url method."""
config = ha.Config(None)
config.allowlist_external_urls = [
"http://x.com/",
"https://y.com/bla/",
"https://z.com/images/1.jpg/",
]
valid = [
"http://x.com/1.jpg",
"http://x.com",
"https://y.com/bla/",
"https://y.com/bla/2.png",
"https://z.com/images/1.jpg",
]
for url in valid:
assert config.is_allowed_external_url(url)
invalid = [
"https://a.co",
"https://y.com/bla_wrong",
"https://y.com/bla/../image.jpg",
"https://z.com/images",
]
for url in invalid:
assert not config.is_allowed_external_url(url)
async def test_event_on_update(hass): async def test_event_on_update(hass):