Skip to content

Commit

Permalink
Add typing to tests with single hass argument (2) (home-assistant#87675)
Browse files Browse the repository at this point in the history
* Add typing to tests with single hass argument (2)

* a few more
  • Loading branch information
epenet authored Feb 8, 2023
1 parent 1bbc03d commit c98b4e3
Show file tree
Hide file tree
Showing 50 changed files with 816 additions and 671 deletions.
13 changes: 7 additions & 6 deletions tests/auth/mfa_modules/test_insecure_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
from homeassistant import auth, data_entry_flow
from homeassistant.auth.mfa_modules import auth_mfa_module_from_config
from homeassistant.auth.models import Credentials
from homeassistant.core import HomeAssistant

from tests.common import MockUser


async def test_validate(hass):
async def test_validate(hass: HomeAssistant) -> None:
"""Test validating pin."""
auth_module = await auth_mfa_module_from_config(
hass,
Expand All @@ -26,7 +27,7 @@ async def test_validate(hass):
assert result is False


async def test_setup_user(hass):
async def test_setup_user(hass: HomeAssistant) -> None:
"""Test setup user."""
auth_module = await auth_mfa_module_from_config(
hass, {"type": "insecure_example", "data": []}
Expand All @@ -39,7 +40,7 @@ async def test_setup_user(hass):
assert result is True


async def test_depose_user(hass):
async def test_depose_user(hass: HomeAssistant) -> None:
"""Test despose user."""
auth_module = await auth_mfa_module_from_config(
hass,
Expand All @@ -54,7 +55,7 @@ async def test_depose_user(hass):
assert len(auth_module._data) == 0


async def test_is_user_setup(hass):
async def test_is_user_setup(hass: HomeAssistant) -> None:
"""Test is user setup."""
auth_module = await auth_mfa_module_from_config(
hass,
Expand All @@ -67,7 +68,7 @@ async def test_is_user_setup(hass):
assert await auth_module.async_is_user_setup("invalid-user") is False


async def test_login(hass):
async def test_login(hass: HomeAssistant) -> None:
"""Test login flow with auth module."""
hass.auth = await auth.auth_manager_from_config(
hass,
Expand Down Expand Up @@ -134,7 +135,7 @@ async def test_login(hass):
assert result["data"].id == "mock-id"


async def test_setup_flow(hass):
async def test_setup_flow(hass: HomeAssistant) -> None:
"""Test validating pin."""
auth_module = await auth_mfa_module_from_config(
hass,
Expand Down
23 changes: 12 additions & 11 deletions tests/auth/mfa_modules/test_notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
from homeassistant.auth import auth_manager_from_config, models as auth_models
from homeassistant.auth.mfa_modules import auth_mfa_module_from_config
from homeassistant.components.notify import NOTIFY_SERVICE_SCHEMA
from homeassistant.core import HomeAssistant

from tests.common import MockUser, async_mock_service

MOCK_CODE = "123456"
MOCK_CODE_2 = "654321"


async def test_validating_mfa(hass):
async def test_validating_mfa(hass: HomeAssistant) -> None:
"""Test validating mfa code."""
notify_auth_module = await auth_mfa_module_from_config(hass, {"type": "notify"})
await notify_auth_module.async_setup_user("test-user", {"notify_service": "dummy"})
Expand All @@ -22,7 +23,7 @@ async def test_validating_mfa(hass):
assert await notify_auth_module.async_validate("test-user", {"code": MOCK_CODE})


async def test_validating_mfa_invalid_code(hass):
async def test_validating_mfa_invalid_code(hass: HomeAssistant) -> None:
"""Test validating an invalid mfa code."""
notify_auth_module = await auth_mfa_module_from_config(hass, {"type": "notify"})
await notify_auth_module.async_setup_user("test-user", {"notify_service": "dummy"})
Expand All @@ -34,7 +35,7 @@ async def test_validating_mfa_invalid_code(hass):
)


async def test_validating_mfa_invalid_user(hass):
async def test_validating_mfa_invalid_user(hass: HomeAssistant) -> None:
"""Test validating an mfa code with invalid user."""
notify_auth_module = await auth_mfa_module_from_config(hass, {"type": "notify"})
await notify_auth_module.async_setup_user("test-user", {"notify_service": "dummy"})
Expand All @@ -45,7 +46,7 @@ async def test_validating_mfa_invalid_user(hass):
)


async def test_validating_mfa_counter(hass):
async def test_validating_mfa_counter(hass: HomeAssistant) -> None:
"""Test counter will move only after generate code."""
notify_auth_module = await auth_mfa_module_from_config(hass, {"type": "notify"})
await notify_auth_module.async_setup_user(
Expand Down Expand Up @@ -81,7 +82,7 @@ async def test_validating_mfa_counter(hass):
assert after_generate_count == notify_setting.counter


async def test_setup_depose_user(hass):
async def test_setup_depose_user(hass: HomeAssistant) -> None:
"""Test set up and despose user."""
notify_auth_module = await auth_mfa_module_from_config(hass, {"type": "notify"})
await notify_auth_module.async_setup_user("test-user", {})
Expand All @@ -96,7 +97,7 @@ async def test_setup_depose_user(hass):
assert len(notify_auth_module._user_settings) == 1


async def test_login_flow_validates_mfa(hass):
async def test_login_flow_validates_mfa(hass: HomeAssistant) -> None:
"""Test login flow with mfa enabled."""
hass.auth = await auth_manager_from_config(
hass,
Expand Down Expand Up @@ -232,7 +233,7 @@ async def test_login_flow_validates_mfa(hass):
assert result["data"].id == "mock-id"


async def test_setup_user_notify_service(hass):
async def test_setup_user_notify_service(hass: HomeAssistant) -> None:
"""Test allow select notify service during mfa setup."""
notify_calls = async_mock_service(hass, "notify", "test1", NOTIFY_SERVICE_SCHEMA)
async_mock_service(hass, "notify", "test2", NOTIFY_SERVICE_SCHEMA)
Expand Down Expand Up @@ -286,7 +287,7 @@ async def test_setup_user_notify_service(hass):
assert step["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY


async def test_include_exclude_config(hass):
async def test_include_exclude_config(hass: HomeAssistant) -> None:
"""Test allow include exclude config."""
async_mock_service(hass, "notify", "include1", NOTIFY_SERVICE_SCHEMA)
async_mock_service(hass, "notify", "include2", NOTIFY_SERVICE_SCHEMA)
Expand Down Expand Up @@ -320,7 +321,7 @@ async def test_include_exclude_config(hass):
assert services == ["include1"]


async def test_setup_user_no_notify_service(hass):
async def test_setup_user_no_notify_service(hass: HomeAssistant) -> None:
"""Test setup flow abort if there is no available notify service."""
async_mock_service(hass, "notify", "test1", NOTIFY_SERVICE_SCHEMA)
notify_auth_module = await auth_mfa_module_from_config(
Expand All @@ -336,7 +337,7 @@ async def test_setup_user_no_notify_service(hass):
assert step["reason"] == "no_available_service"


async def test_not_raise_exception_when_service_not_exist(hass):
async def test_not_raise_exception_when_service_not_exist(hass: HomeAssistant) -> None:
"""Test login flow will not raise exception when notify service error."""
hass.auth = await auth_manager_from_config(
hass,
Expand Down Expand Up @@ -382,7 +383,7 @@ async def test_not_raise_exception_when_service_not_exist(hass):
await hass.async_block_till_done()


async def test_race_condition_in_data_loading(hass):
async def test_race_condition_in_data_loading(hass: HomeAssistant) -> None:
"""Test race condition in the data loading."""
counter = 0

Expand Down
13 changes: 7 additions & 6 deletions tests/auth/mfa_modules/test_totp.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
from homeassistant import data_entry_flow
from homeassistant.auth import auth_manager_from_config, models as auth_models
from homeassistant.auth.mfa_modules import auth_mfa_module_from_config
from homeassistant.core import HomeAssistant

from tests.common import MockUser

MOCK_CODE = "123456"


async def test_validating_mfa(hass):
async def test_validating_mfa(hass: HomeAssistant) -> None:
"""Test validating mfa code."""
totp_auth_module = await auth_mfa_module_from_config(hass, {"type": "totp"})
await totp_auth_module.async_setup_user("test-user", {})
Expand All @@ -20,7 +21,7 @@ async def test_validating_mfa(hass):
assert await totp_auth_module.async_validate("test-user", {"code": MOCK_CODE})


async def test_validating_mfa_invalid_code(hass):
async def test_validating_mfa_invalid_code(hass: HomeAssistant) -> None:
"""Test validating an invalid mfa code."""
totp_auth_module = await auth_mfa_module_from_config(hass, {"type": "totp"})
await totp_auth_module.async_setup_user("test-user", {})
Expand All @@ -32,7 +33,7 @@ async def test_validating_mfa_invalid_code(hass):
)


async def test_validating_mfa_invalid_user(hass):
async def test_validating_mfa_invalid_user(hass: HomeAssistant) -> None:
"""Test validating an mfa code with invalid user."""
totp_auth_module = await auth_mfa_module_from_config(hass, {"type": "totp"})
await totp_auth_module.async_setup_user("test-user", {})
Expand All @@ -43,7 +44,7 @@ async def test_validating_mfa_invalid_user(hass):
)


async def test_setup_depose_user(hass):
async def test_setup_depose_user(hass: HomeAssistant) -> None:
"""Test despose user."""
totp_auth_module = await auth_mfa_module_from_config(hass, {"type": "totp"})
result = await totp_auth_module.async_setup_user("test-user", {})
Expand All @@ -62,7 +63,7 @@ async def test_setup_depose_user(hass):
assert len(totp_auth_module._users) == 1


async def test_login_flow_validates_mfa(hass):
async def test_login_flow_validates_mfa(hass: HomeAssistant) -> None:
"""Test login flow with mfa enabled."""
hass.auth = await auth_manager_from_config(
hass,
Expand Down Expand Up @@ -130,7 +131,7 @@ async def test_login_flow_validates_mfa(hass):
assert result["data"].id == "mock-id"


async def test_race_condition_in_data_loading(hass):
async def test_race_condition_in_data_loading(hass: HomeAssistant) -> None:
"""Test race condition in the data loading."""
counter = 0

Expand Down
3 changes: 2 additions & 1 deletion tests/auth/providers/test_homeassistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
auth_provider_from_config,
homeassistant as hass_auth,
)
from homeassistant.core import HomeAssistant


@pytest.fixture
Expand Down Expand Up @@ -273,7 +274,7 @@ async def test_legacy_get_or_create_credentials(hass, legacy_data):
assert credentials1 is not credentials3


async def test_race_condition_in_data_loading(hass):
async def test_race_condition_in_data_loading(hass: HomeAssistant) -> None:
"""Test race condition in the hass_auth.Data loading.
Ref issue: https://github.com/home-assistant/core/issues/21569
Expand Down
3 changes: 2 additions & 1 deletion tests/auth/test_auth_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from unittest.mock import patch

from homeassistant.auth import auth_store
from homeassistant.core import HomeAssistant


async def test_loading_no_group_data_format(hass, hass_storage):
Expand Down Expand Up @@ -232,7 +233,7 @@ async def test_system_groups_store_id_and_name(hass, hass_storage):
]


async def test_loading_race_condition(hass):
async def test_loading_race_condition(hass: HomeAssistant) -> None:
"""Test only one storage load called when concurrent loading occurred ."""
store = auth_store.AuthStore(hass)
with patch(
Expand Down
26 changes: 15 additions & 11 deletions tests/auth/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
models as auth_models,
)
from homeassistant.auth.const import GROUP_ID_ADMIN, MFA_SESSION_EXPIRATION
from homeassistant.core import callback
from homeassistant.core import HomeAssistant, callback
from homeassistant.util import dt as dt_util

from tests.common import (
Expand Down Expand Up @@ -140,7 +140,7 @@ async def test_auth_manager_from_config_auth_modules(mock_hass):
]


async def test_create_new_user(hass):
async def test_create_new_user(hass: HomeAssistant) -> None:
"""Test creating new user."""
events = []

Expand Down Expand Up @@ -355,7 +355,7 @@ async def test_saving_loading(hass, hass_storage):
pytest.fail(f"Unknown client_id: {r_token.client_id}")


async def test_cannot_retrieve_expired_access_token(hass):
async def test_cannot_retrieve_expired_access_token(hass: HomeAssistant) -> None:
"""Test that we cannot retrieve expired access tokens."""
manager = await auth.auth_manager_from_config(hass, [], [])
user = MockUser().add_to_auth_manager(manager)
Expand All @@ -377,7 +377,7 @@ async def test_cannot_retrieve_expired_access_token(hass):
assert await manager.async_validate_access_token(access_token) is None


async def test_generating_system_user(hass):
async def test_generating_system_user(hass: HomeAssistant) -> None:
"""Test that we can add a system user."""
events = []

Expand Down Expand Up @@ -416,7 +416,7 @@ def user_added(event):
assert events[1].data["user_id"] == user.id


async def test_refresh_token_requires_client_for_user(hass):
async def test_refresh_token_requires_client_for_user(hass: HomeAssistant) -> None:
"""Test create refresh token for a user with client_id."""
manager = await auth.auth_manager_from_config(hass, [], [])
user = MockUser().add_to_auth_manager(manager)
Expand All @@ -433,7 +433,9 @@ async def test_refresh_token_requires_client_for_user(hass):
assert token.access_token_expiration == auth_const.ACCESS_TOKEN_EXPIRATION


async def test_refresh_token_not_requires_client_for_system_user(hass):
async def test_refresh_token_not_requires_client_for_system_user(
hass: HomeAssistant,
) -> None:
"""Test create refresh token for a system user w/o client_id."""
manager = await auth.auth_manager_from_config(hass, [], [])
user = await manager.async_create_system_user("Hass.io")
Expand All @@ -448,7 +450,9 @@ async def test_refresh_token_not_requires_client_for_system_user(hass):
assert token.token_type == auth_models.TOKEN_TYPE_SYSTEM


async def test_refresh_token_with_specific_access_token_expiration(hass):
async def test_refresh_token_with_specific_access_token_expiration(
hass: HomeAssistant,
) -> None:
"""Test create a refresh token with specific access token expiration."""
manager = await auth.auth_manager_from_config(hass, [], [])
user = MockUser().add_to_auth_manager(manager)
Expand All @@ -461,7 +465,7 @@ async def test_refresh_token_with_specific_access_token_expiration(hass):
assert token.access_token_expiration == timedelta(days=100)


async def test_refresh_token_type(hass):
async def test_refresh_token_type(hass: HomeAssistant) -> None:
"""Test create a refresh token with token type."""
manager = await auth.auth_manager_from_config(hass, [], [])
user = MockUser().add_to_auth_manager(manager)
Expand All @@ -479,7 +483,7 @@ async def test_refresh_token_type(hass):
assert token.token_type == auth_models.TOKEN_TYPE_NORMAL


async def test_refresh_token_type_long_lived_access_token(hass):
async def test_refresh_token_type_long_lived_access_token(hass: HomeAssistant) -> None:
"""Test create a refresh token has long-lived access token type."""
manager = await auth.auth_manager_from_config(hass, [], [])
user = MockUser().add_to_auth_manager(manager)
Expand Down Expand Up @@ -963,7 +967,7 @@ async def test_enable_mfa_for_user(hass, hass_storage):
await manager.async_disable_user_mfa(user, "insecure_example")


async def test_async_remove_user(hass):
async def test_async_remove_user(hass: HomeAssistant) -> None:
"""Test removing a user."""
events = async_capture_events(hass, "user_removed")
manager = await auth.auth_manager_from_config(
Expand Down Expand Up @@ -1100,7 +1104,7 @@ async def test_rename_does_not_change_refresh_token(mock_hass):
assert token_before == token_after


async def test_event_user_updated_fires(hass):
async def test_event_user_updated_fires(hass: HomeAssistant) -> None:
"""Test the user updated event fires."""
manager = await auth.auth_manager_from_config(hass, [], [])
user = MockUser().add_to_auth_manager(manager)
Expand Down
Loading

0 comments on commit c98b4e3

Please sign in to comment.