Use current request context in OAuth redirect (#38692)

This commit is contained in:
Franck Nijhof 2020-08-15 15:26:54 +02:00 committed by GitHub
parent a1dfa8ebc6
commit 755761867d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 40 additions and 17 deletions

View File

@ -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:

View File

@ -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,

View File

@ -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,

View File

@ -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,

View File

@ -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,

View File

@ -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,

View File

@ -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,

View File

@ -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)

View File

@ -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."""

View File

@ -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(