Hass.io allow to reset password with CLI (#30755)

* Hass.io allow to reset passwort with CLI

* Add improvments

* fix comments

* fix lint

* Fix tests

* more tests

* Address comments

* sort imports

* fix test python37
This commit is contained in:
Pascal Vizeli authored and GitHub committed 2020-01-14 23:49:56 +01:00
1 parent 9f62b58929
commit 4731f7c721
5 files changed
+197 -33

No files matched your search

+12 -1
View File
@@ -32,7 +32,7 @@ def hassio_stubs(hassio_env, hass, hass_client, aioclient_mock):
with patch(
"homeassistant.components.hassio.HassIO.update_hass_api",
return_value=mock_coro({"result": "ok"}),
), patch(
) as hass_api, patch(
"homeassistant.components.hassio.HassIO.update_hass_timezone",
return_value=mock_coro({"result": "ok"}),
), patch(
@@ -42,6 +42,8 @@ def hassio_stubs(hassio_env, hass, hass_client, aioclient_mock):
hass.state = CoreState.starting
hass.loop.run_until_complete(async_setup_component(hass, "hassio", {}))
return hass_api.call_args[0][1]
@pytest.fixture
def hassio_client(hassio_stubs, hass, hass_client):
@@ -55,6 +57,15 @@ def hassio_noauth_client(hassio_stubs, hass, aiohttp_client):
return hass.loop.run_until_complete(aiohttp_client(hass.http.app))
@pytest.fixture
async def hassio_client_supervisor(hass, aiohttp_client, hassio_stubs):
"""Return an authenticated HTTP client."""
access_token = hass.auth.async_create_access_token(hassio_stubs)
return await aiohttp_client(
hass.http.app, headers={"Authorization": f"Bearer {access_token}"},
)
@pytest.fixture
def hassio_handler(hass, aioclient_mock):
"""Create mock hassio handler."""
+110 -12
View File
@@ -6,14 +6,14 @@ from homeassistant.exceptions import HomeAssistantError
from tests.common import mock_coro
async def test_login_success(hass, hassio_client):
async def test_auth_success(hass, hassio_client_supervisor):
"""Test no auth needed for ."""
with patch(
"homeassistant.auth.providers.homeassistant."
"HassAuthProvider.async_validate_login",
Mock(return_value=mock_coro()),
) as mock_login:
resp = await hassio_client.post(
resp = await hassio_client_supervisor.post(
"/api/hassio_auth",
json={"username": "test", "password": "123456", "addon": "samba"},
)
@@ -23,12 +23,12 @@ async def test_login_success(hass, hassio_client):
mock_login.assert_called_with("test", "123456")
async def test_login_error(hass, hassio_client):
"""Test no auth needed for error."""
async def test_auth_fails_no_supervisor(hass, hassio_client):
"""Test if only supervisor can access."""
with patch(
"homeassistant.auth.providers.homeassistant."
"HassAuthProvider.async_validate_login",
Mock(side_effect=HomeAssistantError()),
Mock(return_value=mock_coro()),
) as mock_login:
resp = await hassio_client.post(
"/api/hassio_auth",
@@ -36,32 +36,66 @@ async def test_login_error(hass, hassio_client):
)
# Check we got right response
assert resp.status == 403
assert resp.status == 401
assert not mock_login.called
async def test_auth_fails_no_auth(hass, hassio_noauth_client):
"""Test if only supervisor can access."""
with patch(
"homeassistant.auth.providers.homeassistant."
"HassAuthProvider.async_validate_login",
Mock(return_value=mock_coro()),
) as mock_login:
resp = await hassio_noauth_client.post(
"/api/hassio_auth",
json={"username": "test", "password": "123456", "addon": "samba"},
)
# Check we got right response
assert resp.status == 401
assert not mock_login.called
async def test_login_error(hass, hassio_client_supervisor):
"""Test no auth needed for error."""
with patch(
"homeassistant.auth.providers.homeassistant."
"HassAuthProvider.async_validate_login",
Mock(side_effect=HomeAssistantError()),
) as mock_login:
resp = await hassio_client_supervisor.post(
"/api/hassio_auth",
json={"username": "test", "password": "123456", "addon": "samba"},
)
# Check we got right response
assert resp.status == 401
mock_login.assert_called_with("test", "123456")
async def test_login_no_data(hass, hassio_client):
async def test_login_no_data(hass, hassio_client_supervisor):
"""Test auth with no data -> error."""
with patch(
"homeassistant.auth.providers.homeassistant."
"HassAuthProvider.async_validate_login",
Mock(side_effect=HomeAssistantError()),
) as mock_login:
resp = await hassio_client.post("/api/hassio_auth")
resp = await hassio_client_supervisor.post("/api/hassio_auth")
# Check we got right response
assert resp.status == 400
assert not mock_login.called
async def test_login_no_username(hass, hassio_client):
async def test_login_no_username(hass, hassio_client_supervisor):
"""Test auth with no username in data -> error."""
with patch(
"homeassistant.auth.providers.homeassistant."
"HassAuthProvider.async_validate_login",
Mock(side_effect=HomeAssistantError()),
) as mock_login:
resp = await hassio_client.post(
resp = await hassio_client_supervisor.post(
"/api/hassio_auth", json={"password": "123456", "addon": "samba"}
)
@@ -70,14 +104,14 @@ async def test_login_no_username(hass, hassio_client):
assert not mock_login.called
async def test_login_success_extra(hass, hassio_client):
async def test_login_success_extra(hass, hassio_client_supervisor):
"""Test auth with extra data."""
with patch(
"homeassistant.auth.providers.homeassistant."
"HassAuthProvider.async_validate_login",
Mock(return_value=mock_coro()),
) as mock_login:
resp = await hassio_client.post(
resp = await hassio_client_supervisor.post(
"/api/hassio_auth",
json={
"username": "test",
@@ -90,3 +124,67 @@ async def test_login_success_extra(hass, hassio_client):
# Check we got right response
assert resp.status == 200
mock_login.assert_called_with("test", "123456")
async def test_password_success(hass, hassio_client_supervisor):
"""Test no auth needed for ."""
with patch(
"homeassistant.components.hassio.auth.HassIOPasswordReset._change_password",
Mock(return_value=mock_coro()),
) as mock_change:
resp = await hassio_client_supervisor.post(
"/api/hassio_auth/password_reset",
json={"username": "test", "password": "123456"},
)
# Check we got right response
assert resp.status == 200
mock_change.assert_called_with("test", "123456")
async def test_password_fails_no_supervisor(hass, hassio_client):
"""Test if only supervisor can access."""
with patch(
"homeassistant.auth.providers.homeassistant.Data.async_save",
Mock(return_value=mock_coro()),
) as mock_save:
resp = await hassio_client.post(
"/api/hassio_auth/password_reset",
json={"username": "test", "password": "123456"},
)
# Check we got right response
assert resp.status == 401
assert not mock_save.called
async def test_password_fails_no_auth(hass, hassio_noauth_client):
"""Test if only supervisor can access."""
with patch(
"homeassistant.auth.providers.homeassistant.Data.async_save",
Mock(return_value=mock_coro()),
) as mock_save:
resp = await hassio_noauth_client.post(
"/api/hassio_auth/password_reset",
json={"username": "test", "password": "123456"},
)
# Check we got right response
assert resp.status == 401
assert not mock_save.called
async def test_password_no_user(hass, hassio_client_supervisor):
"""Test no auth needed for ."""
with patch(
"homeassistant.auth.providers.homeassistant.Data.async_save",
Mock(return_value=mock_coro()),
) as mock_save:
resp = await hassio_client_supervisor.post(
"/api/hassio_auth/password_reset",
json={"username": "test", "password": "123456"},
)
# Check we got right response
assert resp.status == 500
assert not mock_save.called