mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
OAuth2 to use current request header (#43668)
This commit is contained in:
@@ -7,6 +7,7 @@ import ssl
|
||||
import threading
|
||||
|
||||
from aiohttp.test_utils import make_mocked_request
|
||||
import multidict
|
||||
import pytest
|
||||
import requests_mock as _requests_mock
|
||||
|
||||
@@ -22,11 +23,11 @@ from homeassistant.components.websocket_api.auth import (
|
||||
from homeassistant.components.websocket_api.http import URL
|
||||
from homeassistant.const import ATTR_NOW, EVENT_TIME_CHANGED
|
||||
from homeassistant.exceptions import ServiceNotFound
|
||||
from homeassistant.helpers import event
|
||||
from homeassistant.helpers import config_entry_oauth2_flow, event
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.util import location
|
||||
|
||||
from tests.async_mock import MagicMock, Mock, patch
|
||||
from tests.async_mock import MagicMock, patch
|
||||
from tests.ignore_uncaught_exceptions import IGNORE_UNCAUGHT_EXCEPTIONS
|
||||
|
||||
pytest.register_assert_rewrite("tests.common")
|
||||
@@ -277,19 +278,29 @@ def hass_client(hass, aiohttp_client, hass_access_token):
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def current_request(hass):
|
||||
def current_request():
|
||||
"""Mock current request."""
|
||||
with patch("homeassistant.helpers.network.current_request") as mock_request_context:
|
||||
with patch("homeassistant.components.http.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)
|
||||
mock_request_context.get.return_value = mocked_request
|
||||
yield mock_request_context
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def current_request_with_host(current_request):
|
||||
"""Mock current request with a host header."""
|
||||
new_headers = multidict.CIMultiDict(current_request.get.return_value.headers)
|
||||
new_headers[config_entry_oauth2_flow.HEADER_FRONTEND_BASE] = "https://example.com"
|
||||
current_request.get.return_value = current_request.get.return_value.clone(
|
||||
headers=new_headers
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def hass_ws_client(aiohttp_client, hass_access_token, hass):
|
||||
"""Websocket client fixture connected to websocket server."""
|
||||
|
||||
Reference in New Issue
Block a user