diff --git a/homeassistant/core.py b/homeassistant/core.py index 53dc75472c76b6135cdf271828793fc704db6654..ca4a27eef3da8cc6ebdda69bebeb338243364ff9 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -1530,7 +1530,7 @@ class Config: self.safe_mode: bool = False # Use legacy template behavior - self.legacy_templates: bool = False + self.legacy_templates: bool = True def distance(self, lat: float, lon: float) -> Optional[float]: """Calculate distance from Home Assistant. diff --git a/tests/common.py b/tests/common.py index 8f4ceb5754688e18c72c7c42dbcd1edaefb91cd5..0b56d10188e5532aea05552c5a9655a398ca08a3 100644 --- a/tests/common.py +++ b/tests/common.py @@ -207,6 +207,7 @@ async def async_test_home_assistant(loop): hass.config.units = METRIC_SYSTEM hass.config.media_dirs = {"local": get_test_config_dir("media")} hass.config.skip_pip = True + hass.config.legacy_templates = False hass.config_entries = config_entries.ConfigEntries(hass, {}) hass.config_entries._entries = [] diff --git a/tests/test_core.py b/tests/test_core.py index 9c5ab2059720d4027a548083ed008eb5c5a19a54..119c0269ba755fb09f07b5b8aec70fee189d2fe2 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -885,108 +885,132 @@ class TestServiceRegistry(unittest.TestCase): self.hass.block_till_done() -class TestConfig(unittest.TestCase): - """Test configuration methods.""" +def test_config_defaults(): + """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 + + +def test_config_path_with_file(): + """Test get_config_path method.""" + config = ha.Config(None) + config.config_dir = "/test/ha-config" + assert config.path("test.conf") == "/test/ha-config/test.conf" + + +def test_config_path_with_dir_and_file(): + """Test get_config_path method.""" + config = ha.Config(None) + config.config_dir = "/test/ha-config" + assert config.path("dir", "test.conf") == "/test/ha-config/dir/test.conf" + + +def test_config_as_dict(): + """Test as dict.""" + config = ha.Config(None) + config.config_dir = "/test/ha-config" + config.hass = MagicMock() + 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, + } - # 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): - """Test get_config_path method.""" - self.config.config_dir = "/test/ha-config" - assert self.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): - """Test as dict.""" - self.config.config_dir = "/test/ha-config" - self.config.hass = MagicMock() - type(self.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, - } + assert expected == config.as_dict() - assert expected == self.config.as_dict() - - def test_is_allowed_path(self): - """Test is_allowed_path method.""" - 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. - self.config.allowlist_external_dirs = {os.path.realpath(tmp_dir)} - - test_file = os.path.join(tmp_dir, "test.jpg") - 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"} - - invalid = [ - "/hass/config/secure", - "/etc/passwd", - "/root/secure_file", - "/var/../etc/passwd", - test_file, - ] - for path in invalid: - assert not self.config.is_allowed_path(path) - - with pytest.raises(AssertionError): - self.config.is_allowed_path(None) - - def test_is_allowed_external_url(self): - """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) +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)} + + test_file = os.path.join(tmp_dir, "test.jpg") + 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 config.is_allowed_path(path) + + config.allowlist_external_dirs = {"/home", "/var"} invalid = [ - "https://a.co", - "https://y.com/bla_wrong", - "https://y.com/bla/../image.jpg", - "https://z.com/images", + "/hass/config/secure", + "/etc/passwd", + "/root/secure_file", + "/var/../etc/passwd", + test_file, ] - for url in invalid: - assert not self.config.is_allowed_external_url(url) + for path in invalid: + 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):