From 755761867dec2962e338b7b8019526a3e5991f44 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Sat, 15 Aug 2020 15:26:54 +0200 Subject: [PATCH] Use current request context in OAuth redirect (#38692) --- .../helpers/config_entry_oauth2_flow.py | 2 +- tests/components/almond/test_config_flow.py | 2 +- .../home_connect/test_config_flow.py | 2 +- tests/components/netatmo/test_config_flow.py | 2 +- tests/components/smappee/test_config_flow.py | 2 +- tests/components/somfy/test_config_flow.py | 2 +- tests/components/spotify/test_config_flow.py | 6 ++++-- tests/components/toon/test_config_flow.py | 19 ++++++++++++------- tests/conftest.py | 18 +++++++++++++++++- .../helpers/test_config_entry_oauth2_flow.py | 2 +- 10 files changed, 40 insertions(+), 17 deletions(-) diff --git a/homeassistant/helpers/config_entry_oauth2_flow.py b/homeassistant/helpers/config_entry_oauth2_flow.py index acaa0e52ab1..b845db966bb 100644 --- a/homeassistant/helpers/config_entry_oauth2_flow.py +++ b/homeassistant/helpers/config_entry_oauth2_flow.py @@ -118,7 +118,7 @@ class LocalOAuth2Implementation(AbstractOAuth2Implementation): @property def redirect_uri(self) -> str: """Return the redirect uri.""" - return f"{get_url(self.hass)}{AUTH_CALLBACK_PATH}" + return f"{get_url(self.hass, require_current_request=True)}{AUTH_CALLBACK_PATH}" @property def extra_authorize_data(self) -> dict: diff --git a/tests/components/almond/test_config_flow.py b/tests/components/almond/test_config_flow.py index a6785d2eff0..3cbcb2d58bb 100644 --- a/tests/components/almond/test_config_flow.py +++ b/tests/components/almond/test_config_flow.py @@ -87,7 +87,7 @@ async def test_abort_if_existing_entry(hass): assert result["reason"] == "already_setup" -async def test_full_flow(hass, aiohttp_client, aioclient_mock): +async def test_full_flow(hass, aiohttp_client, aioclient_mock, current_request): """Check full flow.""" assert await setup.async_setup_component( hass, diff --git a/tests/components/home_connect/test_config_flow.py b/tests/components/home_connect/test_config_flow.py index 57c6fb2af7f..5d65df98e5b 100644 --- a/tests/components/home_connect/test_config_flow.py +++ b/tests/components/home_connect/test_config_flow.py @@ -14,7 +14,7 @@ CLIENT_ID = "1234" CLIENT_SECRET = "5678" -async def test_full_flow(hass, aiohttp_client, aioclient_mock): +async def test_full_flow(hass, aiohttp_client, aioclient_mock, current_request): """Check full flow.""" assert await setup.async_setup_component( hass, diff --git a/tests/components/netatmo/test_config_flow.py b/tests/components/netatmo/test_config_flow.py index 53922ead93c..1ad2fd196cf 100644 --- a/tests/components/netatmo/test_config_flow.py +++ b/tests/components/netatmo/test_config_flow.py @@ -42,7 +42,7 @@ async def test_abort_if_existing_entry(hass): assert result["reason"] == "already_configured" -async def test_full_flow(hass, aiohttp_client, aioclient_mock): +async def test_full_flow(hass, aiohttp_client, aioclient_mock, current_request): """Check full flow.""" assert await setup.async_setup_component( hass, diff --git a/tests/components/smappee/test_config_flow.py b/tests/components/smappee/test_config_flow.py index 1d9bb547f61..38f3ac480ef 100644 --- a/tests/components/smappee/test_config_flow.py +++ b/tests/components/smappee/test_config_flow.py @@ -320,7 +320,7 @@ async def test_abort_cloud_flow_if_local_device_exists(hass): assert len(hass.config_entries.async_entries(DOMAIN)) == 1 -async def test_full_user_flow(hass, aiohttp_client, aioclient_mock): +async def test_full_user_flow(hass, aiohttp_client, aioclient_mock, current_request): """Check full flow.""" assert await setup.async_setup_component( hass, diff --git a/tests/components/somfy/test_config_flow.py b/tests/components/somfy/test_config_flow.py index f5cf97bacff..b1df938b3b1 100644 --- a/tests/components/somfy/test_config_flow.py +++ b/tests/components/somfy/test_config_flow.py @@ -52,7 +52,7 @@ async def test_abort_if_existing_entry(hass): assert result["reason"] == "already_setup" -async def test_full_flow(hass, aiohttp_client, aioclient_mock): +async def test_full_flow(hass, aiohttp_client, aioclient_mock, current_request): """Check full flow.""" assert await setup.async_setup_component( hass, diff --git a/tests/components/spotify/test_config_flow.py b/tests/components/spotify/test_config_flow.py index 860840c477f..cc10ddb887d 100644 --- a/tests/components/spotify/test_config_flow.py +++ b/tests/components/spotify/test_config_flow.py @@ -40,7 +40,7 @@ async def test_zeroconf_abort_if_existing_entry(hass): assert result["reason"] == "already_configured" -async def test_full_flow(hass, aiohttp_client, aioclient_mock): +async def test_full_flow(hass, aiohttp_client, aioclient_mock, current_request): """Check a full flow.""" assert await setup.async_setup_component( hass, @@ -96,7 +96,9 @@ async def test_full_flow(hass, aiohttp_client, aioclient_mock): } -async def test_abort_if_spotify_error(hass, aiohttp_client, aioclient_mock): +async def test_abort_if_spotify_error( + hass, aiohttp_client, aioclient_mock, current_request +): """Check Spotify errors causes flow to abort.""" await setup.async_setup_component( hass, diff --git a/tests/components/toon/test_config_flow.py b/tests/components/toon/test_config_flow.py index e9ad7480928..37e174b6dc4 100644 --- a/tests/components/toon/test_config_flow.py +++ b/tests/components/toon/test_config_flow.py @@ -1,5 +1,4 @@ """Tests for the Toon config flow.""" - from toonapi import Agreement, ToonError from homeassistant import data_entry_flow @@ -39,7 +38,9 @@ async def test_abort_if_no_configuration(hass): assert result["reason"] == "missing_configuration" -async def test_full_flow_implementation(hass, aiohttp_client, aioclient_mock): +async def test_full_flow_implementation( + hass, aiohttp_client, aioclient_mock, current_request +): """Test registering an integration and finishing flow works.""" await setup_component(hass) @@ -95,7 +96,7 @@ async def test_full_flow_implementation(hass, aiohttp_client, aioclient_mock): } -async def test_no_agreements(hass, aiohttp_client, aioclient_mock): +async def test_no_agreements(hass, aiohttp_client, aioclient_mock, current_request): """Test abort when there are no displays.""" await setup_component(hass) result = await hass.config_entries.flow.async_init( @@ -127,7 +128,9 @@ async def test_no_agreements(hass, aiohttp_client, aioclient_mock): assert result3["reason"] == "no_agreements" -async def test_multiple_agreements(hass, aiohttp_client, aioclient_mock): +async def test_multiple_agreements( + hass, aiohttp_client, aioclient_mock, current_request +): """Test abort when there are no displays.""" await setup_component(hass) result = await hass.config_entries.flow.async_init( @@ -169,7 +172,9 @@ async def test_multiple_agreements(hass, aiohttp_client, aioclient_mock): assert result4["data"]["agreement_id"] == 1 -async def test_agreement_already_set_up(hass, aiohttp_client, aioclient_mock): +async def test_agreement_already_set_up( + hass, aiohttp_client, aioclient_mock, current_request +): """Test showing display form again if display already exists.""" await setup_component(hass) MockConfigEntry(domain=DOMAIN, unique_id=123).add_to_hass(hass) @@ -202,7 +207,7 @@ async def test_agreement_already_set_up(hass, aiohttp_client, aioclient_mock): assert result3["reason"] == "already_configured" -async def test_toon_abort(hass, aiohttp_client, aioclient_mock): +async def test_toon_abort(hass, aiohttp_client, aioclient_mock, current_request): """Test we abort on Toon error.""" await setup_component(hass) result = await hass.config_entries.flow.async_init( @@ -247,7 +252,7 @@ async def test_import(hass): assert result["reason"] == "already_in_progress" -async def test_import_migration(hass, aiohttp_client, aioclient_mock): +async def test_import_migration(hass, aiohttp_client, aioclient_mock, current_request): """Test if importing step with migration works.""" old_entry = MockConfigEntry(domain=DOMAIN, unique_id=123, version=1) old_entry.add_to_hass(hass) diff --git a/tests/conftest.py b/tests/conftest.py index 5c90dcb063e..ba02ae5de2f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -3,8 +3,10 @@ import asyncio import datetime import functools import logging +import ssl import threading +from aiohttp.test_utils import make_mocked_request import pytest import requests_mock as _requests_mock @@ -24,7 +26,7 @@ from homeassistant.helpers import event from homeassistant.setup import async_setup_component from homeassistant.util import location -from tests.async_mock import MagicMock, patch +from tests.async_mock import MagicMock, Mock, patch from tests.ignore_uncaught_exceptions import IGNORE_UNCAUGHT_EXCEPTIONS pytest.register_assert_rewrite("tests.common") @@ -263,6 +265,20 @@ def hass_client(hass, aiohttp_client, hass_access_token): return auth_client +@pytest.fixture +def current_request(hass): + """Mock current request.""" + with patch("homeassistant.helpers.network.current_request") as mock_request_context: + mocked_request = make_mocked_request( + "GET", + "/some/request", + headers={"Host": "example.com"}, + sslcontext=ssl.SSLContext(ssl.PROTOCOL_TLS), + ) + mock_request_context.get = Mock(return_value=mocked_request) + yield mock_request_context + + @pytest.fixture def hass_ws_client(aiohttp_client, hass_access_token, hass): """Websocket client fixture connected to websocket server.""" diff --git a/tests/helpers/test_config_entry_oauth2_flow.py b/tests/helpers/test_config_entry_oauth2_flow.py index 957bd507af7..37b8ea5b3fd 100644 --- a/tests/helpers/test_config_entry_oauth2_flow.py +++ b/tests/helpers/test_config_entry_oauth2_flow.py @@ -194,7 +194,7 @@ async def test_abort_discovered_existing_entries(hass, flow_handler, local_impl) async def test_full_flow( - hass, flow_handler, local_impl, aiohttp_client, aioclient_mock + hass, flow_handler, local_impl, aiohttp_client, aioclient_mock, current_request ): """Check full flow.""" await async_process_ha_core_config(