11"""Test websocket API."""
22
3+ from collections .abc import Generator
34from typing import Any
45from unittest .mock import AsyncMock , patch
5- from uuid import UUID
6+ from uuid import UUID , uuid4
67
78import pytest
89from syrupy .assertion import SnapshotAssertion
910
1011from homeassistant .auth .const import GROUP_ID_ADMIN
12+ from homeassistant .auth .models import User
13+ from homeassistant .components .hassio import HASSIO_USER_NAME
1114from homeassistant .components .hassio .const import DATA_CONFIG_STORE , DOMAIN
1215from homeassistant .core import HomeAssistant
1316from homeassistant .setup import async_setup_component
@@ -98,7 +101,24 @@ def mock_all(
98101 )
99102
100103
101- @pytest .mark .usefixtures ("hassio_env" )
104+ @pytest .fixture
105+ def mock_hassio_user_id () -> Generator [None ]:
106+ """Mock the HASSIO user ID for snapshot testing."""
107+ original_user_init = User .__init__
108+
109+ def mock_user_init (self , * args , ** kwargs ):
110+ with patch ("homeassistant.auth.models.uuid.uuid4" ) as mock_uuid :
111+ if kwargs .get ("name" ) == HASSIO_USER_NAME :
112+ mock_uuid .return_value = UUID (bytes = b"very_very_random" , version = 4 )
113+ else :
114+ mock_uuid .return_value = uuid4 ()
115+ original_user_init (self , * args , ** kwargs )
116+
117+ with patch .object (User , "__init__" , mock_user_init ):
118+ yield
119+
120+
121+ @pytest .mark .usefixtures ("hassio_env" , "mock_hassio_user_id" )
102122@pytest .mark .parametrize (
103123 "storage_data" ,
104124 [
@@ -151,18 +171,15 @@ async def test_load_config_store(
151171 await hass .auth .async_create_refresh_token (user )
152172 await hass .auth .async_update_user (user , group_ids = [GROUP_ID_ADMIN ])
153173
154- with (
155- patch ("homeassistant.components.hassio.config.STORE_DELAY_SAVE" , 0 ),
156- patch ("uuid.uuid4" , return_value = UUID (bytes = b"very_very_random" , version = 4 )),
157- ):
174+ with patch ("homeassistant.components.hassio.config.STORE_DELAY_SAVE" , 0 ):
158175 assert await async_setup_component (hass , "hassio" , {})
159176 await hass .async_block_till_done ()
160177 await hass .async_block_till_done ()
161178
162179 assert hass .data [DATA_CONFIG_STORE ].data .to_dict () == snapshot
163180
164181
165- @pytest .mark .usefixtures ("hassio_env" )
182+ @pytest .mark .usefixtures ("hassio_env" , "mock_hassio_user_id" )
166183async def test_save_config_store (
167184 hass : HomeAssistant ,
168185 hass_ws_client : WebSocketGenerator ,
@@ -171,10 +188,7 @@ async def test_save_config_store(
171188 snapshot : SnapshotAssertion ,
172189) -> None :
173190 """Test saving the config store."""
174- with (
175- patch ("homeassistant.components.hassio.config.STORE_DELAY_SAVE" , 0 ),
176- patch ("uuid.uuid4" , return_value = UUID (bytes = b"very_very_random" , version = 4 )),
177- ):
191+ with patch ("homeassistant.components.hassio.config.STORE_DELAY_SAVE" , 0 ):
178192 assert await async_setup_component (hass , "hassio" , {})
179193 await hass .async_block_till_done ()
180194 await hass .async_block_till_done ()
0 commit comments