core/tests/scripts/test_auth.py

167 lines
4.9 KiB
Python
Raw Normal View History

"""Test the auth script to manage local users."""
import argparse
from asyncio import AbstractEventLoop
from collections.abc import Generator
import logging
2023-02-20 10:42:56 +00:00
from typing import Any
2021-01-01 21:31:56 +00:00
from unittest.mock import Mock, patch
import pytest
2018-07-13 09:43:08 +00:00
from homeassistant.auth.providers import homeassistant as hass_auth
2023-02-20 10:42:56 +00:00
from homeassistant.core import HomeAssistant
from homeassistant.scripts import auth as script_auth
from tests.common import register_auth_provider
@pytest.fixture(autouse=True)
def reset_log_level() -> Generator[None]:
"""Reset log level after each test case."""
logger = logging.getLogger("homeassistant.core")
orig_level = logger.level
yield
logger.setLevel(orig_level)
@pytest.fixture
def provider(hass: HomeAssistant) -> hass_auth.HassAuthProvider:
"""Home Assistant auth provider."""
2019-07-31 19:25:30 +00:00
provider = hass.loop.run_until_complete(
register_auth_provider(hass, {"type": "homeassistant"})
)
hass.loop.run_until_complete(provider.async_initialize())
return provider
async def test_list_user(
hass: HomeAssistant,
provider: hass_auth.HassAuthProvider,
capsys: pytest.CaptureFixture[str],
) -> None:
"""Test we can list users."""
data = provider.data
2019-07-31 19:25:30 +00:00
data.add_auth("test-user", "test-pass")
data.add_auth("second-user", "second-pass")
await script_auth.list_users(hass, provider, None)
captured = capsys.readouterr()
assert captured.out == "test-user\nsecond-user\n\nTotal users: 2\n"
2023-02-20 10:42:56 +00:00
async def test_add_user(
hass: HomeAssistant,
provider: hass_auth.HassAuthProvider,
capsys: pytest.CaptureFixture[str],
hass_storage: dict[str, Any],
2023-02-20 10:42:56 +00:00
) -> None:
"""Test we can add a user."""
data = provider.data
await script_auth.add_user(
2019-07-31 19:25:30 +00:00
hass, provider, Mock(username="paulus", password="test-pass")
)
2019-07-31 19:25:30 +00:00
assert len(hass_storage[hass_auth.STORAGE_KEY]["data"]["users"]) == 1
captured = capsys.readouterr()
2019-07-31 19:25:30 +00:00
assert captured.out == "Auth created\n"
assert len(data.users) == 1
2019-07-31 19:25:30 +00:00
data.validate_login("paulus", "test-pass")
async def test_validate_login(
hass: HomeAssistant,
provider: hass_auth.HassAuthProvider,
capsys: pytest.CaptureFixture[str],
) -> None:
"""Test we can validate a user login."""
data = provider.data
2019-07-31 19:25:30 +00:00
data.add_auth("test-user", "test-pass")
await script_auth.validate_login(
2019-07-31 19:25:30 +00:00
hass, provider, Mock(username="test-user", password="test-pass")
)
captured = capsys.readouterr()
2019-07-31 19:25:30 +00:00
assert captured.out == "Auth valid\n"
await script_auth.validate_login(
2019-07-31 19:25:30 +00:00
hass, provider, Mock(username="test-user", password="invalid-pass")
)
captured = capsys.readouterr()
2019-07-31 19:25:30 +00:00
assert captured.out == "Auth invalid\n"
await script_auth.validate_login(
2019-07-31 19:25:30 +00:00
hass, provider, Mock(username="invalid-user", password="test-pass")
)
captured = capsys.readouterr()
2019-07-31 19:25:30 +00:00
assert captured.out == "Auth invalid\n"
2023-02-20 10:42:56 +00:00
async def test_change_password(
hass: HomeAssistant,
provider: hass_auth.HassAuthProvider,
capsys: pytest.CaptureFixture[str],
hass_storage: dict[str, Any],
2023-02-20 10:42:56 +00:00
) -> None:
"""Test we can change a password."""
data = provider.data
2019-07-31 19:25:30 +00:00
data.add_auth("test-user", "test-pass")
await script_auth.change_password(
2019-07-31 19:25:30 +00:00
hass, provider, Mock(username="test-user", new_password="new-pass")
)
2019-07-31 19:25:30 +00:00
assert len(hass_storage[hass_auth.STORAGE_KEY]["data"]["users"]) == 1
captured = capsys.readouterr()
2019-07-31 19:25:30 +00:00
assert captured.out == "Password changed\n"
data.validate_login("test-user", "new-pass")
with pytest.raises(hass_auth.InvalidAuth):
2019-07-31 19:25:30 +00:00
data.validate_login("test-user", "test-pass")
2023-02-20 10:42:56 +00:00
async def test_change_password_invalid_user(
hass: HomeAssistant,
provider: hass_auth.HassAuthProvider,
capsys: pytest.CaptureFixture[str],
hass_storage: dict[str, Any],
2023-02-20 10:42:56 +00:00
) -> None:
"""Test changing password of non-existing user."""
data = provider.data
2019-07-31 19:25:30 +00:00
data.add_auth("test-user", "test-pass")
await script_auth.change_password(
2019-07-31 19:25:30 +00:00
hass, provider, Mock(username="invalid-user", new_password="new-pass")
)
assert hass_auth.STORAGE_KEY not in hass_storage
captured = capsys.readouterr()
2019-07-31 19:25:30 +00:00
assert captured.out == "User not found\n"
data.validate_login("test-user", "test-pass")
with pytest.raises(hass_auth.InvalidAuth):
2019-07-31 19:25:30 +00:00
data.validate_login("invalid-user", "new-pass")
def test_parsing_args(event_loop: AbstractEventLoop) -> None:
"""Test we parse args correctly."""
called = False
async def mock_func(
hass: HomeAssistant, provider: hass_auth.AuthProvider, args2: argparse.Namespace
) -> None:
"""Mock function to be called."""
nonlocal called
called = True
2019-07-31 19:25:30 +00:00
assert provider.hass.config.config_dir == "/somewhere/config"
assert args2 is args
2019-07-31 19:25:30 +00:00
args = Mock(config="/somewhere/config", func=mock_func)
2019-07-31 19:25:30 +00:00
with patch("argparse.ArgumentParser.parse_args", return_value=args):
script_auth.run(None)
2019-07-31 19:25:30 +00:00
assert called, "Mock function did not get called"