def app(hass): """Fixture to setup a web.Application.""" app = web.Application() app['hass'] = hass app.router.add_get('/', mock_handler) setup_real_ip(app, False, []) return app
def app(hass): """Fixture to set up a web.Application.""" app = web.Application() app['hass'] = hass app.router.add_get('/', mock_handler) setup_real_ip(app, False, []) return app
def app(): """Fixture to setup a web.Application.""" app = web.Application() mock_auth = Mock(async_get_access_token=mock_async_get_access_token) app['hass'] = Mock(auth=mock_auth) app.router.add_get('/', mock_handler) setup_real_ip(app, False, []) return app
def app(): """Fixture to setup a web.Application.""" app = web.Application() mock_auth = Mock(async_get_access_token=mock_async_get_access_token) app['hass'] = Mock(auth=mock_auth) app.router.add_get('/', mock_handler) setup_real_ip(app, False, []) return app
async def test_use_x_forwarded_for_with_nonsense_header(aiohttp_client): """Test that we get the IP from the transport.""" app = web.Application() app.router.add_get('/', mock_handler) setup_real_ip(app, True, [ip_network('127.0.0.1')]) mock_api_client = await aiohttp_client(app) resp = await mock_api_client.get( '/', headers={X_FORWARDED_FOR: 'This value is invalid'}) assert resp.status == 200 text = await resp.text() assert text == '127.0.0.1'
async def test_use_x_forwarded_for_with_untrusted_proxy(aiohttp_client): """Test that we get the IP from the transport.""" app = web.Application() app.router.add_get('/', mock_handler) setup_real_ip(app, True, [ip_network('1.1.1.1')]) mock_api_client = await aiohttp_client(app) resp = await mock_api_client.get( '/', headers={X_FORWARDED_FOR: '255.255.255.255'}) assert resp.status == 200 text = await resp.text() assert text != '255.255.255.255'
async def test_ignore_x_forwarded_for(aiohttp_client): """Test that we get the IP from the transport.""" app = web.Application() app.router.add_get('/', mock_handler) setup_real_ip(app, False, []) mock_api_client = await aiohttp_client(app) resp = await mock_api_client.get( '/', headers={X_FORWARDED_FOR: '255.255.255.255'}) assert resp.status == 200 text = await resp.text() assert text != '255.255.255.255'
async def test_use_x_forwarded_for_with_spoofed_header(aiohttp_client): """Test that we get the IP from the transport.""" app = web.Application() app.router.add_get("/", mock_handler) setup_real_ip(app, True, [ip_network("127.0.0.1")]) mock_api_client = await aiohttp_client(app) resp = await mock_api_client.get( "/", headers={X_FORWARDED_FOR: "222.222.222.222, 255.255.255.255"}) assert resp.status == 200 text = await resp.text() assert text == "255.255.255.255"
def test_use_x_forwarded_for(test_client): """Test that we get the IP from the transport.""" app = web.Application() app.router.add_get('/', mock_handler) setup_real_ip(app, True) mock_api_client = yield from test_client(app) resp = yield from mock_api_client.get('/', headers={ X_FORWARDED_FOR: '255.255.255.255' }) assert resp.status == 200 text = yield from resp.text() assert text == '255.255.255.255'
async def test_use_x_forwarded_for_with_nonsense_header(aiohttp_client): """Test that we get the IP from the transport.""" app = web.Application() app.router.add_get('/', mock_handler) setup_real_ip(app, True, [ip_network('127.0.0.1')]) mock_api_client = await aiohttp_client(app) resp = await mock_api_client.get('/', headers={ X_FORWARDED_FOR: 'This value is invalid' }) assert resp.status == 200 text = await resp.text() assert text == '127.0.0.1'
async def test_use_x_forwarded_for_with_untrusted_proxy(aiohttp_client): """Test that we get the IP from the transport.""" app = web.Application() app.router.add_get('/', mock_handler) setup_real_ip(app, True, [ip_network('1.1.1.1')]) mock_api_client = await aiohttp_client(app) resp = await mock_api_client.get('/', headers={ X_FORWARDED_FOR: '255.255.255.255' }) assert resp.status == 200 text = await resp.text() assert text != '255.255.255.255'
async def test_ignore_x_forwarded_for(aiohttp_client): """Test that we get the IP from the transport.""" app = web.Application() app.router.add_get('/', mock_handler) setup_real_ip(app, False, []) mock_api_client = await aiohttp_client(app) resp = await mock_api_client.get('/', headers={ X_FORWARDED_FOR: '255.255.255.255' }) assert resp.status == 200 text = await resp.text() assert text != '255.255.255.255'
async def async_setup(hass, yaml_config): """Activate the emulated_hue component.""" config = Config(hass, yaml_config.get(DOMAIN, {})) app = web.Application() app["hass"] = hass real_ip.setup_real_ip(app, False, []) # We misunderstood the startup signal. You're not allowed to change # anything during startup. Temp workaround. # pylint: disable=protected-access app._on_startup.freeze() await app.startup() runner = None site = None DescriptionXmlView(config).register(app, app.router) HueUsernameView().register(app, app.router) HueAllLightsStateView(config).register(app, app.router) HueOneLightStateView(config).register(app, app.router) HueOneLightChangeView(config).register(app, app.router) HueAllGroupsStateView(config).register(app, app.router) HueGroupView(config).register(app, app.router) upnp_listener = UPNPResponderThread( config.host_ip_addr, config.listen_port, config.upnp_bind_multicast, config.advertise_ip, config.advertise_port, ) async def stop_emulated_hue_bridge(event): """Stop the emulated hue bridge.""" upnp_listener.stop() if site: await site.stop() if runner: await runner.cleanup() async def start_emulated_hue_bridge(event): """Start the emulated hue bridge.""" upnp_listener.start() nonlocal site nonlocal runner runner = web.AppRunner(app) await runner.setup() site = web.TCPSite(runner, config.host_ip_addr, config.listen_port) try: await site.start() except OSError as error: _LOGGER.error( "Failed to create HTTP server at port %d: %s", config.listen_port, error ) else: hass.bus.async_listen_once( EVENT_HOMEASSISTANT_STOP, stop_emulated_hue_bridge ) hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, start_emulated_hue_bridge) return True
async def async_setup(hass, yaml_config): """Activate the emulated_hue component.""" config = Config(hass, yaml_config.get(DOMAIN, {})) app = web.Application() app['hass'] = hass real_ip.setup_real_ip(app, False, []) # We misunderstood the startup signal. You're not allowed to change # anything during startup. Temp workaround. # pylint: disable=protected-access app._on_startup.freeze() await app.startup() runner = None site = None DescriptionXmlView(config).register(app, app.router) HueUsernameView().register(app, app.router) HueAllLightsStateView(config).register(app, app.router) HueOneLightStateView(config).register(app, app.router) HueOneLightChangeView(config).register(app, app.router) HueAllGroupsStateView(config).register(app, app.router) HueGroupView(config).register(app, app.router) upnp_listener = UPNPResponderThread( config.host_ip_addr, config.listen_port, config.upnp_bind_multicast, config.advertise_ip, config.advertise_port) async def stop_emulated_hue_bridge(event): """Stop the emulated hue bridge.""" upnp_listener.stop() if site: await site.stop() if runner: await runner.cleanup() async def start_emulated_hue_bridge(event): """Start the emulated hue bridge.""" upnp_listener.start() nonlocal site nonlocal runner runner = web.AppRunner(app) await runner.setup() site = web.TCPSite(runner, config.host_ip_addr, config.listen_port) try: await site.start() except OSError as error: _LOGGER.error("Failed to create HTTP server at port %d: %s", config.listen_port, error) else: hass.bus.async_listen_once( EVENT_HOMEASSISTANT_STOP, stop_emulated_hue_bridge) hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, start_emulated_hue_bridge) return True