Fix CORS error in emulated_hue (#59570)

This commit is contained in:
Paulus Schoutsen 2021-11-11 22:11:41 -08:00 committed by GitHub
parent 8de0c7204a
commit 6ef64f6b1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 5 deletions

View File

@ -94,11 +94,15 @@ class HomeAssistantView:
for url in urls: for url in urls:
routes.append(router.add_route(method, url, handler)) routes.append(router.add_route(method, url, handler))
allow_cors = ( # Use `get` because CORS middleware is not be loaded in emulated_hue
app["allow_all_cors"] if self.cors_allowed else app["allow_configured_cors"] if self.cors_allowed:
) allow_cors = app.get("allow_all_cors")
for route in routes: else:
allow_cors(route) allow_cors = app.get("allow_configured_cors")
if allow_cors:
for route in routes:
allow_cors(route)
def request_handler_factory( def request_handler_factory(

View File

@ -1,5 +1,6 @@
"""Test the Emulated Hue component.""" """Test the Emulated Hue component."""
from datetime import timedelta from datetime import timedelta
from unittest.mock import patch
from homeassistant.components.emulated_hue import ( from homeassistant.components.emulated_hue import (
DATA_KEY, DATA_KEY,
@ -7,6 +8,7 @@ from homeassistant.components.emulated_hue import (
SAVE_DELAY, SAVE_DELAY,
Config, Config,
) )
from homeassistant.setup import async_setup_component
from homeassistant.util import utcnow from homeassistant.util import utcnow
from tests.common import async_fire_time_changed from tests.common import async_fire_time_changed
@ -113,3 +115,12 @@ def test_config_alexa_entity_id_to_number():
entity_id = conf.number_to_entity_id("light.test") entity_id = conf.number_to_entity_id("light.test")
assert entity_id == "light.test" assert entity_id == "light.test"
async def test_setup_works(hass):
"""Test setup works."""
hass.config.components.add("network")
with patch(
"homeassistant.components.emulated_hue.create_upnp_datagram_endpoint"
), patch("homeassistant.components.emulated_hue.async_get_source_ip"):
assert await async_setup_component(hass, "emulated_hue", {})