mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 16:57:53 +00:00
Migrate the name for the hassio user (#51771)
This commit is contained in:
parent
97e77ab229
commit
c8755cd896
@ -400,8 +400,12 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: # noqa:
|
||||
if not user.is_admin:
|
||||
await hass.auth.async_update_user(user, group_ids=[GROUP_ID_ADMIN])
|
||||
|
||||
# Migrate old name
|
||||
if user.name == "Hass.io":
|
||||
await hass.auth.async_update_user(user, name="Supervisor")
|
||||
|
||||
if refresh_token is None:
|
||||
user = await hass.auth.async_create_system_user("Hass.io", [GROUP_ID_ADMIN])
|
||||
user = await hass.auth.async_create_system_user("Supervisor", [GROUP_ID_ADMIN])
|
||||
refresh_token = await hass.auth.async_create_refresh_token(user)
|
||||
data["hassio_user"] = user.id
|
||||
await store.async_save(data)
|
||||
|
@ -1001,3 +1001,21 @@ async def test_new_users(mock_hass):
|
||||
)
|
||||
)
|
||||
assert user_cred.is_admin
|
||||
|
||||
|
||||
async def test_rename_does_not_change_refresh_token(mock_hass):
|
||||
"""Test that we can rename without changing refresh token."""
|
||||
manager = await auth.auth_manager_from_config(mock_hass, [], [])
|
||||
user = MockUser().add_to_auth_manager(manager)
|
||||
await manager.async_create_refresh_token(user, CLIENT_ID)
|
||||
|
||||
assert len(list(user.refresh_tokens.values())) == 1
|
||||
token_before = list(user.refresh_tokens.values())[0]
|
||||
|
||||
await manager.async_update_user(user, name="new name")
|
||||
assert user.name == "new name"
|
||||
|
||||
assert len(list(user.refresh_tokens.values())) == 1
|
||||
token_after = list(user.refresh_tokens.values())[0]
|
||||
|
||||
assert token_before == token_after
|
||||
|
@ -179,6 +179,7 @@ async def test_setup_api_push_api_data_default(hass, aioclient_mock, hass_storag
|
||||
assert hassio_user.system_generated
|
||||
assert len(hassio_user.groups) == 1
|
||||
assert hassio_user.groups[0].id == GROUP_ID_ADMIN
|
||||
assert hassio_user.name == "Supervisor"
|
||||
for token in hassio_user.refresh_tokens.values():
|
||||
if token.token == refresh_token:
|
||||
break
|
||||
@ -206,6 +207,25 @@ async def test_setup_adds_admin_group_to_user(hass, aioclient_mock, hass_storage
|
||||
assert user.is_admin
|
||||
|
||||
|
||||
async def test_setup_migrate_user_name(hass, aioclient_mock, hass_storage):
|
||||
"""Test setup with migrating the user name."""
|
||||
# Create user with old name
|
||||
user = await hass.auth.async_create_system_user("Hass.io")
|
||||
await hass.auth.async_create_refresh_token(user)
|
||||
|
||||
hass_storage[STORAGE_KEY] = {
|
||||
"data": {"hassio_user": user.id},
|
||||
"key": STORAGE_KEY,
|
||||
"version": 1,
|
||||
}
|
||||
|
||||
with patch.dict(os.environ, MOCK_ENVIRON):
|
||||
result = await async_setup_component(hass, "hassio", {"http": {}, "hassio": {}})
|
||||
assert result
|
||||
|
||||
assert user.name == "Supervisor"
|
||||
|
||||
|
||||
async def test_setup_api_existing_hassio_user(hass, aioclient_mock, hass_storage):
|
||||
"""Test setup with API push default data."""
|
||||
user = await hass.auth.async_create_system_user("Hass.io test")
|
||||
|
Loading…
x
Reference in New Issue
Block a user