Storage auth (#15192)
* Support parallel loading * Add storage mock * Store auth * Fix tests
This commit is contained in:
1 parent
a277470363
commit
2205090795
8 files changed
+324
-92
No files matched your search
+43
-5
@@ -4,7 +4,7 @@ from unittest.mock import Mock
|
||||
import pytest
|
||||
|
||||
from homeassistant import auth, data_entry_flow
|
||||
from tests.common import MockUser, ensure_auth_manager_loaded
|
||||
from tests.common import MockUser, ensure_auth_manager_loaded, flush_store
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -53,9 +53,9 @@ async def test_auth_manager_from_config_validates_config_and_id(mock_hass):
|
||||
}]
|
||||
|
||||
|
||||
async def test_create_new_user(mock_hass):
|
||||
async def test_create_new_user(hass, hass_storage):
|
||||
"""Test creating new user."""
|
||||
manager = await auth.auth_manager_from_config(mock_hass, [{
|
||||
manager = await auth.auth_manager_from_config(hass, [{
|
||||
'type': 'insecure_example',
|
||||
'users': [{
|
||||
'username': 'test-user',
|
||||
@@ -124,9 +124,9 @@ async def test_login_as_existing_user(mock_hass):
|
||||
assert user.name == 'Paulus'
|
||||
|
||||
|
||||
async def test_linking_user_to_two_auth_providers(mock_hass):
|
||||
async def test_linking_user_to_two_auth_providers(hass, hass_storage):
|
||||
"""Test linking user to two auth providers."""
|
||||
manager = await auth.auth_manager_from_config(mock_hass, [{
|
||||
manager = await auth.auth_manager_from_config(hass, [{
|
||||
'type': 'insecure_example',
|
||||
'users': [{
|
||||
'username': 'test-user',
|
||||
@@ -157,3 +157,41 @@ async def test_linking_user_to_two_auth_providers(mock_hass):
|
||||
})
|
||||
await manager.async_link_user(user, step['result'])
|
||||
assert len(user.credentials) == 2
|
||||
|
||||
|
||||
async def test_saving_loading(hass, hass_storage):
|
||||
"""Test storing and saving data.
|
||||
|
||||
Creates one of each type that we store to test we restore correctly.
|
||||
"""
|
||||
manager = await auth.auth_manager_from_config(hass, [{
|
||||
'type': 'insecure_example',
|
||||
'users': [{
|
||||
'username': 'test-user',
|
||||
'password': 'test-pass',
|
||||
}]
|
||||
}])
|
||||
|
||||
step = await manager.login_flow.async_init(('insecure_example', None))
|
||||
step = await manager.login_flow.async_configure(step['flow_id'], {
|
||||
'username': 'test-user',
|
||||
'password': 'test-pass',
|
||||
})
|
||||
user = await manager.async_get_or_create_user(step['result'])
|
||||
|
||||
client = await manager.async_create_client(
|
||||
'test', redirect_uris=['https://example.com'])
|
||||
|
||||
refresh_token = await manager.async_create_refresh_token(user, client.id)
|
||||
|
||||
manager.async_create_access_token(refresh_token)
|
||||
|
||||
await flush_store(manager._store._store)
|
||||
|
||||
store2 = auth.AuthStore(hass)
|
||||
await store2.async_load()
|
||||
assert len(store2.users) == 1
|
||||
assert store2.users[user.id] == user
|
||||
|
||||
assert len(store2.clients) == 1
|
||||
assert store2.clients[client.id] == client
|
||||
Reference in new issue
Block a user