core/tests/scripts/test_auth.py

128 lines
3.9 KiB
Python
Raw Normal View History

"""Test the auth script to manage local users."""
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
from homeassistant.scripts import auth as script_auth
from tests.common import register_auth_provider
@pytest.fixture
def provider(hass):
"""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, provider, capsys):
"""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()
2019-07-31 19:25:30 +00:00
assert captured.out == "\n".join(
["test-user", "second-user", "", "Total users: 2", ""]
)
async def test_add_user(hass, provider, capsys, hass_storage):
"""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, provider, capsys):
"""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"
async def test_change_password(hass, provider, capsys, hass_storage):
"""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")
2019-07-31 19:25:30 +00:00
async def test_change_password_invalid_user(hass, provider, capsys, hass_storage):
"""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")
Upgrade pytest-aiohttp (#82475) * Upgrade pytest-aiohttp * Make sure executors, tasks and timers are closed Some test will trigger warnings on garbage collect, these warnings spills over into next test. Some test trigger tasks that raise errors on shutdown, these spill over into next test. This is to mimic older pytest-aiohttp and it's behaviour on test cleanup. Discussions on similar changes for pytest-aiohttp are here: https://github.com/pytest-dev/pytest-asyncio/pull/309 * Replace loop with event_loop * Make sure time is frozen for tests * Make sure the ConditionType is not async /home-assistant/homeassistant/helpers/template.py:2082: RuntimeWarning: coroutine 'AsyncMockMixin._execute_mock_call' was never awaited def wrapper(*args, **kwargs): Enable tracemalloc to get traceback where the object was allocated. See https://docs.pytest.org/en/stable/how-to/capture-warnings.html#resource-warnings for more info. * Increase litejet press tests with a factor 10 The times are simulated anyway, and we can't stop the normal event from occuring. * Use async handlers for aiohttp tests/components/motioneye/test_camera.py::test_get_still_image_from_camera tests/components/motioneye/test_camera.py::test_get_still_image_from_camera tests/components/motioneye/test_camera.py::test_get_stream_from_camera tests/components/motioneye/test_camera.py::test_get_stream_from_camera tests/components/motioneye/test_camera.py::test_camera_option_stream_url_template tests/components/motioneye/test_camera.py::test_camera_option_stream_url_template /Users/joakim/src/hass/home-assistant/venv/lib/python3.9/site-packages/aiohttp/web_urldispatcher.py:189: DeprecationWarning: Bare functions are deprecated, use async ones warnings.warn( * Switch to freezegun in modbus tests The tests allowed clock to tick in between steps * Make sure skybell object are fully mocked Old tests would trigger attempts to post to could services: ``` DEBUG:aioskybell:HTTP post https://cloud.myskybell.com/api/v3/login/ Request with headers: {'content-type': 'application/json', 'accept': '*/*', 'x-skybell-app-id': 'd2b542c7-a7e4-4e1e-b77d-2b76911c7c46', 'x-skybell-client-id': '1f36a3c0-6dee-4997-a6db-4e1c67338e57'} ``` * Fix sorting that broke after rebase
2022-11-29 21:36:36 +00:00
def test_parsing_args(event_loop):
"""Test we parse args correctly."""
called = False
async def mock_func(hass, provider, args2):
"""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"