Compare commits

...
Sign in to create a new pull request.

3 commits

Author SHA1 Message Date
Franck Nijhof
af1fc922c6
Add typing 2022-12-21 11:03:07 +01:00
Franck Nijhof
1a992f1d2a
Fix tests, lazy create access tokens in tests 2022-12-21 10:54:37 +01:00
Franck Nijhof
f517b14b21
Update PyJWT to 2.6.0 2022-12-21 08:40:24 +01:00
5 changed files with 33 additions and 14 deletions

View file

@ -1,4 +1,4 @@
PyJWT==2.5.0
PyJWT==2.6.0
PyNaCl==1.5.0
aiodiscover==1.4.13
aiohttp==3.8.1

View file

@ -40,7 +40,7 @@ dependencies = [
"ifaddr==0.1.7",
"jinja2==3.1.2",
"lru-dict==1.1.8",
"PyJWT==2.5.0",
"PyJWT==2.6.0",
# PyJWT has loose dependency. We want the latest one.
"cryptography==38.0.3",
"orjson==3.8.1",

View file

@ -15,7 +15,7 @@ home-assistant-bluetooth==1.9.0
ifaddr==0.1.7
jinja2==3.1.2
lru-dict==1.1.8
PyJWT==2.5.0
PyJWT==2.6.0
cryptography==38.0.3
orjson==3.8.1
pip>=21.0,<22.4

View file

@ -1347,3 +1347,15 @@ def raise_contains_mocks(val):
if isinstance(val, list):
for dict_value in val:
raise_contains_mocks(dict_value)
async def create_hass_access_token(
hass: HomeAssistant, user: auth_models.User, credential: auth_models.Credentials
) -> str:
"""Return an access token to access Home Assistant."""
await hass.auth.async_link_user(user, credential)
refresh_token = await hass.auth.async_create_refresh_token(
user, CLIENT_ID, credential=credential
)
return hass.auth.async_create_access_token(refresh_token)

View file

@ -45,7 +45,7 @@ from homeassistant.components.websocket_api.http import URL
from homeassistant.const import HASSIO_USER_NAME
from homeassistant.core import CoreState, HomeAssistant
from homeassistant.helpers import config_entry_oauth2_flow, recorder as recorder_helper
from homeassistant.helpers.typing import ConfigType
from homeassistant.helpers.typing import UNDEFINED, ConfigType
from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util, location
@ -64,6 +64,7 @@ from tests.common import ( # noqa: E402, isort:skip
get_test_home_assistant,
init_recorder_component,
mock_storage as mock_storage,
create_hass_access_token,
)
from tests.test_util.aiohttp import mock_aiohttp_client # noqa: E402, isort:skip
from tests.components.recorder.common import ( # noqa: E402, isort:skip
@ -479,12 +480,7 @@ async def hass_admin_credential(hass, local_auth):
@pytest.fixture
async def hass_access_token(hass, hass_admin_user, hass_admin_credential):
"""Return an access token to access Home Assistant."""
await hass.auth.async_link_user(hass_admin_user, hass_admin_credential)
refresh_token = await hass.auth.async_create_refresh_token(
hass_admin_user, CLIENT_ID, credential=hass_admin_credential
)
return hass.auth.async_create_access_token(refresh_token)
return await create_hass_access_token(hass, hass_admin_user, hass_admin_credential)
@pytest.fixture
@ -575,13 +571,18 @@ def local_auth(hass):
@pytest.fixture
def hass_client(hass, aiohttp_client, hass_access_token, socket_enabled):
def hass_client(
hass, aiohttp_client, hass_admin_user, hass_admin_credential, socket_enabled
):
"""Return an authenticated HTTP client."""
async def auth_client():
"""Return an authenticated client."""
access_token = await create_hass_access_token(
hass, hass_admin_user, hass_admin_credential
)
return await aiohttp_client(
hass.http.app, headers={"Authorization": f"Bearer {hass_access_token}"}
hass.http.app, headers={"Authorization": f"Bearer {access_token}"}
)
return auth_client
@ -623,11 +624,17 @@ def current_request_with_host(current_request):
@pytest.fixture
def hass_ws_client(aiohttp_client, hass_access_token, hass, socket_enabled):
def hass_ws_client(
aiohttp_client, hass_admin_user, hass_admin_credential, hass, socket_enabled
):
"""Websocket client fixture connected to websocket server."""
async def create_client(hass=hass, access_token=hass_access_token):
async def create_client(hass=hass, access_token=UNDEFINED):
"""Create a websocket client."""
if access_token == UNDEFINED:
access_token = await create_hass_access_token(
hass, hass_admin_user, hass_admin_credential
)
assert await async_setup_component(hass, "websocket_api", {})
client = await aiohttp_client(hass.http.app)
websocket = await client.ws_connect(URL)