Add type hints to core tests (part 2) (#88492)

This commit is contained in:
epenet
2023-02-21 09:27:13 +01:00
committed by GitHub
parent a102883eff
commit a51cc75f03
17 changed files with 383 additions and 216 deletions

View File

@@ -41,7 +41,7 @@ def manager(hass, store, provider):
return AuthManager(hass, store, {(provider.type, provider.id): provider}, {})
async def test_create_new_credential(manager, provider):
async def test_create_new_credential(manager, provider) -> None:
"""Test that we create a new credential."""
credentials = await provider.async_get_or_create_credentials(
{"username": "good-user", "password": "good-pass"}
@@ -52,7 +52,7 @@ async def test_create_new_credential(manager, provider):
assert user.is_active
async def test_match_existing_credentials(store, provider):
async def test_match_existing_credentials(store, provider) -> None:
"""See if we match existing users."""
existing = auth_models.Credentials(
id=uuid.uuid4(),
@@ -68,24 +68,24 @@ async def test_match_existing_credentials(store, provider):
assert credentials is existing
async def test_invalid_username(provider):
async def test_invalid_username(provider) -> 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):
async def test_invalid_password(provider) -> 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):
async def test_good_auth(provider) -> 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):
async def test_good_auth_with_meta(manager, provider) -> None:
"""Test metadata is added upon successful authentication."""
provider.config[command_line.CONF_ARGS] = ["--with-meta"]
provider.config[command_line.CONF_META] = True
@@ -102,7 +102,7 @@ async def test_good_auth_with_meta(manager, provider):
assert user.is_active
async def test_utf_8_username_password(provider):
async def test_utf_8_username_password(provider) -> None:
"""Test that we create a new credential."""
credentials = await provider.async_get_or_create_credentials(
{"username": "ßßß", "password": "äöü"}
@@ -110,7 +110,7 @@ async def test_utf_8_username_password(provider):
assert credentials.is_new is True
async def test_login_flow_validates(provider):
async def test_login_flow_validates(provider) -> None:
"""Test login flow."""
flow = await provider.async_login_flow({})
result = await flow.async_step_init()
@@ -129,7 +129,7 @@ async def test_login_flow_validates(provider):
assert result["data"]["username"] == "good-user"
async def test_strip_username(provider):
async def test_strip_username(provider) -> None:
"""Test authentication works with username with whitespace around."""
flow = await provider.async_login_flow({})
result = await flow.async_step_init(

View File

@@ -31,7 +31,7 @@ def legacy_data(hass):
return data
async def test_validating_password_invalid_user(data, hass):
async def test_validating_password_invalid_user(data, hass: HomeAssistant) -> None:
"""Test validating an invalid user."""
with pytest.raises(hass_auth.InvalidAuth):
data.validate_login("non-existing", "pw")
@@ -46,7 +46,7 @@ async def test_not_allow_set_id() -> None:
)
async def test_new_users_populate_values(hass, data):
async def test_new_users_populate_values(hass: HomeAssistant, data) -> None:
"""Test that we populate data for new users."""
data.add_auth("hello", "test-pass")
await data.async_save()
@@ -59,7 +59,7 @@ async def test_new_users_populate_values(hass, data):
assert user.is_active
async def test_changing_password_raises_invalid_user(data, hass):
async def test_changing_password_raises_invalid_user(data, hass: HomeAssistant) -> None:
"""Test that changing password raises invalid user."""
with pytest.raises(hass_auth.InvalidUser):
data.change_password("non-existing", "pw")
@@ -68,20 +68,20 @@ async def test_changing_password_raises_invalid_user(data, hass):
# Modern mode
async def test_adding_user(data, hass):
async def test_adding_user(data, hass: HomeAssistant) -> None:
"""Test adding a user."""
data.add_auth("test-user", "test-pass")
data.validate_login(" test-user ", "test-pass")
async def test_adding_user_duplicate_username(data, hass):
async def test_adding_user_duplicate_username(data, hass: HomeAssistant) -> None:
"""Test adding a user with duplicate username."""
data.add_auth("test-user", "test-pass")
with pytest.raises(hass_auth.InvalidUser):
data.add_auth("TEST-user ", "other-pass")
async def test_validating_password_invalid_password(data, hass):
async def test_validating_password_invalid_password(data, hass: HomeAssistant) -> None:
"""Test validating an invalid password."""
data.add_auth("test-user", "test-pass")
@@ -95,7 +95,7 @@ async def test_validating_password_invalid_password(data, hass):
data.validate_login("test-user", "Test-pass")
async def test_changing_password(data, hass):
async def test_changing_password(data, hass: HomeAssistant) -> None:
"""Test adding a user."""
data.add_auth("test-user", "test-pass")
data.change_password("TEST-USER ", "new-pass")
@@ -106,7 +106,7 @@ async def test_changing_password(data, hass):
data.validate_login("test-UsEr", "new-pass")
async def test_login_flow_validates(data, hass):
async def test_login_flow_validates(data, hass: HomeAssistant) -> None:
"""Test login flow."""
data.add_auth("test-user", "test-pass")
await data.async_save()
@@ -137,7 +137,7 @@ async def test_login_flow_validates(data, hass):
assert result["data"]["username"] == "test-USER"
async def test_saving_loading(data, hass):
async def test_saving_loading(data, hass: HomeAssistant) -> None:
"""Test saving and loading JSON."""
data.add_auth("test-user", "test-pass")
data.add_auth("second-user", "second-pass")
@@ -149,7 +149,7 @@ async def test_saving_loading(data, hass):
data.validate_login("second-user ", "second-pass")
async def test_get_or_create_credentials(hass, data):
async def test_get_or_create_credentials(hass: HomeAssistant, data) -> None:
"""Test that we can get or create credentials."""
manager = await auth_manager_from_config(hass, [{"type": "homeassistant"}], [])
provider = manager.auth_providers[0]
@@ -165,13 +165,15 @@ async def test_get_or_create_credentials(hass, data):
# Legacy mode
async def test_legacy_adding_user(legacy_data, hass):
async def test_legacy_adding_user(legacy_data, hass: HomeAssistant) -> None:
"""Test in legacy mode adding a user."""
legacy_data.add_auth("test-user", "test-pass")
legacy_data.validate_login("test-user", "test-pass")
async def test_legacy_adding_user_duplicate_username(legacy_data, hass):
async def test_legacy_adding_user_duplicate_username(
legacy_data, hass: HomeAssistant
) -> None:
"""Test in legacy mode adding a user with duplicate username."""
legacy_data.add_auth("test-user", "test-pass")
with pytest.raises(hass_auth.InvalidUser):
@@ -181,7 +183,9 @@ async def test_legacy_adding_user_duplicate_username(legacy_data, hass):
legacy_data.add_auth("Test-user", "test-pass")
async def test_legacy_validating_password_invalid_password(legacy_data, hass):
async def test_legacy_validating_password_invalid_password(
legacy_data, hass: HomeAssistant
) -> None:
"""Test in legacy mode validating an invalid password."""
legacy_data.add_auth("test-user", "test-pass")
@@ -189,7 +193,7 @@ async def test_legacy_validating_password_invalid_password(legacy_data, hass):
legacy_data.validate_login("test-user", "invalid-pass")
async def test_legacy_changing_password(legacy_data, hass):
async def test_legacy_changing_password(legacy_data, hass: HomeAssistant) -> None:
"""Test in legacy mode adding a user."""
user = "test-user"
legacy_data.add_auth(user, "test-pass")
@@ -201,13 +205,15 @@ async def test_legacy_changing_password(legacy_data, hass):
legacy_data.validate_login(user, "new-pass")
async def test_legacy_changing_password_raises_invalid_user(legacy_data, hass):
async def test_legacy_changing_password_raises_invalid_user(
legacy_data, hass: HomeAssistant
) -> None:
"""Test in legacy mode that we initialize an empty config."""
with pytest.raises(hass_auth.InvalidUser):
legacy_data.change_password("non-existing", "pw")
async def test_legacy_login_flow_validates(legacy_data, hass):
async def test_legacy_login_flow_validates(legacy_data, hass: HomeAssistant) -> None:
"""Test in legacy mode login flow."""
legacy_data.add_auth("test-user", "test-pass")
await legacy_data.async_save()
@@ -238,7 +244,7 @@ async def test_legacy_login_flow_validates(legacy_data, hass):
assert result["data"]["username"] == "test-user"
async def test_legacy_saving_loading(legacy_data, hass):
async def test_legacy_saving_loading(legacy_data, hass: HomeAssistant) -> None:
"""Test in legacy mode saving and loading JSON."""
legacy_data.add_auth("test-user", "test-pass")
legacy_data.add_auth("second-user", "second-pass")
@@ -254,7 +260,9 @@ async def test_legacy_saving_loading(legacy_data, hass):
legacy_data.validate_login("test-user ", "test-pass")
async def test_legacy_get_or_create_credentials(hass, legacy_data):
async def test_legacy_get_or_create_credentials(
hass: HomeAssistant, legacy_data
) -> None:
"""Test in legacy mode that we can get or create credentials."""
manager = await auth_manager_from_config(hass, [{"type": "homeassistant"}], [])
provider = manager.auth_providers[0]

View File

@@ -40,7 +40,7 @@ def manager(hass, store, provider):
return AuthManager(hass, store, {(provider.type, provider.id): provider}, {})
async def test_create_new_credential(manager, provider):
async def test_create_new_credential(manager, provider) -> None:
"""Test that we create a new credential."""
credentials = await provider.async_get_or_create_credentials(
{"username": "user-test", "password": "password-test"}
@@ -52,7 +52,7 @@ async def test_create_new_credential(manager, provider):
assert user.is_active
async def test_match_existing_credentials(store, provider):
async def test_match_existing_credentials(store, provider) -> None:
"""See if we match existing users."""
existing = auth_models.Credentials(
id=uuid.uuid4(),
@@ -68,19 +68,19 @@ async def test_match_existing_credentials(store, provider):
assert credentials is existing
async def test_verify_username(provider):
async def test_verify_username(provider) -> 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):
async def test_verify_password(provider) -> 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):
async def test_utf_8_username_password(provider) -> None:
"""Test that we create a new credential."""
credentials = await provider.async_get_or_create_credentials(
{"username": "🎉", "password": "😎"}

View File

@@ -4,6 +4,7 @@ import pytest
from homeassistant import auth, data_entry_flow
from homeassistant.auth import auth_store
from homeassistant.auth.providers import legacy_api_password
from homeassistant.core import HomeAssistant
@pytest.fixture
@@ -26,7 +27,7 @@ def manager(hass, store, provider):
return auth.AuthManager(hass, store, {(provider.type, provider.id): provider}, {})
async def test_create_new_credential(manager, provider):
async def test_create_new_credential(manager, provider) -> None:
"""Test that we create a new credential."""
credentials = await provider.async_get_or_create_credentials({})
assert credentials.is_new is True
@@ -36,7 +37,7 @@ async def test_create_new_credential(manager, provider):
assert user.is_active
async def test_only_one_credentials(manager, provider):
async def test_only_one_credentials(manager, provider) -> None:
"""Call create twice will return same credential."""
credentials = await provider.async_get_or_create_credentials({})
await manager.async_get_or_create_user(credentials)
@@ -45,14 +46,14 @@ async def test_only_one_credentials(manager, provider):
assert credentials2.is_new is False
async def test_verify_login(hass, provider):
async def test_verify_login(hass: HomeAssistant, provider) -> None:
"""Test login using legacy api password auth provider."""
provider.async_validate_login("test-password")
with pytest.raises(legacy_api_password.InvalidAuthError):
provider.async_validate_login("invalid-password")
async def test_login_flow_works(hass, manager):
async def test_login_flow_works(hass: HomeAssistant, manager) -> None:
"""Test wrong config."""
result = await manager.login_flow.async_init(handler=("legacy_api_password", None))
assert result["type"] == data_entry_flow.FlowResultType.FORM

View File

@@ -10,6 +10,7 @@ from homeassistant import auth
from homeassistant.auth import auth_store
from homeassistant.auth.providers import trusted_networks as tn_auth
from homeassistant.components.http import CONF_TRUSTED_PROXIES, CONF_USE_X_FORWARDED_FOR
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from homeassistant.setup import async_setup_component
@@ -115,7 +116,7 @@ def manager_bypass_login(hass, store, provider_bypass_login):
)
async def test_trusted_networks_credentials(manager, provider):
async def test_trusted_networks_credentials(manager, provider) -> 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})
@@ -132,7 +133,7 @@ async def test_trusted_networks_credentials(manager, provider):
await provider.async_get_or_create_credentials({"user": "invalid-user"})
async def test_validate_access(provider):
async def test_validate_access(provider) -> 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"))
@@ -147,7 +148,7 @@ async def test_validate_access(provider):
provider.async_validate_access(ip_address("2001:db8::ff00:42:8329"))
async def test_validate_access_proxy(hass, provider):
async def test_validate_access_proxy(hass: HomeAssistant, provider) -> None:
"""Test validate access from trusted networks are blocked from proxy."""
await async_setup_component(
@@ -170,7 +171,7 @@ async def test_validate_access_proxy(hass, provider):
provider.async_validate_access(ip_address("fd00::1"))
async def test_validate_access_cloud(hass, provider):
async def test_validate_access_cloud(hass: HomeAssistant, provider) -> None:
"""Test validate access from trusted networks are blocked from cloud."""
await async_setup_component(
hass,
@@ -191,7 +192,7 @@ async def test_validate_access_cloud(hass, provider):
provider.async_validate_access(ip_address("192.168.128.2"))
async def test_validate_refresh_token(provider):
async def test_validate_refresh_token(provider) -> None:
"""Verify re-validation of refresh token."""
with patch.object(provider, "async_validate_access") as mock:
with pytest.raises(tn_auth.InvalidAuthError):
@@ -201,7 +202,7 @@ async def test_validate_refresh_token(provider):
mock.assert_called_once_with(ip_address("127.0.0.1"))
async def test_login_flow(manager, provider):
async def test_login_flow(manager, provider) -> None:
"""Test login flow."""
owner = await manager.async_create_user("test-owner")
user = await manager.async_create_user("test-user")
@@ -228,7 +229,7 @@ async def test_login_flow(manager, provider):
assert step["data"]["user"] == user.id
async def test_trusted_users_login(manager_with_user, provider_with_user):
async def test_trusted_users_login(manager_with_user, provider_with_user) -> 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(
@@ -308,7 +309,7 @@ async def test_trusted_users_login(manager_with_user, provider_with_user):
assert schema({"user": sys_user.id})
async def test_trusted_group_login(manager_with_user, provider_with_user):
async def test_trusted_group_login(manager_with_user, provider_with_user) -> None:
"""Test config trusted_user with group_id."""
owner = await manager_with_user.async_create_user("test-owner")
# create a user in user group
@@ -361,7 +362,7 @@ async def test_trusted_group_login(manager_with_user, provider_with_user):
assert schema({"user": user.id})
async def test_bypass_login_flow(manager_bypass_login, provider_bypass_login):
async def test_bypass_login_flow(manager_bypass_login, provider_bypass_login) -> None:
"""Test login flow can be bypass if only one user available."""
owner = await manager_bypass_login.async_create_user("test-owner")