From 74b0740e1cfc3f7fd702434926df00073f56c943 Mon Sep 17 00:00:00 2001 From: Rene Nulsch <33263735+ReneNulschDE@users.noreply.github.com> Date: Sat, 27 Jan 2018 07:30:39 +0100 Subject: [PATCH] Weblink - Allow relative urls in config (#11808) * Allow relative url * Allow absolute urls in config schema * change after pylint build * Add tests and change error message * Change regex to check starting forward slash only * Change error message and const name --- homeassistant/components/weblink.py | 7 +++- tests/components/test_weblink.py | 65 +++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/weblink.py b/homeassistant/components/weblink.py index f55fe1f0bb5..a20b0fc9b0c 100644 --- a/homeassistant/components/weblink.py +++ b/homeassistant/components/weblink.py @@ -16,12 +16,15 @@ import homeassistant.helpers.config_validation as cv _LOGGER = logging.getLogger(__name__) CONF_ENTITIES = 'entities' +CONF_RELATIVE_URL_ERROR_MSG = "Invalid relative URL. Absolute path required." +CONF_RELATIVE_URL_REGEX = r'\A/' DOMAIN = 'weblink' ENTITIES_SCHEMA = vol.Schema({ - # pylint: disable=no-value-for-parameter - vol.Required(CONF_URL): vol.Url(), + vol.Required(CONF_URL): vol.Any( + vol.Match(CONF_RELATIVE_URL_REGEX, msg=CONF_RELATIVE_URL_ERROR_MSG), + cv.url), vol.Required(CONF_NAME): cv.string, vol.Optional(CONF_ICON): cv.icon, }) diff --git a/tests/components/test_weblink.py b/tests/components/test_weblink.py index e8768342db9..249e81d37af 100644 --- a/tests/components/test_weblink.py +++ b/tests/components/test_weblink.py @@ -26,6 +26,71 @@ class TestComponentWeblink(unittest.TestCase): } })) + def test_bad_config_relative_url(self): + """Test if new entity is created.""" + self.assertFalse(setup_component(self.hass, 'weblink', { + 'weblink': { + 'entities': [ + { + weblink.CONF_NAME: 'My router', + weblink.CONF_URL: '../states/group.bla' + }, + ], + } + })) + + def test_bad_config_relative_file(self): + """Test if new entity is created.""" + self.assertFalse(setup_component(self.hass, 'weblink', { + 'weblink': { + 'entities': [ + { + weblink.CONF_NAME: 'My group', + weblink.CONF_URL: 'group.bla' + }, + ], + } + })) + + def test_good_config_absolute_path(self): + """Test if new entity is created.""" + self.assertTrue(setup_component(self.hass, 'weblink', { + 'weblink': { + 'entities': [ + { + weblink.CONF_NAME: 'My second URL', + weblink.CONF_URL: '/states/group.bla' + }, + ], + } + })) + + def test_good_config_path_short(self): + """Test if new entity is created.""" + self.assertTrue(setup_component(self.hass, 'weblink', { + 'weblink': { + 'entities': [ + { + weblink.CONF_NAME: 'My third URL', + weblink.CONF_URL: '/states' + }, + ], + } + })) + + def test_good_config_path_directory(self): + """Test if new entity is created.""" + self.assertTrue(setup_component(self.hass, 'weblink', { + 'weblink': { + 'entities': [ + { + weblink.CONF_NAME: 'My last URL', + weblink.CONF_URL: '/states/bla/' + }, + ], + } + })) + def test_entities_get_created(self): """Test if new entity is created.""" self.assertTrue(setup_component(self.hass, weblink.DOMAIN, {