mirror of
https://github.com/home-assistant/core.git
synced 2025-04-26 18:27:51 +00:00
Improve type hints in auth tests (#120655)
This commit is contained in:
parent
84de2da19f
commit
5e39faa9f8
@ -10,10 +10,11 @@ from homeassistant import data_entry_flow
|
||||
from homeassistant.auth import AuthManager, auth_store, models as auth_models
|
||||
from homeassistant.auth.providers import command_line
|
||||
from homeassistant.const import CONF_TYPE
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def store(hass):
|
||||
async def store(hass: HomeAssistant) -> auth_store.AuthStore:
|
||||
"""Mock store."""
|
||||
store = auth_store.AuthStore(hass)
|
||||
await store.async_load()
|
||||
@ -21,7 +22,9 @@ async def store(hass):
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def provider(hass, store):
|
||||
def provider(
|
||||
hass: HomeAssistant, store: auth_store.AuthStore
|
||||
) -> command_line.CommandLineAuthProvider:
|
||||
"""Mock provider."""
|
||||
return command_line.CommandLineAuthProvider(
|
||||
hass,
|
||||
@ -38,12 +41,18 @@ def provider(hass, store):
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def manager(hass, store, provider):
|
||||
def manager(
|
||||
hass: HomeAssistant,
|
||||
store: auth_store.AuthStore,
|
||||
provider: command_line.CommandLineAuthProvider,
|
||||
) -> AuthManager:
|
||||
"""Mock manager."""
|
||||
return AuthManager(hass, store, {(provider.type, provider.id): provider}, {})
|
||||
|
||||
|
||||
async def test_create_new_credential(manager, provider) -> None:
|
||||
async def test_create_new_credential(
|
||||
manager: AuthManager, provider: command_line.CommandLineAuthProvider
|
||||
) -> None:
|
||||
"""Test that we create a new credential."""
|
||||
credentials = await provider.async_get_or_create_credentials(
|
||||
{"username": "good-user", "password": "good-pass"}
|
||||
@ -57,7 +66,9 @@ async def test_create_new_credential(manager, provider) -> None:
|
||||
assert not user.local_only
|
||||
|
||||
|
||||
async def test_match_existing_credentials(store, provider) -> None:
|
||||
async def test_match_existing_credentials(
|
||||
provider: command_line.CommandLineAuthProvider,
|
||||
) -> None:
|
||||
"""See if we match existing users."""
|
||||
existing = auth_models.Credentials(
|
||||
id=uuid.uuid4(),
|
||||
@ -73,24 +84,26 @@ async def test_match_existing_credentials(store, provider) -> None:
|
||||
assert credentials is existing
|
||||
|
||||
|
||||
async def test_invalid_username(provider) -> None:
|
||||
async def test_invalid_username(provider: command_line.CommandLineAuthProvider) -> None:
|
||||
"""Test we raise if incorrect user specified."""
|
||||
with pytest.raises(command_line.InvalidAuthError):
|
||||
await provider.async_validate_login("bad-user", "good-pass")
|
||||
|
||||
|
||||
async def test_invalid_password(provider) -> None:
|
||||
async def test_invalid_password(provider: command_line.CommandLineAuthProvider) -> None:
|
||||
"""Test we raise if incorrect password specified."""
|
||||
with pytest.raises(command_line.InvalidAuthError):
|
||||
await provider.async_validate_login("good-user", "bad-pass")
|
||||
|
||||
|
||||
async def test_good_auth(provider) -> None:
|
||||
async def test_good_auth(provider: command_line.CommandLineAuthProvider) -> None:
|
||||
"""Test nothing is raised with good credentials."""
|
||||
await provider.async_validate_login("good-user", "good-pass")
|
||||
|
||||
|
||||
async def test_good_auth_with_meta(manager, provider) -> None:
|
||||
async def test_good_auth_with_meta(
|
||||
manager: AuthManager, provider: command_line.CommandLineAuthProvider
|
||||
) -> None:
|
||||
"""Test metadata is added upon successful authentication."""
|
||||
provider.config[command_line.CONF_ARGS] = ["--with-meta"]
|
||||
provider.config[command_line.CONF_META] = True
|
||||
@ -110,7 +123,9 @@ async def test_good_auth_with_meta(manager, provider) -> None:
|
||||
assert user.local_only
|
||||
|
||||
|
||||
async def test_utf_8_username_password(provider) -> None:
|
||||
async def test_utf_8_username_password(
|
||||
provider: command_line.CommandLineAuthProvider,
|
||||
) -> None:
|
||||
"""Test that we create a new credential."""
|
||||
credentials = await provider.async_get_or_create_credentials(
|
||||
{"username": "ßßß", "password": "äöü"}
|
||||
@ -118,7 +133,9 @@ async def test_utf_8_username_password(provider) -> None:
|
||||
assert credentials.is_new is True
|
||||
|
||||
|
||||
async def test_login_flow_validates(provider) -> None:
|
||||
async def test_login_flow_validates(
|
||||
provider: command_line.CommandLineAuthProvider,
|
||||
) -> None:
|
||||
"""Test login flow."""
|
||||
flow = await provider.async_login_flow({})
|
||||
result = await flow.async_step_init()
|
||||
@ -137,7 +154,7 @@ async def test_login_flow_validates(provider) -> None:
|
||||
assert result["data"]["username"] == "good-user"
|
||||
|
||||
|
||||
async def test_strip_username(provider) -> None:
|
||||
async def test_strip_username(provider: command_line.CommandLineAuthProvider) -> None:
|
||||
"""Test authentication works with username with whitespace around."""
|
||||
flow = await provider.async_login_flow({})
|
||||
result = await flow.async_step_init(
|
||||
|
@ -7,10 +7,11 @@ import pytest
|
||||
|
||||
from homeassistant.auth import AuthManager, auth_store, models as auth_models
|
||||
from homeassistant.auth.providers import insecure_example
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def store(hass):
|
||||
async def store(hass: HomeAssistant) -> auth_store.AuthStore:
|
||||
"""Mock store."""
|
||||
store = auth_store.AuthStore(hass)
|
||||
await store.async_load()
|
||||
@ -18,7 +19,9 @@ async def store(hass):
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def provider(hass, store):
|
||||
def provider(
|
||||
hass: HomeAssistant, store: auth_store.AuthStore
|
||||
) -> insecure_example.ExampleAuthProvider:
|
||||
"""Mock provider."""
|
||||
return insecure_example.ExampleAuthProvider(
|
||||
hass,
|
||||
@ -38,12 +41,18 @@ def provider(hass, store):
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def manager(hass, store, provider):
|
||||
def manager(
|
||||
hass: HomeAssistant,
|
||||
store: auth_store.AuthStore,
|
||||
provider: insecure_example.ExampleAuthProvider,
|
||||
) -> AuthManager:
|
||||
"""Mock manager."""
|
||||
return AuthManager(hass, store, {(provider.type, provider.id): provider}, {})
|
||||
|
||||
|
||||
async def test_create_new_credential(manager, provider) -> None:
|
||||
async def test_create_new_credential(
|
||||
manager: AuthManager, provider: insecure_example.ExampleAuthProvider
|
||||
) -> None:
|
||||
"""Test that we create a new credential."""
|
||||
credentials = await provider.async_get_or_create_credentials(
|
||||
{"username": "user-test", "password": "password-test"}
|
||||
@ -55,7 +64,9 @@ async def test_create_new_credential(manager, provider) -> None:
|
||||
assert user.is_active
|
||||
|
||||
|
||||
async def test_match_existing_credentials(store, provider) -> None:
|
||||
async def test_match_existing_credentials(
|
||||
provider: insecure_example.ExampleAuthProvider,
|
||||
) -> None:
|
||||
"""See if we match existing users."""
|
||||
existing = auth_models.Credentials(
|
||||
id=uuid.uuid4(),
|
||||
@ -71,19 +82,21 @@ async def test_match_existing_credentials(store, provider) -> None:
|
||||
assert credentials is existing
|
||||
|
||||
|
||||
async def test_verify_username(provider) -> None:
|
||||
async def test_verify_username(provider: insecure_example.ExampleAuthProvider) -> None:
|
||||
"""Test we raise if incorrect user specified."""
|
||||
with pytest.raises(insecure_example.InvalidAuthError):
|
||||
await provider.async_validate_login("non-existing-user", "password-test")
|
||||
|
||||
|
||||
async def test_verify_password(provider) -> None:
|
||||
async def test_verify_password(provider: insecure_example.ExampleAuthProvider) -> None:
|
||||
"""Test we raise if incorrect user specified."""
|
||||
with pytest.raises(insecure_example.InvalidAuthError):
|
||||
await provider.async_validate_login("user-test", "incorrect-password")
|
||||
|
||||
|
||||
async def test_utf_8_username_password(provider) -> None:
|
||||
async def test_utf_8_username_password(
|
||||
provider: insecure_example.ExampleAuthProvider,
|
||||
) -> None:
|
||||
"""Test that we create a new credential."""
|
||||
credentials = await provider.async_get_or_create_credentials(
|
||||
{"username": "🎉", "password": "😎"}
|
||||
|
@ -17,7 +17,7 @@ from homeassistant.setup import async_setup_component
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def store(hass):
|
||||
async def store(hass: HomeAssistant) -> auth_store.AuthStore:
|
||||
"""Mock store."""
|
||||
store = auth_store.AuthStore(hass)
|
||||
await store.async_load()
|
||||
@ -25,7 +25,9 @@ async def store(hass):
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def provider(hass, store):
|
||||
def provider(
|
||||
hass: HomeAssistant, store: auth_store.AuthStore
|
||||
) -> tn_auth.TrustedNetworksAuthProvider:
|
||||
"""Mock provider."""
|
||||
return tn_auth.TrustedNetworksAuthProvider(
|
||||
hass,
|
||||
@ -45,7 +47,9 @@ def provider(hass, store):
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def provider_with_user(hass, store):
|
||||
def provider_with_user(
|
||||
hass: HomeAssistant, store: auth_store.AuthStore
|
||||
) -> tn_auth.TrustedNetworksAuthProvider:
|
||||
"""Mock provider with trusted users config."""
|
||||
return tn_auth.TrustedNetworksAuthProvider(
|
||||
hass,
|
||||
@ -71,7 +75,9 @@ def provider_with_user(hass, store):
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def provider_bypass_login(hass, store):
|
||||
def provider_bypass_login(
|
||||
hass: HomeAssistant, store: auth_store.AuthStore
|
||||
) -> tn_auth.TrustedNetworksAuthProvider:
|
||||
"""Mock provider with allow_bypass_login config."""
|
||||
return tn_auth.TrustedNetworksAuthProvider(
|
||||
hass,
|
||||
@ -92,13 +98,21 @@ def provider_bypass_login(hass, store):
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def manager(hass, store, provider):
|
||||
def manager(
|
||||
hass: HomeAssistant,
|
||||
store: auth_store.AuthStore,
|
||||
provider: tn_auth.TrustedNetworksAuthProvider,
|
||||
) -> auth.AuthManager:
|
||||
"""Mock manager."""
|
||||
return auth.AuthManager(hass, store, {(provider.type, provider.id): provider}, {})
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def manager_with_user(hass, store, provider_with_user):
|
||||
def manager_with_user(
|
||||
hass: HomeAssistant,
|
||||
store: auth_store.AuthStore,
|
||||
provider_with_user: tn_auth.TrustedNetworksAuthProvider,
|
||||
) -> auth.AuthManager:
|
||||
"""Mock manager with trusted user."""
|
||||
return auth.AuthManager(
|
||||
hass,
|
||||
@ -109,7 +123,11 @@ def manager_with_user(hass, store, provider_with_user):
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def manager_bypass_login(hass, store, provider_bypass_login):
|
||||
def manager_bypass_login(
|
||||
hass: HomeAssistant,
|
||||
store: auth_store.AuthStore,
|
||||
provider_bypass_login: tn_auth.TrustedNetworksAuthProvider,
|
||||
) -> auth.AuthManager:
|
||||
"""Mock manager with allow bypass login."""
|
||||
return auth.AuthManager(
|
||||
hass,
|
||||
@ -119,7 +137,7 @@ def manager_bypass_login(hass, store, provider_bypass_login):
|
||||
)
|
||||
|
||||
|
||||
async def test_config_schema():
|
||||
async def test_config_schema() -> None:
|
||||
"""Test CONFIG_SCHEMA."""
|
||||
# Valid configuration
|
||||
tn_auth.CONFIG_SCHEMA(
|
||||
@ -145,7 +163,9 @@ async def test_config_schema():
|
||||
)
|
||||
|
||||
|
||||
async def test_trusted_networks_credentials(manager, provider) -> None:
|
||||
async def test_trusted_networks_credentials(
|
||||
manager: auth.AuthManager, provider: tn_auth.TrustedNetworksAuthProvider
|
||||
) -> None:
|
||||
"""Test trusted_networks credentials related functions."""
|
||||
owner = await manager.async_create_user("test-owner")
|
||||
tn_owner_cred = await provider.async_get_or_create_credentials({"user": owner.id})
|
||||
@ -162,7 +182,7 @@ async def test_trusted_networks_credentials(manager, provider) -> None:
|
||||
await provider.async_get_or_create_credentials({"user": "invalid-user"})
|
||||
|
||||
|
||||
async def test_validate_access(provider) -> None:
|
||||
async def test_validate_access(provider: tn_auth.TrustedNetworksAuthProvider) -> None:
|
||||
"""Test validate access from trusted networks."""
|
||||
provider.async_validate_access(ip_address("192.168.0.1"))
|
||||
provider.async_validate_access(ip_address("192.168.128.10"))
|
||||
@ -177,7 +197,9 @@ async def test_validate_access(provider) -> None:
|
||||
provider.async_validate_access(ip_address("2001:db8::ff00:42:8329"))
|
||||
|
||||
|
||||
async def test_validate_access_proxy(hass: HomeAssistant, provider) -> None:
|
||||
async def test_validate_access_proxy(
|
||||
hass: HomeAssistant, provider: tn_auth.TrustedNetworksAuthProvider
|
||||
) -> None:
|
||||
"""Test validate access from trusted networks are blocked from proxy."""
|
||||
|
||||
await async_setup_component(
|
||||
@ -200,7 +222,9 @@ async def test_validate_access_proxy(hass: HomeAssistant, provider) -> None:
|
||||
provider.async_validate_access(ip_address("fd00::1"))
|
||||
|
||||
|
||||
async def test_validate_access_cloud(hass: HomeAssistant, provider) -> None:
|
||||
async def test_validate_access_cloud(
|
||||
hass: HomeAssistant, provider: tn_auth.TrustedNetworksAuthProvider
|
||||
) -> None:
|
||||
"""Test validate access from trusted networks are blocked from cloud."""
|
||||
await async_setup_component(
|
||||
hass,
|
||||
@ -221,7 +245,9 @@ async def test_validate_access_cloud(hass: HomeAssistant, provider) -> None:
|
||||
provider.async_validate_access(ip_address("192.168.128.2"))
|
||||
|
||||
|
||||
async def test_validate_refresh_token(provider) -> None:
|
||||
async def test_validate_refresh_token(
|
||||
provider: tn_auth.TrustedNetworksAuthProvider,
|
||||
) -> None:
|
||||
"""Verify re-validation of refresh token."""
|
||||
with patch.object(provider, "async_validate_access") as mock:
|
||||
with pytest.raises(tn_auth.InvalidAuthError):
|
||||
@ -231,7 +257,9 @@ async def test_validate_refresh_token(provider) -> None:
|
||||
mock.assert_called_once_with(ip_address("127.0.0.1"))
|
||||
|
||||
|
||||
async def test_login_flow(manager, provider) -> None:
|
||||
async def test_login_flow(
|
||||
manager: auth.AuthManager, provider: tn_auth.TrustedNetworksAuthProvider
|
||||
) -> None:
|
||||
"""Test login flow."""
|
||||
owner = await manager.async_create_user("test-owner")
|
||||
user = await manager.async_create_user("test-user")
|
||||
@ -258,7 +286,10 @@ async def test_login_flow(manager, provider) -> None:
|
||||
assert step["data"]["user"] == user.id
|
||||
|
||||
|
||||
async def test_trusted_users_login(manager_with_user, provider_with_user) -> None:
|
||||
async def test_trusted_users_login(
|
||||
manager_with_user: auth.AuthManager,
|
||||
provider_with_user: tn_auth.TrustedNetworksAuthProvider,
|
||||
) -> None:
|
||||
"""Test available user list changed per different IP."""
|
||||
owner = await manager_with_user.async_create_user("test-owner")
|
||||
sys_user = await manager_with_user.async_create_system_user(
|
||||
@ -338,7 +369,10 @@ async def test_trusted_users_login(manager_with_user, provider_with_user) -> Non
|
||||
assert schema({"user": sys_user.id})
|
||||
|
||||
|
||||
async def test_trusted_group_login(manager_with_user, provider_with_user) -> None:
|
||||
async def test_trusted_group_login(
|
||||
manager_with_user: auth.AuthManager,
|
||||
provider_with_user: tn_auth.TrustedNetworksAuthProvider,
|
||||
) -> None:
|
||||
"""Test config trusted_user with group_id."""
|
||||
owner = await manager_with_user.async_create_user("test-owner")
|
||||
# create a user in user group
|
||||
@ -391,7 +425,10 @@ async def test_trusted_group_login(manager_with_user, provider_with_user) -> Non
|
||||
assert schema({"user": user.id})
|
||||
|
||||
|
||||
async def test_bypass_login_flow(manager_bypass_login, provider_bypass_login) -> None:
|
||||
async def test_bypass_login_flow(
|
||||
manager_bypass_login: auth.AuthManager,
|
||||
provider_bypass_login: tn_auth.TrustedNetworksAuthProvider,
|
||||
) -> None:
|
||||
"""Test login flow can be bypass if only one user available."""
|
||||
owner = await manager_bypass_login.async_create_user("test-owner")
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user